0% found this document useful (0 votes)
120 views

Os 8

The document presents a C++ program that implements the reader/writer problem using semaphores. It creates 3 reader threads and 2 writer threads that access a shared data variable. Semaphores mutex and rw_mutex are used to allow either multiple readers or a single writer to access the data at any given time in order to prevent race conditions. The reader and writer functions use the semaphores to lock/unlock access to the data as needed.

Uploaded by

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

Os 8

The document presents a C++ program that implements the reader/writer problem using semaphores. It creates 3 reader threads and 2 writer threads that access a shared data variable. Semaphores mutex and rw_mutex are used to allow either multiple readers or a single writer to access the data at any given time in order to prevent race conditions. The reader and writer functions use the semaphores to lock/unlock access to the data as needed.

Uploaded by

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

P a g e | 36

PRACTICAL – 8
Aim:
Write a program to implement reader/writer problem using
semaphore.

#include <iostream> #include


<pthread.h> #include
<semaphore.h>

using namespace std; sem_t


rw_mutex, mutex;int
read_count = 0; int data = 0;

void* reader(void* arg) {


int reader_id = *(int*)arg;

sem_wait(&mutex);
read_count++;
if (read_count == 1) {
sem_wait(&rw_mutex);
}
sem_post(&mutex);

cout << "Reader " << reader_id << " is reading data: " <<data << endl;

sem_wait(&mutex);
read_count--;
if (read_count == 0) {
sem_post(&rw_mutex);
}
sem_post(&mutex); int

usleep(1000000);

pthread_exit(NULL);
}

void* writer(void* arg) {

GAURAV KUMAR(IT-1)
(02515003120)
P a g e | 37

int writer_id = *(int*)arg;

sem_wait(&rw_mutex);

data++;
cout << "Writer " << writer_id << " is writing data: " <<data << endl;

sem_post(&rw_mutex);int

usleep(1000000);

pthread_exit(NULL);
}

int main() {
const int NUM_READERS = 3;const
int NUM_WRITERS = 2;

pthread_t readers[NUM_READERS];
pthread_t writers[NUM_WRITERS];

sem_init(&rw_mutex, 0, 1);
sem_init(&mutex, 0, 1);

for (int i = 0; i < NUM_READERS; i++) {int* reader_id


= new int(i);
pthread_create(&readers[i], NULL, reader,
(void*)reader_id);
}

for (int i = 0; i < NUM_WRITERS; i++) {int* writer_id =


new int(i);
pthread_create(&writers[i], NULL, writer,
(void*)writer_id);
}

for (int i = 0; i < NUM_READERS; i++) {


pthread_join(readers[i], NULL);
}

for (int i = 0; i < NUM_WRITERS; i++) {


pthread_join(writers[i], NULL);
}

GAURAV KUMAR(IT-1)
(02515003120)
P a g e | 38

sem_destroy(&rw_mutex);
sem_destroy(&mutex);

return 0;
}

OUTPUT:

GAURAV KUMAR(IT-1)
(02515003120)

You might also like