命名管道--生产者
-- 命名 管道 生产者
2023-09-27 14:22:42 时间
#include <stdio.h> #include <unistd.h> #include <stdlib.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 #define TEN_MEG (1024 * 1024 * 10) int main(int argc, char *argv[]) { int pipe_fd; int res; int open_mode = O_WRONLY; int bytes_sent = 0; char buffer[BUFFER_SIZE + 1]; 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); } bytes_sent += res; } (void)close(pipe_fd); } else { exit(EXIT_FAILURE); } printf("Process %d finished\n", getpid()); exit(EXIT_SUCCESS); }
相关文章
- MVC5+EF6 入门完整教程11--细说MVC中仓储模式的应用
- 6、Codesys之结构文本--基础标示符命名建议
- 零基础入门--中文命名实体识别(BiLSTM+CRF模型,含代码)
- 敏捷软件开发--敏捷开发原则
- 微信开发之如何使用开发工具--weixin-java-tools
- oracle--表空间基本操作
- oracle--dump块信息操作
- python 爬虫系列06--古诗文
- Halcon实例转OpenCV--低对比度图像中提取圆形轮廓(附源码)
- vue--全局配置
- ansible-playbook -- 变量传递
- HTML-JavaScript--单击按钮改变背景颜色
- 蜂鸟E203图像识别--未完待续
- 引用传参--面向对象的魅力
- string.erase()--已解决
- python--openCV--图像处理