参考来源:(1条消息) 【转】进程间的五种通信方式_进程间的5种通信方式_哈市雪花的博客-CSDN博客
https://blog.csdn.net/baidu_38621657/article/details/105724822?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167860274416800222885633%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167860274416800222885633&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-105724822-null-null.142^v73^control,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=%E8%BF%9B%E7%A8%8B%E9%97%B4%E7%9A%84%E4%BA%94%E7%A7%8D%E9%80%9A%E4%BF%A1%E6%96%B9%E5%BC%8F&spm=1018.2226.3001.4187
无名管道
管道,通常指无名管道,是 UNIX 系统IPC最古老的形式。
1、特点:
它是半双工的(即数据只能在一个方向上流动),具有固定的读端和写端。
它只能用于具有亲缘关系的进程之间的通信(也是父子进程或者兄弟进程之间)。
它可以看成是一种特殊的文件,对于它的读写也可以使用普通的read、write 等函数。但是它不是普通的文件,并不属于其他任何文件系统,并且只存在于内存中
2、原型:
1 #include <unistd.h>
2 int pipe(int fd[2]); // 返回值:若成功返回0,失败返回-1
当一个管道建立时,它会创建两个文件描述符:fd[0]为读而打开,fd[1]为写而打开。要关闭管道只
3、例子
单个进程中的管道几乎没有任何用处。所以,通常调用 pipe 的进程接着调用 fork,这样就创建了父进程与子进程之间的 IPC 通道。
若要数据流从父进程流向子进程,则关闭父进程的读端(fd[0])与子进程的写端(fd[1]);反之,则可以使数据流从子进程流向父进程。
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
int main()
{
//int pipe(int pipefd[2]);
// 返回值:若成功返回0,失败返回-1
//fd[0]为读而打开,fd[1]为写而打开
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){
sleep(3);
printf("this is farther\n");
close(fd[0]);
write(fd[1],"hello from father",strlen("hello from father"));
wait(NULL);
}else{
printf("this is child\n");
close(fd[1]);
read(fd[0],buf,128);
printf("read from father:%s\n",buf);
exit(0);
}
return 0;
}
FIFO
FIFO,也称为命名管道,它是一种文件类型。
1、特点
FIFO可以在无关的进程之间交换数据,与无名管道不同。
FIFO有路径名与之相关联,它以一种特殊设备文件形式存在于文件系统中。
2、原型
1 #include <sys/stat.h>
2 // 返回值:成功返回0,出错返回-1
3 int mkfifo(const char *pathname, mode_t mode);
其中的 mode 参数与open函数中的 mode 相同。一旦创建了一个 FIFO,就可以用一般的文件I/O函数操作它。
当 open 一个FIFO时,是否设置非阻塞标志(O_NONBLOCK)的区别:
若没有指定O_NONBLOCK(默认),只读 open 要阻塞到某个其他进程为写而打开此 FIFO。类似的,只写 open 要阻塞到某个其他进程为读而打开它。
若指定了O_NONBLOCK,则只读 open 立即返回。而只写 open 将出错返回 -1 如果没有进程已经为读而打开该 FIFO,其errno置ENXIO。
实例
创建有名管道
方式1:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
//有名管道
//int mkfifo(const char *pathname, mode_t mode);
// 返回值:成功返回0,出错返回-1
int main()
{
int ret = mkfifo("./file",0600);
if(ret == 0){
printf("mkfifo success\n");
}
if(ret == -1){
printf("mkfifo failuer\n");
perror("why");
}
return 0;
}
方式二:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
//有名管道
//int mkfifo(const char *pathname, mode_t mode);
// 返回值:成功返回0,出错返回-1
int main()
{
if((mkfifo("./file",0600)==-1)&&errno==EEXIST){
printf("mkfifo failuer \n");
perror("why");
}else{
printf("mkfifo success\n");
}
return 0;
}
FIFO的通信方式类似于在进程中使用文件来传输数据,只不过FIFO类型文件同时具有管道的特性。在数据读出时,FIFO管道中同时清除数据,并且“先进先出”。下面的例子演示了使用 FIFO 进行 IPC 的过程:
GuanDao2_read.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
//有名管道
//int mkfifo(const char *pathname, mode_t mode);
// 返回值:成功返回0,出错返回-1
int main()
{
int nread = 0;
char buf[30] = {0};
if((mkfifo("./file",0600)==-1)&&errno!=EEXIST){
printf("mkfifo failed\n");
perror("why");
}
int fd = open("./file",O_RDONLY);
printf("read open success\n");
while(1){
nread = read(fd,buf,30);
printf("read %d byte from fifo,context: %s\n",nread,buf);
}
close(fd);
return 0;
}
GuanDao2_write.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
//有名管道
//int mkfifo(const char *pathname, mode_t mode);
// 返回值:成功返回0,出错返回-1
int main()
{
int cnt = 0;
char *str = "message from fifo";
int fd = open("./file",O_WRONLY);
printf("write open success\n");
while(1){
write(fd,str,strlen(str));
sleep(1);
if(cnt==5){
break;
}
}
close(fd);
return 0;
}
两终端运行情况