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

OS Lab Assignment - 3 Dr. Priya M Name: Rajat Srivastava REG NO: 19BCE2055

The document contains code implementations for various operating systems synchronization problems: 1) The reader-writer problem is implemented using semaphores to allow multiple readers or a single writer at any time but not both. 2) The dining philosophers problem is implemented using semaphores and shared variables to prevent deadlock as philosophers eat with two forks. 3) The producer-consumer problem is implemented using semaphores to synchronize a fixed number of producers adding to a fixed size buffer and consumers removing from the buffer.

Uploaded by

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

OS Lab Assignment - 3 Dr. Priya M Name: Rajat Srivastava REG NO: 19BCE2055

The document contains code implementations for various operating systems synchronization problems: 1) The reader-writer problem is implemented using semaphores to allow multiple readers or a single writer at any time but not both. 2) The dining philosophers problem is implemented using semaphores and shared variables to prevent deadlock as philosophers eat with two forks. 3) The producer-consumer problem is implemented using semaphores to synchronize a fixed number of producers adding to a fixed size buffer and consumers removing from the buffer.

Uploaded by

RAJAT SRIVASTAVA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

OS

LAB ASSIGNMENT -3

DR. PRIYA M

NAME : RAJAT SRIVASTAVA

REG NO : 19BCE2055
(a) Implement the solution for reader – writer’s problem.
Code:

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

sem_t mutex,writeblock;
int data = 0,rcount = 0;

void *reader(void *arg)


{
int f;
f = ((int)arg);
sem_wait(&mutex);
rcount = rcount + 1;
if(rcount==1)
sem_wait(&writeblock);
sem_post(&mutex);
printf("Data read by the reader %d is %d\n",f,data);
sleep(1);
sem_wait(&mutex);
rcount = rcount - 1;
if(rcount==0)
sem_post(&writeblock);
sem_post(&mutex);
}

void *writer(void *arg)


{
int f;
f = ((int) arg);
sem_wait(&writeblock);
data++;
printf("Data writen by the writer %d is %d\n",f,data);
sleep(1);
sem_post(&writeblock);
}

main()
{
int i,b;
pthread_t rtid[5],wtid[5];
sem_init(&mutex,0,1);
sem_init(&writeblock,0,1);
for(i=0;i<=2;i++)
{
pthread_create(&wtid[i],NULL,writer,(void *)i);
pthread_create(&rtid[i],NULL,reader,(void *)i);
}
for(i=0;i<=2;i++)
{
pthread_join(wtid[i],NULL);
pthread_join(rtid[i],NULL);
}
}

(b) Implement the solution for dining philosopher’s problem.

Code:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>

#define N5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N

int state[N];
int phil[N] = { 0, 1, 2, 3, 4 };

sem_t mutex;
sem_t S[N];

void test(int phnum)


{
if (state[phnum] == HUNGRY
&& state[LEFT] != EATING
&& state[RIGHT] != EATING) {
state[phnum] = EATING;

sleep(2);

printf("Philosopher %d takes fork %d and %d\n",


phnum + 1, LEFT + 1, phnum + 1);

printf("Philosopher %d is Eating\n", phnum + 1);


sem_post(&S[phnum]);
}
}

void take_fork(int phnum)


{

sem_wait(&mutex);

// state that hungry


state[phnum] = HUNGRY;

printf("Philosopher %d is Hungry\n", phnum + 1);

test(phnum);

sem_post(&mutex);

sem_wait(&S[phnum]);

sleep(1);
}

void put_fork(int phnum)


{

sem_wait(&mutex);
state[phnum] = THINKING;

printf("Philosopher %d putting fork %d and %d down\n",


phnum + 1, LEFT + 1, phnum + 1);
printf("Philosopher %d is thinking\n", phnum + 1);

test(LEFT);
test(RIGHT);

sem_post(&mutex);
}

void* philospher(void* num)


{

while (1) {

int* i = num;

sleep(1);

take_fork(*i);

sleep(0);
put_fork(*i);
}
}

int main()
{

int i;
pthread_t thread_id[N];
sem_init(&mutex, 0, 1);

for (i = 0; i < N; i++)

sem_init(&S[i], 0, 0);

for (i = 0; i < N; i++) {


pthread_create(&thread_id[i], NULL,
philospher, &phil[i]);

printf("Philosopher %d is thinking\n", i + 1);


}

for (i = 0; i < N; i++)

pthread_join(thread_id[i], NULL);
}
Code:
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
int empty = 10, x = 0;
void producer()
{
--mutex;
++full;
--empty;
x++;
printf("\nProducer produces item %d",x);
++mutex;
}
void consumer()
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes item %d",x);
x--;
++mutex;
}

int main()
{
int n, i;
printf("\n1. Press 1 for Producer\n2. Press 2 for Consumer\n3. Press 3 for Exit");
#pragma omp critical

for (i = 1; i > 0; i++) {

printf("\nEnter your choice:");


scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1)
&& (empty != 0)) {
producer();
}
else {
printf("Buffer is full!");
}
break;

case 2:
if ((mutex == 1)
&& (full != 0)) {
consumer();
}
else {
printf("Buffer is empty!");
}
break;
case 3:
exit(0);
break;
}
}
}
(d) The analogy is based upon a hypothetical barber shop with one barber. There is
a barber shop which has one barber, one barber chair, and n chairs for waiting for
customers if there are any to sit on the chair.

· If there is no customer, then the barber sleeps in his own chair.

· When a customer arrives, he has to wake up the barber.

· If there are many customers and the barber is cutting a customer’s hair, then the
remaining customers either wait if there are empty chairs in the waiting room or
they leave if no chairs are empty.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>

int accessSeats[2];
int customers[2];
int barber[2];
int freeaccessSeats[2];

void randomWait();
void barber_process();
void customer_process();

void V(int pd[]) {


int a=1;
write(pd[1],&a,sizeof(int));
}

void P(int pd[]) {


int a;
read(pd[0],&a,sizeof(int));
}

void main() {
int i;
pipe(accessSeats);
pipe(customers);
pipe(barber);
pipe(freeaccessSeats);

V(accessSeats);

int num = 3;
write(freeaccessSeats[1],&num,sizeof(int));

if (fork() == 0) {
srand(time(0)+1);
barber_process();
return;
}

for (i = 1; i <= 5; i++) {


if (fork() == 0) {
srand(time(0)+2*i);
customer_process();
return;
}
}
sleep(10);
printf("\nDone\n\n");
}

void barber_process() {
int i;
int num;
for (i = 1; i <= 10; ++i) {
printf("Barber %d is trying to get a customer\n",i);
P(customers);
printf("Barber %d is waiting for the seat to become free\n",i);
P(accessSeats);
read(freeaccessSeats[0],&num,sizeof(int));
num++;
write(freeaccessSeats[1],&num,sizeof(int));
printf("Barber %d is increasing the number of free accessSeats to %d\n",i,num);
V(barber);
V(accessSeats);
printf("Barber is now cutting hair %d\n",i);
randomWait();
}
}

void customer_process() {
int i;
int num;
for (i = 1; i <= 2; ++i) {
printf("New customer trying to find a seat\n");
P(accessSeats);
read(freeaccessSeats[0],&num,sizeof(int));
if (num > 0)
{
num--;
write(freeaccessSeats[1],&num,sizeof(int));
printf("Customer left seat in waiting room. The total free accessSeats are now: %d\n",num);
V(customers);
V(accessSeats);
printf("Customer is now waiting for the barber\n");
P(barber);
printf("Customer is now getting a hair cut\n");
}
else
{
write(freeaccessSeats[1],&num,sizeof(int));
V(accessSeats);
printf("No free chairs in waiting room\n");
}
randomWait();
}
}

void randomWait() {
int delay;
delay = random() % 9999999999;
printf(" - wait: %d\n", delay);

}
(e)A pair of processes involved in exchanging a sequence of integers. The number of
integers that can be produced and consumed at a time is limited to 100. Write a
Program to implement the producer and consumer problem using POSIX semaphore
for the above scenario.
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#include<stdlib.h>
#define buffersize 100
pthread_mutex_t mutex;
pthread_t tidP[100],tidC[100];
sem_t full,empty;
int counter;
int buffer[buffersize];
void initialize()
{
pthread_mutex_init(&mutex,NULL);
sem_init(&full,1,0);
sem_init(&empty,1,buffersize);
counter=0;
}
void write(int item)
{
buffer[counter++]=item;
}
int read()
{
return(buffer[--counter]);
}
void * producer (void * param)
{
int waittime,item,i;
item=rand()%5;
waittime=rand()%5;
sem_wait(&empty);pthread_mutex_lock(&mutex);
printf("\nProducer has produced item: %d\n",item);
write(item);
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
void * consumer (void * param)
{
int waittime,item;
waittime=rand()%5;
sem_wait(&full);
pthread_mutex_lock(&mutex);
item=read();
printf("\nConsumer has consumed item: %d\n",item);
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
int main()
{
int n1,n2,i;
initialize();
printf("\nEnter the no of producers: ");
scanf("%d",&n1);
printf("\nEnter the no of consumers: ");
scanf("%d",&n2);
for(i=0;i<n1;i++)
pthread_create(&tidP[i],NULL,producer,NULL);
for(i=0;i<n2;i++)
pthread_create(&tidC[i],NULL,consumer,NULL);
for(i=0;i<n1;i++)
pthread_join(tidP[i],NULL);
for(i=0;i<n2;i++)
pthread_join(tidC[i],NULL);
exit(0);
}

You might also like