Linux进程通信 FIFO
Linux进程通信 FIFO
FIFO也称为有名管道,它是一种文件类型,在文件系统中可以看到。程序中可以查看文件stat结构中st_mode成员的值来判断文件是否是FIFO文件。创建一个FIFO文件类似于创建文件,FIFO文件就像普通文件一样
FIFO中可以很好地解决在无关进程间数据交换的要求,并且由于它们是存在于文件系统中的,这也提供了一种比匿名管道更持久稳定的通信办法
。
。
FIFO的通信方式类似于在进程中使用文件来传输数据,只不过FIFO类型文件同时具有管道的特性。在数据读出时,FIFO管道中同时清除数据。在shell中
mkfifo命令可以建立有名管道,
mkfifo命令可以建立有名管道,
下面通过一个实例来帮助读者理解FIFO。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "clist.h"
#include "clist.c"
int main(int argc, char *argv[])
{
if(mkfifo(argv[1], 0666) == -1)
{
if(EEXIST == errno)
goto l;
else
{
perror("mkfifo");
exit(-1);
}
}
l;
int my_fd;
my_fd=open(argv[1], O_RDONLY);
if(NULL == my_fd)
{
perror("open");
exit(-1);
}
int my_service;
my_service=open(argv[1],O_RDONLY);
if(NULL == my_service)
{
perror("my_service");
exit(-1);
}
void *fun(void * a)
{
;
}
head_online=
pthread_t pid;
if(pthread_create(&pid, NULL, fun, &my_fd) < 0)
{
perror("pthread_creat");
exit(-1);
}
MSG buf;
sprintf(buf, "hello!");
write(my_service, buf, sizeof buf);
}
syw_selfimpr新浪微博地址: http://weibo.com/u/2945271402
如果觉得本文对您有帮助,请点击‘顶’支持一下,您的支持是我写作最大的动力,谢谢。