Linux_ mkfifo 命名管道 操作demo
来源:互联网 发布:淘宝卖家开店 编辑:程序博客网 时间:2024/05/16 09:36
main1.c
#include <stdlib.h>#include <stdio.h>#define MY_FIFO "/tmp/myfifo"int main(void){ int ret; ret = mkfifo(MY_FIFO, 0777); if (ret == -1) { printf("create fifo failed!\n"); } return 0;}
main2.c
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#define MY_FIFO2 "/tmp/myfifo2"int main(void){ int ret; //ret = mkfifo(MY_FIFO, 0777); ret = mknod(MY_FIFO2, 0777|S_IFIFO, 0); if (ret == -1) { printf("create fifo failed!\n"); } return 0;}
main3_r.c
#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>#include <stdio.h>#define FIFO_NAME "/tmp/myfifo"int main(void){ int ret; int fd; /* œ»ºÏ≤È÷∏∂®µƒ√¸√˚π‹µ¿ «∑Ò“—æ≠¥Ê‘⁄ */ if (access(FIFO_NAME, F_OK) == -1) { ret = mkfifo(FIFO_NAME, 0777); if (ret == -1) { printf("creat fifo failed!\n"); exit(1); } } printf("process(%d) is opening fifo...\n", getpid()); fd = open(FIFO_NAME, O_RDONLY); //fd = open(FIFO_NAME, O_RDONLY | O_NONBLOCK); printf("process(%d) had opened fifo. ret = %d\n", getpid(), fd); sleep(10); return 0;}
main3_w.c
#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>#include <stdio.h>#define FIFO_NAME "/tmp/myfifo"int main(void){ int ret; int fd; /* œ»ºÏ≤È÷∏∂®µƒ√¸√˚π‹µ¿ «∑Ò“—æ≠¥Ê‘⁄ */ if (access(FIFO_NAME, F_OK) == -1) { ret = mkfifo(FIFO_NAME, 0777); if (ret == -1) { printf("creat fifo failed!\n"); exit(1); } } printf("process(%d) is opening fifo...\n", getpid()); fd = open(FIFO_NAME, O_WRONLY); printf("process(%d) had opened fifo. ret = %d\n", getpid(), fd); sleep(10); return 0;}
main4_r.c
#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <limits.h>#include <stdio.h>#include <stdlib.h>#define MY_FIFO "/tmp/myfifo"#define DATA_SIZE (10*1024*1024)int main(void){ int ret; int fifo_fd; int cnt = 0; char buff[PIPE_BUF]; if (access(MY_FIFO, F_OK) == -1) { ret = mkfifo(MY_FIFO, 0777); if (ret == -1) { printf("create fifo failed!\n"); exit(1); } } fifo_fd = open(MY_FIFO, O_RDONLY); if (fifo_fd == -1) { printf("open fifo failed!\n"); exit(1); } printf("open fifo succeed!\n"); while(1) { ret = read(fifo_fd, buff, sizeof(buff)); if (ret > 0) { printf("read %d bytes!\n", ret); } else if (ret == 0) { printf("read finished!\n"); break; } else if (ret == -1) { printf("read failed!\n"); exit(1); } cnt += ret; } printf("read finished! cnt=%d\n", cnt);}
main4_w.c
#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <limits.h>#include <stdio.h>#include <stdlib.h>#define MY_FIFO "/tmp/myfifo"#define DATA_SIZE (10*1024*1024)int main(void){ int ret; int fifo_fd; int cnt = 0; char buff[PIPE_BUF]; if (access(MY_FIFO, F_OK) == -1) { ret = mkfifo(MY_FIFO, 0777); if (ret == -1) { printf("create fifo failed!\n"); exit(1); } } fifo_fd = open(MY_FIFO, O_WRONLY); if (fifo_fd == -1) { printf("open fifo failed!\n"); exit(1); } printf("open fifo succeed!\n"); while (cnt < DATA_SIZE) { ret = write(fifo_fd, buff, sizeof(buff)); if (ret == -1) { printf("write fifo failed!\n"); exit(1); } cnt += ret; printf("write %d bytes to fifo! cnt=%d\n", ret, cnt); } printf("write finished!cnt = %d\n", cnt); close(fifo_fd);}
main5_r.c
#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <limits.h>#include <stdio.h>#include <stdlib.h>#define MY_FIFO "/tmp/myfifo"#define DATA_SIZE (10*1024*1024)int main(void){ int ret; int fifo_fd; int cnt = 0; char buff[PIPE_BUF]; if (access(MY_FIFO, F_OK) == -1) { ret = mkfifo(MY_FIFO, 0777); if (ret == -1) { printf("create fifo failed!\n"); exit(1); } } fifo_fd = open(MY_FIFO, O_RDONLY); if (fifo_fd == -1) { printf("open fifo failed!\n"); exit(1); } printf("open fifo succeed!\n"); while(1) { ret = read(fifo_fd, buff, sizeof(buff)); if (ret > 0) { buff[ret] =0; printf("received: %s\n", buff); } else if (ret == 0) { printf("read finished!\n"); break; } else if (ret == -1) { printf("read failed!\n"); exit(1); } } close(fifo_fd); return 0;}
main5_w.c
#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <limits.h>#include <stdio.h>#include <stdlib.h>#define MY_FIFO "/tmp/myfifo"#define DATA_SIZE (10*1024*1024)int main(void){ int ret; int fifo_fd; int cnt = 0; char buff[PIPE_BUF]; if (access(MY_FIFO, F_OK) == -1) { ret = mkfifo(MY_FIFO, 0777); if (ret == -1) { printf("create fifo failed!\n"); exit(1); } } fifo_fd = open(MY_FIFO, O_WRONLY); if (fifo_fd == -1) { printf("open fifo failed!\n"); exit(1); } printf("open fifo succeed!\n"); while(1) { scanf("%s", buff); if (!strcmp(buff, "exit")) { break; } ret = write(fifo_fd, buff, sizeof(buff)); if (ret == -1) { printf("write fifo failed!\n"); exit(1); } } printf("write finished!cnt = %d\n", cnt); close(fifo_fd);}
0 0
- Linux_ mkfifo 命名管道 操作demo
- Linux_ mkfifo 命名管道 操作
- Linux_ pipe 匿名管道demo
- Linux_信号操作 demo
- mkfifo命令不好使时创建命名管道
- Linux下进程间通信:命名管道-mkfifo
- Linux下进程间通信:命名管道-mkfifo
- 【linux】mkfifo 命令创建命名管道实现进程之间通信
- linux系统编程之管道(三):命名管道FIFO和mkfifo函数
- linux系统编程之管道(三):命名管道FIFO和mkfifo函数
- 管道(pipe、mkfifo)
- linux学习---linux基于文件的IPC(匿名管道pipe,命名管道mkfifo,普通文件,socket文件)
- mkfifo函数创建有名管道
- shell mkfifo 阻塞式管道
- Linux_管道、通配符
- 管道的使用——mkfifo
- mkfifo 创建管道IO 进程间交互
- Linux_ 内存管理demo
- Context作用、类型、使用建议
- 码农小汪-剑指Offer之29 -整数中1出现的次数(从1到n整数中1出现的次数)
- Java异常
- ECfinal UVALive 7512 November 11th (公式)
- spark+hadoop-2.6配置
- Linux_ mkfifo 命名管道 操作demo
- 【12】Linux-自己动手写驱动
- 在Node.js使用mysql模块时遇到的坑
- C++作业2:分段函数
- SPOJ GSS5 Can you answer these queries V(区间合并)
- bzoj 3262: 陌上花开
- NS3 填写数据包内容
- Java生成验证码并进行验证
- Android-全屏动画