资源简介
可以并发读,读写、写读、写写互斥,写者优先,代码已在Ubuntu11.10下编译运行通过
代码片段和文件信息
#include
#include
#define W_NUM 5 //最大写者数
#define R_NUM 10 //最大读者数
#define MAX_NUM 10
pthread_mutex_t write_lock;
pthread_mutex_t read_lock;
pthread_mutex_t change_writer_num_lock;
pthread_mutex_t chane_reader_num_lock;
pthread_t writers[MAX_NUM];
pthread_t readers[MAX_NUM];
int writer_cnt = 0;
int reader_cnt = 0;
void *writer(void *writer_id){
int *ptr_idtask_id;
ptr_id = (int *)writer_id;
task_id = *ptr_id;
printf(“ Writer %d send write require\n“task_id);
pthread_mutex_lock(&change_writer_num_lock);
writer_cnt++;
//printf(“ %d\n“writer_cnt);
if(writer_cnt == 1){
pthread_mutex_lock(&read_lock); // 读者将阻塞在这儿
}
pthread_mutex_unlock(&change_writer_num_lock);
pthread_mutex_lock(&write_lock);
printf(“ Writer %d begin to writing\n“task_id);
printf(“ Writer %d is writing.......\n“task_id);
sleep(1);
printf(“ Writer %d finished writing\n“task_id);
//sleep(2);
pthread_mutex_unlock(&write_lock);
pthread_mutex_lock(&change_writer_num_lock);
writer_cnt--;
//printf(“ %d\n“writer_cnt);
if(writer_cnt == 0){
pthread_mutex_unlock(&read_lock);
}
pthread_mutex_unlock(&change_writer_num_lock);
sleep(3);
pthread_exit(NULL);
}
void *reader(void *reader_id){
int *ptr_idtask_id;
ptr_id = (int *)reader_id;
task_id = *ptr_id;
printf(“Reader %d send read require\n“task_id);
pthread_mutex_lock(&read_lock); //readers choked here
pthread_mutex_lock(&chane_reader_num_lock);
reader_cnt++;
if(reader_cnt == 1){
pthread_mutex_lock(&write_lock);
}
pthread_mutex_unlock(&chane_reader_num_lock);
pthread_mutex_unlock(&read_lock);
printf(“Reader %d begin to reading\n“task_id);
printf(“Reader %d is reading.......\n“task_id);
sleep(1);
printf(“Reader %d finished reading\n“task_id);
//sleep(1);
pthread_mutex_lock(&chane_reader_num_lock);
reader_cnt--;
if(reader_cnt == 0){
pthread_mutex_unlock(&write_lock);
}
pthread_mutex_unlock(&chane_reader_num_lock);
sleep(3);
pthread_exit(NULL);
}
int main(){
int writer_id[MAX_NUM];
int reader_id[MAX_NUM];
pthread_mutex_init(&write_lockNULL);
pthread_mutex_init(&read_lockNULL);
pthread_mutex_init(&change_writer_num_lockNULL);
pthread_mutex_init(&chane_reader_num_lockNULL);
int i_W = 0;
int i_R = 0;
for(i_W = 0; i_W < W_NUM; i_W++){
writer_id[i_W] = i_W;
pthread_create(&writers[i_W]NULLwriter(void *)&writer_id[i_W]);
//sleep(0.5);
}
for(i_R = 0; i_R < R_NUM; i_R++){
reader_id[i_R] = i_R;
pthread_create(&readers[i_R]NULLreader(void *)&reader_id[i_R]);
//sleep(0.5);
}
sleep(10);
for(i_W = 0; i_W < W_NUM; i_W++){
pthread_join(writers[i_W]NULL);
}
for(i_R = 0; i_R < R_NUM; i_R++){
pthread_join(readers[i_R]NULL);
}
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2014-04-18 10:39 writer_reader\
文件 2820 2014-04-18 10:37 writer_reader\main.c
- 上一篇:银行排队系统课程设计
- 下一篇:VC++6.0完美运行在win7和win10系统上
相关资源
- C语言编写简单聊天室
- 贪吃蛇 linux开发 C语言 linux系统编程
- 使用HackRF在windows和linux下扫描GSM频道
- 用线程实现睡觉的理发师问题linux实现
- 生产者消费者问题信号量linux实现代码
- linux环境下模拟实现简单命令解释器
- Linux下C语言编写服务端、客户端程序
- linux系统下C语言实现带有图形界面的
- mysql封装.zip
- Linux文件模拟器
- linux 下 python调用c或者c++编写的代码使
- 《嵌入式Linux上的C语言编程实践》
- linux opencv车牌识别
- 尚观教育李慧芹Linux下C语言前嵌入式
- 基于Linux C语言的多线程模拟智能家具
- p2p的C语言编写,LINUX可执行程序+运行
- linux命令c语言自己实现大概40来个
- linux下网络编程TCP客户端和服务器端数
- linux环境下C语言编程指南 源代码
- C/C++实现linux和Windows查看系统、进程、
- Linux GTK 聊天室 MySql C语言聊天室
- Linux 串口源代码纯C++
- udp 聊天室 基于linux c语言实现
- C语言实现Linux文件系统模拟文档内有
- linux下alsa架构采集声音成wav格式播放
- Linux软件工程师(C语言)实用教程_
- C C++ Linux多线程文件程序
- 模拟战争游戏
- c语言直接读写xls文件,无需安装off
- OSPF基于C语言的算法实现
评论
共有 条评论