Swe3001-Da 4
Swe3001-Da 4
Swe3001-Da 4
(LAB)
DIGITAL ASSESSMENT – 4
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()
{
printf("\n\nThis File is Created by: SHANMATHY SHREE AE
21MIS0498\n");
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:
CODE:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
pthread_t philosopher[5];
pthread_mutex_t chopstick[5];
void *fun(int n)
{
printf("Philosopher %d is thinking\n",n);
pthread_mutex_lock(&chopstick[n]);
pthread_mutex_lock(&chopstick[(n+1)%5]);
printf ("Philosopher %d is Eating\n",n);
Sleep(3);
pthread_mutex_unlock(&chopstick[n]);
pthread_mutex_unlock(&chopstick[(n+1)%5]);
printf ("Philosopher %d Finished Eating\n",n);
return(NULL);
}
int main()
{
int i;
printf("\n\nThis File is Created by: SHANMATHY SHREE AE
21MIS0498\n");
printf ("\nAll Philosophers are Sitting on the Round Table: \n\n");
for(i=0;i<5;i++)
pthread_mutex_init(&chopstick[i],NULL);
for(i=0;i<5;i++)
pthread_create(&philosopher[i],NULL,(void *)fun,(void *)i);
for(i=0;i<5;i++)
pthread_join(philosopher[i],NULL);
for(i=0;i<5;i++)
pthread_mutex_destroy(&chopstick[i]);
printf("\nAll Philosopher Finished Eating at least for Once. \n\n");
return 0;
}
OUTPUT:
(c) 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. Medium
CODE:
#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("\n\nThis File is Created by: SHANMATHY SHREE AE
21MIS0498\n");
printf("\nEnter the Number of Producers: ");
scanf("%d",&n1);
printf("\nEnter the Number 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);
Sleep(5);
exit(0);
}
OUTPUT:
(d) Write a Program to implement banker’s algorithm
for Deadlock avoidance. Medium
CODE:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int Max[10][10],need[10][10],alloc[10][10],avail[10], completed[10],
safeSequence[10];
int p,r,i,j,process,count; count = 0;
printf("\n\nThis File is Created by: SHANMATHY SHREE AE
21MIS0498\n");
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i=0;i<p;i++)
completed[i] = 0;
printf("\n\nEnterthenoofresources:");
scanf("%d", &r);
printf("\n\nEntertheMaxMatrixforeachprocess:");
for(i = 0; i < p; i++)
{
printf("\nForprocess%d:",i+1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEntertheallocationforeachprocess:");
for(i = 0; i < p; i++)
{
printf("\nForprocess %d:",i+1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEntertheAvailableResources:");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j =0; j < r; j++)
need[i][j] = Max[i][j]- alloc[i][j];
do
{
printf("\nMaxmatrix:\tAllocationmatrix:\n");
for(i= 0; i< p; i++)
{
for( j = 0; j < r; j++)
printf("%d ",Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)//if not completed
{
process = i ; for(j=0;j
<r;j++)
{
if(avail[j] < need[i][j])
{
process =-1;
break;
}
}
}
if(process !=-1)
break;
}
if(process != -1)
{
printf("\nProcess%drunstocompletion!", process+1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count!=p&&process!=-1);
if(count ==p)
{
printf("\nThesystemisinasafestate!!\n");
printf("Safe Sequence :< ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]); printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
OUTPUT: