Os Lab Da-3: Process Synchronization A. Implement The Solution For Reader - Writer's Problem. (Easy) Code
Os Lab Da-3: Process Synchronization A. Implement The Solution For Reader - Writer's Problem. (Easy) Code
Os Lab Da-3: Process Synchronization A. Implement The Solution For Reader - Writer's Problem. (Easy) Code
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;
*reader(void *arg)
{ int f; f =
((int)arg);
sem_wait(&mutex);
sem_wait(&writeblock); sem_post(&mutex);
sleep(1);
sem_wait(&mutex); rcount
= rcount - 1; if(rcount==0)
sem_post(&writeblock);
sem_post(&mutex);
{ int f; f =
((int) arg);
sem_wait(&writeblock); data++; printf("Data writen
sem_post(&writeblock);
int main()
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:
#define n 4 int
compltedPhilo = 0,i;
ForkAvil[n]; struct
philosp{
Philostatus[n]; void
goForDinner(int philID)
if(philID==(n-1)){ if(ForkAvil[philID].taken==0){
%d\n",philID+1,philID+1);
philID=(n-1);
if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[dupphilID].right = 1; printf("Fork
}else{
}}}
else if(Philostatus[philID].left==0){
if(philID==(n-1))
{ if(ForkAvil[philID-1].taken==0){
%d\n",philID+1,philID);
}else{
if(ForkAvil[philID].taken == 0){
%d\n",philID+1,philID+1);
}else{}
ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0; while(compltedPhilo<n){
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
full;
int in = 0; int
out = 0; int
buffer[Buffe
rSize];
*producer(void *pno)
sem_wait(&empty);
pthread_mutex_lock(&mutex);
%d in %d\n", *((int
*)pno),buffer[in],in);
in = (in+1)%BufferSize;
pthread_mutex_unlock(&mutex);
sem_post(&full);
{ sem_wait(&full);
pthread_mutex_lock(&mutex);
%d\n",*((int *)cno),item,
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);
} for(int i = 0; i < 5; i+
+) {
pthread_join(pro[i], NULL);
{ 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.
CODE:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
#define MAX_CUSTOMERS 25
randwait(int secs);
tid[MAX_CUSTOMERS]; long
numChairs;
int Number[MAX_CUSTOMERS];
printf("---------WELCOME--------\n");
scanf("%d",&numCustomers) ; printf("Enter
MAX_CUSTOMERS);
exit(-1);
Number[i] = i;
sem_init(&waitingRoom, 0, numChairs);
*)&Number[i]); sleep(1);
{ pthread_join(tid[i],NULL);
sleep(1);
allDone = 1;
sem_post(&barberPillow);
pthread_join(btid,NULL);
sem_post(&waitingRoom); printf("Customer %d
sem_post(&barberPillow); sem_wait(&seatBelt);
sem_post(&barberChair); printf("Customer %d is
Zzzz... \n");
sem_wait(&barberPillow);
if (!allDone) {
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);}
sem_destroy(&full_sem);
sem_destroy(&empty_sem);
return 0;
}
OUTPUT: