本文主要是介绍William Stallings 《操作系统内核和设计原理》书中Linux下C语言实现读者写者问题(写者优先)代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码能跑,不过实在是观察不出来什么读者优先,或者写者优先。不知道这种优先级的冲突的场景从何而来,也就不知道书上讲得那些情况到底是什么。
特别是在下面代码的writer中,如果引入 sem_wait(&z)的话,writer进程会一直阻塞,从而引起读者也阻塞。去掉后,呈现出读者优先的状况。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/ipc.h>
#include <semaphore.h>
#include <fcntl.h>void * reader(void *) ;
void *writer (void *) ;sem_t x,y,z,wsem,rsem ;
int readcount=0 ;
int writecount=0 ;main()
{int a=1,b=1;system("clear");sem_init(&wsem,0,1) ;sem_init(&x,0,1) ;sem_init(&rsem,0,1) ;sem_init(&y,0,1) ;sem_init(&z,0,1) ;pthread_t r1,r2,r3;pthread_t w1,w2,w3,w4,w5 ;pthread_create(&r1,NULL,reader,(void *)a);a++;pthread_create(&r2,NULL,reader,(void *)a);a++;pthread_create(&w1,NULL,writer,(void *)b);b++;pthread_create(&w2,NULL,writer,(void *)b);b++;pthread_create(&r3,NULL,reader,(void *)a);a++;pthread_create(&w3,NULL,writer,(void *)b);b++;pthread_create(&w4,NULL,writer,(void *)b);b++;pthread_create(&w5,NULL,writer,(void *)b); //add a readerb++; printf("main begin join \n");pthread_join(r1,NULL);pthread_join(r2,NULL);pthread_join(w1,NULL);pthread_join(w2,NULL);pthread_join(r3,NULL);pthread_join(w3,NULL) ;pthread_join(w4,NULL);pthread_join(w5,NULL); printf("main end join\n");sleep(30);printf("main terminated\n");
}void * reader(void * arg)
{int c=(int)arg ;printf("\nreader %d is created",c);//sleep(1);//sem_wait(&z) ;{sem_wait(&rsem) ;{sem_wait(&x) ;readcount++;if( readcount == 1){sem_wait(&wsem) ;} sem_post(&x) ;}sem_post(&rsem) ;}//sem_wait(&z) ;//sleep(1);/*Critcal Section */printf("\nreader %d is reading\n ",c);sleep(10) ;/* critical section completd */sem_wait(&x) ;readcount-- ;if(readcount==0)sem_post(&wsem) ;printf("\nreader %d finished reading,readCount=%d\n",c,readcount); sem_post(&x) ;
}void * writer(void * arg)
{int c=(int)arg ;printf("\n--------------------writer %d is created",c);sleep(5);sem_wait(&y) ;writecount++;if( writecount == 1){sem_wait(&rsem) ;} sem_post(&y) ;sem_wait(&wsem) ;printf("\n--------------------writer %d is writing\n",c) ;sleep(1);sem_post(&wsem) ;sem_wait(&y) ;writecount--;printf("\n--------------------writer %d finished writing,writecount=%d\n",c,writecount);if( writecount == 0){sem_post(&rsem) ;} sem_post(&y) ; }
编译方法 : gcc -o pvrww pv-posix-reader-writer.c -lpthread
这里采用posix的信号量机制。
下面修改代码,能达到写者优先的目的:只要有产生了一个写者线程,那么正在运行的读者进程会在执行结束后,立刻被终止调度,让写者
先
运行。
#include <stdio.h>先
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/ipc.h>
#include <semaphore.h>
#include <fcntl.h>sem_t x,y,z,wsem,rsem ;
int readcount=0 ;
int writecount=0 ;void * reader(void * arg)
{int c= *(int *)arg;printf("\nreader %d is created",c);sem_wait(&z) ;sem_wait(&rsem) ;sem_wait(&x) ;readcount++;printf("\n-------------readcount=%d----- ",readcount);if( readcount == 1){sem_wait(&wsem) ;}sem_post(&x) ;sem_post(&rsem) ;sem_wait(&z) ;/*Critcal Section */printf("\nreader %d is reading\n ",c);sleep(10); /* critical section completd */sem_wait(&x) ;readcount-- ;if(readcount==0)sem_post(&wsem) ;printf("\nreader %d finished reading,readCount=%d\n",c,readcount); sem_post(&x) ;pthread_exit(0);
}void * writer(void * arg)
{int c= *(int *)arg;printf("\n--------------------writer %d is created",c);//sleep(5);sem_wait(&y) ;writecount++;printf("\n--------------------writer %d is created,and writecount ==%d",writecount);if( writecount == 1){if( readcount <= 1 ){sem_post(&z) ;sem_post(&rsem) ;}else{sem_wait(&rsem) ;}} sem_post(&y) ;sem_wait(&wsem) ;printf("\n--------------------writer %d is writing\n",c) ;sleep(2);sem_post(&wsem) ;sem_wait(&y) ;writecount--;printf("\n--------------------writer %d finished writing,writecount=%d\n",c,writecount);if( writecount == 0){sem_post(&rsem) ;} sem_post(&y) ; pthread_exit(0);
}main()
{int a=1,b=1;sem_init(&wsem,0,1) ;sem_init(&x,0,1) ;sem_init(&rsem,0,1) ;sem_init(&y,0,1) ;sem_init(&z,0,1) ;pthread_t PReader[3];pthread_t PWriter[5];int k =0;for(k = 1; k <= 3; k++) //创建顾客线程{pthread_create(&(PReader[k-1]),NULL,(void *)reader,&k);srand(time(0));sleep(rand() % 2 + 1); //1到3的随机数}for(k = 1; k <= 5; k++) //创建顾客线程{pthread_create(&(PWriter[k-1]),NULL,(void *)writer,&k);srand(time(0));sleep(rand() % 2 + 1); //1到3的随机数}for(k = 0; k < 3; k++){pthread_join(PReader[k],NULL); }sleep(30);for(k = 1; k <= 5; k++){pthread_kill(PWriter[k-1],0); }//sleep(30);printf("\nmain terminated\n");
}
这篇关于William Stallings 《操作系统内核和设计原理》书中Linux下C语言实现读者写者问题(写者优先)代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!