实验四-进程的软中断通信和管道通信(5页).doc
-实验四-进程的软中断通信和管道通信-第 5 页实验四 进程的软中断通信和管道通信一、实验目的1. 掌握Linux系统软中断通信的实现方法。2. 掌握Linux系统软中断通信的基本原理。3. 学会使用Linux系统中关于进程通信的一些系统调用。4. 掌握管道通信的使用方法。二、实验内容1. 软中断通信 编写一段程序,使其实现进程的软中断通信。使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上来的中断信号(即按Del键),当捕捉到中断信号后,父进程用系统调用Kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止: Child Processll is Killed by Parent! Child Processl2 is Killed by Parent! 父进程等待两个子进程终止后,输出如下的信息后终止: Parent Process is Killed!2. 进程的管道通信 编制一段程序,实现进程的管理通信。 使用系统调用pipe()建立一条管道线。两个子进程P1和P2分别向管道中写一句话: Child 1 is sending a message! Child 2 is sending a message! 而父进程则从管道中读出来自于两个子进程的信息,显示在屏幕上。 要求父进程先接收子进程P1发来的消息,然后再接收子进程P2发来的消息。3. 管道编程练习 编写一个程序,使用系统调用fork生成3个子进程,并使用系统调用pipe创建一个管道,使得这3个子进程和父进程共用同一个管道进行通信。三、实验源程序程序1 软中断通信 #include <stdio.h> #include <signal.h>#include<unistd.h> void waiting(),stop(); /引用函数声明/int wait_mark;main() int p1,p2;while(p1=fork()=-1); /创建进程p1/if(p1>0)while(p2=fork()=-1);if(p2>0) wait_mark=1; /以下为主进程/signal(SIGINT,stop); /接受del信号,并转stop/waiting(0);kill(p1,17); /向p1发中断信号17/kill(p2,17); /向p2发中断信号17/wait(0); /将当前进程挂起,直至子进程发出信号/wait(0); printf(“parent process is killed!n”);exit(0); /主进程终止自己/else wait_mark=1; /以下为子进程p2/signal(17,stop);waiting();lockf(1,1,0); /加锁/printf(“child process 2 is killed by parent!n”);lockf(1,0,0); /解锁/exit(0);else wait_mark=1; /进程p1/signal(17,stop);waiting();lockf(1,1,0);printf(“child process 1 is killed by parent!n”);lockf(1,0,0);exit(0); /返回主进程,本进程自我终止/void waiting() while(wait_mark=0);void stop() wait_mark=0;运行时,从键盘输入中断信号del键。程序2 进程的管道通信#include <unistd.h>#include <signal.h>#include <stdio.h>int pid1,pid2;main() int fd2; /fd2用于存放管道文件的描述符/char Outpipe100,Inpipe100;pipe(fd); /创建管道,fd0、fd1分别为读、写描述符/while(pid1=fork()=-1);if(pid1=0) lockf(fd1,1,0); /锁定写/sprintf(Outpipe,”child 1 process is sending message!”);write(fd1,Outpipe,50); /将Outpipe中的数据写入管道/sleep(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!”);write(fd1,Outpipe,50);sleep(5);lockf(fd1,0,0);exit(0);else wait(0); /等待子进程执行/ read(fd0,Inpipe,50); /将Iupipe中的数据读入管道/printf(“%sn”,Inpipe);wait(0);read(fd0,Inpipe,50);printf(“%sn”,Inpipe);exit(0);程序3管道通信编程练习main() int r,p1,p2,p3,fd2; /fd2用于存放管道文件的描述符/char buf50,s5;pipe(fd); /创建管道,fd0、fd1分别为读、写描述符/while(p1=fork()=-1); / 创建子进程p1/if(p1=0); /在子进程p1中执行/ lockf(fd1,1,0); /锁定写/sprintf(buf,“child process p1 is sending message!n”);printf(“child process p1!n”);write(fd1,buf,50); /将buf中的数据写入管道/sleep(5); / 睡眠等待/lockf(fd1,0,0);exit(0);else while(p2=fork()=-1); /创建子进程p2/if(p2=0); /在子进程p2中执行/ lockf(fd1,1,0); /锁定写/sprintf(buf,“child process p2 is sending message!n”);printf(“child process p2!n”);write(fd1,buf,50); /将buf中的数据写入管道/sleep(5); / 睡眠等待/ lockf(fd1,0,0); /释放写/exit(0);else while(p3=fork()=-1);if(p3=0); lockf(fd1,1,0);sprintf(buf,“child process p3 is sending message!n”);printf(“child process p3!n”);write(fd1,buf,50);sleep(5);lockf(fd1,0,0);exit(0);wait(0); 等待子进程执行if(r=(read(fd0,s,50)=-1) /读管道内容到s/printf(“cant read pipen”);elseprintf(“%sn”,s);wait(0); 等待另一子进程执行if(r=(read(fd0,s,50)=-1) 读管道内容到sprintf(“cant read pipen”);elseprintf(“%sn”,s);wait(0); /最后一子进程执行/if(r=(read(fd0,s,50)=-1) /读管道内容到s/printf(“cant read pipen”);elseprintf(“%sn”,s);exit(0);思考:1.进程通信有什么特点?2.程序1中在kill(p2,17)后为什么用两个wait(0).