实验五--进程间通信.doc
《实验五--进程间通信.doc》由会员分享,可在线阅读,更多相关《实验五--进程间通信.doc(86页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date实验五-进程间通信实验五 进程间通信实验五 进程间通信UNIX/LINUX系统的进程间通信机构(IPC)允许在任意进程间大批量地交换数据。本实验的目的是了解和熟悉LINUX支持的信号机制、管道机制、消息队列通信机制及共享存储区机制。5.1信号机制实验(一)【实验目的】1了解什么是信号。2熟悉LINUX系统中进程之间软中断通信的基本原理。【实验原理】利用signal来实
2、现发送信号和接受信号的原理【实验内容】1编写一段程序,使用系统调用fork( )创建两个子进程,再用系统调用signal( )让父进程捕捉键盘上来的中断信号(即按ctrl+c键),当捕捉到中断信号后,父进程用系统调用kill( )向两个子进程发出信号,子进程捕捉到父进程发来的信号后,分别输出下列信息后终止: Child process 1 is killed by parent!Child process 2 is killed by parent!父进程等待两个子进程终止后,输出以下信息后终止: Parent process is killed! # include# include# in
3、cludeint wait_mark;void waiting(),stop();void main() int p1, p2;signal(SIGINT,stop);while(p1=fork()=-1);if(p10)/*在父进程中*/ while(p2=fork()= =-1); If(p20)/*在父进程中*/ wait_mark=1; waiting(0); kill(p1,10); kill(p2,12); wait( ); wait( ); printf(“parent process is killed!n”); exit(0); else/*在子进程2中*/ wait_mar
4、k=1;signal(12,stop);waiting();lockf(1,1,0);printf(“child process 2 is killed by parent!n”);lockf(1,0,0);exit(0); else/*在子进程1中*/ wait_mark=1; signal(10,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()
5、 wait_mark=0;实验要求: 运行程序并分析结果。 如果把signal(SIGINT,stop)放在号和号位置,结果会怎样并分析原因。 该程序段前面部分用了两个wait(0),为什么? 该程序段中每个进程退出时都用了语句exit(0),为什么?5.2信号机制实验(二)【实验目的】学习signal的函数的使用【实验原理】利用signal的函数的机制来实习我们发送截获信号的功能【实验内容】修改上面的程序,增加语句signal(SIGINT,SIG_IGN)和语句signal(SIGQUIT,SIG_IGN),再观察程序执行时屏幕上出现的现象,并分析其原因。# include# includ
6、e# includeint pid1, pid2;int EndFlag=0;pf1=0;pf2=0;void IntDelete() kill(pid1,10); kill(pid2,12);EndFlag=1;void Int1() printf(“child process 1 is killed by parent !n”); exit(0);void Int2() printf(“child process 2 is killed by parent !n”); exit(0);main() int exitcode; signal(SIGINT,SIG_IGN); signal(S
7、IGQUIT,SIG_IGN);while(pid1=fork()=-1); if(pid1=0) signal(SIGUSR1,Int1);signal(SIGINT,SIG_IGN);pause();exit(0); else while(pid2=fork()= =-1); if(pid2=0) signal(SIGUSR2,Int2);signal(SIGINT,SIG_IGN);pause();exit(0); else signal(SIGINT,IntDelete); waitpid(-1,&exitcode,0);/*等待任何子进程中断或结束*/ printf(“parent
8、process is killed n”); exit(0); 实验要求:运行程序并分析结果。司机售票员问题(选做题)编程用fork()创建一个子进程代表售票员,司机在父进程中,再用系统调用signal()让父进程(司机)捕捉来自子进程(售票员)发出的中断信号,让子进程(售票员)捕捉来自(司机)发出的中断信号,以实现进程间的同步运行。5.3管道通信实验(一)【实验目的】1、了解什么是管道2、熟悉UNIX/LINUX支持的管道通信方式【实验内容】编写程序实现进程的管道通信。用系统调用pipe( )建立一管道,二个子进程P1和P2分别向管道各写一句话: Child 1 is sending a m
9、essage! Child 2 is sending a message!父进程从管道中读出二个来自子进程的信息并显示(要求先接收P1,后P2)。参考程序#include #include #include int pid1,pid2;main( ) int fd2;char outpipe100,inpipe100;pipe(fd); /*创建一个管道*/while (pid1=fork( )= =-1);if(pid1= =0)lockf(fd1,1,0); sprintf(outpipe,child 1 process is sending message!); /*把串放入数组outp
10、ipe中*/ write(fd1,outpipe,50); /*向管道写长为50字节的串*/ sleep(5); /*自我阻塞5秒*/ lockf(fd1,0,0); exit(0);elsewhile(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,i
11、npipe,50); /*从管道中读长为50字节的串*/ printf(%s/n,inpipe); wait(0); read(fd0,inpipe,50); printf(%s/n,inpipe); exit(0); 五、运行结果 延迟5秒后显示child 1 process is sending message! 再延迟5秒child 2 process is sending message!5.4管道通信实验(二)【实验目的】1、掌握有名管道的创建和读写方式2、熟悉UNIX/LINUX支持的有名管道通信方式【实验内容】1. 创建有名管道2. 本进程执行循环等待数据被写入到管道中并读有名管
12、道3. 打开有名管道并写数据到名管道参考代码:/read_fifo.c#include #include #include #include #include #include #include #define BUFFER_SIZE1024int main(int argc, char *argv) int fd; if (argc 2) fprintf(stdout, Usage: %s n, argv0); exit(1); /int open(const char *path, int oflag, .); if (fd = open(argv1, O_RDONLY) 0) fprin
13、tf(stderr, open fifo %s for reading failed: %sn, argv1, strerror(errno); exit(1); fprintf(stdout, open fifo %s for reading successed.n, argv0); char bufferBUFFER_SIZE; ssize_t n; while (1) again: /ssize_t read(int fd, void *buf, size_t count); if (n = read(fd, buffer, BUFFER_SIZE) 0) if (errno = EIN
14、TR) goto again; else fprintf(stderr, read failed on %s: %sn, argv1, strerror(errno);exit(1); else if (n = 0) fprintf(stderr, peer closed fifo.n); break; else buffern = 0; fprintf(stdout, read %d bytes from fifo: %sn, n, buffer); return 0;/ write_fifo.c#include #include #include #include #include #in
15、clude #include #include #define BUFFER_SIZE1024void signal_handler(int s);int main(int argc, char *argv) int fd; if (argc 2) fprintf(stdout, Usage: %s n, argv0); exit(1); signal(SIGPIPE, signal_handler); /int open(const char *path, int oflag, .); if (fd = open(argv1, O_WRONLY) 0) fprintf(stderr, ope
16、n fifo %s for writting failed: %sn, argv1, strerror(errno); exit(1); fprintf(stdout, open fifo %s for writting successed.n, argv0); char bufferBUFFER_SIZE; ssize_t n; /char *fgets(char *s, int size, FILE * stream); while (fgets(buffer, BUFFER_SIZE, stdin) again: /ssize_t write(int fd, const void *bu
17、f, size_t count); if (n = write(fd, buffer, strlen(buffer) 0) if (errno = EINTR) goto again; else fprintf(stderr, write() failed on fifo: %sn, strerror(errno);/ FIXME:break; return 0;void signal_handler(int s) fprintf(stdout, Caught signal %dn, s);/ create_fifo.c#include #include #include #include #
18、include int main(int argc, char *argv) if (argc 2) fprintf(stdout, Usage: %s n, argv0); exit(1); /int mkfifo(const char *path, mode_t mode); if (mkfifo(argv1, 0644) 0) fprintf(stderr, mkfifo() failed: %sn, strerror(errno); exit(1); return 0;5.5共享内存通信实验【实验目的】1、掌握共享内存的创建和读写方式2、熟悉UNIX/LINUX支持的共享内存通信方式【
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 进程 通信
限制150内