Os Lab Da-3: Process Synchronization A. Implement The Solution For Reader - Writer's Problem. (Easy) Code

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

OS LAB DA-3

Process Synchronization
a. Implement the solution for reader – writer’s problem. (Easy)
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);

int 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);

return 0;

OUTPUT:

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


CODE
#include<stdio.h>

#define n 4 int

compltedPhilo = 0,i;

struct fork{ int taken; }

ForkAvil[n]; struct

philosp{

int left; int right; }

Philostatus[n]; void

goForDinner(int philID)

if(Philostatus[philID].left==10 && Philostatus[philID].right==10)

printf("Philosopher %d has completed dinner\n",philID+1); else

if(Philostatus[philID].left==1 && Philostatus[philID].right==1)

{ printf("Philosopher %d has completed dinner\n",philID+1);

Philostatus[philID].left = Philostatus[philID].right = 10; int

otherFork = philID-1; if(otherFork== -1) otherFork=(n-1);

ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0; printf("Philosopher %d is not using

fork %d and fork %d\n",philID+1,philID+1,otherFork+1); compltedPhilo++;

else if(Philostatus[philID].left==1 && Philostatus[philID].right==0){

if(philID==(n-1)){ if(ForkAvil[philID].taken==0){

ForkAvil[philID].taken = Philostatus[philID].right = 1; printf("Fork

%d taken by philosopher %d\n",philID+1,philID+1);

else{ printf("Philosopher %d waits for fork

%d\n",philID+1,philID+1);

}else{ int dupphilID = philID;

philID-=1; if(philID== -1)

philID=(n-1);

if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[dupphilID].right = 1; printf("Fork

%d taken by Philosopher %d\n",philID+1,dupphilID+1);

}else{

printf("Philosopher %d waits for Fork %d\n",dupphilID+1,philID+1);

}}}

else if(Philostatus[philID].left==0){

if(philID==(n-1))

{ if(ForkAvil[philID-1].taken==0){

ForkAvil[philID-1].taken = Philostatus[philID].left = 1; printf("Fork

%d taken by philosopher %d\n",philID,philID+1);

}else{ printf("Philosopher %d waits for fork

%d\n",philID+1,philID);

}else{

if(ForkAvil[philID].taken == 0){

ForkAvil[philID].taken = Philostatus[philID].left = 1; printf("Fork

%d taken by Philosopher %d\n",philID+1,philID+1);

}else{ printf("Philosopher %d is waiting for Fork

%d\n",philID+1,philID+1);

}else{}

int main(){ for(i=0;i<n;i++)

ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0; while(compltedPhilo<n){

for(i=0;i<n;i++) goForDinner(i); printf("\nTill now %d\n\n philosophers completed

their dinner....",compltedPhilo);

return 0;

}
OUTPUT:
c. Implement the solution for producer consumer problem (Easy)
CODE:
#include <pthread.h>

#include <semaphore.h>

#include <stdlib.h>

#include <stdio.h>

#define MaxItems 5

#define BufferSize 5

sem_t empty; sem_t

full;

int in = 0; int

out = 0; int
buffer[Buffe

rSize];

pthread_mutex_t mutex; void

*producer(void *pno)

int item; for(int i = 0; i <

MaxItems; i++) { item = rand();

sem_wait(&empty);

pthread_mutex_lock(&mutex);

buffer[in] = item; printf("Producer %d inserted item

%d in %d\n", *((int

*)pno),buffer[in],in);

in = (in+1)%BufferSize;

pthread_mutex_unlock(&mutex);

sem_post(&full);

void *consumer(void *cno)

for(int i = 0; i < MaxItems; i++)

{ sem_wait(&full);

pthread_mutex_lock(&mutex);

int item = buffer[out]; printf("Consumer %d removed item %d from

%d\n",*((int *)cno),item,

out); out = (out+1)%BufferSize;

pthread_mutex_unlock(&mutex);

sem_post(&empty);

int main()

{
pthread_t pro[5],con[5];

pthread_mutex_init(&mutex, NULL);

sem_init(&empty,0,BufferSize);

sem_init(&full,0,0); int a[5] =

{1,2,3,4,5}; for(int i = 0; i < 5; i++) {

pthread_create(&pro[i], NULL, (void *)producer, (void *)&a[i]);

} for(int i = 0; i < 5; i+

+) {

pthread_create(&con[i], NULL, (void *)consumer, (void *)&a[i]);

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

pthread_join(pro[i], NULL);

for(int i = 0; i < 5; i++)

{ pthread_join(con[i], NULL);

pthread_mutex_destroy(&mutex);

sem_destroy(&empty);

sem_destroy(&full); return 0;

OUTPUT:
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 <unistd.h>

#include <stdlib.h>

#include <time.h>
#include <pthread.h>

#include <semaphore.h>

#define MAX_CUSTOMERS 25

void *customer(void *num);

void *barber(void *); void

randwait(int secs);

sem_t waitingRoom; sem_t

barberChair; sem_t barberPillow;

sem_t seatBelt; int allDone = 0;

int main(int argc, char *argv[])

{ pthread_t btid; pthread_t

tid[MAX_CUSTOMERS]; long

RandSeed; int i, numCustomers,

numChairs;

int Number[MAX_CUSTOMERS];

printf("---------WELCOME--------\n");

printf("Enter the num. of customers: ");

scanf("%d",&numCustomers) ; printf("Enter

the num. of chairs: ");

scanf("%d",&numChairs); if (numCustomers > MAX_CUSTOMERS)

{ printf("The maximum number of Customers is %d.\n",

MAX_CUSTOMERS);

exit(-1);

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

Number[i] = i;

sem_init(&waitingRoom, 0, numChairs);

sem_init(&barberChair, 0, 1); sem_init(&barberPillow, 0, 0);

sem_init(&seatBelt, 0, 0); pthread_create(&btid, NULL,

barber, NULL); for (i=0; i<numCustomers; i++)


{ pthread_create(&tid[i], NULL, customer, (void

*)&Number[i]); sleep(1);

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

{ pthread_join(tid[i],NULL);

sleep(1);

allDone = 1;

sem_post(&barberPillow);

pthread_join(btid,NULL);

void *customer(void *number) { int num = *(int

*)number; printf("Customer %d leaavs for barber

shop.\n", num); randwait(2); printf("Customer %d

arrives at barber shop.\n", num);

sem_wait(&waitingRoom); printf("Customer %d enters

waiting room.\n", num); sem_wait(&barberChair);

sem_post(&waitingRoom); printf("Customer %d

waking for the barber.\n", num);

sem_post(&barberPillow); sem_wait(&seatBelt);

sem_post(&barberChair); printf("Customer %d is

leaving barber shop.\n", num);

void *barber(void *junk) { while (!

allDone) { printf("The barber sleeps

Zzzz... \n");

sem_wait(&barberPillow);

if (!allDone) {

printf("The barber is trimming\n");

randwait(2); printf("The barber has doen the

haircut!! \n"); sem_post(&seatBelt);


}

else { printf("The barber leaves to his

house.....\n"); printf("-------GOOD BYE-------");

void randwait(int secs) {

int len; len = (int) ((1 *

secs) + 1); sleep(len);

OUTPUT:
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.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define SIZE 5
#define NUMB_THREADS 6
#define PRODUCER_LOOPS 2
typedef int buffer_t;
buffer_t buffer[SIZE]; int
buffer_index;
pthread_mutex_t buffer_mutex;
sem_t full_sem; sem_t
empty_sem;
void insertbuffer(buffer_t value) { if
(buffer_index < SIZE)
{ buffer[buffer_index++] = value;
} else {
printf("Buffer overflow\n");
}}
buffer_t dequeuebuffer() { if
(buffer_index > 0) { return
buffer[--buffer_index];
} else {
printf("Buffer underflow\n"); }
return 0; }
void *producer(void *thread_n) { int
thread_numb = *(int *)thread_n;
buffer_t value;
int i=0;
while (i++ < PRODUCER_LOOPS) { sleep(rand() % 10); value = rand()
% 100; sem_wait(&full_sem);
pthread_mutex_lock(&buffer_mutex); insertbuffer(value);
pthread_mutex_unlock(&buffer_mutex); sem_post(&empty_sem);
printf("Producer %d added %d to buffer\n", thread_numb, value); }
pthread_exit(0);}

void *consumer(void *thread_n)


{ int thread_numb = *(int
*)thread_n; buffer_t value;
int i=0;
while (i++ < PRODUCER_LOOPS) { sem_wait(&empty_sem);
pthread_mutex_lock(&buffer_mutex); value = dequeuebuffer(value);
pthread_mutex_unlock(&buffer_mutex); sem_post(&full_sem);
printf("Consumer %d dequeue %d from buffer\n", thread_numb, value);
} pthread_exit(0); } int main(int argc, int
**argv) { buffer_index = 0;
pthread_mutex_init(&buffer_mutex, NULL);
sem_init(&full_sem, 0,SIZE);
sem_init(&empty_sem,0,0); pthread_t
thread[NUMB_THREADS]; int
thread_numb[NUMB_THREADS];
int i;
for (i = 0; i < NUMB_THREADS; ) { thread_numb[i] = i;
pthread_create(thread + i,NULL,producer,thread_numb + i);
i++;
thread_numb[i] = i; pthread_create(&thread[i], NULL, consumer,
&thread_numb[i]); i++;} for (i = 0; i < NUMB_THREADS; i++)
pthread_join(thread[i], NULL);
pthread_mutex_destroy(&buffer_mutex);

sem_destroy(&full_sem);
sem_destroy(&empty_sem);

return 0;
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy