管道(PIPE):
我们把一个进程连接到另一个进程的一个数据流称之为管道,是Unix中最古老的进程间通信方式。我们可以分为匿名管道和命名管道。
1、匿名管道
特点:
- 只能用于具有血缘关系的进程之间通信
- 生命周期随进程,进程退出,管道释放
- 管道是半双工的,数据只能从一个方向传输
- 管道是基于字节流的
- 管道是自带同步机制的,在保证数据安全的前提下,按照特定顺序访问临界资源
函数原型:
#include <unisted>
int pipe(int fd[2]);
功能:创建一个匿名管道
参数:fd文件描述符数组,fd[0]表示读,fd[1]表示写
返回值:成功返回0,失败返回-1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd[2];
char buf[] = "hello world";
//创建管道
if(pipe(fd) == -1)
{
perror("pipe");
exit(1);
}
pid_t pid;
pid = fork();
//child
if(pid == 0)
{
close(fd[0]);
//通过管道写入数据
if( write(fd[1], buf, sizeof(buf)) == -1)
{
perror("write");
exit(1);
}
close(fd[1]);
exit(1);
}
//father
else
{
memset(buf, 0, sizeof(buf));
close(fd[1]);
//通过管道读取数据
if(read(fd[0], buf, sizeof(buf)) == -1)
{
perror("read");
exit(1);
}
printf("buf:%s\n", buf);
close(fd[0]);
exit(1);
}
return 0;
}
//结果:buf:hello world
2、命名管道
特点:
- 命名管道是一种特殊类型的文件
- 命名管道可以用于不具有血缘关系的进程
- 其他与匿名管道类似
- 缺点是长期存于系统中,使用不当容易出错并且缓冲区有限
//写入数据:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
mkfifo("my.pipe", 0664);
int outfd = open("my.pipe", O_WRONLY);
char buf[1024] = {0};
int n = 0;
while(fgets(buf, 1024, stdin))
{
write(outfd, buf, 1024);
memset(buf, 0, 1024);
}
close(outfd);
}
//读取数据:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int infd = open("my.pipe", O_RDONLY);
char buf[1024] = {0};
int n = 0;
while( (n = read(infd, buf, 1024)) > 0)
{
write(1, buf, n);
memset(buf, 0, 1024);
}
close(infd);
unlink("my.pipe");
return 0;
}
结果:
写入端
[root@livedvd tmp]# ./pipe_2
hello1
hello2
读取端
[root@livedvd tmp]# ./pipe_3
hello1
hello2
这样就实现的无关进程之间的通信。
匿名管道和命名管道的区别:
- 匿名管道由pipe函数创建并打开
- 命名管道由mkfifo函数创建,用open打开
- FIFO(命名管道)与PIPE(匿名管道)之间的区别在它们创建与打开的方式不同,一但这些工作完成之后,它们具有相同的功能。
参考:
https://blog.csdn.net/wei_cheng18/article/details/79658209
https://www.cnblogs.com/zhonglongbo/p/8976730.html