进程间通讯:IPC概念html
IPC:Interprocess Communication,经过内核提供的缓冲区进行数据交换的机制。c++
IPC通讯的方式:shell
通讯种类:bash
pipe通讯是单双工的。微信
#include <unistd.h> int pipe(int pipefd[2]);
例子:socket
#include <unistd.h> #include <sys/types.h> #include <stdio.h> int main(){ int fds[2]; pipe(fds); pid_t pid = fork(); if(pid == 0){ write(fds[1], "hello\n", 6); char buf[10] = {0}; int ret = read(fds[0], buf, sizeof buf); if(ret > 0){ printf("%s", buf); } } if(pid > 0){ char buf[10] = {0}; int ret = read(fds[0], buf, sizeof buf); if(ret > 0){ printf("%s", buf); } write(fds[1], "world\n", 6); sleep(1); } }
例子1:子进程写,父进程读。函数
#include <unistd.h> #include <sys/types.h> #include <stdio.h> int main(){ int fds[2]; pipe(fds); pid_t pid = fork(); if(pid == 0){ write(fds[1], "hello\n", 6); /* char buf[10] = {0}; int ret = read(fds[0], buf, sizeof buf); if(ret > 0){ printf("%s", buf); } */ } if(pid > 0){ char buf[10] = {0}; int ret = read(fds[0], buf, sizeof buf); if(ret > 0){ printf("%s", buf); } //write(fds[1], "world\n", 6); //sleep(1); } }
例子2:用管道实现【ps aux | grep bash】命令。学习
实现办法,用dup2函数把标准输出,重定向到写端;再把标准输入重定向到读端。code
#include <unistd.h> #include <sys/types.h> #include <stdio.h> int main(){ int fds[2]; pipe(fds); pid_t pid = fork(); int stdoutfd = dup(STDOUT_FILENO); if(pid == 0){ //close(fds[0]);//------------① dup2(fds[1], STDOUT_FILENO); execlp("ps", "ps", "aux", NULL); } if(pid > 0){ //close(fds[1]);//----------② dup2(fds[0], STDIN_FILENO); execlp("grep", "grep", "bash", NULL); dup2(stdoutfd, STDOUT_FILENO); } }
运行结果:发现程序没有结束,阻塞住了,必须按ctol-c才能结束。htm
ys@ys:~/test$ ./pi2 ys 1551 0.0 0.2 29692 5548 pts/0 Ss 10:05 0:00 bash ys 2316 0.0 0.2 29560 5328 pts/1 Ss+ 11:33 0:00 bash ys 2486 0.0 0.0 21536 1060 pts/0 S+ 11:56 0:00 grep bash
用【ps aux】调查一下,发现,因为父进程【grep bash】没有结束尚未回收子进程,致使【ps】变成了僵尸进程。
ys 2437 0.0 0.0 21536 1088 pts/0 S+ 11:50 0:00 grep bash ys 2438 0.1 0.0 0 0 pts/0 Z+ 11:50 0:00 [ps] <defunct> ys 2439 0.0 0.1 44472 3800 pts/1 R+ 11:50 0:00 ps aux
为何父进程【grep bash】没有结束呢?确实在子进程里给父进程【ps aux】的输出结果了啊!
这是grep命令自己的缘故,在终端执行【grep bash】的话,就变成了阻塞状态,grep在等待标准输入,若是输入了【bash】grep就会给出结果,可是仍是在继续等待标准输入,因此这就是父进程没有结束,阻塞在【grep bash】那里的缘由。
解决办法:告诉【grep】,管道的写端不会再写入数据了后,grep就不会再继续等待,因此grep就会结束。grep的结束了,父进程也就结束了,因此僵尸进程也就自动消失了。
须要改代码的地方是②处,加上【close(fds[1]);】,就告诉了grep,已经没有写入了,因此grep就不会阻塞,父进程就可以结束掉。
注意:其实应该在子进程里也应该加上【close(fds[1]);】,才能达到写端所有关闭了,为何没写也没错误呢,由于子进程先执行结束了,进程结束后,系统会自动把进程中打开的文件描述符所有关闭,因此没在子进程里写关闭写端的代码,也没出问题。
管道有以下的规则:
例子1:写端所有关闭:read函数返回0。
在①和②两处必须都关闭写端,read函数才能返回0.
#include <unistd.h> #include <sys/types.h> #include <stdio.h> #include <sys/wait.h> int main(){ int fds[2]; pipe(fds); pid_t pid = fork(); if(pid == 0){ char buf[10] = {0}; int ret = read(fds[0], buf, sizeof buf); if(ret > 0){ printf("%s", buf); } close(fds[1]);//----① sleep(1); if(read(fds[0],buf, sizeof buf) == 0){ printf("all closed\n"); } } if(pid > 0){ int ret = write(fds[1], "hello\n", 6); close(fds[1]);//------② wait(NULL); } }
例子2:读端所有关闭:write函数会产生SIGPIPE信号,程序异常结束。
在①和②两处必须都关闭读端,write函数会产生SIGPIPE信号。
#include <unistd.h> #include <sys/types.h> #include <stdio.h> #include <sys/wait.h> int main(){ int fds[2]; pipe(fds); pid_t pid = fork(); if(pid == 0){ close(fds[0]);//------② int ret = write(fds[1], "hello\n", 6); } if(pid > 0){ close(fds[0]);//----① //close(fds[1]); int status; wait(&status); if(WIFSIGNALED(status)){ printf("killed by %d\n", WTERMSIG(status)); } } }
执行结果:【killed by 13】。13是SIGPIPE
查看系统默认的管道缓冲区的大小:ulimit -a
pipe size (512 bytes, -p) 8
查看系统默认的管道缓冲区的大小的函数:fpathconf
#include <unistd.h> long fpathconf(int fd, int name);
例子:
#include <unistd.h> #include <stdio.h> int main(){ int fds[2]; pipe(fds); long ret = fpathconf(fds[0], _PC_PIPE_BUF); printf("size:%ld\n", ret); }
执行结果:size:4096
上面的【例子:用管道实现【ps aux | grep bash】命令】有个问题,父进程直接调用了exec函数,致使没法在父进程中回收子进程的资源。下面的例子就去解决这个问题,方法是,不在父进程里调用exec函数,在2个兄弟子进程里分别调用exec函数,而后在父进程里回收资源。
#include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <sys/wait.h> int main(){ int fds[2]; pipe(fds); pid_t pid = fork(); if(pid == 0){ pid_t pid1 = fork(); if(pid1 == 0){ dup2(fds[1], STDOUT_FILENO); execlp("ps", "ps", "aux", NULL); } else if(pid1 > 0){ close(fds[1]);//----① dup2(fds[0], STDIN_FILENO); execlp("grep", "grep", "bash", NULL); //dup2(stdoutfd, STDOUT_FILENO); } } else if(pid > 0){ close(fds[1]);//----② wait(NULL); } }
注意在①和②处的关闭代码。
到此为止,能够看出来管道的
建立FIFO伪文件的命令:【mkfifo】
prw-r--r-- 1 ys ys 0 4月 29 15:59 myfifo
文件类型为P,大小为0。
也能够用函数:mkfifo建立
#include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode);
FIFO通讯原理:内核对fifo文件开辟一个缓冲区,操做fifo伪文件,就至关于操做缓冲区,实现里进程间的通讯。实际上就是文件读写。
FIFO例子:传进一个事先用mkfifo 建立好的FIFO文件。能够同时打开多个读端和写端。
写端:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main(int argc, char* argv[]){ printf("begin write\n"); int fd = open(argv[1], O_WRONLY); printf("end write\n"); int num = 0; char buf[20] = {0}; while(1){ sprintf(buf, "num=%04d\n", num++); write(fd, buf, strlen(buf)); sleep(1); } close(fd); }
读端:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <string.h> int main(int argc, char* argv[]){ printf("begin read\n"); int fd = open(argv[1], O_RDONLY); printf("end read\n"); int num = 0; char buf[20] = {0}; while(1){ memset(buf, 0x00, sizeof buf); int ret = read(fd, buf, sizeof buf); if(ret > 0){ printf("%s\n", buf); } else if(ret == 0){ break; } sleep(1); } close(fd); }
例子里有两个注意点:
open的时候是阻塞的,只有当读端和写端都打开后,open函数才会返回。非FIFO文件的open函数不是阻塞的。
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.
很是重要的一点:从fifo里读出数据后,这个被读出来的数据在fifo里就消失了。后面讲的mmap进程间通讯就不同,读完了,再读还有,由于是映射到内存了。
A进程发送一个mp3文件,B进程接收这个mp3文件,并存储到磁盘上,代码以下:
发送端:先取得mp3文件的大小,把文件的大小先发给接收端,而后在把文件的内容发过去。
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main(int argc, char* argv[]){ struct stat sbuf; int ret = stat("02.mp3", &sbuf); if(ret < 0){ perror("stat"); return -1; } //get file size int sz = sbuf.st_size; printf("size:%d\n", sz); char buf[20] = {0}; sprintf(buf, "%d", sz); //open fifo file int fd = open(argv[1], O_RDWR); //send file size write(fd, buf, sizeof(buf)); //open src mp3 file int src = open("02.mp3", O_RDONLY); char srcBuf[1024] = {0}; //send file content to dec file int sent = 0; while((sent = read(src, srcBuf, sizeof(srcBuf))) > 0){ write(fd, srcBuf, sent); memset(srcBuf, 0x00, sizeof(srcBuf)); } close(fd); close(src); }
接收端:先从发送端取得要发过来的MP3文件的大小,而后根据这个大小,先建立一个空的文件,而后再向这个空的文件里写内容。
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <string.h> int main(int argc, char* argv[]){ //open fifo file int fd = open(argv[1], O_RDONLY); //send file size char buf[20] = {0}; //get file size read(fd, buf, sizeof(buf)); int sz = atoi(buf); printf("sz:%d\n", sz); int dsc = open("des.mp3", O_RDWR|O_CREAT|O_TRUNC, 0666); int ret = ftruncate(dsc, sz); if(ret < 0){ perror("ftruncate"); return -1; } char srcBuf[1024] = {0}; //recv file content from src file int sent = 0; while((sent = read(fd, srcBuf, sizeof(srcBuf))) > 0){ write(dsc, srcBuf, sent); memset(srcBuf, 0x00, sizeof(srcBuf)); } close(fd); close(dsc); }