0% found this document useful (0 votes)
23 views2 pages

OS Ass 4b

The document contains C code that implements readers and writers accessing a shared database using mutexes and semaphores for synchronization. The code creates reader and writer threads, each of which access the database and print status messages.

Uploaded by

Rajat 3030
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)
23 views2 pages

OS Ass 4b

The document contains C code that implements readers and writers accessing a shared database using mutexes and semaphores for synchronization. The code creates reader and writer threads, each of which access the database and print status messages.

Uploaded by

Rajat 3030
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/ 2

Rajat OS Assignment 4 B

#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);
pthread_mutex_t mutex;
sem_t wrt;
int readcount=0,nwt,nrd;

void main()
{
long int i;
pthread_mutex_init(&mutex,0);
sem_init(&wrt,0,1);
pthread_t reader[100],writer[100];
printf("\n Enter number of readers:");
scanf("%d",&nrd);
printf("\n Enter 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);
pthread_mutex_destroy(&mutex);

void *reader_thr(int temp)


{
printf("\n Reader %d is trying to enter database for reading.",temp);
pthread_mutex_lock(&mutex);
readcount++;
if(readcount==1)
sem_wait(&wrt);
pthread_mutex_unlock(&mutex);

printf("\nReader %d is now reading in database.",temp);

pthread_mutex_lock(&mutex);
readcount--;
if(readcount==0)

sem_post(&wrt);
pthread_mutex_unlock(&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("\n Writer %d is writing in database.",temp);
sleep(3);
printf("\n Writer %d is leaving the database.\n",temp);
sem_post(&wrt);
}

Result :-
pvg@pvg-HP-ProDesk-400-G4-SFF:~/3030$ cc 4b.c -o th -lpthread
pvg@pvg-HP-ProDesk-400-G4-SFF:~/3030$ ./th

Enter number of readers:3

Enter number of writers:4

Writer 1 is trying to enter database for modifying data


Writer 1 is writing in database.
Writer 1 is leaving the database.

Writer 2 is trying to enter database for modifying data


Writer 2 is writing in database.
Writer 2 is leaving the database.

Writer 3 is trying to enter database for modifying data


Writer 3 is writing in database.
Writer 3 is leaving the database.

Writer 4 is trying to enter database for modifying data


Writer 4 is writing in database.
Writer 4 is leaving the database.

Reader 1 is trying to enter database for reading.


Reader 1 is now reading in database.
Reader 1 has left the database.

Reader 2 is trying to enter database for reading.


Reader 2 is now reading in database.
Reader 2 has left the database.

Reader 3 is trying to enter database for reading.


Reader 3 is now reading in database.
Reader 3 has left the database.
pvg@pvg-HP-ProDesk-400-G4-SFF:~/3030$

You might also like