我在使用 sigchld 时遇到了一些麻烦...
我想要做的是用fork创建一个子进程并使子进程打印并休眠几次......在这些过程中我想向孩子发送信号(SIGSTOP和SIGCONTINUED)并且我想要父母打印信号是什么......这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
void handler (int i) {
int x;
waitpid(-1,&x, 0);
printf("WIFSTOPPED=%d, WIFCONTINUED=%d\n", WIFSTOPPED(x),WIFCONTINUED(x) );
}
int main(){
int x;
int q=fork();
if(q==0){
int i=0;
printf("%d\n",getpid());
while (i<20){
printf("%d\n", i++);
sleep(1);
}
_exit(0);
}
else {
signal(SIGCHLD, handler);
waitpid(-1,&x, 0);
while(WIFEXITED(x)!=1){
waitpid(-1,&x, 0);
sleep(1);
}
exit(0);
}
}
但它不起作用,因为当我向孩子发送 SIGSTOP 或 SIGCONTINUED 时,孩子停止并继续但父母不打印任何东西
有什么建议吗?