进程间通信(二):FIFO
2015-05-01 00:34
92 查看
一.FIFO 文件的创建,创建一个fifo在文件系统上
int ret = mkfifo("/tmp/fifo", mode);
二.打开FIFO
open("/tmp/fifo", flag);
根据flag的不同有四种打开方式:
O_RDONLY:阻塞直到,此fifo被O_WRONLY方式打开
O_WRONLY:阻塞直到,此fifo被O_RDONLY方式打开
O_RDONLY|O_NONBLOCK:立即返回
O_WRONLY|O_NONBLOCK:如果此fifo已经被只读打开,立即返回文件描述符,如果没有,立即返回-1
FIFO 支持open,close,read,write.unlink函数,O_RDONLY和O_WRONLY组合打开的结果是未定义的
四种打开方式测试代码:
fifo_read.c
#include<sys/stat.h>
#include<stdio.h>
#include <errno.h>
#include <fcntl.h>
int main()
{
char line[1024+1] = {'\0'};
mode_t mode;
mode = S_IRWXU|S_IRWXG;
if(mkfifo("/tmp/fifo", mode) < 0 && errno!=EEXIST)
{
printf("mkfifo failed !\n");
}
printf("\n");
int fd = open("/tmp/fifo", O_RDONLY);
if(fd<0)
{
printf("reader:first open fifo failed !\n");
}
else
{
printf("reader:open fifo secceed !\n");
}
sleep(1);
fd = open("/tmp/fifo", O_RDONLY );
if(fd<0)
{
printf("reader:second open fifo failed !\n");
}
else
{
printf("reader:second open fifo secceed !\n");
}
if(read(fd, line, 1024) < 0)
{
printf("reader:read fifo failed !\n");
}
printf("get msg:%s \n", line);
return 0;
}
fifo_writer.c
#include<sys/stat.h>
#include<stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#define MSG "hello world !"
int main()
{
char line[1024] = {'\0'};
mode_t mode;
mode = S_IRWXU|S_IRWXG;
if(mkfifo("/tmp/fifo", mode) < 0 && errno!=EEXIST)
{
printf("mkfifo failed !");
}
printf("\n");
int fd = open("/tmp/fifo", O_WRONLY);
if(fd<0)
{
printf("open fifo failed !");
}
printf("writer:open secceed !\n");
close(fd);
fd = open("/tmp/fifo", O_WRONLY|O_NONBLOCK);
if(fd < 0)
{
printf("writer:second open failed !\n");
}
else
{
printf("writer:second open failed !\n");
}
sleep(2);
fd = open("/tmp/fifo", O_WRONLY|O_NONBLOCK);
if(fd < 0)
{
printf("writer:third open failed !\n");
}
else
{
printf("writer:third open secceed !\n");
}
if(write(fd, MSG, strlen(MSG)) < 0)
{
printf("write fifo failed !");
}
return 0;
}
fifo_read.c 生成可执行文件reader
fifo_writer.c 生成可执行文件writer
测试结果:
[read_ng@wukong workspace]$ ./reader &
[1] 17862
[read_ng@wukong workspace]$ ./writer &
[2] 17863
[read_ng@wukong workspace]$
writer:open secceed !
reader:open fifo secceed !
writer:second open failed !
reader:second open fifo secceed !
writer:third open secceed !
get msg:hello world !
[1]- Done ./reader
[2]+ Done ./writer
[read_ng@wukong workspace]$
三.FIFO 的读写
1.如果一个FIFO,所有打开的描述符关闭后,内容会清空,否则不会清空
2.read时,如果FIFO中有数据,会读出来,如果没有,而且没有被写方式打开返回0,表示结束
3.write 时,如果FIFO没有被读方式打开,产生信号SIGPIPE,导致进程终止
int ret = mkfifo("/tmp/fifo", mode);
二.打开FIFO
open("/tmp/fifo", flag);
根据flag的不同有四种打开方式:
O_RDONLY:阻塞直到,此fifo被O_WRONLY方式打开
O_WRONLY:阻塞直到,此fifo被O_RDONLY方式打开
O_RDONLY|O_NONBLOCK:立即返回
O_WRONLY|O_NONBLOCK:如果此fifo已经被只读打开,立即返回文件描述符,如果没有,立即返回-1
FIFO 支持open,close,read,write.unlink函数,O_RDONLY和O_WRONLY组合打开的结果是未定义的
四种打开方式测试代码:
fifo_read.c
#include<sys/stat.h>
#include<stdio.h>
#include <errno.h>
#include <fcntl.h>
int main()
{
char line[1024+1] = {'\0'};
mode_t mode;
mode = S_IRWXU|S_IRWXG;
if(mkfifo("/tmp/fifo", mode) < 0 && errno!=EEXIST)
{
printf("mkfifo failed !\n");
}
printf("\n");
int fd = open("/tmp/fifo", O_RDONLY);
if(fd<0)
{
printf("reader:first open fifo failed !\n");
}
else
{
printf("reader:open fifo secceed !\n");
}
sleep(1);
fd = open("/tmp/fifo", O_RDONLY );
if(fd<0)
{
printf("reader:second open fifo failed !\n");
}
else
{
printf("reader:second open fifo secceed !\n");
}
if(read(fd, line, 1024) < 0)
{
printf("reader:read fifo failed !\n");
}
printf("get msg:%s \n", line);
return 0;
}
fifo_writer.c
#include<sys/stat.h>
#include<stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#define MSG "hello world !"
int main()
{
char line[1024] = {'\0'};
mode_t mode;
mode = S_IRWXU|S_IRWXG;
if(mkfifo("/tmp/fifo", mode) < 0 && errno!=EEXIST)
{
printf("mkfifo failed !");
}
printf("\n");
int fd = open("/tmp/fifo", O_WRONLY);
if(fd<0)
{
printf("open fifo failed !");
}
printf("writer:open secceed !\n");
close(fd);
fd = open("/tmp/fifo", O_WRONLY|O_NONBLOCK);
if(fd < 0)
{
printf("writer:second open failed !\n");
}
else
{
printf("writer:second open failed !\n");
}
sleep(2);
fd = open("/tmp/fifo", O_WRONLY|O_NONBLOCK);
if(fd < 0)
{
printf("writer:third open failed !\n");
}
else
{
printf("writer:third open secceed !\n");
}
if(write(fd, MSG, strlen(MSG)) < 0)
{
printf("write fifo failed !");
}
return 0;
}
fifo_read.c 生成可执行文件reader
fifo_writer.c 生成可执行文件writer
测试结果:
[read_ng@wukong workspace]$ ./reader &
[1] 17862
[read_ng@wukong workspace]$ ./writer &
[2] 17863
[read_ng@wukong workspace]$
writer:open secceed !
reader:open fifo secceed !
writer:second open failed !
reader:second open fifo secceed !
writer:third open secceed !
get msg:hello world !
[1]- Done ./reader
[2]+ Done ./writer
[read_ng@wukong workspace]$
三.FIFO 的读写
1.如果一个FIFO,所有打开的描述符关闭后,内容会清空,否则不会清空
2.read时,如果FIFO中有数据,会读出来,如果没有,而且没有被写方式打开返回0,表示结束
3.write 时,如果FIFO没有被读方式打开,产生信号SIGPIPE,导致进程终止
相关文章推荐
- 4. 卷2(进程间通信)---管道和FIFO
- 《unix高级环境编程》进程间通信——管道和FIFO
- 使用 FIFO 实现进程间通信示例
- Linux进程间通信之管道(pipe)、命名管道(FIFO)与信号(Signal)
- Linux--进程间通信(管道及有名管道FIFO)
- Linux环境进程间通信(一)——管道(pipe)和命名管道(fifo)
- 进程间通信(IPC):FIFO
- 进程间通信之管道(pipe、fifo)
- 进程间通信(一): 管道(pipe+fifo)
- 进程间通信之命名管道FIFO通信
- 进程间通信系列 之 命名管道FIFO及其应用实例
- linux进程间通信-有名管道(FIFO)
- 进程间通信--管道、命名管道、FIFO
- linux系统编程:进程间通信-fifo
- Linux系统编程——进程间通信:命名管道(FIFO)
- 不相关进程间通信--命名管道(FIFO文件)
- 进程间通信系列 之 命名管道FIFO及其应用实例
- 进程间通信之FIFO
- linux编程---进程间通信---FIFO---有名管道
- Linux环境编程之IPC进程间通信(三):FIFO