操作系统:进程的软中断通信实验

版权声明:本文为博主原创文章! github地址:https://github.com/lina-not-linus;简书:https://www.jianshu.com/u/516d28ccd858;博客: https://blog.csdn.net/Lina_ACM/article/details/51519559
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<signal.h>
#include<stdlib.h>

int wait_flag;
void stop();
 
int main()
{
	int pid1,pid2;
	signal(2,stop);
	pause();
	while((pid1 = fork())== -1);
	if(pid1 > 0)
	{
	  while((pid2 = fork()) == -1 );
	  if(pid2 > 0)
	  {
	    wait_flag= 1;
	    sleep(5);  //----------------------//
	    kill(pid1, 16);
	    kill(pid2,17);
	    wait(0);
	    wait(0);
	    printf("\nparent process is killed!!\n");
	    exit(0);
	  }
      else
   	  {
	    wait_flag= 1;
		signal(17,stop);
	    printf("\nchild process 2 is killed by parent!!\n");
	    exit(0);
	  }
	}
	else 
	{
	  wait_flag= 1;
	  signal(16,stop);
	  printf("\nchild process 1 is killed by parent!!\n");
	}
	
}

void stop()
{
	wait_flag = 0;
}

没有更多推荐了,返回首页