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

OS Assignment 2

This document provides code for solving the reader-writer problem using mutexes and semaphores. It defines functions for reader and writer threads, initializes semaphores for mutex and writing, creates threads for the given number of readers and writers, and joins the threads. The reader and writer functions implement locking using semaphores - the reader function locks mutex before/after reading, and increments/decrements the read count, while the writer function waits on the write semaphore to enter the critical section.

Uploaded by

Sohan Wagh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

OS Assignment 2

This document provides code for solving the reader-writer problem using mutexes and semaphores. It defines functions for reader and writer threads, initializes semaphores for mutex and writing, creates threads for the given number of readers and writers, and joins the threads. The reader and writer functions implement locking using semaphores - the reader function locks mutex before/after reading, and increments/decrements the read count, while the writer function waits on the write semaphore to enter the critical section.

Uploaded by

Sohan Wagh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

OS Tutorial No.

Name : Sohan Wagh


Roll no.: 333066
PRN.: 22120133
Batch: C3

Reader Writer Problem solution with Mutex , Semaphore


Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
void *writer_thr(int temp);
void *reader_thr(int temp);
sem_t mutex;
sem_t wrt;
int readcount = 0, nwt, nrd;
void main()
{
long int i;
sem_init(&mutex, 0, 1);
sem_init(&wrt, 0, 1);
pthread_t reader[100], writer[100];
printf("\nEnter number of readers:");
scanf("%d", &nrd);
printf("\nEnter number of writers:");
scanf("%d", &nwt);
for (i = 1; i <= nwt; i++)
{
pthread_create(&writer[i], NULL, (void *)writer_thr, (int *)i);
pthread_join(writer[i], NULL);
}
for (i = 1; i <= nrd; i++)
{
pthread_create(&reader[i], NULL, (void *)reader_thr, (int *)i);
}
for (i = 1; i <= nrd; i++)
{
pthread_join(reader[i], NULL);
}
sem_destroy(&wrt);
sem_destroy(&mutex);
}
void *reader_thr(int temp)
{
printf("\nReader %d is trying to enter database for reading.", temp);
sem_wait(&mutex);
readcount++;
if (readcount == 1)
sem_wait(&wrt);
sem_post(&mutex);
printf("\nReader %d is now reading in database.", temp);
sem_wait(&mutex);
readcount--;
if (readcount == 0)
sem_post(&wrt);
sem_post(&mutex);
printf("\nReader %d has left the database.\n", temp);
sleep(3);
}
void *writer_thr(int temp)
{
printf("\nWriter %d is trying to enter database for modifying data", temp);
sem_wait(&wrt);
printf("\nWriter %d is writing in database.", temp);
sleep(3);
printf("\nWriter %d is leaving the database.\n", temp);
sem_post(&wrt);
}
Output

You might also like