linux进程通信--有名管道(FIFO)(含实例代码)
来源:互联网 发布:甄嬛传 槿汐 知乎 编辑:程序博客网 时间:2024/04/30 03:24
有名管道(FIFO):与管道不同,可以在没有亲缘关系的多个进程间通信。
创建完FIFO,读或写FIFO的进程首先要有打开FIFO的操作:
读FIFO的进程以阻塞方式读打开,如果没有其它进程写则打开操作阻塞;
写FIFO的进程以阻塞方式写打开,没有其它进程读则打开操作阻塞。
某进程以阻塞方式读FIFO,如果FIFO没有数据或是有数据但其它进程在读,则发生阻塞。
某进程以阻塞方式写FIFO,与写管道是一样的,写入字节数小于PIPE_BUF是原子操作,
写操作在管道缓冲区没有及时读走数据时发生阻塞。
主要函数:
int mkfifo (const char *pathname, mode_t mode);
附代码
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include<stdlib.h>
#include<stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FIFO "fifo"
main()
...{
pid_t pid;
int fd_r,fd_w;
int num_r,num_w;
int len = 40;
char buf_r[len];
char buf_w[len];
//也可以在shell下用mkfifo创建fifo
if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
...{
printf("create fifo fail! ");
return;
}
if((pid=fork())==0)
...{
sleep(1);
printf("进程(%d):准备打开fifo ",getpid());
fd_r = open(FIFO,O_RDONLY,0);
if(fd_r==-1)//以阻塞方式读打开,没有其它进程写则打开操作阻塞
...{
printf("进程(%d):打开fifo失败... ",getpid());
return;
};
printf("进程(%d):打开fifo成功. ",getpid());
while(1)
...{
num_r = read(fd_r,buf_r,sizeof(buf_r));
printf( "读字节数:%d ",num_r);
sleep(1);
//这里将验证写阻塞,写进程不休眠,很快就把管道写满,再写的时候就阻塞了
}
}
else if(pid>0)
...{
printf("进程(%d):准备打开fifo ",getpid());
//if(open(FIFO,O_WRONLY|O_NONBLOCK,0)==-1)
fd_w = open(FIFO,O_WRONLY,0);
if(fd_w ==-1)//以阻塞方式写打开,没有其它进程读则打开操作阻塞
...{
printf("进程(%d):打开fifo失败... ",getpid());
return;
}
printf("进程(%d):打开fifo成功. ",getpid());
while(1)
...{
num_w = write(fd_w,buf_w,sizeof(buf_w));
printf( "写字节数:%d ",num_w);
}
}
}
#include <sys/types.h>
#include <errno.h>
#include<stdlib.h>
#include<stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FIFO "fifo"
main()
...{
pid_t pid;
int fd_r,fd_w;
int num_r,num_w;
int len = 40;
char buf_r[len];
char buf_w[len];
//也可以在shell下用mkfifo创建fifo
if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
...{
printf("create fifo fail! ");
return;
}
if((pid=fork())==0)
...{
sleep(1);
printf("进程(%d):准备打开fifo ",getpid());
fd_r = open(FIFO,O_RDONLY,0);
if(fd_r==-1)//以阻塞方式读打开,没有其它进程写则打开操作阻塞
...{
printf("进程(%d):打开fifo失败... ",getpid());
return;
};
printf("进程(%d):打开fifo成功. ",getpid());
while(1)
...{
num_r = read(fd_r,buf_r,sizeof(buf_r));
printf( "读字节数:%d ",num_r);
sleep(1);
//这里将验证写阻塞,写进程不休眠,很快就把管道写满,再写的时候就阻塞了
}
}
else if(pid>0)
...{
printf("进程(%d):准备打开fifo ",getpid());
//if(open(FIFO,O_WRONLY|O_NONBLOCK,0)==-1)
fd_w = open(FIFO,O_WRONLY,0);
if(fd_w ==-1)//以阻塞方式写打开,没有其它进程读则打开操作阻塞
...{
printf("进程(%d):打开fifo失败... ",getpid());
return;
}
printf("进程(%d):打开fifo成功. ",getpid());
while(1)
...{
num_w = write(fd_w,buf_w,sizeof(buf_w));
printf( "写字节数:%d ",num_w);
}
}
}
- linux进程通信--有名管道(FIFO)(含实例代码)
- linux进程通信----FIFO(有名管道)
- 进程通信-有名管道FIFO
- Linux--进程间通信(管道及有名管道FIFO)
- Linux--进程间通信(管道及有名管道FIFO)
- Linux--进程间通信(管道及有名管道FIFO)
- linux进程间通信-有名管道(FIFO)
- linux进程间通信-有名管道(FIFO)
- Linux进程间通信二--有名管道FIFO
- linux编程---进程间通信---FIFO---有名管道
- Linux进程间通信——有名管道 FIFO 详解
- 进程间通信______有名管道(FIFO)
- 进程间的通信----有名管道fifo
- linux进程通信--管道(pipe)(含实例代码)
- linux 进程编程:有名管道fifo
- 有名管道FIFO实例
- linux进程通信-有名管道
- linux进程通信--有名管道
- 没人能完全幸免的美国次贷危机
- 多线程调用DataGridView
- 如何让你的发言语出惊人
- 业务实体对象(Business Entity Object)的序列化
- Java开发中的几个概念介绍
- linux进程通信--有名管道(FIFO)(含实例代码)
- 使用Ext.Window时出现No center region defined in BorderLayout错误的愿原因
- 7.3.2 时间和字符串之间的转换
- 硬盘代理列表
- 传新浪高层陷入空前危机 陈彤“逼宫”曹国伟
- 中国大豆产业的“退化”路径
- 7.3.1 时间数据类型检查
- ant介绍
- 戴尔中国采购额将增至230亿美元 挖联想墙脚