管道
什么是管道
- 管道是Unix中最古老的进程间通信形式。
- 行一个进程连接到另一个进程的一个数据流称为“管道”。
匿名管道
#include <unistd.h>
int pipe(int fd[2]);
功能: 创建匿名管道
参数:fd:⽂文件描述符数组,其中fd[0]表⽰示读端, fd[1]表⽰示写端
返回值: 成功返回0,失败返回错误代码
实现代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main()
{
int fds[2];
char buf[100];
int len;
if(pipe(fds) == -1)
perror("make pipe"),exit(1);
//读取数据
while(fgets(buf,100,stdin))
{
len = strlen(buf);
//写入管道
if(write(fds[1],buf,len) != len)
{
perror("write to pipe");
break;
}
memset(buf,0x00,sizeof(buf));
//从管道中读取
if((len=read(fds[0],buf,100)) == -1)
{
perror("read from pipe");
break;
}
//显示管道中内容
if(write(1,buf,len) != len)
{
perror("write to stdout");
break;
}
}
}
用fork来共享管道原理
文件描述符角度-深度理解管道
内核角度-管道本质
由此可见,管道的使用和文件一样,正如Linux中的一切皆文件思想。
fork进程代码实现
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
}while(0)
int main()
{
int pipefd[2];
char buf[10] = {0};
if(pipe(pipefd) == -1)//创建pipe管道
ERR_EXIT("pipe error");
pid_t pid;
pid = fork();//创建子进程
if(pid == -1)
ERR_EXIT("fork error");
if(pid == 0)
{
close(pipefd[0]);//关闭子进程读操作
write(pipefd[1],"hello",5);//写入字符
printf("pid = %d,ppid = %d\n",getpid(),getppid());//确认为子进程
close(pipefd[1]);
exit(EXIT_SUCCESS);//退出子进程
}
printf("pid = %d,ppid = %d\n",getppid(),getpid());//确认为父进程
close(pipefd[1]);//关闭写操作
read(pipefd[0],buf,10);//读10个字符
printf("buf = %s\n",buf);//显示读的字符
return 0;
}
匿名管道读写规则
没有数据时:
- O_NONBLOCK disable:read调⽤用阻塞,即进程暂停执行,一直等到有数据来到为⽌止。
- O_NONBLOCK enable:read调⽤用返回-1,errno值为EAGAIN。
当管道满时:
- O_NONBLOCK disable: write调⽤用阻塞,直到有进程读走数据。
- O_NONBLOCK enable:调⽤用返回-1,errno值为EAGAIN。
如果所有管道写端对应的文件描述符被关闭,则read返回0。
如果所有管道读端对应的文件描述符被关闭,则write操作会产生信号SIGPIPE,进而可能导致write进程退出。
当要写入的数据量不大于PIPE_BUF时,linux将保证写入的原子性。
当要写入的数据量大于PIPE_BUF时,linux将不再保证写入的原子性。
匿名管道pipe的特点
- 只能用于具有共同祖先的进程(具有亲缘关系的进程)之间进行通信;通常,一个管道由一个进程创建,然后该进程调用fork,此后父、子进程之间就可应用该管道。
- 管道提供流式服务。
- 一般而⾔言,进程退出,管道释放,所以管道的生命周期随进程。
- 管道是半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道。
命名管道
进行不相关进程的进程间通信。
创建命名管道
命名管道可以从命令行上创建:
$ mkfilo filename
从程序创建:
int mkfile(const char *filename,mode_t mode);
创建命名管道:
int main(int argc, char &argv[])
{
mkfifo("管道名(一个FIFO文件)", 0644);
return 0;
}
匿名管道与命名管道的区别
- 匿名管道由pipe函数创建并打开。
- 命名管道由mkfile函数创建,open函数打开。
- 匿名管道用于亲缘进程间的通信,命名管道用于不相关进程间的通信。
命名管道的读写规则
读操作:
- O_NONBLOCK disable:阻塞直到有相应进程为写而打开该FIFO
- O_NONBLOCK enable:立刻返回成功
写操作:
- O_NONBLOCK disable:阻塞直到有相应进程为读而打开该FIFO
- O_NONBLOCK enable:立刻返回失败,错误码为ENXIO
命名管道实现文件拷贝
读取文件,写入命名管道:
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
}while(0)
int main()
{
int infd;
int outfd;
char buf[1024];
int n;
mkfifo("tp.c", 0644);//创建命名管道文件
infd = open("1.c", O_RDONLY);
if(infd == -1)
ERR_EXIT("open");
outfd = open("tp.c", O_WRONLY);
if(outfd == -1)
ERR_EXIT("open");
while((n = read(infd, buf, 1024)) > 0)//读取文件内容到buf上
{
write(outfd, buf, n);//变量内容写到管道文件中
}
close(infd);//关闭读操作
close(outfd);//关闭写操作
return 0;
}
读取文件,从命名管道读出:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
}while(0)
int main()
{
int infd;
int outfd;
int n;
char buf[1024];
outfd = open("1.c.bak",O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(outfd == -1)
ERR_EXIT("open");
infd = open("tp.c",O_RDONLY);
if(outfd == -1)
ERR_EXIT("open");
while((n = read(infd,buf,1024)) > 0)
{
write(outfd,buf,n);
}
close(infd);
close(outfd);
unlink("tp.c");
return 0;
}