资源描述:
《东北大学操作系统实验三》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验报告实验名称实验三进程通信班级学号姓名日期2014.1.13成绩评阅人软件学院(一)信号量机制实验【实验目的】1.了解什么是信号。2.熟悉LINUX系统中进程之间软中断通信的基本原理。【实验报告】1.#include#include#includevoidwaiting(),stop();intwait_mark;main(){intp1,p2;if(p1=fork())/*创建子进程p1*/{if(p2=fork())/*创建子进程p2*/{wait_mark=1;signal(SIGINT,stop);/*
2、接收到^c信号,转stop*/waiting();kill(p1,16);/*向p1发软中断信号16*/kill(p2,17);/*向p2发软中断信号17*/wait(0);/*同步*/wait(0);printf("parentprocessiskilled!");exit(0);}else{wait_mark=1;signal(SIGINT,stop);waiting();lockf(1,0,0);printf("childprocess2iskilledbyparent!");lockf(1,0,0);exit(0);}}else{wait_mark=1;s
3、ignal(SIGINT,stop);waiting();lockf(1,0,0);printf("childprocess1iskilledbyparent!");lockf(1,0,0);exit(0);}}voidwaiting(){while(wait_mark!=0);}voidstop(){wait_mark=0;}2.#include#include#includeintpid1,pid2;intEndFlag=0;intpf1=0;intpf2=0;voidIntDelete(){kill(pi
4、d1,16);kill(pid2,17);EndFlag=1;}voidInt1(){printf("childprocess1iskilled!byparent");exit(0);}voidInt2(){printf("childprocess2iskilled!byparent");exit(0);}main(){intexitpid;signal(SIGINT,SIG_IGN);signal(SIGQUIT,SIG_IGN);if(pid1=fork()){signal(SIGUSR1,Int1);signal(SIGINT,SIG_IGN);pause(
5、);exit(0);}else{if(pid2=fork()){signal(SIGUSR1,Int1);signal(SIGINT,SIG_IGN);pause();exit(0);}else{signal(SIGINT,IntDelete);waitpid(-1,&exitpid,0);printf("parentprocessiskilled");exit(0);}}}〈分析〉由于忽略了中断与退出信号,程序会一直保持阻塞状态而无法退出。(二)进程的管道通信实验【实验目的】1.了解什么是管道2.熟悉UNIX/LINUX支持的管道通信方式【实验内容】1.〈程序〉#i
6、nclude#include#includeintpid1,pid2;main(){intfd[2];charoutpipe[100],inpipe[100];pipe(fd);/*创建一个管道*/while((pid1=fork())==-1);if(pid1==0){lockf(fd[1],1,0);sprintf(outpipe,"child1processissendingmessage!");write(fd[1],outpipe,50);/*向管道写长为50字节的串*/sleep(5);/*自我阻塞5秒
7、*/lockf(fd[1],0,0);exit(0);}else{while((pid2=fork())==-1);if(pid2==0){lockf(fd[1],1,0);/*互斥*/sprintf(outpipe,"child2processissendingmessage!");write(fd[1],outpipe,50);sleep(5);lockf(fd[1],0,0);exit(0);}else{wait(0);/*同步*/read(fd[0],inpipe,50);/*从管道中读长为50字节的串*/printf