《操作系统实验一、进程间的通信(共5页).docx》由会员分享,可在线阅读,更多相关《操作系统实验一、进程间的通信(共5页).docx(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上#include#include#include#include#include #include int i,wait_mark;void waiting(),stop();void shiyan1_part1()int p1,p2;while(p1=fork()=-1);if(p1=0)printf(bn);elsewhile(p2=fork()=-1);if(p2=0)printf(cn);elseprintf(an);void shiyan1_part2_1()int p1,p2;while(p1=fork()=-1);if(p1=0)for(i=0;i10;
2、i+)printf(我是子进程1n);elsewhile(p2=fork()=-1);if(p2=0)for(i=0;i10;i+)printf(我是子进程2n);elsefor(i=0;i10;i+)printf(我是父进程n);void shiyan1_part2_2()int p1,p2;while(p1=fork()=-1);if(p1=0)lockf(1,1,0);for(i=0;i10;i+)printf(我是子进程1n);lockf(1,0,0);elsewhile(p2=fork()=-1);if(p2=0)lockf(1,1,0);for(i=0;i10;i+)printf(
3、我是子进程2n);lockf(1,0,0);elselockf(1,1,0);for(i=0;i0) while(p2=fork()=-1); if(p20) printf(this is parent processn); wait_mark=1; signal(SIGINT,stop); waiting(); kill(p1,1); kill(p2,2); wait(0); wait(0); printf(nparent process is killedn); exit(0); else printf(this is child process p2n); signal(SIGINT,s
4、top); /signal(SIGQUIT,SIG-IGN); wait_mark=1; signal(2,stop); waiting(); lockf(1,1,0); printf(nchild process p2 is killed by parentn); lockf(1,0,0); exit(0); else printf(this is child process p1n); signal(SIGINT,stop); /signal(SIGQUIT,SIG-IGN); wait_mark=1; signal(1,stop); waiting(); lockf(1,1,0); pr
5、intf(nchild process p1 is killed by parent!n); lockf(1,0,0); exit(0); void shiyan1_part3_2() int p1,p2; while(p1=fork()=-1); if(p10) while(p2=fork()=-1); if(p20) printf(this is parent processn); wait_mark=1; signal(SIGINT,stop); waiting(); kill(p1,1); kill(p2,2); wait(0); wait(0); printf(nparent pro
6、cess is killedn); exit(0); else printf(this is child process p2n); signal(SIGINT,SIG_IGN); /signal(SIGQUIT,SIG_IGN); wait_mark=1; signal(2,stop); waiting(); lockf(1,1,0); printf(nchild process p2 is killed by parentn); lockf(1,0,0); exit(0); else printf(this is child process p1n); signal(SIGINT,SIG_
7、IGN); /signal(SIGQUIT,SIG_IGN); wait_mark=1; signal(1,stop); waiting(); lockf(1,1,0); printf(nchild process p1 is killed by parent!n); lockf(1,0,0); exit(0); void shiyan1_part4() int pid1,pid2; int fd3; char OutPipe100,InPipe100; pipe(fd); while(pid1=fork()=-1); if(pid1=0) printf(Subprocess p1 is go
8、ing to send msg!n); lockf(fd1,1,0); sprintf(OutPipe,Subprocess p1 sent msg!n); write(fd1,OutPipe,50); sleep(5); lockf(fd1,0,0); exit(0); else while(pid2=fork()=-1); if(pid2=0) sleep(5); printf(Subprocess p2 is going to send msg!n); lockf(fd1,1,0); sprintf(OutPipe,Subprocess p2 sent msg!n); write(fd1
9、,OutPipe,50); sleep(1); lockf(fd1,0,0); exit(0); else printf(parentn); wait(0); read(fd0,InPipe,50); printf(%sn,InPipe); wait(0); read(fd0,InPipe,50); printf(%sn,InPipe); exit(0); int main()/shiyan1_part1();/shiyan1_part2_1();/shiyan1_part2_2();/shiyan1_part3_1();/shiyan1_part3_2(); /shiyan1_part4();return 0;void waiting() while(wait_mark!=0);void stop() wait_mark=0;专心-专注-专业