进程间通信(IPC)2 ------ 有名管道
2017-12-18 13:56
232 查看
管道的一个不足之处是没有名字,因此只能在具有亲缘关系的进程之间通信。而“有名管道”与此不同,它提供了一个路径名与之关联,作为一个设备文件存在,即使无亲缘关系的进程之间,只要能访问该路径,也可以通过FIFO进行通信。FIFO总是按照先进先出的原则工作,第一个被写入的数据将首先从管道中读出。
函数原型:
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *path,mode_t mode);
path为创建有名管道的路径名;mode为创建有名管道的模式,指明其存取权限。函数调用成功返回0,失败返回-1。
使用一个存在的有名管道之前,需要用open()将其打开。因为有名管道是一个存在于硬盘上的文件,而管道是存在于内存中的特殊文件。
以下程序演示有名管道在无亲缘关系的进程之间如何通信。
//procwriter.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_NAME "myfifo"
#define BUF_SIZE 1024
int main(void)
{
int fd;
char buf[BUF_SIZE] = "Hello procwrite, I come from process named procread!";
umask(0);
//指明创建一个有名管道且存取权限为0666,即创建者、与创建者同组的用户、其他用户对该有名管道的访问权限都是可读可写
if (mkfifo (FIFO_NAME, S_IFIFO | 0666) == -1)
{
perror ("mkfifo error!");
exit (1);
}
if((fd = open (FIFO_NAME, O_WRONLY) ) == -1)/*以写方式打开FIFO*/
{
perror ("fopen error!");
exit (1);
}
write (fd, buf, strlen(buf)+1); /*向FIFO写数据*/
close (fd);
exit (0);
}
procreader.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#define FIFO_NAME "myfifo"
#define BUF_SIZE 1024
int main(void)
{
int fd;
char buf[BUF_SIZE];
umask (0);
fd = open(FIFO_NAME, O_RDONLY);
read (fd, buf, BUF_SIZE);
printf ("Read content: %s\n", buf);
close (fd);
exit (0);
}
首先运行procwrite(运行后处于阻塞状态),打开另一个终端运行程序procread。结果如下:
![](http://img.blog.csdn.net/20171218135538425)
函数原型:
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *path,mode_t mode);
path为创建有名管道的路径名;mode为创建有名管道的模式,指明其存取权限。函数调用成功返回0,失败返回-1。
使用一个存在的有名管道之前,需要用open()将其打开。因为有名管道是一个存在于硬盘上的文件,而管道是存在于内存中的特殊文件。
以下程序演示有名管道在无亲缘关系的进程之间如何通信。
//procwriter.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_NAME "myfifo"
#define BUF_SIZE 1024
int main(void)
{
int fd;
char buf[BUF_SIZE] = "Hello procwrite, I come from process named procread!";
umask(0);
//指明创建一个有名管道且存取权限为0666,即创建者、与创建者同组的用户、其他用户对该有名管道的访问权限都是可读可写
if (mkfifo (FIFO_NAME, S_IFIFO | 0666) == -1)
{
perror ("mkfifo error!");
exit (1);
}
if((fd = open (FIFO_NAME, O_WRONLY) ) == -1)/*以写方式打开FIFO*/
{
perror ("fopen error!");
exit (1);
}
write (fd, buf, strlen(buf)+1); /*向FIFO写数据*/
close (fd);
exit (0);
}
procreader.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#define FIFO_NAME "myfifo"
#define BUF_SIZE 1024
int main(void)
{
int fd;
char buf[BUF_SIZE];
umask (0);
fd = open(FIFO_NAME, O_RDONLY);
read (fd, buf, BUF_SIZE);
printf ("Read content: %s\n", buf);
close (fd);
exit (0);
}
首先运行procwrite(运行后处于阻塞状态),打开另一个终端运行程序procread。结果如下:
相关文章推荐
- APUE读书笔记---进程间通信(IPC)之管道和有名管道(FIFO)
- 进程间通信IPC之--无名管道(pipe)和有名管道(fifo)(转)
- Linux进程间通信IPC学习笔记之有名管道
- 进程间通信IPC之--无名管道(pipe)和有名管道(fifo)
- Linux 进程间通信 (IPC) //管道
- Linux进程间通信(IPC)之二——命名管道(FIFO)
- 深入理解Linux进程间通信(IPC)-- 管道pipe
- 进程间通信--------有名管道如何产生阻塞
- linux 进程间通信之管道和有名管道
- 进程间通信——管道(有名管道、无名管道)
- Linux进程间通信——有名管道 FIFO 详解
- 进程间通信-管道 IPC pipe
- Linux进程间通信(IPC)之一——管道
- 进程间通信编程(3) - 有名管道
- Linux环境进程间通信(一):管道及有名管道
- 传统的进程间通信方式 1.无名管道(pipe) 2.有名管道(fifo) 3.信号(signal)
- linux进程间通信2——有名管道
- 进程间通信方式之有名管道
- Linux进程间通信(九)---综合实验之有名管道通信实验
- Linux进程间通信(一)之无名管道(PIPE)和有名管道(FIFO)