资源描述:
《实验三进程间通信》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验三进程间通信(2学时)一、实验目的(1)了解什么是信号。(2)熟悉LTNUX系统中进程之间软中断通信的基本原理。(3)熟悉LINUX支持的管道通信方式。二、实验内容(1)编写一段程序,使其现实进程的软中断通信。BU:使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上來的中断信号(即按ctrl+c键);当捕捉到中断信号后,父进程用系统调用kill()向两个子进程发出信号,子进程捕捉到信号后,分别输出下列信息后终止:ChildProcesslliskilledbyP
2、arent!ChildProcessl2iskilledbyParent!父进程等待两个了进程终止后,输出如卜•的信息后终止ParentProcessiskilled!要求:运行以下参考程序并分析结果。〈参考程序〉#include#include#include#includevoidwaiting(),stop(),alarming();intwait_mark;main(){intpl,p2;if(pl=fork())/
3、*创建了进程pl*/{if(p2=fork())/*创建子进程p2*/{//父进程wait_mark=l;signalCSIGINT^stop);/*接收到X信号,转stop*/signal(SIGALRM,alarming);/*接受SIGALRM*/waiting();kill(pl,16);/*向pl发软中断信号16*/kill(p2,17);/*向p2发软中断信号17*/wait(0);/*同步*/wait(0);printf(,fparentprocessiskilled!N);exit
4、(0);//会暂时停止目前进程的执行,直到有信号来到或了进程结束。}else{wait_mark=l;signal(17,stop);signalCSIGINT.SIG^GN);/*忽略玄信号*/while(wait_mark!=0);lockf(1,1,0);printf("childprocess2iskilledbyparent!”);lockf(1,0,0);exit(0);}}else{waitmark=l;—*signal(16,stop);signal(SIGINT,SIG_IGN)
5、;/*忽略X信号*/while(wait_mark!=0);lockf(1,1,0);printf("childprocessliskilledbyparent!");lockf(1?0^0);exit(0);}}voidwaiting(){sleep(5);if(wait_mark!=0)kill(getpid()ySIGALRM);}voidalarming()wait_mark=0;voidstop()wait_mark=0;}bjxy®)bjxy-virtual-machine:~gedi
6、tgcccomm.cgcccomm.c・ocomm•/commchildprocessliskilledbyparent!childprocess2iskilledbyparent!parentprocessiskilled!)jxy@bjxy・virtual-machine:-$@(2)修改上面的程序増加语句signal(SIGINT,SIG_IGN)和语句signal(SIGQUIT,SIG_IGN),再观察程序执行吋屏幕上出现的现象,并分析其原因。这里,signal(SIGINT,SIG_IGN
7、)^nsignal(SIGQUIT,SIG_IGN)分别为忽略键信号以及忽略中断信号。〈程序〉#include#include#includeintpidl,pid2;intEndFlag=0;intpfl=0;intpf2=0;voidIntDelete()kill(pidl,16);kill(pid2,17);voidIntl(){printf("childprocess1iskilled!byparent”);exit(0);}voi
8、dInt2(){printf(Hchildprocess2iskilled!byparent”);exit(0);}main(){intexitpid;if(pidl=fork()){if(pid2=fork()){signalCSIGINT,IntDelete);waitpidd&exitpid,0);waitpid&exitpid,0);printf("parentprocessiskilledrT);exit(0);}else{signal(