0% found this document useful (0 votes)
4 views4 pages

Os Assign4

The document presents a C program that implements the Readers-Writers problem using semaphores to ensure mutual exclusion while allowing multiple readers and one writer to access a shared resource concurrently. It includes the necessary code with functions for both readers and writers, along with synchronization mechanisms. The program runs until the user presses ENTER, at which point it terminates and cleans up resources.

Uploaded by

omkarkekan2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Os Assign4

The document presents a C program that implements the Readers-Writers problem using semaphores to ensure mutual exclusion while allowing multiple readers and one writer to access a shared resource concurrently. It includes the necessary code with functions for both readers and writers, along with synchronization mechanisms. The program runs until the user presses ENTER, at which point it terminates and cleans up resources.

Uploaded by

omkarkekan2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment No.

04
Title : Write a program using semaphores to implement the Readers-Writers problem,
ensuring mutual exclusion while allowing multiple readers and one writer to access a
shared resource concurrently.

Name:Rasal Aarya Vijay Batch : B Roll No : 23107101

Program :

#include <stdio.h>

#include <unistd.h>

#include <semaphore.h>

#include <pthread.h>

#include <stdbool.h>

sem_t w;

pthread_mutex_t

m; int Rc = 0; bool

running = true;

void *writer(void *arg) { while

(running) { sem_wait(&w);

printf("writer is writing....\n");

sleep(1); printf("writer finished

writing..\n"); sem_post(&w);

sleep(2);

return NULL;

}
void *reader(void *arg)

{ while (running)

{ pthread_mutex_lock(&m

); Rc++; if (Rc == 1) {

sem_wait(&w);

pthread_mutex_unlock(&m);

printf("reader is reading...\n");

sleep(1); printf("reader finished

reading...\n");

pthread_mutex_lock(&m);

Rc--; if (Rc

== 0)

{ sem_post(&w

);

pthread_mutex_unlock(&m);

sleep(2);

return NULL;

int main()

{ pthread_t r1, r2,

w1;
sem_init(&w, 0, 1);

pthread_mutex_init(&m, NULL);

pthread_create(&r1, NULL, reader,

NULL); pthread_create(&r2, NULL,

reader, NULL); pthread_create(&w1,

NULL, writer, NULL);

printf("Press ENTER to stop...\n");

getchar(); running = false;

pthread_join(r1, NULL);

pthread_join(r2, NULL);

pthread_join(w1, NULL);

sem_destroy(&w);

pthread_mutex_destroy(&m);

return 0;

Output :

Press ENTER to stop...

reader is reading...

reader is reading...

reader finished reading...

reader finished reading...


writer is writing....

writer finished writing..

reader is reading...

reader is reading...

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy