1.管道介绍
在Linux中,管道是Linux中的一种极为重要的 并且比较古老的通信方式,我们把一个进程链接到另一个进程的数据流称为一个“管道”;管道即一个大小固定的缓冲区,也是⼀种文件。它可以将一个程序的输出传递为另一个程序的输入。
管道分为两种:一种是匿名管道,一种是命名管道。
其中无名管道在实际中用处最为广泛,两者最大的区别在于有名管道就像数据结构中所讲的队列,有先进先出原则,可用函数mkfifo()创建,而无名管道只能用于有亲缘关系的进程之间。
2.无名管道
2.1无名管道简介

- 半双工,数据再同一时刻只能在一个方向上流动。
- 管道不是普通的文件,不属于某个文件系统,其只存在于内存中。
- 管道没有名称,只能在具有公共祖先的进程之间使用。
- 管道的缓冲区大小是有限的,在linux中,该缓冲区的大小固定为4k。
2.2无名管道的创建
函数原型:int pipe(int fd[2])
所属头文件:#include <unistd.h>
功能:创建一个无名管道
参数说明:
fd:管道的2个文件描述符,fd表示int类型数组的首地址。
当管道建立时,它会自动创建这两个文件描述符:
fd[0]是管道读端的描述符
fd[1]是管道写端的描述符
返回值:成功,返回0,否则返回-1,错误码放在errno。
无名管道示例函数
例1.创建匿名管道,写入和读取管道
#include<unistd.h> // 提供pipe(), write(), read(), close()等系统调用
#include<sys/types.h> // 提供系统数据类型(如pid_t)
#include<stdio.h> // 提供标准输入输出函数(如printf)
int main()
{
// 1. 创建管道
int fd_arr[2]; // 用于存储管道的两个文件描述符
int ret = pipe(fd_arr); // 创建管道,成功返回0,失败返回-1
if(ret == -1)
{
printf("create pipe failed\n");
return -1;
}
// 2. 写入和读取管道
write(fd_arr[1], "hello", 6); // 向写端写入字符串(包含结尾的'\0')
char buff[50] = {0}; // 初始化缓冲区
read(fd_arr[0], buff, 50); // 从读端读取数据到buff
printf("read data=%s\n", buff); // 打印读取的内容
// 3. 关闭管道
close(fd_arr[0]); // 关闭读端
close(fd_arr[1]); // 关闭写端
return 0;
}
运行结果

例2.父子进程通信
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/wait.h>
int main()
{
// 打印主进程 PID
printf("start---%d\n", getpid());
// 1. 创建无名管道
int fd_arr[2]; // fd_arr[0] 读端,fd_arr[1] 写端
int ret = pipe(fd_arr);
if(ret == -1)
{
printf("create pipe failed\n");
return -1;
}
// 2. fork 出子进程
pid_t pid = fork();
if(pid > 0) // 父进程逻辑
{
printf("entry main process\n");
sleep(5); // 先睡 5 秒,确保子进程先进入 read 阻塞等待
// 向管道写数据(写 20 字节,但字符串实际没那么长,会有多余垃圾字节)
write(fd_arr[1], "hello child\n", 20);
// 父进程关闭写端
close(fd_arr[1]);
// wait(NULL); // 如果加上这一行,父进程会等待子进程结束再退出
}
else if(pid == 0) // 子进程逻辑
{
char buff[50] = {0}; // 接收缓冲区
printf("wait reading\n");
// 从管道读数据(阻塞等待父进程写入)
read(fd_arr[0], buff, 50);
// 打印读取到的数据
printf("read data=%s\n", buff);
// 子进程关闭读端
close(fd_arr[0]);
printf("son---%d\n", getpid());
exit(0); // 子进程结束
}
// 父子进程都会执行到这里(因为 fork 之后两份代码都继续往下走)
printf("end---%d\n", getpid());
return 0;
}
运行结果

3.有名管道
3.1有名管道简介
- 有名管道可以使互不相关的两个进程互相通信。
- 有名管道可以通过路径名来指出,并且在文件系统中可见,但内容存放在内存中。
- 访问命名管道文件与访问文件系统中的其他文件相似,都是需要先用open打开文件,然后对文件进行读写数据。两个进程结束后,信息会自动消失,但管道文件路径依然存在。
- 进程通过文件IO来操作有名管道,可以用read()和write()来读和写。
- 有名管道遵循先进先出规则。
- 不支持如lseek() 操作。
3.2有名管道创建
函数原型:int mkfifo(const char *pathname, mode_t mode);
所属头文件:#include <fcntl.h> #include <sys/stat.h>
功能:创建有名函数
参数说明:
pathname:要创建的有名管道(FIFO)文件的路径名。
mode:新建FIFO文件的权限位(和open/chmod的权限位一致)。
返回值:0成功;1失败,并设置errno。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
if(access("mypipe",F_OK)!=0)
{
// 1. create named pipe
int ret = mkfifo("mypipe", 0700);
if(ret == -1)
{
printf("create pipe failed\n");
return -1;
}
}
// 2. open pipe for writing
int fd = open("mypipe", O_WRONLY);
if(fd == -1)
{
printf("open pipe failed\n");
return -2;
}
// 3. write to pipe
printf("write...\n");
write(fd, "hello", 10);
printf("write finished\n");
// 4. close pipe
close(fd);
return 0;
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
char buff[50] = {0}; // 初始化缓冲区为0
// 1. 以只读方式打开命名管道
int fd = open("mypipe", O_RDONLY);
if(fd == -1)
{
printf("open pipe failed\n");
return -1;
}
// 2. 从管道读取数据
int bytes_read = read(fd, buff, sizeof(buff) - 1); // 保留一个字节给'\0'
if(bytes_read > 0) {
buff[bytes_read] = '\0'; // 确保字符串正确结束
printf("Received: %s\n", buff); // 打印接收到的数据
}
// 3. 关闭管道
close(fd);
// 4. 删除管道文件(可选)
unlink("mypipe");
return 0;
}
打开两个终端运行程序
运行结果

353




被折叠的 条评论
为什么被折叠?



