OS Lab Assignment - 3 Dr. Priya M Name: Rajat Srivastava REG NO: 19BCE2055
OS Lab Assignment - 3 Dr. Priya M Name: Rajat Srivastava REG NO: 19BCE2055
OS Lab Assignment - 3 Dr. Priya M Name: Rajat Srivastava REG NO: 19BCE2055
LAB ASSIGNMENT -3
DR. PRIYA M
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;
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);
}
}
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];
sleep(2);
sem_wait(&mutex);
test(phnum);
sem_post(&mutex);
sem_wait(&S[phnum]);
sleep(1);
}
sem_wait(&mutex);
state[phnum] = THINKING;
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
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);
sem_init(&S[i], 0, 0);
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
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 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 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;
}
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);
}