5.1进程间通信之管道


管道:
无名管道:用于具有亲缘关系的两个进程间完成通信
有名管道:用于任意的两个进程之间完成通信

pipe_1.c

 1 /*
 2  * 进程间通信
 3  * 管道:
 4  * 无名管道:用于具有亲缘关系的两个进程间完成通信
 5  * 有名管道:用于任意的两个进程之间完成通信
 6  * */
 7 #include <stdio.h>
 8 #include <unistd.h>
 9 #include <error.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #define error_exit(_errmsg_) error(EXIT_FAILURE,errno,_errmsg_)
15 
16 int main(void)
17 {
18     /* 创建在具有亲缘关系的两个进程间完成通信的管道
19      * int pipe(int pipefd[2]);
20      *参数:
21          pipefd:具有两个元素的整型数组名
22         成功返回0;失败返回-1.
23      */
24     int pipefd[2]; /*对应管道的两个端口*/
25     if(-1 == pipe(pipefd))
26         error_exit("fail to pipe!");
27 
28     pid_t pid;
29     if(-1 == (pid = fork()))
30         error_exit("fail to fork!");
31     if(pid == 0) {
32         char buff[] = "hello world!";
33         write(pipefd[1],buff,sizeof(buff));/*子进程把buff中数据向管道中写*/
34     }2
35 
36     if(pid > 0) {
37         char ret[64] = {0};
38         read(pipefd[0],ret,sizeof(ret));     /*父进程从管道另一头中读取到ret*/
39         printf("ret = %s\n",ret);
40     }
41 
42     return 0;
43 }

pipe_2.c

 1 /*
 2  * 进程间通信
 3  * 管道:
 4  * 无名管道:用于具有亲缘关系的两个进程间完成通信
 5  * 有名管道:用于任意的两个进程之间完成通信
 6  ////////////////////本程序是特殊之父子进程
 7  */
 8 #include <stdio.h>
 9 #include <unistd.h>
10 #include <error.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include <fcntl.h>
17 
18 #define error_exit(_errmsg_) error(EXIT_FAILURE,errno,_errmsg_)
19 
20 int main(void)
21 {
22     /* 创建一个有名管道文件
23      * int mkfifo(const char *pathname, mode_t mode);
24      * 参数:
25      *     pathname:管道文件路径.
26      *     mode:权限值.
27         成功返回0;失败返回-1.
28      */
29 
30     int fd = 0;        /*定义管道文件描述符*/
31     if(-1 == mkfifo("fifo",0644))
32         error_exit("fail to mkfifo!");
33 
34     pid_t pid;
35     if(-1 == (pid = fork()))
36         error_exit("fail to fork!");
37     if(pid == 0) {
38         char buff[] = "hello world!";
39         if(-1 == (fd = open("fifo",O_WRONLY)))
40             error_exit("fail to open!");
41         write(fd,buff,sizeof(buff));/*进程1把buff中数据向管道文件中写*/
42     //    write(fd,buff,sizeof(buff)+1);
43         /*进程1把buff中数据向管道文件中写:注意必须多写个'/n',可以让read函数读到'/n'时停止,避免多读垃圾数据*/
44     }
45 
46     if(pid > 0) {
47         char ret[64] = {0};
48         if(-1 == (fd = open("fifo",O_RDONLY)))
49             error_exit("fail  to open!");
50 
51         memset(ret,'a',sizeof(ret));//注意上面的strlen+1
52         read(fd,ret,sizeof(ret));     /*子进程从管道中读取到ret
53                                 read 有阻塞等待功能,碰到‘\0’停止读取*/
54         printf("ret = %s\n",ret);
55     }
56     close(fd);
57     return 0;
58 }
59 
60 /*
61  * #include <string.h>
62  *        void *memset(void *s, int c, size_t n);
63  *        DESCRIPTION
64  *               The  memset()  function  fills  the  first  n  bytes of the memory area
65  *                                pointed to by s with the constant byte c.
66  *
67  * */

 

posted @ 2017-03-14 21:29  bkycrmn  阅读(136)  评论(0)    收藏  举报