填写标记代码行的意义,给出功能描述和前6行程序输出。
#include
#include
int ntimes=0;
main(int argc, char *argv[])
int pid,ppid;
int p_action______, c_action______;
signal(SIGUSR1 , p_action); #(1)
switch(pid=fork______) #(2)
case -1: perror("fork failed);
exit(1);
case 0: signal(SIGUSR1,c_action);
ppid=getppid______;
for ( ; ; ) ;
default:
for( ; ; )
pause;
sleep(1);
kill(pid,SIGUSR1); #(3)
p_action______
printf("parent caught signal #%d\n",++ntimes);
c_action______
printf("child caught signal #%d\n",++ntimes/*, ++ltimes*/);
int ppid;
ppid = getppid______; #(4)
kill(ppid, SIGUSR1);
sleep(3);
参考答案:#(1)在父进程中为信号SIGUSR1绑定函数p_action
#(2)创建子进程
#(3)向子进程发送信号SIGUSR1
#(4)获取父进程的PID
父进程无线循环向子进程发送信号,子进程收到信号后向父进程发送相同信号,父子进程之间各自记录全局变量的变化,结果如:
child caught signal #1
parent caught signal #1
child caught signal #2
parent caught signal #2
child caught signal #3
parent caught signal #3