资源描述:
《操作系统实验一、进程间的通信.docx》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#include#include#include#includeinti,wait_mark;voidwaiting(),stop();voidshiyan1_part1(){intp1,p2;while((p1=fork())==-1);if(p1==0){printf("b");}else{while((p2=fork())==-1);if(p2==0){printf("c");}else{printf("a");}
2、}}voidshiyan1_part2_1(){intp1,p2;while((p1=fork())==-1);if(p1==0){for(i=0;i<10;i++)printf("我是子进程1");}else{while((p2=fork())==-1);if(p2==0){for(i=0;i<10;i++)printf("我是子进程2");}else{for(i=0;i<10;i++)printf("我是父进程");}}}voidshiyan1_part2_2(){intp1,p2;while((p1=fork())==-1);if(p1==0){lockf(1,1,0);
3、for(i=0;i<10;i++)printf("我是子进程1");lockf(1,0,0);}else{while((p2=fork())==-1);if(p2==0){lockf(1,1,0);for(i=0;i<10;i++)printf("我是子进程2");lockf(1,0,0);}else{lockf(1,1,0);for(i=0;i<10;i++)printf("我是父进程");lockf(1,0,0);}}}voidshiyan1_part3_1(){intp1,p2;while((p1=fork())==-1);if(p1>0){while((p2=fork(
4、))==-1);if(p2>0){printf("thisisparentprocess");wait_mark=1;signal(SIGINT,stop);waiting();kill(p1,1);kill(p2,2);wait(0);wait(0);printf("parentprocessiskilled");exit(0);}else{printf("thisischildprocessp2");signal(SIGINT,stop);//signal(SIGQUIT,SIG-IGN);wait_mark=1;signal(2,stop);waiting();loc
5、kf(1,1,0);printf("childprocessp2iskilledbyparent");lockf(1,0,0);exit(0);}}else{printf("thisischildprocessp1");signal(SIGINT,stop);//signal(SIGQUIT,SIG-IGN);wait_mark=1;signal(1,stop);waiting();lockf(1,1,0);printf("childprocessp1iskilledbyparent!");lockf(1,0,0);exit(0);}}voidshiyan1_part3
6、_2(){intp1,p2;while((p1=fork())==-1);if(p1>0){while((p2=fork())==-1);if(p2>0){printf("thisisparentprocess");wait_mark=1;signal(SIGINT,stop);waiting();kill(p1,1);kill(p2,2);wait(0);wait(0);printf("parentprocessiskilled");exit(0);}else{printf("thisischildprocessp2");signal(SIGINT,SIG_IGN);//
7、signal(SIGQUIT,SIG_IGN);wait_mark=1;signal(2,stop);waiting();lockf(1,1,0);printf("childprocessp2iskilledbyparent");lockf(1,0,0);exit(0);}}else{printf("thisischildprocessp1");signal(SIGINT,SIG_IGN);//signal(