亲缘关系之间共享文件描述符,所以可以通信。
pipe()
/*
#include <unistd.h>
int pipe(int pipefd[2]);
功能:
创建一个命名管道来进程间通信
参数:
- int pipefd[2]:传出参数
-pipefd[0] : 读端
-pipefd[1] ; 写端
返回值:
- 成功: 0
- 失败: -1, 并且设置错误号
注意:
只能在亲缘关系进程间通信
管道默认是阻塞的:如果管道中没有数据,read阻塞,如果管道满了,write阻塞
*/
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
//子进程发送数据给父进程,父进程收数据并输出
int main(){
int fd[2];
int ret = pipe(fd);
if(ret == -1){
perror("pipe");
exit(0);
}
pid_t pid = fork();
if(pid > 0){
//parent
char buff[1024] = {0};
int len = read(fd[0], buff, sizeof(buff)); //阻塞状态,有数据才读
printf("PARENT read : %s , pid = %d \n", buff, getpid());
}else if(pid == 0){
//child
sleep(10);
char *buf = "hello world\n";
int rt = write(fd[1], buf, strlen(buf));
}
return 0;
}
交替收发
/*
#include <unistd.h>
int pipe(int pipefd[2]);
功能:
创建一个命名管道来进程间通信
参数:
- int pipefd[2]:传出参数
-pipefd[0] : 读端
-pipefd[1] ; 写端
返回值:
- 成功: 0
- 失败: -1, 并且设置错误号
注意:
只能在亲缘关系进程间通信
管道默认是阻塞的:如果管道中没有数据,read阻塞,如果管道满了,write阻塞
*/
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
//子进程发送数据给父进程,父进程收数据并输出
int main(){
int fd[2];
int ret = pipe(fd);
if(ret == -1){
perror("pipe");
exit(0);
}
pid_t pid = fork();
if(pid > 0){
//parent
printf("PARENT : pid = %d \n", getpid());
while(1){
char buff[1024] = {0};
int len = read(fd[0], buff, sizeof(buff)); //阻塞状态,有数据才读
printf("PARENT read : %s , pid = %d \n", buff, getpid());
char *buf1 = "I am not good \n";
write(fd[1], buf1, strlen(buf1));
sleep(2);
}
}else if(pid == 0){
//child
printf("child : pid = %d \n", getpid());
while(1){
char *buf = "hello world\n";
write(fd[1], buf, strlen(buf));
sleep(2);
char buff1[1024] = {0};
int len = read(fd[0], buff1, sizeof(buff1)); //阻塞状态,有数据才读
printf("child read : %s , pid = %d \n", buff1, getpid());
}
}
return 0;
}
如果去掉sleep,那么则会出现一个进程自写自读。优化就是只一端读或写,然后关闭另一端。
查看、修改管道大小
1,shell命令
ulimit -a #查看
ulimit -p #修改
管道的特点
管道的读写特点:
使用管道时,需要注意以下几种特殊的情况(假设都是阻塞I/O操作):
1,所有的指向管道写端的文件描述符都关闭了(管道写端引用计数为0),有进程从管道的读端读数据,那么管道中剩余的数据被读取以后,再次read会返回0, 就像文件末尾一样。
2,如果有指向管道写端的文件描述符没有关闭(管道的写端引用计数大于0),而持有管道写端的进程也没有往里写数据,这个时候有进程从管道中读取数据,那么管道中剩余的数据被读完后再次read会阻塞,直到管道中有数据可以读取才读取数据并返回。
3,如果所有指向管道读端的文件描述符都关闭(管道的读端的引用计数为0),这时有进程向管道中写数据,那么该进程会受到一个信号SIGPIPE,通常会导致进程异常终止。
4,如果有指向管道读端的文件描述符没有关闭(管道的读端的引用计数大于0),而持有管道读端的进程也没有从管道中读数据,这时有进程往管道写数据,那么在管道被写满的时候再次write会阻塞,知道管道有空位置才能再次写入数据并返回。
总结:
读管道:
管道中有数据,read返回实际读到的字节数。
管道中无数据:
写端被全部关闭,read返回0(相当于读到文件的末尾).
写端没有完全关闭,read阻塞等待.
写管道:
管道读端全部被关闭,进程异常终止(进程收到SIGPIPE信号)
管道读端没有全部关闭:
管道已满,write阻塞
管道没有满,write将数据写入,并返回实际写入的字节
/*
#include <unistd.h>
int pipe(int pipefd[2]);
功能:
创建一个命名管道来进程间通信
参数:
- int pipefd[2]:传出参数
-pipefd[0] : 读端
-pipefd[1] ; 写端
返回值:
- 成功: 0
- 失败: -1, 并且设置错误号
注意:
只能在亲缘关系进程间通信
管道默认是阻塞的:如果管道中没有数据,read阻塞,如果管道满了,write阻塞
*/
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <string.h>
//子进程发送数据给父进程,父进程收数据并输出
/*
设置管道非阻塞
fcntl(fd[0], F_GETFL)//获取原来的flag
flahs |= O_NONBLOCK;//管道非阻塞
fcntl(fd[0], F_SETFL, flags)//设置值
*/
int main(){
int fd[2];
int ret = pipe(fd);
if(ret == -1){
perror("pipe");
exit(0);
}
pid_t pid = fork();
if(pid > 0){
//parent
printf("PARENT : pid = %d \n", getpid());
close(fd[1]);
int flags = fcntl(fd[0], F_GETFL);
flags |= O_NONBLOCK;
fcntl(fd[0], F_SETFL, flags);
while(1){
char buff[1024] = {0};
int len = read(fd[0], buff, sizeof(buff)); //阻塞状态,有数据才读
printf("PARENT read : %s, len = %d , pid = %d \n", buff, len, getpid());
memset(buff, 0, 1024);
sleep(2);
}
}else if(pid == 0){
//child
printf("child : pid = %d \n", getpid());
while(1){
close(fd[0]);
char *buf = "hello world";
write(fd[1], buf, strlen(buf));
sleep(2);
}
}
return 0;
}
write 端
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
//判断管道是否存在
if(access("test", F_OK) == -1){
printf("fifo is not exist, create pipe\n");
//创建管道
int ret = mkfifo("test", 0664);
if(ret == -1){
perror("mkfifo");
exit(0);
}
}
//打开管道文件
int fd = open("test", O_WRONLY);
if(fd == -1){
perror("open");
exit(0);
}
for(int i=0; i<10; i++){
char buf[1024];
sprintf(buf, "boring %d \n", i);
int len = write(fd, buf, sizeof(buf));
printf("write data : %s \n", buf);
sleep(1);
}
close(fd);
return 0;
}
read端
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
//打开管道文件
int fd = open("test", O_RDONLY);
if(fd == -1){
perror("read open");
exit(0);
}
//读数据
while(1){
char buff[1024];
int len = read(fd, buff, sizeof(buff));
if(len == 0){
printf("写端断开连接了。。。\n");
break;
}
printf("read data : %s\n", buff);
}
close(fd);
return 0;
}
注意事项
有名管道的注意事项:
1.一个为只读而打开一个管道的进程会阻塞,直到另外一个进程为只写打开管道
2.一个为只写而打开一个管道的进程会阻塞,直到另外一个进程为只读打开管道
读管道:
管道中有数据,read返回实际读到的字节数
I 管道中无数据:
管道写端被全部关闭,read返回0,(相当于读到文件末尾)
写端没有全部被关闭,read阻塞等待
写管道;
管道读端被全部关闭,进行异常终止(收到一个SIGPIPE信号)
管道读端没有全部关闭:
管道已经满了,write会阻塞
管道没有满,write将数据写入,并返回实际写入的字节数。