linux内核 读者 写者,Linux读者和写者(写者优先)

#include#define N_WRITER 5 //写者数目

#define N_READER 20 //读者数目

#define W_SLEEP 1 //控制写频率

#define R_SLEEP 0.5 //控制读频率

pthread_t wid[N_WRITER],rid[N_READER];

int data = 0;

int readerCnt = 0, writerCnt = 0;

pthread_mutex_t sem_read;

pthread_mutex_t sem_write;

pthread_mutex_t mutex_write;

pthread_mutex_t mutex_read;

void write()

{

int rd = rand();

printf("write %d\n",rd);

data = rd;

}

void read()

{

printf("read %d\n",data);

}

void * writer(void * in)

{

// while(1)

// {

sem_wait(&sem_write);

{//临界区,希望修改 writerCnt,独占 writerCnt

writerCnt++;

if(writerCnt == 1){

//阻止后续的读者加入待读队列

pthread_mutex_lock(&mutex_read);

}

}

sem_post(&sem_write);

pthread_mutex_lock(&mutex_write);

{//临界区,限制只有一个写者修改数据

printf("写线程id%d进入数据集\n",pthread_self());

write();

printf("写线程id%d退出数据集\n",pthread_self());

}

pthread_mutex_unlock(&mutex_write);

sem_wait(&sem_write);

{//临界区,希望修改 writerCnt,独占 writerCnt

writerCnt--;

if(writerCnt == 0){

//阻止后续的读者加入待读队列

pthread_mutex_unlock(&mutex_read);

}

}

sem_post(&sem_write);

sleep(W_SLEEP);

// }

pthread_exit((void *) 0);

}

void * reader (void * in)

{

// while(1)

// {

//假如写者锁定了mutex_read,那么成千上万的读者被锁在这里

pthread_mutex_lock(&mutex_read);//只被一个读者占有

{//临界区

sem_wait(&sem_read);//代码段 1

{//临界区

readerCnt++;

if(readerCnt == 1){

pthread_mutex_lock(&mutex_write);

}

}

sem_post(&sem_read);

}

pthread_mutex_unlock(&mutex_read);//释放时,写者将优先获得mutex_read

printf("读线程id%d进入数据集\n",pthread_self());

read();

printf("读线程id%d退出数据集\n",pthread_self());

sem_wait(&sem_read);//代码段2

{//临界区

readerCnt--;

if(readerCnt == 0){

pthread_mutex_unlock(&mutex_write);//在最后一个并发读者读完这里开始禁止写者执行写操作

}

}

sem_post(&sem_read);

sleep(R_SLEEP);

// }

pthread_exit((void *) 0);

}

int main()

{

printf("多线程,写者优先\n");

pthread_mutex_init(&mutex_write,NULL);

pthread_mutex_init(&mutex_read,NULL);

sem_init(&sem_write,0,1);

sem_init(&sem_read,0,1);

int i = 0;

for(i = 0; i < N_READER; i++)

{

pthread_create(&rid[i],NULL,reader,NULL);

}

for(i = 0; i < N_WRITER; i++)

{

pthread_create(&wid[i],NULL,writer,NULL);

}

sleep(1);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值