《东北大学操作系统实验三.doc》由会员分享,可在线阅读,更多相关《东北大学操作系统实验三.doc(8页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、实验报告 实验名称实验三 进程通信班 级学 号姓 名日 期2014.1.13成 绩评 阅 人软件学院(一)信号量机制实验【实验目的】1了解什么是信号。2熟悉LINUX系统中进程之间软中断通信的基本原理。【实验报告】1.#include#include#include void waiting(),stop();int wait_mark;main()int p1,p2;if(p1=fork() /*创建子进程p1*/ if(p2=fork() /*创建子进程p2*/wait_mark=1;signal(SIGINT,stop); /*接收到c信号,转stop*/waiting();kill(p
2、1,16); /*向p1发软中断信号16*/ kill(p2,17); /*向p2发软中断信号17*/ wait(0); /*同步*/wait(0);printf(parent process is killed!n);exit(0);elsewait_mark=1;signal(SIGINT,stop);waiting();lockf(1,0,0);printf(child process2 is killed by parent!n);lockf(1,0,0);exit(0);elsewait_mark=1;signal(SIGINT,stop);waiting();lockf(1,0,0
3、);printf(child process1 is killed by parent!n);lockf(1,0,0);exit(0);void waiting()while (wait_mark!=0);void stop()wait_mark=0;2.#include#include#includeint pid1,pid2;int EndFlag=0;int pf1=0;int pf2=0;void IntDelete() kill(pid1,16);kill(pid2,17);EndFlag=1;void Int1() printf(child process 1 is killed
4、!by parentn);exit(0);void Int2() printf(child process 2 is killed !by parentn);exit(0);main() int exitpid;signal(SIGINT,SIG_IGN);signal(SIGQUIT,SIG_IGN);if(pid1=fork()signal(SIGUSR1,Int1);signal(SIGINT,SIG_IGN);pause();exit(0);elseif(pid2=fork()signal(SIGUSR1,Int1);signal(SIGINT,SIG_IGN);pause();exi
5、t(0);elsesignal(SIGINT,IntDelete);waitpid(-1,&exitpid,0);printf(parent process is killedn);exit(0);分析由于忽略了中断与退出信号,程序会一直保持阻塞状态而无法退出。(二)进程的管道通信实验【实验目的】1了解什么是管道2熟悉UNIX/LINUX支持的管道通信方式【实验内容】1.程序#include #include #include int pid1,pid2;main( ) int fd2;char outpipe100,inpipe100;pipe(fd); /*创建一个管道*/while (p
6、id1=fork( )=-1);if(pid1=0) lockf(fd1,1,0); sprintf(outpipe,child 1 process is sending message!); write(fd1,outpipe,50); /*向管道写长为50字节的串*/ sleep(5); /*自我阻塞5秒*/ lockf(fd1,0,0); exit(0); else while(pid2=fork( )=-1); if(pid2=0) lockf(fd1,1,0); /*互斥*/ sprintf(outpipe,child 2 process is sending message!);
7、write(fd1,outpipe,50); sleep(5); lockf(fd1,0,0); exit(0); else wait(0); /*同步*/ read(fd0,inpipe,50); /*从管道中读长为50字节的串*/ printf(%sn,inpipe); wait(0); read(fd0,inpipe,50); printf(%sn,inpipe); exit(0); 运行结果延迟5秒后显示:child1 process is sending message! 再延迟5秒:child2 process is sending message!2.#include main(
8、)int filedes2;char buffer80;pipe(filedes);if(fork()0)/* 父进程*/char s = “hello!n”;write(filedes1,s,sizeof(s);else/*子进程*/read(filedes0,buffer,80);printf(“%s”,buffer);(三) 消息的发送与接收实验#include #include #include #define MSGKEY 75struct msgform long mtype; char mtext1000;msg;int msgqid;void client() int i;ms
9、gqid=msgget(MSGKEY,0777); /*打开75#消息队列*/for(i=10;i=1;i-) msg.mtype=i;printf(“(client)sentn”);msgsnd(msgqid,&msg,1024,0); /*发送消息*/exit(0);main( ) client( );2、server.c#include #include #include #define MSGKEY 75struct msgform long mtype; char mtext1000;msg;int msgqid;void server( ) msgqid=msgget(MSGKEY
10、,0777|IPC_CREAT); /*创建75#消息队列*/do msgrcv(msgqid,&msg,1030,0,0); /*接收消息*/ printf(“(server)receivedn”);while(msg.mtype!=1);msgctl(msgqid,IPC_RMID,0); /*删除消息队列,归还资源*/exit(0);main( ) server( );(四)、程序说明1、为了便于操作和观察结果,编制二个程序client.c和server.c,分别用于消息的发送与接收。2、server建立一个 Key 为75的消息队列,等待其它进程发来的消息。当遇到类型为1的消息,则作为
11、结束信号,取消该队列,并退出server。server每接收到一个消息后显示一句“(server)received。”3、client使用 key为75的消息队列,先后发送类型从10到1的消息,然后退出。最后一个消息,即是 server端需要的结束信号。client 每发送一条消息后显示一句 “(client)sent”。4、注意: 二个程序分别编辑、编译为client与server。执行:./server&ipcs -q./client。(五)、运行结果从理想的结果来说,应当是每当client发送一个消息后,server接收该消息,client再发送下一条。也就是说“(client)sent”和 “(server)received”的字样应该在屏幕上交替出现。实际的结果大多是,先由client发送了两条消息,然后server接收一条消息。此后client 、server交替发送和接收消息。最后server一次接收两条消息。client 和server 分别发送和接收了10条消息,与预期设想一致。
限制150内