Code can run, but it is really not observed what the reader first, or write a priority. It is not known where this priority conflict is coming from, and it does not know what the book says.
Especially in the writer of the following code, if Sem_wait (&z) is introduced, the writer process blocks, causing the reader to block. After the removal, the reader gives priority to the situation.
#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 reader b++;
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);
}
Compilation method: Gcc-o PVRWW Pv-posix-reader-writer.c-lpthread
The POSIX semaphore mechanism is used here.
The following code can be modified to achieve the writer's priority: As long as a writer thread is generated, the running reader process will be terminated immediately after execution, allowing the writer
First
Run.
#include <stdio.h> first #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 (&amP;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++)//create customer Thread {pthread_create (& (Preader[k-1)), NULL, (void *) reader,&k);
Srand (Time (0)); Sleep (rand ()% 2 + 1); 1 to 3 random number} for (k = 1; k <= 5; k++)//create customer Thread {pthread_create (& (Pwriter[k-1)), NULL (voi
d *) writer,&k);
Srand (Time (0)); Sleep (rand ()% 2 + 1);
1 to 3 random number} 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");
}