3.进程间的软中断通信

编写程序,使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上发出的中断信号(即按ctrl + c 或是 ctrl + \)5秒内,若未接收到这两个软中断的某一个,则父进程用系统调用kill()向两个子进程分别发送软中断信号signal1 和 signal2,子进程获得对应的软中断信号,然后分别输出下列信息后终止:

Child process (pid = ?) be killed!

Child process (pid = ?) be killed!

父进程调用wat()函数等待两个子进程终止后, 输出以下信息,结束执行:

Parent  process (pid = ?) fiished!

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
void parent_stop()
{
	printf("This is parent process, pid = %d\n", getpid());
	exit(0);
}
void child_stop()
{
	printf("Child process (pid = %d) be killed \n", getpid()) ;
	exit(0);
}
int main()
{
	int p1, p2;
	p1 = fork();
	if(p1)
	{
		p2 = fork();
		if(p2)
		{
			//parent
			signal(2, parent_stop);
			signal(3, parent_stop);
			sleep(5);
			kill(p1, 10);
			kill(p2, 12);
			wait(0);
			wait(0);
			printf("Parent finish (pid = %d)!\n", getpid());
			exit(0);//
		}
		else
		{
			//child2;
			signal(12, child_stop);
			sleep(6);
			exit(0);//永远不会执行;
		}
	}
	else
	{
		//child1;
		signal(10, child_stop);
		sleep(6);
		exit(0);//
	}
	return 0;
}


你可能感兴趣的:(kill,进程间的软中断)