操作系统实验报告进程通信管理课案
漳 州 师 范 学 院
实 验 报 告
班 级 13网络1班 学号1308990337 姓名 成绩
同组人 实验日期
课程名称:操作系统 实验题目:进程通信管理PC 兼容机。Window xp 以上操作系统 实验内容与具体步骤
1.软中断通信
编制一段程序,使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上来的中断信号(即按键),当捕捉到中断信号后,父进程用系统调用kill()向两个子进程发出信号,子进程捕捉到信号后,分别输出下列信息后终止:
child process 1 is killed by parent!
child process 2 is killed by parent!
父进程等待两个子进程终止后,输出一下信息后终止:
parent process is killed!
#include unistd.h
#include stdio.h
#include signal.h
void waiting();
void stop();
int wait_mark;
int main()
{
int p1,p2;
while((p1=fork())==-1);
if(p10) //父进程产生子进程1
{
while((p2=fork())==-1);
if(p20)//父进程产生了一个子进程2
{//父进程
printf(parent\n);
wait_mark=1;//将等待标志设置为1
signal(SIGINT,stop);
waiting();
kill(p1,16);//
kill(p2,17);
wait(0);
wait(0);
printf(parent process is killed!\n);// 父进程等待两个子进程终止后,输出一下信息后终止
exit(0);
}
else
{//子进程2
printf(p2\n);
wait_mark=1;
signal(17,stop);
waiting();
lockf(1,1,0);
printf(child process 2 is killed by parent!\n);
lockf(1,0,0);
exit(0);
}
}
else
{//子进程1
printf(p1\n);
wait_mark=1;
signal(16,stop);
waiting();
lockf(1,1,0);
printf(child process 1 is killed by parent!\n);
lockf(1,0,0);
exit(0);
}
}
void waiting()
{
while(wait_mark!=0);
}
void stop()
{
wait_mark=0;
}
分析结果并理解以上程序。
理解分析:1.先创建父进程,由父进程分别产生子进程1和子进程2,依次输出p1,p2,parent。
2.给父进程中断信号,父进程终止子进程,运行stop函数wait_mark=0;跳出waiting函数,输出parent process is killed!
2.修改程序,查看修改前结果跟修改后结果的区别,分析原因。... ... else { signal(SIGINT, SIG_IGN); /* add this line */ signal(17,stop); ... ...
... ...}
}else { signal(SIGINT, SIG_IGN); /* add this line */ signal(16,stop); ... ... ... ...
理解分析:1.先创建父进程,由父进程分别产生子进程1和子进程2,依次输出p1,p2,parent。
2. 给父进程中断信号,父进程开始终止子进程,但是由于添加了signal(SIGINT, SIG_IGN);语句, 相当于使子进程忽略键入信号,此时按ctrl+c不会将子进程终止,程序可以正常运行。于是输出child process 1 is killed by parent!和child process 2 is killed by parent! 实验内容与具体步骤 2.进程的管道通信
编制一段程序,实现进程的管道通信。使用系统调用pipe()建立一条管道线。两个子进程p1和p2分别向管道各写
原创力文档

文档评论(0)