无名管道
pipe
特点:
(1)它是半双工的(即数据只能在一个方向上流动),具有固定的读端和写端。(同一时间单向,一端读另一端就是写,管道中数据读走就没了)
(2)它只能用于具有亲缘关系的进程之间的通信(也是父子进程或者兄弟进程之间)。
(3)它可以看成是一种特殊的文件,对于它的读写也可以使用普通的read、write 等函数。但是它不是普通的文件,并不属于其他任何文件系统,并且只存在于内存中。
函数原型
int pipe(int pipefd[2]);
由参数pipefd返回两个文件描述符:pipefd[0]为读而打开,pipefd[1]为写而打开
返回值
成功返回0,出错返回-1
管道的建立存在于内核之中,如下图:
示例
//实现父进程写,子进程读。
//运行时让父进程先延迟2秒,子进程执行过程会堵塞2秒,等父进程写完才能读出来。
int main()
{
int fd[2];
int pid;
char buf[128]={'\0'};
if(pipe(fd)==-1){//创建管道
printf("create pipe failed\n");
}
pid=fork();//创建进程
if(pid<0){
printf("create child failed\n");
}else if(pid>0){
sleep(2);
close(fd[0]);//关闭父进程的读端
write(fd[1],"hello!!!",strlen("hello!!!"));
//父进程对管道写入数据
printf("this is father\n");
wait(NULL);
}else{
printf("this is child\n");
close(fd[1]);//关闭子进程的写端
read(fd[0],buf,128);
//子进程从管道读出数据到buf
printf("from father:%s\n",buf);
exit(0);
}
return 0;
}
this is child
this is father
from father:hello!!!
命名管道
FIFO
函数原型
int mkfifo(const char *pathname, mode_t mode);
返回值
返回值:成功返回0,出错返回-1
特点
(1)FIFO可以在无关的进程之间交换数据,与无名管道不同(FIFO类似于管道,它是一个单向数据流)。
(2)FIFO 有路径名(无名管道没有)与之相关联,它以一种特殊设备文件形式存在于文件系统中。
参数
pathname:生成的FIFO特殊文件名字
mode:与open函数中的 mode 相同(如:0600-读写)。一旦创建了一个 FIFO,就可以用一般的文件I/O函数操作它。
当 open 一个FIFO时,是否设置非阻塞标志(O_NONBLOCK)的区别:
若没有指定O_NONBLOCK(默认没指定)
只读open 要阻塞到某个其他进程为写而打开此 FIFO。
只写open 要阻塞到某个其他进程为读而打开它。
若指定了O_NONBLOCK,则只读 open 立即返回。而只写 open 将出错返回 -1, 如果没有进程已经为读而打开该 FIFO,其errno置ENXIO
示例1
// int mkfifo(const char *pathname, mode_t mode);
int main(){
mkfifo("./file",0600);
return 0;
}
示例2
//read
int main()
{
char buf[128]={'/0'};
if((mkfifo("./file",0600)==-1) && errno!=EEXIST){//创建管道
printf("mkfifo failure\n");
perror("why");
}
printf("this is read\n");
int fd = open("./file",O_RDONLY);//打开管道,O_RDINLY-只读模式打开,如果没有另一个进程以只写模式打开管道,程序会阻塞此处
printf("open file success\n");
read(fd,buf,128);//从管道内读数据
printf("context:%s\n",buf);
close(fd);//关闭管道
return 0;
}
单独运行read:
//write
int main()
{
char *buf="abcdefg.......";
printf("this is write\n");
int fd = open("./file",O_WRONLY);//打开管道,O_WRONLY-只写模式打开,如果没有另一个进程读它,程序会阻塞此处
printf("write file success\n");
sleep(5);
write(fd,buf,strlen(buf));//写入数据
return 0;
}
单独运行write:
两个都运行的情况下:
这时候,read.c程序在运行到read函数的时候,会堵塞5秒,直到write.c程序中的sleep结束后,write之后才会读到数据。
深入探究:
write.c
int main(){
int cnt=0;
int fd=open("./file",O_WRONLY);
printf("write open success\n");
char *buf="meaasge from fifo";
while(cnt!=5){
sleep(2);
cnt++;
if(cnt==3){
write(fd,"hhh",3);
}else{
write(fd,buf,strlen(buf));
}
}
// close(fd);
printf("done\n");
return 0;
}
read.c
int main(){
int ret;
int cont=0;
ret=mkfifo("./file",0600);
if(ret==-1 && errno!=EEXIST){
printf("mkfifo failed\n");
perror("why");
}
char buf[30]={'\0'};
int fd=open("./file",O_RDONLY);
printf("open success\n");
while(cont!=10){
cont++;
memset(buf,0,30);
int n=read(fd,buf,30);
printf("read %d byt :%s\n",n,buf);
}
close(fd);
return 0;
}
write一共写了5次,每次的间隔2秒,第三次写的不同内容,read中
读取10次,前5次中都是堵塞2秒等到write程序去写,然后才读到数据。后面5次是一口气快速打印出5个的。
FIFO的通信方式类似于在进程中使用文件来传输数据,只不过FIFO类型文件同时具有管道的特性。在数据读出时,FIFO管道中同时清除数据,并且“先进先出”。
正常write的时候,对于普通文件,打开文件之后,进行重复write,每次write都是会用追加的方式去写(比如第一次写了“123”,第二次写了“abc”那么最后文件里面的内容就是“123abc”)
但是在FIFO文件的时候,数据读出就会被清除,所以不会出现“累积的数据”。当没有数据写入的时候,read会直接返回,不会堵塞。