Linux操作系统实验系列之实验五进程的软中断通信

一、实验目的
1、了解什么是信号
2、熟悉LINUX系统中进程之间软中断通信的基本原理

二、实验内容:
1、编写程序:用fork( )创建两个子进程,再用系统调用signal( )让父进程捕捉键盘上来的中断信号(即按^c键);捕捉到中断信号后,父进程用系统调用kill( )向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止:
Child process1 is killed by parent!
Child process2 is killed by parent!
父进程等待两个子进程终止后,输出如下的信息后终止:
Parent process is killed!
2、分析利用软中断通信实现进程同步的机理

三、实验环境

Linux操作系统

四、实验过程与运行结果

源代码:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include<stdlib.h>

void waiting( ),stop( );
int wait_mark;
main( )
{
int p1,p2,stdout;
while((p1=fork( ))-1); /创建子进程p1/
if (p1>0)
{
while((p2=fork( ))
-1); /创建子进程p2/
if(p2>0)
{
wait_mark=1;
signal(SIGINT,stop); /接收到^c信号,转stop/
waiting( );
kill(p1,16); /向p1发软中断信号16/
kill(p2,17); /向p2发软中断信号17/
wait(0); /同步/
wait(0);
printf(“Parent process is killed!/n”);
exit(0);
}
else
{
wait_mark=1;
signal(17,stop); /接收到软中断信号17,转stop/
waiting( );
lockf(stdout,1,0);
printf(“Child process 2 is killed by parent!/n”);
lockf(stdout,0,0);
exit(0);
}
}
else
{
wait_mark=1;
signal(16,stop); /接收到软中断信号16,转stop/
waiting( );
lockf(stdout,1,0);
printf(“Child process 1 is killed by parent!/n”);
lockf(stdout,0,0);
exit(0);
}
}

void waiting( )
{
while(wait_mark!=0);
}

void stop( )
{
wait_mark=0;
}

结果图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43372169/article/details/110521692
今日推荐