服务器上的网站怎么做301蜜雪冰城推广软文
什么是管道?
管道的本质是操作系统在内核中创建出的一块缓冲区,也就是内存
管道的应用
$ 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.h>#define ERR_EXIT(m) \{ \perror(#m" error\n"); \exit(EXIT_FAILURE); \}int main()
{int fds[2];pid_t pid;char buf[10] = {0};if (0 != pipe(fds))ERR_EXIT(pipe)pid = fork();if (-1 == pid)ERR_EXIT(fork)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 "./myfifo"int main()
{
#if 0umask(0); // prw-rw-rw-
#else/** prw-rw-r--* because 0666 & ~022 = 0644*/
#endifif (mkfifo(MYFIFO, 0666) < 0&& EEXIST != errno){perror("mkfifo error");return EXIT_FAILURE;}printf("successfully create FIFO file '%s'\n", MYFIFO);return 0;
}
命名管道打开规则
利用匿名管道实现文件拷贝 demo
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#define MYFIFO "./myfifo"
#define S_FILE "./test.txt"#define ERR_EXIT(m) \{ \perror(#m" error\n"); \exit(EXIT_FAILURE); \}int main()
{int ifd = -1, ofd = -1;char buf[1024];int n;umask(0);if (mkfifo(MYFIFO, 0666) < 0&& EEXIST != errno)ERR_EXIT(mkfifo)ifd = open(S_FILE, O_RDONLY);if (ifd < 0)ERR_EXIT(open)ofd = open(MYFIFO, O_WRONLY);if (ofd < 0)ERR_EXIT(open)while ((n = read(ifd, buf, sizeof(buf))) > 0){if (n != write(ofd, buf, n)){printf("write error\n");return EXIT_FAILURE;}}close(ifd);close(ofd);return 0;
}#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#define MYFIFO "./myfifo"
#define D_FILE "./test.txt.bak"#define ERR_EXIT(m) \{ \perror(#m" error\n"); \exit(EXIT_FAILURE); \}int main()
{int ifd = -1, ofd = -1;char buf[1024];int n;umask(0);ifd = open(MYFIFO, O_RDONLY);if (ifd < 0)ERR_EXIT(open)ofd = open(D_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0666);if (ofd < 0)ERR_EXIT(open)while ((n = read(ifd, buf, sizeof(buf))) > 0){if (n != write(ofd, buf, n)){printf("write error\n");return EXIT_FAILURE;}}close(ifd);close(ofd);unlink(MYFIFO);return 0;
}
特点
1、可用于同一主机上的任意进程间通信,这是命名管道和匿名管道的最大区别
2、面向字节流
3、半双工通信
4、进程退出,命名管道被释放,但命名管道文件还在
5、内核会对命名管道操作进行同步与互斥