多线程/多进程写FIFO为原子操作(一次写小于PIPE_BUF内容)
来源:互联网 发布:中经网产业数据库 编辑:程序博客网 时间:2024/05/29 02:29
#include <stdio.h>#include <string.h>#include <errno.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <limits.h>#define WRITENUM 4typedef struct writeattr writeattr;struct writeattr{ int id; int fd;};int run_flag;writeattr wattr[WRITENUM];void *readfifo(void *arg){ int n, num=0; int fd = (int)arg; char buf[PIPE_BUF]; while(run_flag){ n = read(fd, buf, PIPE_BUF); if(n < 0) continue; if(n != PIPE_BUF){ printf("[read]:error:%d,n:%d\n", errno, n); break; } if(strncmp(buf, &buf[PIPE_BUF/2], PIPE_BUF/2)){ printf("[read]:not equal\n"); break; } if(++num % 1000000 == 0) printf("read:%d\n", num); }}void *writefifo(void *arg){ int id = ((writeattr*)arg)->id; int fd = ((writeattr*)arg)->fd; char buf[PIPE_BUF]; memset(buf, '0'+id, PIPE_BUF); while(run_flag){ write(fd, buf, PIPE_BUF); }}int main(){ int stat; char fn[]="test.fifo"; int fd1, fd2, n, s=0; pthread_t th_w[WRITENUM], th_r; int i; printf("PIPE_BUF:%d\n", PIPE_BUF); /* create fifo */ mkfifo(fn, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); if((fd1 = open(fn, O_RDONLY | O_NONBLOCK, 0)) < 0){ printf("open error\n"); return 10; } if((fd2 = open(fn, O_RDWR | O_NONBLOCK, 0)) < 0){ printf("open error\n"); return 20; } /* create thread */ run_flag = 1; for(i=0; i<WRITENUM; i++){ wattr[i].id = i; wattr[i].fd = fd2; pthread_create(&th_w[i], NULL, writefifo, &wattr[i]); } pthread_create(&th_r, NULL, readfifo, fd1); /* destroy thread */ pthread_join(th_r, NULL); for(i=0; i<WRITENUM; i++) pthread_join(&th_w[i], NULL); close(fd1); close(fd2); return 0;}
0 0
- 多线程/多进程写FIFO为原子操作(一次写小于PIPE_BUF内容)
- FIFO 两个进程同时写同一个FIFO
- 原子操作基本内容计算机操作系统进程
- 从头认识多线程-3.2 使用volatile声明的变量的写操作是非原子性的
- 多线程写文件操作
- FIFO管道写规则
- 多进程、多线程并发写同一log文件问题
- [linux]原子化的写文件操作writev
- 原子操作基本内容
- 多线程----原子操作
- windows多线程--原子操作
- 多线程之原子操作
- 多线程与原子操作
- 多线程 之原子操作
- 多线程-原子操作
- windows多线程--原子操作
- 多线程-原子操作
- 多线程原子操作:AtomicBoolean
- 高精度之乘法运算
- IOS UI简单的控件位置/尺寸/旋转变换
- 安卓弹出对话框
- 欢迎使用CSDN-markdown编辑器
- SDUT 2482 二叉排序树
- 多线程/多进程写FIFO为原子操作(一次写小于PIPE_BUF内容)
- Linux查看系统cpu个数、核心数、线程数
- rpc_server.py经典python程序实例
- windows Server2008 iis7 部署MVC4 项目的时候出现 403 错误
- 自定义导航栏去掉黑条
- C#方法的封装
- Rust中文翻译33
- 关于烂代码的那些事
- SUSE HA的配置