linux - SIGSTOP 在 Linux 内核中是如何工作的?

标签 linux linux-kernel signals linux-device-driver

我想知道 SIGSTOP 在 Linux 内核中是如何工作的。它是如何处理的?以及内核如何在处理时停止运行?

我熟悉内核代码库。所以,如果你能引用内核函数就好了,事实上这就是我想要的。我不是在从用户的角度寻找高级描述。

我已经用 printk() 语句调试了 get_signal_to_deliver()(现在正在编译)。但我希望有人能更详细地解释事情。

最佳答案

自从我接触内核以来已经有一段时间了,但我会尽量提供尽可能多的细节。我不得不在其他不同的地方查找其中的一些内容,所以一些细节可能有点困惑,但我认为这很好地说明了幕后发生的事情。

当发出信号时,TIF_SIGPENDING 标志会在进程描述符结构中设置。在返回用户模式之前,内核使用 test_thread_flag(TIF_SIGPENDING) 测试此标志,它将返回 true(因为信号未决)。

发生这种情况的确切细节似乎取决于体系结构,但您可以 see an example for um :

void interrupt_end(void)
{
    struct pt_regs *regs = &current->thread.regs;

    if (need_resched())
        schedule();
    if (test_thread_flag(TIF_SIGPENDING))
        do_signal(regs);
    if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
        tracehook_notify_resume(regs);
}

无论如何,它最终会调用 arch_do_signal(),它也是架构相关的并且在相应的 signal.c 文件 (see the example for x86) 中定义:

void arch_do_signal(struct pt_regs *regs)
{
    struct ksignal ksig;

    if (get_signal(&ksig)) {
        /* Whee! Actually deliver the signal.  */
        handle_signal(&ksig, regs);
        return;
    }

    /* Did we come from a system call? */
    if (syscall_get_nr(current, regs) >= 0) {
        /* Restart the system call - no handlers present */
        switch (syscall_get_error(current, regs)) {
        case -ERESTARTNOHAND:
        case -ERESTARTSYS:
        case -ERESTARTNOINTR:
            regs->ax = regs->orig_ax;
            regs->ip -= 2;
            break;

        case -ERESTART_RESTARTBLOCK:
            regs->ax = get_nr_restart_syscall(regs);
            regs->ip -= 2;
            break;
        }
    }

    /*
     * If there's no signal to deliver, we just put the saved sigmask
     * back.
     */
    restore_saved_sigmask();
}

如您所见,arch_do_signal() 调用了 get_signal(),它也在 signal.c 中。

大部分工作发生在 get_signal() 内部,这是一个巨大的函数,但最终它似乎在这里处理了 SIGSTOP 的特殊情况:

    if (sig_kernel_stop(signr)) {
        /*
         * The default action is to stop all threads in
         * the thread group.  The job control signals
         * do nothing in an orphaned pgrp, but SIGSTOP
         * always works.  Note that siglock needs to be
         * dropped during the call to is_orphaned_pgrp()
         * because of lock ordering with tasklist_lock.
         * This allows an intervening SIGCONT to be posted.
         * We need to check for that and bail out if necessary.
         */
        if (signr != SIGSTOP) {
            spin_unlock_irq(&sighand->siglock);

            /* signals can be posted during this window */

            if (is_current_pgrp_orphaned())
                goto relock;

            spin_lock_irq(&sighand->siglock);
        }

        if (likely(do_signal_stop(ksig->info.si_signo))) {
            /* It released the siglock.  */
            goto relock;
        }

        /*
         * We didn't actually stop, due to a race
         * with SIGCONT or something like that.
         */
        continue;
    }

See the full function here .

do_signal_stop() 进行必要的处理以处理SIGSTOP,您也可以在signal.c 中找到它。它使用 set_special_state(TASK_STOPPED) 将任务状态设置为 TASK_STOPPED,这是一个在 include/sched.h 中定义的宏,用于更新当前进程描述符状态。 (see the relevant line in signal.c)。再往下,it calls freezable_schedule()依次调用 schedule() . schedule() 在循环中调用 __schedule()(也在同一文件中),直到找到符合条件的任务。 __schedule() 试图找到下一个要调度的任务(代码中的next),当前任务是prev。检查 prev 的状态,因为它已更改为 TASK_STOPPEDdeactivate_task() is called , 它将任务从运行队列移动到 sleep 队列:

    } else {
        ...

        deactivate_task(rq, prev, DEQUEUE_SLEEP | DEQUEUE_NOCLOCK);

        ...

    }

deactivate_task()(也在同一文件中)通过将 task_structon_rq 字段递减为 0 并调用 dequeue_task(),这会将进程移动到新的(等待)队列。

然后,schedule() 检查可运行进程的数量,并根据有效的调度策略选择下一个进入 CPU 的任务(我认为这现在有点超出范围).

在一天结束时,SIGSTOP 将进程从可运行队列移至等待队列,直到该进程收到 SIGCONT

关于linux - SIGSTOP 在 Linux 内核中是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31946854/

相关文章:

linux - 将 Linux 内核 .config 文件复制到不同版本是否安全?

linux - 我应该担心流程组中的哪些流程接收信号的顺序吗?

c++ - 如何将数据从线程传递到父进程?

linux - "-bash: !D: event not found"在腻子上写入命令时

php - Flex命令问题

c - 从信号处理程序上下文获取 ARC 平台的回溯跟踪

linux - 如何将多个结构 BIO 组合成一个结构请求?

c - 更新全局变量时屏蔽信号

c++ - GCC 编译器支持经典 C++ 吗?

c - 为什么 syslog 和 gdb 对同一个共享库显示不同的加载地址?