FIFO 和普通文件
来源:互联网 发布:使用ip连接mysql数据库 编辑:程序博客网 时间:2024/06/02 07:20
FIFO; a named pipe
并且还可以从外部查看通信的内容和大小
但是不管open中是否用了O_NONBLOCK,都会O_NONBLOCK的
[root@localhost ch13]# cat fifo3.c /*************write*****/#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <fcntl.h>#include <limits.h>#include <sys/types.h>#include <sys/stat.h>#define FIFO_NAME "/tmp/my_fifo"#define BUFFER_SIZE PIPE_BUF//PIPE_BUF=4096#define TEN_MEG (1024 * 1024 * 10)int main(){ int pipe_fd; int res; int open_mode = O_WRONLY; int bytes_sent = 0; char buffer[BUFFER_SIZE + 1]="agds"; if (access(FIFO_NAME, F_OK) == -1) { res = mkfifo(FIFO_NAME, 0777); if (res != 0) { fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME); exit(EXIT_FAILURE); } } printf("Process %d opening FIFO O_WRONLY\n", getpid()); pipe_fd = open(FIFO_NAME, open_mode); printf("Process %d result %d\n", getpid(), pipe_fd); if (pipe_fd != -1) { while(bytes_sent < TEN_MEG) { res = write(pipe_fd, buffer, BUFFER_SIZE); if (res == -1) { fprintf(stderr, "Write error on pipe\n"); exit(EXIT_FAILURE); }printf("%s\n",buffer); bytes_sent += res; } (void)close(pipe_fd); } else { exit(EXIT_FAILURE); } printf("Process %d finished\n", getpid()); exit(EXIT_SUCCESS);}[root@localhost ch13]# [root@localhost ch13]# [root@localhost ch13]# [root@localhost ch13]# cat fifo4.c /*************read*****/#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <fcntl.h>#include <limits.h>#include <sys/types.h>#include <sys/stat.h>#define FIFO_NAME "/tmp/my_fifo"#define BUFFER_SIZE PIPE_BUFint main(){ int pipe_fd; int res; int open_mode = O_RDONLY; char buffer[BUFFER_SIZE + 1]; int bytes_read = 0; memset(buffer, '\0', sizeof(buffer)); printf("Process %d opening FIFO O_RDONLY\n", getpid()); pipe_fd = open(FIFO_NAME, open_mode); printf("Process %d result %d\n", getpid(), pipe_fd); if (pipe_fd != -1) { do { res = read(pipe_fd, buffer, BUFFER_SIZE);printf("%s\n",buffer); bytes_read += res; } while (res > 0); (void)close(pipe_fd); } else { exit(EXIT_FAILURE); } printf("Process %d finished, %d bytes read\n", getpid(), bytes_read); exit(EXIT_SUCCESS);}[root@localhost ch13]#
[root@localhost ch13]# rm /tmp/my_fifo -f[root@localhost ch13]# ./fifo3 &[2] 17216[root@localhost ch13]# Process 17216 opening FIFO O_WRONLY[root@localhost ch13]# time ./fifo4 ...agdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsagdsProcess 17220 finished, 10485760 bytes readreal 0m0.610suser 0m0.002ssys 0m0.284sProcess 17216 finished[root@localhost ch13]# //FIFO文件是fifo3创建,所以先运行fifo3,如果先运行fifo4则找不到my_fifo而出错fifo3 is blocked when it opens without NONBLOCK mode until fifo4 also open the same FIFO file
[root@localhost ch13]# ll /tmp/my_fifo prwxr-xr-x. 1 root root 0 Aug 26 03:14 /tmp/my_fifothe FIFO file my_fifo is 0 bytes
如果他们之间读写的不是FIFO文件,而是一个普通文件会是怎么样呢
[root@localhost ch13]# rm /tmp/my_fifo -f[root@localhost ch13]# touch /tmp/my_fifo [root@localhost ch13]# ll /tmp/my_fifo -rw-r--r--. 1 root root 0 Aug 26 03:20 /tmp/my_fiforun fifo3 and fifo4 just as the cmd above,then
Process 17276 finished, 10485760 bytes readreal0m0.253suser0m0.006ssys0m0.235s
[root@localhost ch13]# ll /tmp/my_fifo -rw-r--r--. 1 root root 10485760 Aug 26 03:23 /tmp/my_fifo看来,普通文件用于进程间通信也不慢哈
并且还可以从外部查看通信的内容和大小
但是不管open中是否用了O_NONBLOCK,都会O_NONBLOCK的
- FIFO 和普通文件
- 【进程间通信】普通管道和命名管道(FIFO)
- 像操作普通文件一样操作FIFO --- 非阻塞方式打开,阻塞方式读取
- 同步FIFO和异步FIFO
- linux设备文件和普通文件
- 用文件初始化FIFO
- FIFO文件实现分析
- fifo文件(未完)
- java文件普通复制和NIO复制
- 管道和FIFO
- FIFO和DMA
- pipe和FIFO
- FIFO和LRU算法
- 管道和FIFO
- 【IPC】管道和FIFO
- 管道和FIFO
- 管道和FIFO
- 管道和FIFO
- My Blog
- 手机短信息的编解码类
- c#判断上网方式
- 896M 的谜思
- 一键开启局域网共享
- FIFO 和普通文件
- 动态调用WebService(C#)
- 修改sql服务器名称(保持与电脑名同步)
- webservice中WebMethod的属性
- c#委托 跳出线程
- 解决office07 只显示图片框的问题
- c#生成高质量缩略图
- 带你学习Jquery(二):选择器
- c#光棒效果