管道程序
1.1 管道介绍
1.2 匿名管道
只用于有亲缘关系的进程之间通信(单向传递),如父子进程之间。
- 具体实现:OS在内核缓冲区开辟一页内存,两个进程共同指向同一个物理界面。
- 自带同步互斥。
1.2.1 相关函数
/*
* @brief 创建匿名管道
* @param fd 文件描述符数组,fd[1]:写端,fd[0]:读端
* @return 0成功
*/
int pipe(int fd[2]);
/*
* @brief 写入数据
* @param fd 文件描述符
* @param buf 指定的缓冲区,即指针,指向一段内存单元
* @param nbyte 要写入文件指定的字节数
* @return 写入文档的字节数(成功);-1(出错)
*/
ssize_t write(int fd, const void *buf, size_t nbyte);
/*
* @brief 读取数据
* @param fd 文件描述符
* @param buf 指定的缓冲区,即指针,指向一段内存单元
* @param count 要读取指定的字节数
* @return 成功返回读取的字节数,出错返回-1并设置errno。
*/
ssize_t read(int fd, void *buf, size_t count);
//顺便提一下 sprintf(str, "...", 参数); => <stdlib.h>
1.2.2 流程
-
建立管道
#include <unistd.h> // 头文件 #include <sys/wait> // wait(0); 的头文件 /*建立一个长度为2的int数组*/ int fd[2]; /* 建立or初始化管道 * 注意:fd[1]为写端,fd[0]为读端 */ pipe(fd);
-
对管道进行写操作
close(fd[0]); //关闭读端 write(fd[1], str, 30); //写入str中的数据 close(fd[1]); //关闭写端
-
对管道进行读操作
close(fd[1]); //关闭写端 read(fd[0], str, sizeof(str)); //读取数据到str中 close(fd[0]); //关闭读端
1.3 命名管道
1.3.1 相关函数
/* 表头文件
* #include<sys/types.h>
* #include<sys/stat.h>
* @brief 建立命名管道,mkfifo()会依参数pathname建立特殊的FIFO文件,该文件必须不存在,
* 而参数mode为该文件的权限(mode%~umask),因此 umask值也会影响到FIFO文件的权限。
* @param name 文件名或路径
* @param mode 文件权限
* @return 若成功则返回0,否则返回-1,错误原因存于errno中。
*/
int mkfifo(const char *name,mode_t mode)
//打开管道文件
int fd =open(name,O_RDONLY);//读
int fd = open(name,O_WRONLY);//写
//read/write,语义和匿名管道一样
//close
//unlink("管道文件name")
1.3.2 流程
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(char argc,char *argv[])
{
mkfifo("tp",0666);//创建一个命名管道
int infd=open("abc",O_RDONLY);//表示要复制的文件
if(infd<0)
perror("open"),exit(1);
int outfd=open("tp",O_WRONLY);//写入到管道文件中
if(outfd<0)
perror("open"),exit(1);
char buf[100];
int n=0;
while((n=read(infd,buf,100))>0){
write(outfd,buf,n);//往outfd中写入数据
}
close(infd);
close(outfd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main(int argc,char *argv[])
{
int outfd=open("abc_bk",O_RDONLY|O_WRONLY|O_CREAT|O_TRUNC,0644);//需要拷贝的文件
if(outfd<0)
perror("open"),exit(1);
int infd=open("tp",O_RDONLY);//打开管道,将管道中的数据读走
if(infd<0)
perror("open"),exit(1);
char buf[100];
int n;
while((n=read(infd,buf,100))>0){
write(outfd,buf,n);
}
close(infd);
close(outfd);
unlink("tp");//删除指定名字的文件
return 0;
}