[英]Child doesn't continue running after receiving signal using pause()
...
signal( SIGUSR1, sigusr);
bla = 0;
for(;;)
{
if(pid=fork()==0)
{
fprintf(stderr,"Child %d: Waiting in queue.\n",getpid());
pause();
fprintf(stderr,"im here"); //can't get to this line
kill(deque(q),SIGUSR1)
_exit(0);
}
else
{
sem_wait(&q2);
enque(q,pid);
sem_post(&q2);
if(!bla)
{
bla=1;
sem_wait(&q2);
kill(deque(q),SIGUSR1);
sem_post(&q2);
}
sleep(n);
}
}
...
void sigusr()
{
signal(SIGUSR1, sigusr);
fprintf(stderr, "Child %d: Got it.\n", getpid());
}
The expression pid=fork()==0
does not work as you expect it to. 表达式pid=fork()==0
不能正常工作。 It assigns the valie of fork() == 0
to the variable pid
, because of the operator precedence for equality is higher than for assignment. 它将fork() == 0
的值分配给变量pid
,因为等于的运算符优先级高于赋值的优先级 。
That means that pid
will be either 0
or 1
, neither a correct process identifier. 这意味着pid
将为0
或1
,都不是正确的进程标识符。
Change to (pid = fork()) == 0
instead. 改为(pid = fork()) == 0
。
In addition to pid=fork()==0
there is a race condition, the child may receive the signal before the call to pause()
, and then wait for another signal in pause()
.除了pid=fork()==0
还有一个竞态条件,孩子可能在调用pause()
之前收到信号,然后在pause()
中等待另一个信号。
To avoid this problem:为避免此问题:
sigprocmask
.使用sigprocmask
阻止信号。sigsuspend
.使用sigsuspend
等待信号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.