编写一段C语言程序,使其实现进程的软中断通讯。 二、要求: 使用系统调用fork()创造二个进程,父进程睡眠2秒后,用系统调用Kill() 向两个子进程发出信号,子进程捕捉到信息后分别输出下列信息终止: Child Processl1 is Killed by Parent! Child Processl2 is Killed by Parent! 父进程等待两个子进程终止后,输出如下的信息后终止: Parent Process is Killed! |
Parent Process is Killed!
#include<signal.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/msg.h>
#include<sys/ipc.h>
void waiting(),stop();
int wait_mark;
void waiting(){
while(wait_mark==1);
}
void stop(){
wait_mark=0;
}
int main(){
int i,p1,p2;
p1=fork();
if(p1>0){
p2=fork();
if(p2>0){
sleep(2);
kill(p1,16);
kill(p2,17);
wait(0);
wait(0);
printf(“parent is killed”);
}
else{
signal(17,stop);
witting();
printf(“p2 iskilled”);
exit(0);
}
}
else {
signal(16,stop);
waiting();
printf(“p1 iskilled“);
exit(0);
}
}