内容型网站有哪些,构建网站空间,注册一个投资公司需要多少钱,化妆品网站建设方案的预算什么是管道#xff1f; 管道的本质是操作系统在内核中创建出的一块缓冲区#xff0c;也就是内存
管道的应用 $ ps aux | grep xxx ps aux 的标准输出写到管道#xff0c;grep 从管道这块内存中读取数据来作为它的一个标准输入#xff0c;而且 ps 和 grep 之间是兄弟关系 管道的本质是操作系统在内核中创建出的一块缓冲区也就是内存
管道的应用 $ ps aux | grep xxx ps aux 的标准输出写到管道grep 从管道这块内存中读取数据来作为它的一个标准输入而且 ps 和 grep 之间是兄弟关系因为二者的父进程都是 bash
一、匿名管道
功能创建一个匿名管道#include unistd.h
int pipe(int fd[2]);
输出型参数 fd文件描述符数组其中fd[0] 是读端fd[1] 是写端
返回值成功返回 0失败返回 -1并设置错误码一个进程通过系统调用 pipe() 创建出一个匿名管道操作系统就会在内核中创建一块没有明确标识的缓冲区并返回给创建进程两个文件描述符作为管道的操作句柄供进程来操作管道其中一个文件描述符(fd[0])用于从管道中读另一个(fd[1])用于往管道中写返回两个文件描述符是为了让用户自己确定半双工的方向
由于匿名管道对应的这块缓冲区没有明确标识这也就意味着其他进程无法找到该缓冲区也就无法通信因此匿名管道只能用于具有亲缘关系的进程间通信因为子进程能复制父进程的文件描述符表
#include stdio.h
#include stdlib.h
#include unistd.hint main()
{int fds[2];pid_t pid;char buf[10] {0};if(0 ! pipe(fds)){perror(pipe error);exit(EXIT_FAILURE);}pid fork();if (-1 pid){perror(fork error);exit(EXIT_FAILURE);}if (0 pid){close(fds[0]); //关闭读端printf(child write data: hello\n);write(fds[1], hello, 5);close(fds[1]);exit(0);}close(fds[1]); //关闭写端read(fds[0], buf, sizeof(buf));printf(father read data: %s\n, buf);close(fds[0]);waitpid(pid, NULL, 0);return 0;
}
/** child write data: hello* father read data: hello */通过上述示例我们发现在操作匿名管道的时候完全是把它当作文件去使用的抛开 Linux 一切皆文件的思想主要还是因为这块内存是在内核中用户态的代码没法直接操作但是可以借助文件读写的系统函数来操作这块内存
特点 1、只能用于具有亲缘关系的进程像 ps aux | grep xxx 这种兄弟进程等 2、提供流式服务也就是面向字节流
优点读写灵活一次性写 10 字节分 10 次读或 5 次读或……也可以 1 字节/次分 10 次写缺点存在粘包问题原因是两条数据间没有明显的间隔
3、半双工通信(可以选择方向的单向传输a 可以给 b 发b 也可以给 a 发但是确定好方向后就只能这么发了此外还有全双工通信、单工通信(已经确定好方向的单向传输))双方彼此都进行通信时需要创建两个匿名管道 4、进程退出匿名管道被释放也就是匿名管道的生命周期随进程这里的进程指持有匿名管道的最后一个进程当然也可以主动关闭所有进程的有关匿名管道的那两个文件描述符 5、内核会对匿名管道操作进行同步与互斥
二、命名管道 内核中的一块有明确标识的缓冲区该标识实际上是一个管道文件p可见于文件系统这也就意味着同一主机上的任意进程都可以通过打开管道文件进而访问到内核中对应的缓冲区进行通信
注意管道文件并不是命名管道的本体仅是命名管道的入口即便通过 mkfifo 命令/函数创建出管道文件内核中也并没有与之对应的缓冲区
$ mkfifo myfifo
$ ll myfifo
prw-rw-r-- 1 mam mam 0 3月 18 16:16 myfifo或
功能创建一个管道文件#include sys/types.h
#include sys/stat.h
int mkfifo(const char *pathname, mode_t mode);
返回值成功返回 0失败返回 -1并设置错误码$ cat main.c
#include stdio.h
#include stdlib.h
#include errno.h
#include sys/types.h
#include sys/stat.h#define MYFIFO ./myfifoint main()
{
#if 0umask(0); // prw-rw-rw-
#else/** prw-rw-r--* because 0666 ~022 0644*/
#endifif (mkfifo(MYFIFO, 0666) 0 EEXIST ! errno){perror(mkfifo perror);return EXIT_FAILURE;}printf(successfully create FIFO file %s\n, MYFIFO);return 0;
}命名管道打开规则 1、当前为了读而打开 FIFO 时
O_NONBLOCK disableopen 调用阻塞直到有进程为写而打开该 FIFOO_NONBLOCK enable open 调用返回文件描述符
2、当前为了写而打开 FIFO 时
O_NONBLOCK disableopen 调用阻塞直到有进程为读而打开该 FIFOO_NONBLOCK enable open 调用返回 -1errno 值为 ENXIO
特点 1、可用于同一主机上的任意进程间通信这是命名管道和匿名管道的最大区别 2、面向字节流 3、半双工通信 4、进程退出命名管道被释放但命名管道文件还在 5、内核会对命名管道操作进行同步与互斥
验证如下 1、O_NONBLOCK disableopen 调用阻塞直到有进程为写而打开该 FIFO
#include stdio.h
#include stdlib.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h#define MYFIFO ./myfifoint main()
{int fd -1;if (-1 access(MYFIFO, F_OK) -1 mkfifo(MYFIFO, 0664)){perror(mkfifo error);return EXIT_FAILURE;}fd open(MYFIFO, O_RDONLY);printf(open %s for reading, fd: %d\n, MYFIFO, fd);if (fd 0)close(fd);return 0;
}2、O_NONBLOCK enable open 调用返回文件描述符
#include stdio.h
#include stdlib.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h#define MYFIFO ./myfifoint main()
{int fd -1;if (-1 access(MYFIFO, F_OK) -1 mkfifo(MYFIFO, 0664)){perror(mkfifo error);return EXIT_FAILURE;}fd open(MYFIFO, O_RDONLY | O_NONBLOCK);printf(open %s for reading, fd: %d\n, MYFIFO, fd);if (fd 0)close(fd);return 0;
}
/** open ./myfifo for reading, fd: 3*/3、O_NONBLOCK disableopen 调用阻塞直到有进程为读而打开该 FIFO
#include stdio.h
#include stdlib.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h#define MYFIFO ./myfifoint main()
{int fd -1;if (-1 access(MYFIFO, F_OK) -1 mkfifo(MYFIFO, 0664)){perror(mkfifo error);return EXIT_FAILURE;}fd open(MYFIFO, O_WRONLY);printf(open %s for writing, fd: %d\n, MYFIFO, fd);if (fd 0)close(fd);return 0;
}4、O_NONBLOCK enable open 调用返回 -1errno 值为 ENXIO(6)
#include stdio.h
#include stdlib.h
#include errno.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h#define MYFIFO ./myfifoint main()
{int fd -1;if (-1 access(MYFIFO, F_OK) -1 mkfifo(MYFIFO, 0664)){perror(mkfifo error);return EXIT_FAILURE;}fd open(MYFIFO, O_WRONLY | O_NONBLOCK);if (fd 0){perror(open error);printf(errno: %d\n, errno);return EXIT_FAILURE;}printf(open %s for writing, fd: %d\n, MYFIFO, fd);if (fd 0)close(fd);return 0;
}
/** open error: No such device or address* errno: 6*/三、管道读写规则