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

2 C D 3 Prodcons Pgms

The code simulates the producer-consumer problem using semaphores in C. It creates 5 producer and 5 consumer threads that share a fixed size buffer. Producers add random items to the buffer using semaphores for synchronization and consumers remove items from the buffer. The output shows the producers producing random items which are then consumed by consumers in no particular order.

Uploaded by

Khyathi Kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

2 C D 3 Prodcons Pgms

The code simulates the producer-consumer problem using semaphores in C. It creates 5 producer and 5 consumer threads that share a fixed size buffer. Producers add random items to the buffer using semaphores for synchronization and consumers remove items from the buffer. The output shows the producers producing random items which are then consumed by consumers in no particular order.

Uploaded by

Khyathi Kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

c) Round Robin

#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d
:",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)

{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);

return 0;
}
Output:

d) Priority

#include<stdio.h>

int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);

printf("\nEnter Burst Time and Priority\n");


for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1;
}

for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;

for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=total/n;
total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


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

tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=total/n;
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;
}

Output:
Program-3
Develop a C program to simulate producer-consumer problem using semaphores.

prodcons.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

#define BUFFER_SIZE 5

sem_t mutex, full, empty;


int buffer[BUFFER_SIZE];
int in = 0, out = 0;
void *producer(void *p){
int item;
item = rand() % 10; // Produce a random item within 0-9

sem_wait(&empty);
sem_wait(&mutex);

// Critical Section: Add item to the buffer


buffer[in] = item;
printf("Producer %d produced item %d\n", *((int*)p),item);
in = (in + 1) % BUFFER_SIZE;

sem_post(&mutex);
sem_post(&full);

pthread_exit(NULL);
}

void *consumer(void *c) {


int item;
sem_wait(&full);
sem_wait(&mutex);

// Critical Section: Remove item from the buffer


item = buffer[out];
printf("Consumer %d consumed item %d \n", *((int*)c),item);
out = (out + 1) % BUFFER_SIZE;

sem_post(&mutex);
sem_post(&empty);
pthread_exit(NULL);
}

int main() {
// Initialize semaphores
sem_init(&mutex, 0, 1);
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);

int a[5]={1,2,3,4,5};
int i;
pthread_t p[5],c[5];

// Creating 5 producer and consumer threads


for(i=0;i<5;i++)
pthread_create(&p[i], NULL, producer, (void *)&a[i]);
for(i=0;i<5;i++)
pthread_create(&c[i], NULL, consumer, (void *)&a[i]);

// Wait for threads to finish


for(i=0;i<5;i++)
pthread_join(p[i], NULL);
for(i=0;i<5;i++)
pthread_join(c[i], NULL);

// Destroy semaphores
sem_destroy(&mutex);
sem_destroy(&full);
sem_destroy(&empty);

return 0;
}

Compilation steps:

cc prodcons.c
./a.out
Or
cc prodcons.c –pthread
./a.out

OUTPUT:

Producer 1 produced item 3


Consumer 3 consumed item 3
Producer 5 produced item 6
Consumer 2 consumed item 6
Producer 4 produced item 7
Consumer 1 consumed item 7
Producer 3 produced item 5
Producer 2 produced item 3
Consumer 5 consumed item 5
Consumer 4 consumed item 3

You might also like