You are on page 1of 11

PRODUCER CONSUMER PROBLEM:

#include <stdio.h>
#include <stdlib.h>
int mutex = 1, full = 0, empty = 3, x = 0;
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return (++s);
}
void producer()
{
mutex = wait(mutex);
full = signal(full);
empty = wait(empty);
x++;
printf("\nProducer produces the item %d", x);
mutex = signal(mutex);
}
void consumer()
{
mutex = wait(mutex);
full = wait(full);
empty = signal(empty);
printf("\nConsumer consumes item %d", x);
x--;
mutex = signal(mutex);
}

int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while (1)

{
printf("\nEnter your choice:");
scanf("%d", &n);
switch (n)
{
case 1:
if ((mutex == 1) && (empty != 0))
producer();
else
printf("Buffer is full!!");
break;
case 2:
if ((mutex == 1) && (full != 0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}}
return 0;}
OUTPUT:
SEMAPHORES:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
int X = 5; // Shared variable
sem_t S; // Semaphore
void *th1(void *args)
{
// Entry Section
sem_wait(&S); // WAIT
// Critical Section
int temp = X;
temp = temp + 1;
sleep(1); // preempt the thread so that it can switch to
another thread
X = temp; OUTPUTS:
// Exit Section
sem_post(&S); // SIGNAL
return NULL;
}
void *th2(void *args)
{
// Entry Section
sem_wait(&S);
// Critical Section
int temp = X;
temp = temp - 1;
sleep(1);
X = temp;
// Exit Section
sem_post(&S);
return NULL;
}
int main()
{
// initialize semaphore
sem_init(&S, 0, 1);
pthread_t thread1, thread2;
printf("X: %d\n", X);
pthread_create(&thread1, NULL, th1, (void *)&thread1);
pthread_create(&thread2, NULL, th2, (void *)&thread2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("X: %d\n", X);
return 0;
}
First Come First Serve(FCFS):
#include <stdio.h>
void print_gantt_chart(int pid[],int bt[],int tatt[], int n);
int main()
{
int pid[15], bt[15], tatt[15], n;

printf("Enter the number of processes: ");


scanf("%d",&n);

printf("Enter process id of all the processes: ");


for(int i=0;i<n;i++)
{
scanf("%d",&pid[i]);
}

printf("Enter burst time of all the processes: ");


for(int i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}

int i, wt[n];
wt[0]=0;

//for calculating waiting time of each process


for(i=1; i<n; i++)
{
wt[i]= bt[i-1]+ wt[i-1];
}

printf("Process ID Burst Time Waiting


Time TurnAround Time\n");
float twt=0.0;
float tat= 0.0;
for(i=0; i<n; i++)
{
printf("%d\t\t", pid[i]);
printf("%d\t\t", bt[i]);
printf("%d\t\t", wt[i]);

//calculating and printing turnaround time of each process


printf("%d\t\t", bt[i]+wt[i]);
printf("\n");
//for calculating total waiting time
twt += wt[i];

//for calculating total turnaround time


tatt[i]=(wt[i]+bt[i]);
tat += (wt[i]+bt[i]);

}
float att,awt;

//for calculating average waiting time


awt = twt/n;

//for calculating average turnaround time


att = tat/n;
printf("\nAvg. waiting time= %f\n",awt);
printf("Avg. turnaround time= %f",att);

printf("\n");
printf("\n"); // Empty line
printf(" GANTT CHART \n");
printf(" *********** \n");
print_gantt_chart(pid,bt,tatt,n);
}

void print_gantt_chart(int pid[],int bt[],int tatt[], int n)


{
int i, j;
// print top bar
printf(" ");
for(i=0; i<n; i++) {
for(j=0; j<bt[i]; j++) printf("--");
printf(" ");
}
printf("\n|");

// printing process id in the middle


for(i=0; i<n; i++) {
for(j=0; j<bt[i] - 1; j++) printf(" ");
printf("P%d", pid[i]);
for(j=0; j<bt[i] - 1; j++) printf(" ");
printf("|");
}
printf("\n ");
// printing bottom bar
for(i=0; i<n; i++) {
for(j=0; j<bt[i]; j++) printf("--");
printf(" ");
}
printf("\n");

// printing the time line


printf("0");
for(i=0; i<n; i++) {
for(j=0; j<bt[i]; j++) printf(" ");
if(tatt[i] > 9) printf("\b"); // backspace : remove 1
space
printf("%d", tatt[i]);

}
printf("\n");
}

OUTPUT:
Shortest Remaining Time First (SRTF):
#include <stdio.h>

int main()
{
int AT[10], BT[10], temp[10];
int i, smallest, count = 0, time, limit;
double WT = 0, TAT = 0, end;
float AWT, ATT;
printf("Enter the Total Number of Processes: ");
scanf("%d", &limit);
printf("Enter Details of %d Processes:\n", limit);
for(i = 0; i < limit; i++)
{
printf("Enter Arrival Time: ");
scanf("%d", &AT[i]); OUTPUT:
printf("Enter Burst Time: ");
scanf("%d", &BT[i]);
temp[i] = BT[i];
}
BT[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(AT[i] <= time && BT[i] < BT[smallest] && BT[i] > 0)
{
smallest = i;
}
}
BT[smallest]--;
if(BT[smallest] == 0)
{
count++;
end = time + 1;
WT = WT + end - AT[smallest] - temp[smallest];
TAT = TAT + end - AT[smallest];
}
}
AWT = WT / limit;
ATT = TAT / limit;
printf("Average Waiting Time %lf\n", AWT);
printf("Average Turnaround Time %lf", ATT);
return 0;
}
Banker’s Algorithm:
#include <stdio.h>
int main()
{
int max[10][10], need[10][10], alloc[10][10], avail[10],
completed[10],
safeseq[10];
int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for (i = 0; i < p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for (i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for (j = 0; j < r; j++)
scanf("%d", &max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for (i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for (j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
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("\n Max matrix:\tAllocation matrix:\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 %d runs to completion!", process +
1);
safeseq[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("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for (i = 0; i < p; i++)
printf("%d ", safeseq[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
return 0;
}
OUTPUT:
PAGE REPLACEMENT ALGORITHMS:
1. FIFO:

#include <stdio.h>
int main()
{
int i, j, n, a[50], frame[10], no, k, avail, count = 0;
printf("ENTER THE NUMBER OF PAGES: ");
scanf("%d", &n);
printf("\n ENTER THE PAGE NUMBER: ");
for (i = 1; i <= n; i++)
scanf("%d", &a[i]);
printf("\n ENTER THE NUMBER OF FRAMES: ");
scanf("%d", &no);
for (i = 0; i < no; i++)
frame[i] = -1;
j = 0;
printf("ref string\t page frames\n"); OUTPUT:
for (i = 1; i <= n; i++)
{
printf("%d\t\t", a[i]);
avail = 0;
for (k = 0; k < no; k++)
if (frame[k] == a[i])
avail = 1;
if (avail == 0)
{
frame[j] = a[i];
j = (j + 1) % no;
count++;
for (k = 0; k < no; k++)
printf("%d\t", frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d", count);
return 0;
}

You might also like