William Stallings "operating system kernel and design principles" in the book Linux C language to achieve the reader problem (writer first) code __linux

Source: Internet
Author: User
Tags posix rand semaphore

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 (&AMP;Z);
        {sem_wait (&AMP;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 (&AMP;X);
    readcount--;
    if (readcount==0) sem_post (&wsem);    
    printf ("\nreader%d finished reading,readcount=%d\n", C,readcount);
Sem_post (&AMP;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 (&AMP;Z);
            Sem_wait (&rsem);
            Sem_wait (&AMP;X);
            readcount++;
            printf ("\ n-------------readcount=%d-----", readcount);
            if (Readcount = = 1) {sem_wait (&wsem);
        } sem_post (&x);
    Sem_post (&rsem);

    Sem_wait (&AMP;Z);
    
    /*critcal section */printf ("\nreader%d is reading\n", c);    

    Sleep (10);
        /* Critical Section COMPLETD * * sem_wait (&AMP;X);
        readcount--;
        if (readcount==0) sem_post (&wsem); printf ("\nreader%d Finished reading,readcount=%d\n ", C,readcount);
    Sem_post (&AMP;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");
 }



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.