Linux C编程第十一章 进程间通信1

网友投稿 673 2022-05-29

一、整体大纲

二、进程间通信概念及方法

Linux环境下,进程地址空间相互独立,每个进程各自有不同的用户地址空间。任何一个进程的全局变量在另一个进程中都看不到,所以进程和进程之间不能相互访问,要交换数据必须通过内核,在内核中开辟一块缓冲区,进程1把数据从用户空间拷到内核缓冲区,进程2再从内核缓冲区把数据读走,内核提供的这种机制称为进程间通信(IPC,InterProcess Communication)。

在进程间完成数据传递需要借助操作系统提供特殊的方法,如:文件、管道、信号、共享内存、消息队列、套接字、命名管道等。随着计算机的蓬勃发展,一些方法由于自身设计缺陷被淘汰或者弃用。现今常用的进程间通信方式有:

1)管道 (使用最简单)

2)信号 (开销最小)

3)共享映射区 (无血缘关系)

4)本地套接字 (最稳定)

三、进程间通信方法介绍

1. 管道

(1)管道的概念:

管道是一种最基本的IPC机制,作用于有血缘关系的进程之间,完成数据传递。调用pipe系统函数即可创建一个管道。有如下特质:

1) 其本质是一个伪文件(实为内核缓冲区)

2)由两个文件描述符引用,一个表示读端,一个表示写端。

3) 规定数据从管道的写端流入管道,从读端流出。

管道的原理: 管道实为内核使用环形队列机制,借助内核缓冲区(4k)实现。

管道的局限性:

1) 数据一旦被读走,便不在管道中存在,不可反复读取。

2) 由于管道采用半双工通信方式。因此,数据只能在一个方向上流动。

3) 只能在有公共祖先的进程间使用管道。

常见的通信方式有,单工通信、半双工通信、全双工通信。

【Linux C编程】第十一章 进程间通信1

(2)管道相关函数

创建管道

int pipe(int pipefd[2]);               成功:0;失败:-1,设置errno

函数调用成功返回r/w两个文件描述符。无需open,但需手动close。规定:fd[0] → r; fd[1] → w,就像0对应标准输入,1对应标准输出一样。向管道文件读写数据其实是在读写内核缓冲区。

管道创建成功以后,创建该管道的进程(父进程)同时掌握着管道的读端和写端。如何实现父子进程间通信呢?通常可以采用如下步骤:

1)父进程调用pipe函数创建管道,得到两个文件描述符fd[0]、fd[1]指向管道的读端和写端。

2)父进程调用fork创建子进程,那么子进程也有两个文件描述符指向同一管道。

3)父进程关闭管道读端,子进程关闭管道写端。父进程可以向管道中写入数据,子进程将管道中的数据读出。由于管道是利用环形队列实现的,数据从写端流入管道,从读端流出,这样就实现了进程间通信。

练习:父子进程使用管道通信,父写入字符串,子进程读出并打印到屏幕?

父进程写子进程读

1 #include 2 #include 3 #include 4 5 int main() 6 { 7 int fd[2]; 8 pipe(fd); 9 pid_t pid = fork(); 10 11 if (pid == 0) 12 { 13 //子进程关闭写端 14 close(fd[1]); 15 char buf[256] = {0}; 16 while(1) 17 { 18 memset(buf, 0, sizeof(buf)); 19 int ret = read(fd[0], buf, sizeof(buf)); 20 if (ret == 0) 21 { 22 printf("read over!\n"); 23 break; 24 } 25 else if(ret > 0) 26 { 27 write(STDOUT_FILENO,buf,ret); 28 } 29 } 30 } 31 else if (pid > 0) 32 { 33 //父进程关闭读端 34 close(fd[0]); 35 char buf[256] = {0}; 36 int num = 0; 37 while(1) 38 { 39 memset(buf, 0, sizeof(buf)); 40 sprintf(buf, "data from parent %d\n", num++); 41 write(fd[1], buf, sizeof(buf)); 42 sleep(1); 43 } 44 } 45 46 return 0; 47 }

(3)管道的读写行为

使用管道需要注意以下4种特殊情况(假设都是阻塞I/O操作,没有设置O_NONBLOCK标志):

1)如果所有指向管道写端的文件描述符都关闭了(管道写端引用计数为0),而仍然有进程从管道的读端读数据,那么管道中剩余的数据都被读取后,再次read会返回0,就像读到文件末尾一样。

2) 如果有指向管道写端的文件描述符没关闭(管道写端引用计数大于0),而持有管道写端的进程也没有向管道中写数据,这时有进程从管道读端读数据,那么管道中剩余的数据都被读取后,再次read会阻塞,直到管道中有数据可读了才读取数据并返回。

3)如果所有指向管道读端的文件描述符都关闭了(管道读端引用计数为0),这时有进程向管道的写端write,那么该进程会收到信号SIGPIPE,通常会导致进程异常终止。当然也可以对SIGPIPE信号实施捕捉,不终止进程。具体方法信号章节详细介绍。

4)如果有指向管道读端的文件描述符没关闭(管道读端引用计数大于0),而持有管道读端的进程也没有从管道中读数据,这时有进程向管道写端写数据,那么在管道被写满时再次write会阻塞,直到管道中有空位置了才写入数据并返回。

总结:

1)读管道:  1. 管道中有数据,read返回实际读到的字节数。

2. 管道中无数据:

(1) 管道写端被全部关闭,read返回0 (好像读到文件结尾)

(2) 写端没有全部被关闭,read阻塞等待(不久的将来可能有数据递达,此时会让出cpu)

2)写管道:  1. 管道读端全部被关闭, 进程异常终止(也可使用捕捉SIGPIPE信号,使进程不终止)

2. 管道读端没有全部关闭:

(1) 管道已满,write阻塞。

(2) 管道未满,write将数据写入,并返回实际写入的字节数。

练习1:使用管道实现父子进程间通信,完成:ls | wc -l。假定父进程实现ls,子进程实现wc?

注意:ls命令正常会将结果集写出到stdout,但现在会写入管道的写端;wc -l 正常应该从stdin读取数据,但此时会从管道的读端读。

实现父进程ls 子进程wc -l

1 #include 2 #include 3 4 int main() 5 { 6 int fd[2]; 7 pipe(fd); 8 9 pid_t pid = fork(); 10 if(pid == 0){ 11 //son 12 //son -- > ls 13 //关闭 写端 14 close(fd[1]); 15 //1. 先重定向 16 dup2(fd[0], STDIN_FILENO);//标准输入重定向到管道写端 17 //2. execlp 18 execlp("wc","wc","-l",NULL); 19 }else if(pid > 0){ 20 //parent 21 //关闭读端 22 close(fd[0]); 23 //1. 先重定向,标准输出重定向到管道读端 24 dup2(fd[1], STDOUT_FILENO); 25 //2. execlp 26 execlp("ls","ls",NULL); 27 } 28 29 return 0; 30 }

父子进程实现ps aux | grep bash

1 #include 2 #include 3 4 int main() 5 { 6 int fd[2]; 7 pipe(fd); 8 9 pid_t pid = fork(); 10 if(pid == 0){ 11 //son 12 //son -- > ps 13 //关闭 读端 14 close(fd[0]); 15 //1. 先重定向 16 dup2(fd[1],STDOUT_FILENO);//标准输出重定向到管道写端 17 //2. execlp 18 execlp("ps","ps","aux",NULL); 19 }else if(pid > 0){ 20 //parent 21 //关闭写端 22 close(fd[1]); 23 //1. 先重定向,标准输入重定向到管道读端 24 dup2(fd[0],STDIN_FILENO); 25 //2. execlp 26 execlp("grep","grep","bash",NULL); 27 } 28 29 return 0; 30 }

执行结果分析:程序执行,发现程序执行结束,shell还在阻塞等待用户输入。这是因为,shell → fork → ./pipe2, 程序pipe2的子进程将stdin重定向给管道,父进程执行的ls会将结果集通过管道写给子进程。若父进程在子进程打印wc的结果到屏幕之前被shell调用wait回收,shell就会先输出$提示符。

练习2:使用管道实现兄弟进程间通信。 兄:ls  弟: wc -l  父:等待回收子进程?要求,使用“循环创建N个子进程”模型创建兄弟进程,使用循环因子i标示。注意管道读写行为。

实现思路:父进程关闭读写端,两个子进程,一个关闭管道的读端去写,一个关闭管道的写端去读。

兄弟进程间实现ls | wc -l

1 #include 2 #include 3 4 int main() 5 { 6 int fd[2]; 7 pid_t pid; 8 int n = 2, i = 0; 9 pipe(fd); 10 11 for (i = 0; i < n; i++) 12 { 13 pid = fork(); 14 if (pid == 0) 15 { 16 break; 17 } 18 } 19 20 //兄弟进程 21 if (i == 0) 22 { 23 printf("Brother1 pid = %d, ppid = %d\n", getpid(), getppid()); 24 //1. 关闭写端 25 close(fd[1]); 26 //2. 先重定向 27 dup2(fd[0], STDIN_FILENO);//标准输入重定向到管道写端 28 //3. 执行execlp 29 execlp("wc","wc","-l",NULL); 30 31 } 32 //兄弟进程 33 else if (i == 1) 34 { 35 printf("Brother2 pid = %d, ppid = %d\n", getpid(), getppid()); 36 //1. 关闭读端 37 close(fd[0]); 38 //2. 先重定向,标准输出重定向到管道读端 39 dup2(fd[1], STDOUT_FILENO); 40 //3. execlp 41 execlp("ls","ls",NULL); 42 43 } 44 45 else if (i == 2) 46 { 47 //parent 48 //父亲需要关闭读写两端 49 close(fd[0]); 50 close(fd[1]); 51 //回收子进程 52 wait(NULL); 53 wait(NULL); 54 } 55 56 return 0; 57 }

兄弟进程实现ps aux | grep bash

1 #include 2 #include 3 #include 4 #include 5 6 int main() 7 { 8 int fd[2]; 9 pipe(fd); 10 pid_t pid; 11 int n =2,i = 0; 12 for(i = 0; i < n; i ++){ 13 pid = fork(); 14 if(pid == 0){ 15 break; 16 } 17 } 18 19 //i = 0 ,代表兄长,1 - 代表弟弟,2- 父亲 20 if(i == 0){ 21 //兄长进程 22 //1. 关闭读端 23 close(fd[0]); 24 //2. 重定向 25 dup2(fd[1],STDOUT_FILENO); 26 //3. 执行 execlp 27 execlp("ps","ps","aux",NULL); 28 29 }else if(i == 1){ 30 //弟弟 31 //1. 关闭写端 32 close(fd[1]); 33 //2. 重定向 34 dup2(fd[0],STDIN_FILENO); 35 //3. 执行ececlp 36 execlp("grep","grep","bash",NULL); 37 38 }else if(i == 2){ 39 //parent 40 //父亲需要关闭读写两端 41 close(fd[0]); 42 close(fd[1]); 43 //回收子进程 44 wait(NULL); 45 wait(NULL); 46 } 47 48 49 return 0; 50 }

测试:是否允许,一个pipe有一个写端多个读端呢?是否允许有一个读端多个写端呢?

一写多读

1 #include 2 #include 3 #include 4 5 int main() 6 { 7 int fd[2]; 8 pid_t pid; 9 int n = 3, i = 0; 10 pipe(fd); 11 12 for (i = 0; i < n; i++) 13 { 14 pid = fork(); 15 if (pid == 0) 16 { 17 break; 18 } 19 } 20 21 //兄弟进程 22 if (i == 0) 23 { 24 printf("Brother1 pid = %d, ppid = %d\n", getpid(), getppid()); 25 //1. 关闭写端 26 close(fd[1]); 27 //2. 先重定向 28 dup2(fd[0], STDIN_FILENO);//标准输入重定向到管道写读端 29 //3. 执行execlp 30 char buf[256] = {0}; 31 while(1) 32 { 33 memset(buf, 0, sizeof(buf)); 34 int ret = read(fd[0], buf, sizeof(buf)); 35 if (ret == 0) 36 { 37 printf("read over"); 38 break; 39 } 40 } 41 } 42 43 //兄弟进程 44 else if (i == 1) 45 { 46 printf("Brother2 pid = %d, ppid = %d\n", getpid(), getppid()); 47 //1. 关闭读端 48 close(fd[1]); 49 //2. 先重定向,标准输出重定向到管道读端 50 dup2(fd[0], STDIN_FILENO); 51 char buf[256] = {0}; 52 while(1) 53 { 54 memset(buf, 0, sizeof(buf)); 55 int ret = read(fd[0], buf, sizeof(buf)); 56 if (ret == 0) 57 { 58 printf("read over"); 59 break; 60 } 61 } 62 } 63 //兄弟进程 64 else if (i == 2) 65 { 66 printf("Brother3 pid = %d, ppid = %d\n", getpid(), getppid()); 67 //1. 关闭读端 68 close(fd[0]); 69 //2. 先重定向,标准输出重定向到管道读端 70 dup2(fd[1], STDOUT_FILENO); 71 //3. 写数据到写端 72 int num = 0; 73 char buf[256] = {0}; 74 while(1) 75 { 76 memset(buf, 0, sizeof(buf)); 77 sprintf(buf, "from broth3 %d\n", num++); 78 int ret = write(fd[1], buf, sizeof(buf)); 79 if (ret == -1) 80 { 81 perror("write err:"); 82 break; 83 } 84 sleep(1); 85 } 86 } 87 else if (i == 3) 88 { 89 //parent 90 //父亲需要关闭读写两端 91 close(fd[0]); 92 close(fd[1]); 93 //回收子进程 94 wait(NULL); 95 wait(NULL); 96 wait(NULL); 97 } 98 99 return 0; 100 }

结论:一个读多个写会hang住。

多写一读

1 #include 2 #include 3 #include 4 5 int main() 6 { 7 int fd[2]; 8 pid_t pid; 9 int n = 3, i = 0; 10 pipe(fd); 11 12 for (i = 0; i < n; i++) 13 { 14 pid = fork(); 15 if (pid == 0) 16 { 17 break; 18 } 19 } 20 21 //兄弟进程 22 if (i == 0) 23 { 24 printf("Brother1 pid = %d, ppid = %d\n", getpid(), getppid()); 25 //1. 关闭写端 26 close(fd[1]); 27 //2. 先重定向 28 dup2(fd[0], STDIN_FILENO);//标准输入重定向到管道写读端 29 //3. 执行execlp 30 char buf[256] = {0}; 31 while(1) 32 { 33 memset(buf, 0, sizeof(buf)); 34 int ret = read(fd[0], buf, sizeof(buf)); 35 if (ret == 0) 36 { 37 printf("read over"); 38 break; 39 } 40 } 41 } 42 //兄弟进程 43 else if (i == 1) 44 { 45 printf("Brother2 pid = %d, ppid = %d\n", getpid(), getppid()); 46 //1. 关闭读端 47 close(fd[0]); 48 //2. 先重定向,标准输出重定向到管道读端 49 dup2(fd[1], STDOUT_FILENO); 50 int num = 0; 51 char buf[256] = {0}; 52 while(1) 53 { 54 memset(buf, 0, sizeof(buf)); 55 sprintf(buf, "from broth2 %d\n", num++); 56 int ret = write(fd[1], buf, sizeof(buf)); 57 if (ret == -1) 58 { 59 perror("write err:"); 60 break; 61 } 62 sleep(1); 63 } 64 } 65 //兄弟进程 66 else if (i == 2) 67 { 68 printf("Brother3 pid = %d, ppid = %d\n", getpid(), getppid()); 69 //1. 关闭读端 70 close(fd[0]); 71 //2. 先重定向,标准输出重定向到管道读端 72 dup2(fd[1], STDOUT_FILENO); 73 //3. 写数据到写端 74 int num = 0; 75 char buf[256] = {0}; 76 while(1) 77 { 78 memset(buf, 0, sizeof(buf)); 79 sprintf(buf, "from broth3 %d\n", num++); 80 int ret = write(fd[1], buf, sizeof(buf)); 81 if (ret == -1) 82 { 83 perror("write err:"); 84 break; 85 } 86 sleep(1); 87 } 88 } 89 else if (i == 3) 90 { 91 //parent 92 //父亲需要关闭读写两端 93 close(fd[0]); 94 close(fd[1]); 95 //回收子进程 96 wait(NULL); 97 wait(NULL); 98 wait(NULL); 99 } 100 101 return 0; 102 }

结论:一个写多个读会hang住。

(4)管道缓冲区大小

可以使用ulimit -a 命令来查看当前系统中创建管道文件所对应的内核缓冲区大小。通常为:

pipe size (512 bytes, -p) 8

也可以使用fpathconf函数,借助参数        选项来查看。使用该宏应引入头文件

long fpathconf(int fd, int name);      成功:返回管道的大小         失败:-1,设置errno

(5)管道优劣

优点:简单,相比信号,套接字实现进程间通信,简单很多。

缺点:1. 只能单向通信,双向通信需建立两个管道。

2. 只能用于父子、兄弟进程(有共同祖先)间通信。该问题后来使用fifo有名管道解决。

2. FIFO

FIFO常被称为有名管道,以区分管道(pipe)。管道(pipe)只能用于“有血缘关系”的进程间。但通过FIFO,不相关的进程也能交换数据。

FIFO是Linux基础文件类型中的一种。但FIFO文件在磁盘上没有数据块,仅仅用来标识内核中一条通道。各进程可以打开这个文件进行read/write,实际上是在读写内核通道,这样就实现了进程间通信。

创建方式:

1. 命令:mkfifo 管道名

2. 库函数:int mkfifo(const char *pathname,  mode_t mode);  成功:0; 失败:-1

一旦使用mkfifo创建了一个FIFO,就可以使用open打开它,常见的文件I/O函数都可用于fifo。如:close、read、write、unlink等。

fifo_w.c

1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 9 int main(int argc,char * argv[]) 10 { 11 if(argc != 2){ 12 printf("./a.out fifoname\n"); 13 return -1; 14 } 15 16 int ret = mkfifo(argv[1], 0666); 17 if (ret < 0 && errno != EEXIST) 18 { 19 printf("create fifo file %s failed\n", argv[1]); 20 return -1; 21 } 22 23 // 创建一个 myfifo 文件 24 //打开fifo文件 25 printf("begin open ....\n"); 26 int fd = open(argv[1],O_WRONLY); 27 printf("end open ....\n"); 28 //写 29 char buf[256]; 30 int num = 1; 31 while(1){ 32 memset(buf,0x00,sizeof(buf)); 33 sprintf(buf,"from write data:%04d",num++); 34 write(fd,buf,strlen(buf)); 35 sleep(1); 36 //循环写 37 } 38 //关闭描述符 39 close(fd); 40 return 0; 41 }

fifo_r.c

1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 8 int main(int argc,char *argv[]) 9 { 10 if(argc != 2){ 11 printf("./a.out fifoname\n"); 12 return -1; 13 } 14 printf("begin oepn read...\n"); 15 int fd = open(argv[1],O_RDONLY); 16 printf("end oepn read...\n"); 17 18 char buf[256]; 19 int ret; 20 while(1){ 21 //循环读 22 memset(buf,0x00,sizeof(buf)); 23 ret = read(fd,buf,sizeof(buf)); 24 if(ret > 0){ 25 printf("read:%s\n",buf); 26 } 27 } 28 29 close(fd); 30 return 0; 31 }

注意:

FIFOs

Opening the read or write end of a FIFO blocks until the other end is also opened (by another process or thread). See fifo(7) for further details.

open注意事项,打开fifo文件的时候,read端会阻塞等待write端open,write端同理,也会阻塞等待另外一端打开。

【Linux C编程】第十一章 进程间通信2

Linux 任务调度

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:用管理产品和管理球队的方式管理员工,读《奈飞文化手册》有感
下一篇:Android之系统服务-WindowManager
相关文章