无名管道
1. 无名管道(Unnamed Pipe)
1.1 特点
①无名管道是一种半双工的通信机制,只能用于具有父子关系的进程之间或者同一进程的不同线程之间的通信。
②无名管道是一种单向通信,数据只能从管道的一端读取(读端)或者写入(写端),不能同时进行读写操作。
③无名管道的读写操作是阻塞的,如果没有数据可读,则读取操作会阻塞,直到有数据可读为止;如果管道已满,则写入操作会阻塞,直到有空间可写入为止。
1.2 创建方法
无名管道通过系统调用pipe来创建,它返回两个文件描述符,一个用于读取数据,另一个用于写入数据。
#include <unistd.h>
int pipe(int pipefd[2]);
功能:创建出来无名管道
返回值:成功返回0,失败返回-1
参数说明:
pipefd:用于存放读写端文件描述符的数组fd[0]用于读(读端),fd[1]用于写(写端)
注:管道是特殊的文件,但是存在于内核空间,不能直接读写内存,只能系统调用去请求读写(read / write)。pipe会保存用于读写的文件描述符(分开保存)
读写机制
①读端:
(a).写端存在:如果管道中有内容就直接读取,如果没有内容,则会阻塞等待
(b).写端不存在:如果管道中有内容直接读取,如果没有内容则返回0
②写端:
(a).读端存在:如果管道剩余空间足够,那么可以将内容写入管道;如果管道剩余空间小于要写入的数据,那么写一部分后,write会阻塞直到管道中又有了新的空间。
(b).读端不存在:管道破裂
简单父进程与子进程通信
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
int fd[2];
char buf[256] = {"hello child"};
//创建无名管道
int ret = pipe(fd);
if(ret < 0){
perror("pipe");
return -1;
}
pid_t pid = fork();
if(pid < 0){
perror("fork");
return -1;
}else if(pid == 0){
//子进程
//关闭写端
close(fd[1]);
//从管道中读取数据
read(fd[0], buf, sizeof(buf));
printf("子进程接受到的数据:%s\n",buf);
//关闭读端
close(fd[0]);
}else{
//父进程
//关闭读端
close(fd[0]);
//向管道中写入数据
write(buf[1], buf, sizeof(buf));
close(fd[1]);
}
return 0;
}
计算无名管道的大小
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
int fd[2];
int ret = pipe(fd);
if(ret < 0){
perror("pipe");
return -1;
}
char buffer[1024];
int count = 0;
while(1){
int rets = write(fd[1], buffer, sizeof(fd));
if(rets < 0){
perror("write");
return -1;
}
count++;
printf("write %d bytes\n",count);
}
close(fd[1]);
return 0;
}
2. 有名管道(Named Pipe)
2.1 特点
①有名管道是一种特殊的文件,可以用于不具有父子关系的进程之间的通信,即不同进程之间的无关联通信。
②有名管道是一种单向通信,数据只能从管道的一端读取(读端)或者写入(写端),不能同时进行读写操作。
③有名管道的读写操作是阻塞的,如果没有数据可读,则读取操作会阻塞,直到有数据可读为止;如果管道已满,则写入操作会阻塞,直到有空间可写入为止。
④有名管道创建后,可以通过文件名来访问,因此它可以用于不同进程之间的通信。
2.2 创建方法
有名管道通过系统调用mkfifo来创建,它需要指定一个文件路径作为管道的名称。
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
功能:创建一个有名管道文件
返回值:成功返回0,失败返回-1
参数说明:
pathname:文件名(包含路径)
mode:创建文件的权限。通常使用0666来设置可读可写权限。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int ret = mkfifo("myfifo",0666);
if(ret < 0){
perror("mkfifo");
return -1;
}
pid_t pid = fork();
if(pid < 0){
perror("foek");
return -1;
}else if(pid == 0){
char buf[64];
int fd = open("myfifo", O_RDONLY);
read(fd, buf, sizeof(buf));
printf("子进程接收到的数据:%s\n",buf);
close(fd);
}else{
char buf[64] = {"hello child"};
int fd = open("myfifo", O_WRONLY);
write(fd, buf, sizeof(buf));
close(fd);
}
return 0;
}