进程通信介绍
进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息。
IPC 的方式通常有管道(包括无名管道和命名管道)、消息队列、信号量、共享存储、Socket、Streams 等。其中 Socket 和 Streams 支持不同主机上的两个进程 IPC。其它为单机的通信方式
无名管道
1.无名管道为半双工通信,只是一端读,一端写。无法双向交互。
2.无名管道存在于亲缘关系间的通信(父子进程或兄弟进程)
3.无名管道并不会产生一个实质性的文件,管道会在内存的缓冲区产生,当通信结束后,管道也会随之消失
常见方式:
#include <unistd.h>
int pipe(int fd[2]); // 返回值:若成功返回0,失败返回-1
其中fd[0]代表读端,fd[1]代表写端
#include <stdio.h>
#include <unistd.h>
#include <string.h>
// ssize_t write(int fd, const void *buf, size_t count);
int main()
{
char buf[20];
int fd[2];
int pid;
if(pipe(fd)<0){
printf("create pipe fail!\n");
perror("why");
}
pid = fork();
if(pid>0){
close(fd[0]);
write(fd[1],"message from father",strlen("message from father"));
}
if(pid==0){
close(fd[1]);
read(fd[0],buf,strlen("message from father"));
printf("message:%s\n",buf);
}
}
有名管道
FIFO,也称为命名管道,它是一种文件类型。它也是半双工
1.有名管道和无名管道不同之处在FIFO 可以在无关的进程之间交换数据。
2.FIFO 有路径名与之相关联,它以一种特殊设备文件形式存在于文件系统中
一般形式:
#include <sys/stat.h>
// 返回值:成功返回0,出错返回-1
int mkfifo(const char *pathname, mode_t mode);
其中的 mode 参数与 open 函数中的 mode 相同。一旦创建了一个 FIFO,就可以用一般的文件 I/O 函数操作它。
无名管道要同时运行两个进程,否则一端将会堵塞
实列如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
// int mkfifo(const char *pathname, mode_t mode);
int main()
{
if(mkfifo("./file",0600)<0&&errno!=EEXIST){
printf("creat fail!");
}
char *str;
str = "get message success";
int fd = open("./file",O_WRONLY);
write (fd,str,strlen(str));
close(fd);
printf("success");
}
我们光运行这个代码时,效果如下:
会一直卡死在这里,调试信息也无法打印。说明被阻塞
接下来,我们运行下面代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
// int mkfifo(const char *pathname, mode_t mode);
int main()
{
char readbuf[128];
int fd = open("./file", O_RDONLY);
int n_read=read(fd,readbuf,128);
close(fd);
printf("read_byte:%d,message:%s\n",n_read,readbuf);
}
它是读端,我们同时运行读端和写端看下效果:
可以看到消息被成功读取
总结
有名管道和无名管道区别
1.相同点:
都是半双工
数据只能单向流动
不同点
1.无名管道只能用于父子进程或兄弟进程中
2.无名管道建立并不会产生实质性文件,用完就消失,而FIFO不会
3.无名管道有固定读端和写端
3.FIFO可以用于任何不相干进程
4.FIFO通过产生文件管道来传输消息