无名管道
无名管道概述
管道(pipe)又称无名管道。
无名管道是一种特殊类型的文件,在应用层体现为两个打开的文件描述符。任何一个进程在创建的时候,系统都会 给他分配4G的虚拟内存,分为3G的用户空间和1G 的内核空间,内核空间是所有进程公有的,无名管道就是创建在内核空间的,多个进程知道 同一个无名管道的空间,就可以利用他来进行通信。
无名管道虽然是在内核空间创建的,但是会给当前用户进程两个文件描述符,一个负责执行 读操作,一个负责执行写操作。
pipefd[0] 负责对管道执行读操作,
pipefd[1] 负责对管道执行写操作。
管道是最古老的UNIX IPC方式,其特点是
1、半双工,数据在同一时刻只能在一个方向上流动。
2、数据只能从管道的一端写入,从另一端读出。
3、写入管道中的数据遵循先入先出的规则。
4、管道所传送的数据是无格式的,这要求管道的读出方与写入方必须事先约定好数据的格 式,如多少字节算
一个消息等。
5、管道不是普通的文件,不属于某个文件系统,其只存在于内存中。
6、管道在内存中对应一个缓冲区。不同的系统其大小不一定相同。
7、从管道读数据是一次性操作,数据一旦被读走,它就从管道中被抛弃,释放空间以便写 更多的数据。
8、管道没有名字,只能在具有公共祖先的进程之间使用
无名管道的创建 – pipe函数
若要数据流从父进程流向子进程,则关闭父进程的读端(fd[0])与子进程的写端(fd[1]);反之,则可以使数据流从子进程流向父进程。
例子1
#include<stdio.h>
#include<unistd.h>
#include <sys/types.h>
//若要数据流从父进程流向子进程,则关闭父进程的读端(fd[0])与子进程的写端(fd[1]);反之,则可以使数据流从子进程流向父进程。半双工单向
//无名管道不以文件的形式出现在文档中 且只适用于父子
int main()
{
int fd[2]; // 两个文件描述符
pid_t pid;
char buff[20];
if(pipe(fd) < 0) // 创建管道
printf("Create Pipe Error!\n");
if((pid = fork()) < 0) // 创建子进程
printf("Fork Error!\n");
else if(pid > 0) // 父进程
{
close(fd[0]); // 关闭读端
write(fd[1], "hello world\n", 12);//strlen("hello world")+1
}
else
{
close(fd[1]); // 关闭写端
read(fd[0], buff, 20);
printf("%s", buff);
}
return 0;
}
例子2
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
//注意父进程在sleep时子进程在读取时属于无数据写入的堵塞
int main()
{
int fd[2];
int pid ;
char buf[128];
if(pipe(fd)==-1)
{
printf("creat pipe failed\n");
}
pid=fork();
if(pid<0)
{
printf("creat child failed\n");
}
else if(pid >0)
{
printf("this is father\n");
sleep(3);
close(fd[0]);
write(fd[1],"hello from father",strlen("hello from father\n"));
wait(NULL);
}
else if(pid==0)
{
printf("this is child\n");
close(fd[1]);
read(fd[0],buf,128);
printf("read from father:%s\n",buf);
exit(0);
}
}
例子3
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
//使用无名管道实现父子进程间的通信
//由于无名管道创建之后给当前进程两个文件描述符,所以如果是完全不相关的进程
//无法获取同一个无名管道的文件描述符,所以无名管道只能在具有亲缘关系的进程间通信
int main(int argc, char const *argv[])
{
//创建一个无名管道
int pipefd[2];
if(pipe(pipefd) == -1)
{
perror("fail to pipe");
exit(1);
}
//使用fork函数创建子进程
pid_t pid;
if((pid = fork()) < 0)
{
perror("fail to fork");
exit(1);
}
else if(pid > 0) // 父进程
{
//父进程负责给子进程发送数据
char buf[128] = {};
while(1)
{
//读字符串函数fgets函数的功能是从指定的文件中读一个字符串到字符数组中
//例如:fgets(str,n,fp);的意义是从fp所指的文件中读出n-1个字符送入 字符数组str中。
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
if(write(pipefd[1], buf, sizeof(buf)) == -1)
{
perror("fail to write");
exit(1);
}
}
}
else //子进程
{
//子进程接收父进程的数据
char buf[128] = "";
while(1)
{
if(read(pipefd[0], buf, sizeof(buf)) == -1)
{
perror("fail to read");
exit(1);
}
printf("from parent: %s\n", buf);
}
}
return 0;
}
无名管道的读写规律
1.读写端都存在,只读不写
如果管道中有数据,会正常读取数据
如果管道中没有数据,则读操作会阻塞等待,直到有数据为止。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
int pipefd[2];
if(pipe(pipefd) == -1)
{
perror("fail to pipe");
exit(1);
}
//读写端都存在,只读不写
//如果管道中有数据,会正常读取数据
//如果管道中没有数据,则读操作会阻塞等待,直到有数据为止
write(pipefd[1], "hello world", 11);
char buf[128] = "";
if(read(pipefd[0], buf, sizeof(buf)) == -1)
{
perror("fail to read");
exit(1);
}
printf("buf = %s\n", buf);
if(read(pipefd[0], buf, sizeof(buf)) == -1)
{
perror("fail to read");
exit(1);
}
printf("buf = %s\n", buf);
return 0;
}
2.读写端都存在,只写不读
如果一直执行写操作,则无名管道对应的缓冲区会被写满,写满之后,write函数也会阻塞等待
默认无名管道的缓冲区64K字节。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
int pipefd[2];
if(pipe(pipefd) == -1)
{
perror("fail to pipe");
exit(1);
}
//读写端都存在,只写不读
//如果一直执行写操作,则无名管道对应的缓冲区会被写满,写满之后,write函数也会阻塞等待
//默认无名管道的缓冲区64K字节
int num = 0;
while(1)
{
if(write(pipefd[1], "6666", 1024) == -1)
{
perror("fail to write");
exit(1);
}
num++;
printf("num = %d\n", num);
sleep(3);
}
return 0;
}
3.只有读端,没有写端
关闭写文件描述符,只有读端
如果原本管道中有数据,则读操作正常读取数据
如果管道中没有数据,则read函数会返回0
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char const *argv[])
{
int pipefd[2];
if(pipe(pipefd) == -1)
{
perror("fail to pipe");
exit(1);
}
write(pipefd[1], "hello world",11);
//关闭写文件描述符,只有读端
//如果原本管道中有数据,则读操作正常读取数据
//如果管道中没有数据,则read函数会返回0
close(pipefd[1]);
char buf[128] = "";
ssize_t bytes;
if((bytes = read(pipefd[0], buf, sizeof(buf))) == -1)
{
perror("fail to read");
exit(1);
}
printf("bytes = %ld\n", bytes);
printf("buf = %s\n", buf);
//清除字符串中的内容
memset(buf, 0, sizeof(buf));
if((bytes = read(pipefd[0], buf, sizeof(buf))) == -1)
{
perror("fail to read");
exit(1);
}
printf("bytes = %ld\n", bytes);
printf("buf = %s\n", buf);
return 0;
}
4.只有写端,没有读端
关闭读操作文件描述符,只有写端
如果关闭读端,一旦执行写操作,就会产生一个信号SIGPIPE(管道破裂),
这个信号的默认处理方式是退出进程
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void handler(int sig)
{
printf("SIGPIPE信号产生了,管道破裂了\n");
}
int main(int argc, char const *argv[])
{
signal(SIGPIPE, handler);
int pipefd[2];
if(pipe(pipefd) == -1)
{
perror("fail to pipe");
exit(1);
}
//关闭读操作文件描述符,只有写端
//如果关闭读端,一旦执行写操作,就会产生一个信号SIGPIPE(管道破裂),
//这个信号的默认处理方式是退出进程
close(pipefd[0]);
int num = 0;
while(1)
{
if(write(pipefd[1], "hello world", 1024) == -1)
{
perror("fail to write");
exit(1);
}
num++;
printf("num = %d\n", num);
}
return 0;
}