Reader Writer Problem C thread implementation Linux platform

Source: Internet
Author: User

1. First, the reader's semaphore implementation

Set three mutex semaphores:

  • Rwmutex is used to access Shared data that is mutually exclusive to other readers/writers.
  • Rmutex is used to access the reader counter readcount that is mutually exclusive to the reader.
  • Wmutex is used by the writer to wait for the reader to exit.
  • VaR rwmutex, rmutex, wmutex: semaphore: =, 1; int readcount = 0; cobegin readeri begin // I = ,.... P (rwmutex); P (rmutex); readcount ++; If (readcount = 1) P (wmutex); // a reader enters and a mutex write operation V (rmutex ); V (rwmutex); // release the read/write mutex semaphores in time to allow other read and write processes to apply for resource read data; P (rmutex); readcount --; If (readcount = 0) V (wmutex); // all readers exit and allow write update V (rmutex); End writerj begin // J = ,.... P (rwmutex); // mutex is followed by other readers and writers P (wmutex); // If a reader is reading, wait for all readers to finish writing updates; V (wmutex ); // allow the next new reader to enter the next mutex write operation V (rwmutex); // allow the subsequent new reader and other writers to endcoend

    2. on the Linux platform, I used pthread_creat, pthread_mutex_trylock, and pthread_mutex_unlock;

    There must be bugs in code implementation. If you want to pass by, you may criticize and correct them.

    # 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;/* Mutual Exclusion of buffer access */pthread_mutex_t rlock = pthread_mutex_initializer;/* Mutual Exclusion of readcount access */pthread_mutex _ T wlock = pthread_mutex_initializer;/* write thread access mutex */INT readcount; int readpos, writepos; const int n = 10; int pool [10]; pthread_t pthreadarray [10]; void print () {int I; printf ("\ 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 * R Ead_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, rea DPOs, 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, THR 2, 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 threa D 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 );}
    Reference: http://blog.csdn.net/lenic/article/details/4675142address of my independent blog: http://www.fuxiang90.me /? P = 314 welcome

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.