讀者寫者 問題C線程實現 linux平台

來源:互聯網
上載者:User

1、首先 讀者寫者的訊號量實現

設定三個互斥訊號量:

  • rwmutex 用於寫者與其他讀者/寫者互斥的訪問共用資料
  • rmutex  用於讀者互斥的訪問讀者計數器readcount
  • wmutex 用於寫者等待已進入讀者退出,所有讀者退出前互斥寫操作
  • var rwmutex, rmutex,wmutex : semaphore := 1,1,1 ;int readcount = 0;cobegin       readeri begin  // i=1,2,….              P(rwmutex);              P(rmutex);              Readcount++;              If (readcount == 1) P(wmutex); //有讀者進入,互斥寫操作              V(rmutex);              V(rwmutex); // 及時釋放讀寫互斥訊號量,允許其它讀、寫進程申請資源              讀資料;              P(rmutex);              Readcount--;              If (readcount == 0) V(wmutex); //所有讀者退出,允許寫更新              V(rmutex);       End       Writerj begin // j = 1,2,….              P(rwmutex);  // 互斥後續其它讀者、寫者              P(wmutex);  //如有讀者正在讀,等待所有讀者讀完              寫更新;              V(wmutex);   //允許後續新的第一個讀者進入後互斥寫操作              V(rwmutex);  //允許後續新讀者及其它寫者       EndCoend

    2 然後再linux平台下 我用到了 pthread_creat  , pthread_mutex_trylock  , pthread_mutex_unlock ;

    代碼實現,肯定還存在bug ,希望路過的同學批評指正。

    #include <stdlib.h>#include <stdlib.h>        /* for convenience */#include <stddef.h>        /* for offsetof */#include <string.h>        /* for convenience */#include <unistd.h>        /* for convenience */#include <signal.h>        /* for SIG_ERR */#include <time.h>#include <pthread.h>pthread_mutex_t rwlock = PTHREAD_MUTEX_INITIALIZER; /*對緩衝訪問的互斥*/pthread_mutex_t rlock = PTHREAD_MUTEX_INITIALIZER; /*對readcount訪問的互斥*/pthread_mutex_t wlock = PTHREAD_MUTEX_INITIALIZER; /*write 線程 訪問互斥*/int readcount ;int readpos ,writepos ;const int N = 10;int pool[10];pthread_t pthreadarray[10];void print(){    int i;    printf("\n\n and now the pool is \n");    for(i = 0 ; i < N ; i ++)        printf("%d  is : %d\n ",i,pool[i]);    sleep(2);}pthread_t find(pthread_t tid){    int i;    for(i = 0 ; i < N ; i ++)        if(pthreadarray[i] == tid)            return i;    return -1;}void *read_fn(void *arg){    pthread_t tid ;    for(;;)    {        pthread_mutex_trylock(&rwlock);        pthread_mutex_trylock(&rlock);        readcount ++;        if(readcount == 1)            pthread_mutex_trylock(&wlock);        tid = pthread_self();        printf("\n****************************************************************\n \               now is read thread reading and the number is  %d ,tid is %ld\n \               the pool pos is %d and content is %d\n",find(tid),(long)tid,readpos,pool[readpos]);        print();        pool[readpos] = -1;        readpos = (readpos+1)%N;        readcount --;        pthread_mutex_unlock(&rlock);        if(readcount == 0)            pthread_mutex_unlock(&wlock);        pthread_mutex_unlock(&rwlock);    }}void *write_fn(void *arg){    int val;    pthread_t tid;    for(;;)    {        pthread_mutex_trylock(&rwlock);        pthread_mutex_trylock(&wlock);        val = rand();        tid = pthread_self();        printf("\n****************************************************************\n \               now is write thread writing and the number is  %d ,tid is %ld\n \               the pool pos is %d and content is %d\n",find(tid),(long)tid,writepos,val);        pool[writepos] = val;        writepos = (writepos+1)%N;        pthread_mutex_unlock(&wlock);        pthread_mutex_unlock(&rwlock);        print();    }}void init(){    int i;    for(i = 0 ; i < 10 ; i ++)        pool[i] = -1;}int main(){    init();    pthread_t thr1,thr2,thr3,thr4;    int i = 0;    srand((int)time(0));    /* pthread_create(&thr1,NULL,write_fn,NULL);     printf("the thread is wirte and  tid is %d\n",(int)thr1);     pthread_create(&thr2,NULL,read_fn,NULL);     printf("the thread is read and  tid is %d\n",(int)thr2);     pthread_create(&thr3,NULL,read_fn,NULL);     printf("the thread is read and  tid is %d\n",(int)thr3);    pthread_create(&thr4,NULL,write_fn,NULL);     printf("the thread is wirte and  tid is %d\n",(int)thr4);    */    for(i = 0 ; i < 10 ; i ++)    {        if(i%3 != 0)        {            pthread_create(&pthreadarray[i],NULL,write_fn,NULL);            printf("start :the %d thread is wirte  and  tid is %ld\n",i,(long)pthreadarray[i]);        }        else        {            pthread_create(&pthreadarray[i],NULL,read_fn,NULL);            printf("start :the %d thread is read and  tid is %ld\n",i,(long)pthreadarray[i]);        }    }    sleep(10);    exit(0);}
    參考:http://blog.csdn.net/lenic/article/details/4675142我獨立部落格的地址 :http://www.fuxiang90.me/?p=314 歡迎訪問交流

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.