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

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);  //允許後續新讀者及其它寫者
           End
    Coend

    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 歡迎訪問交流
    
    
    
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章