C 优先读者 读者/写者问题
2017-01-04 21:11
260 查看
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define N_WRITER 2 //写者数目
#define N_READER 5 //读者数目
#define W_SLEEP 1 //控制写频率
#define R_SLEEP 1 //控制读频率
pthread_t wid[N_WRITER],rid[N_READER];
pthread_mutex_t writeLock = PTHREAD_MUTEX_INITIALIZER;//同一时间只能一个人写文件,互斥
pthread_mutex_t accessReaderCnt = PTHREAD_MUTEX_INITIALIZER;//同一时间只能有一个人访问 readerCnt
int readerid = 0;
int writerid = 0;
int data = 0;
int readerCnt = 0;
void write(int id)
{
int rd = rand()%100;
printf("writer %d write %d\n",id,rd);
data = rd;
}
void read(int id)
{
printf("reader %d read %d\n",id,data);
}
void * writer(void * in)
{
int id;
id = ++writerid;
while(1)
{
pthread_mutex_lock(&writeLock);
write(id);
pthread_mutex_unlock(&writeLock);
sleep(W_SLEEP);
}
pthread_exit((void *) 0);
}
void * reader (void * in)
{
int id;
id = ++readerid;
while(1)
{
pthread_mutex_lock(&accessReaderCnt);
readerCnt++;
if(readerCnt == 1)
{
pthread_mutex_lock(&writeLock);
}
pthread_mutex_unlock(&accessReaderCnt);
read(id);
pthread_mutex_lock(&accessReaderCnt);
readerCnt--;
if(readerCnt == 0)
{
pthread_mutex_unlock(&writeLock);
}
pthread_mutex_unlock(&accessReaderCnt);
sleep(R_SLEEP);
}
pthread_exit((void *) 0);
}
int main()
{
int i = 0;
for(i = 0; i < N_READER; i++)
{
pthread_create(&wid[i],NULL,reader,NULL);
}
for(i = 0; i < N_WRITER; i++)
{
pthread_create(&rid[i],NULL,writer,NULL);
}
while(1)
{
sleep(10);
}
return 0;
}
#include <stdlib.h>
#include <pthread.h>
#define N_WRITER 2 //写者数目
#define N_READER 5 //读者数目
#define W_SLEEP 1 //控制写频率
#define R_SLEEP 1 //控制读频率
pthread_t wid[N_WRITER],rid[N_READER];
pthread_mutex_t writeLock = PTHREAD_MUTEX_INITIALIZER;//同一时间只能一个人写文件,互斥
pthread_mutex_t accessReaderCnt = PTHREAD_MUTEX_INITIALIZER;//同一时间只能有一个人访问 readerCnt
int readerid = 0;
int writerid = 0;
int data = 0;
int readerCnt = 0;
void write(int id)
{
int rd = rand()%100;
printf("writer %d write %d\n",id,rd);
data = rd;
}
void read(int id)
{
printf("reader %d read %d\n",id,data);
}
void * writer(void * in)
{
int id;
id = ++writerid;
while(1)
{
pthread_mutex_lock(&writeLock);
write(id);
pthread_mutex_unlock(&writeLock);
sleep(W_SLEEP);
}
pthread_exit((void *) 0);
}
void * reader (void * in)
{
int id;
id = ++readerid;
while(1)
{
pthread_mutex_lock(&accessReaderCnt);
readerCnt++;
if(readerCnt == 1)
{
pthread_mutex_lock(&writeLock);
}
pthread_mutex_unlock(&accessReaderCnt);
read(id);
pthread_mutex_lock(&accessReaderCnt);
readerCnt--;
if(readerCnt == 0)
{
pthread_mutex_unlock(&writeLock);
}
pthread_mutex_unlock(&accessReaderCnt);
sleep(R_SLEEP);
}
pthread_exit((void *) 0);
}
int main()
{
int i = 0;
for(i = 0; i < N_READER; i++)
{
pthread_create(&wid[i],NULL,reader,NULL);
}
for(i = 0; i < N_WRITER; i++)
{
pthread_create(&rid[i],NULL,writer,NULL);
}
while(1)
{
sleep(10);
}
return 0;
}
相关文章推荐
- 读者写者问题——读者优先,写者优先,公平竞争 解决方法
- 很有借鉴意义!读者写者问题--使用信号量的读者优先与写者优先程序分析
- 读者-写者问题 写者优先与公平竞争
- 进程同步的经典问题1——读者写者问题(写者优先与公平竞争)
- 优先读者的读者/写者问题的算法设计
- “读者-写者问题”的写者优先算法实现
- 读者-写者同步问题的写者优先实现
- 使用信号量解决读者写者问题--写者优先
- linux多线程编程——读者优先、写者优先问题
- 读者写者问题--使用信号量的读者优先与写者优先程序分析
- 进程同步的经典问题1——读者写者问题(写者优先与公平竞争)
- (转)很有借鉴意义!读者写者问题--使用信号量的读者优先与写者优先程序分析
- 读者写者问题之写者优先(java)
- 使用信号量解决读者写者问题--读者优先
- 基于信号量与P/V操作同步机制的读者/写者问题的设计与实现 (写者优先)
- IPC之读者写者问题:写者优先
- 进程同步的经典问题1——读者写者问题(写者优先与公平竞争)
- 读者-写着问题中写者优先的实现
- 读者写者问题-写者优先参考答案(vc代码)
- 读者写者问题--使用信号量的读者优先与写者优先程序分析