gpt4 book ai didi

linux - 信号如何中断系统调用

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:32 24 4
gpt4 key购买 nike

我在读一篇文章,我读到以下内容:假设我们要执行一个外部命令并等待它退出。我们不想永远等待,我们想设置一些超时时间,之后我们将终止子进程。这个怎么做?要运行命令,我们使用 fork(2) 和 execve(2)。要等待特定进程退出,我们可以使用 waitpid(2) 函数,但它没有超时参数。我们还可以创建一个循环,在该循环中以超时作为参数调用 sleep(3),并利用 sleep(3) 将被 SIGCHLD 信号中断的事实。该解决方案将起作用……几乎。它将包含一个竞争条件:如果进程立即退出,在我们调用 sleep(3) 之前我们将等到超时到期。这是一场类似于前面描述的比赛。 系统调用是如何中断的?子进程是否将信号发送给父进程或系统调用 sleep(3)?我不明白系统调用如何停止执行并传递给父进程,系统调用是否像另一个进程?

最佳答案

How is the system call interrupted? does the child send the signal to the parent process or the the system call sleep(3)?

当子进程执行系统调用exit(2)时,内核函数do_exit() ,从那里exit_notify()从那里do_notify_parent()被调用,它将 SIGCHLD 发送到父级并调用 __wake_up_parent()

I don't get how the system call stop execution and passes to the parent process, is the system call like another process?

底层系统调用 sleep() 将调用进程置于 TASK_INTERRUPTIBLE 状态 - 参见 e。 G。 Sleeping in the Kernel .之后,进程不会被调度,直到被上面的 __wake_up_parent() 或超时唤醒。

关于linux - 信号如何中断系统调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37766771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com