为什么父进程在处理信号后没有返回到确切的位置?
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
sig_atomic_t child_exit_status;
void clean_up_child_process (int signal_number)
{
/* Clean up the child process. */
int status;
wait (&status);
/* Store its exit status in a global variable. */
child_exit_status = status;
}
int main ()
{
/* Handle SIGCHLD by calling clean_up_child_process. */
struct sigaction sigchld_action;
memset (&sigchld_action, 0, sizeof (sigchld_action));
sigchld_action.sa_handler = &clean_up_child_process;
sigaction (SIGCHLD, &sigchld_action, NULL);
/* Now do things, including forking a child process. */
/* ... */
pid_t t = fork();
if (t!=0) {
// parent
sleep(30); // After handling signal, why does it not continue to sleep 20 (30-10) more seconds?
printf("Parent exits\n");
}
else {
// child
sleep(10);
printf("child exists\n");
}
return 0;
}
我得到的结果是 10 秒后,子进程和父进程都打印出其消息然后退出。我期望子进程首先打印出消息,然后父进程将再休眠大约 20 秒,然后打印出消息并退出。为什么父进程在处理信号之前不恢复到“确切位置”,它又休眠了 20 秒?有什么办法可以实现这个目标吗?
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
sig_atomic_t child_exit_status;
void clean_up_child_process (int signal_number)
{
/* Clean up the child process. */
int status;
wait (&status);
/* Store its exit status in a global variable. */
child_exit_status = status;
}
int main ()
{
/* Handle SIGCHLD by calling clean_up_child_process. */
struct sigaction sigchld_action;
memset (&sigchld_action, 0, sizeof (sigchld_action));
sigchld_action.sa_handler = &clean_up_child_process;
sigaction (SIGCHLD, &sigchld_action, NULL);
/* Now do things, including forking a child process. */
/* ... */
pid_t t = fork();
if (t!=0) {
// parent
sleep(30); // After handling signal, why does it not continue to sleep 20 (30-10) more seconds?
printf("Parent exits\n");
}
else {
// child
sleep(10);
printf("child exists\n");
}
return 0;
}
The result that I got is after 10 seconds, both child and parent process prints out its message then exit. What I expect is child process prints out the message first, then parent process will sleep about 20 more seconds before printing out its message and exit. Why doesn't the parent process resume to "exact location" before handling signal, which is sleeping 20 more seconds? Is there a way that I can achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果出现信号,
sleep(3)
不会重新启动。所以你应该检查返回值并再次睡眠。像(未经测试)的东西:
sleep(3)
isn't restarted in the event of a signal.So you should check the return value and sleep again. Something like (untested):
考虑一下您可能想要中断阻塞操作/
select()
/poll()
/sleep()
。一种方法是发送一个信号,这可能会导致当前函数以EINTR
退出。有一个
SA_RESTART
标志会导致某些函数在收到信号后重新启动,主要是read()
和write()
。其他函数的行为取决于实现。Consider that you may want to interrupt a blocking operation/
select()
/poll()
/sleep()
. One way to do it is to send a signal, which may cause the current function to exit withEINTR
.There is a
SA_RESTART
flag that causes some functions to be restarted on receipt of a signal, mainlyread()
andwrite()
. Other functions' behaviour depends on implementation.