Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

FIRST COME FIRST SERVE (FCFS)

ALGORITHM:

Step1)Start
Step 2) In function int waiting time(int proc[], int n, int burst_time[], int wait_time[])
Set wait_time[0] = 0
Loop For i = 1 and i < n and i++
Set wait_time[i] = burst_time[i-1] + wait_time[i-1]
End For
Step 3) In function int turnaroundtime( int proc[], int n, int burst_time[], int wait_time[], int
tat[])
Loop For i = 0 and i < n and i++
Set tat[i] = burst_time[i] + wait_time[i]
End For
Step 4) In function int avgtime( int proc[], int n, int burst_time[])
Declare and initialize wait_time[n], tat[n], total_wt = 0, total_tat = 0;
Call waitingtime(proc, n, burst_time, wait_time)
Call turnaroundtime(proc, n, burst_time, wait_time, tat)
Loop For i=0 and i<n and i++
Set total_wt = total_wt + wait_time[i]
Set total_tat = total_tat + tat[i]
Print process number, burstime wait time and turnaround time
End For
Print "Average waiting time =i.e. total_wt / n
Print "Average turn around time = i.e. total_tat / n
Step 5) In int main()
Declare the input int proc[] = { 1, 2, 3}
Declare and initialize n = sizeof proc / sizeof proc[0]
Declare and initialize burst_time[] = {10, 5, 8}
Call avgtime(proc, n, burst_time)
Step 6) Stop

CODE:

#include <stdio.h>
// Function to find the waiting time for all processes
int waitingtime(int proc[], int n,
int burst_time[], int wait_time[]) {
// waiting time for first process is 0
wait_time[0] = 0;
// calculating waiting time
for (int i = 1; i < n ; i++ )
wait_time[i] = burst_time[i-1] + wait_time[i-1] ;
return 0;
}
// Function to calculate turn around time
int turnaroundtime( int proc[], int n,
int burst_time[], int wait_time[], int tat[]) {
// calculating turnaround time by adding
// burst_time[i] + wait_time[i]
int i;
for ( i = 0; i < n ; i++)
tat[i] = burst_time[i] + wait_time[i];
return 0;
}
//Function to calculate average time
int avgtime( int proc[], int n, int burst_time[]) {
int wait_time[n], tat[n], total_wt = 0, total_tat = 0;
int i;
//Function to find waiting time of all processes
waitingtime(proc, n, burst_time, wait_time);
//Function to find turn around time for all processes
turnaroundtime(proc, n, burst_time, wait_time, tat);
//Display processes along with all details
printf("Processes Burst Waiting Turn around \n");
// Calculate total waiting time and total turn
// around time
for ( i=0; i<n; i++) {
total_wt = total_wt + wait_time[i];
total_tat = total_tat + tat[i];
printf(" %d\t %d\t\t %d \t%d\n", i+1, burst_time[i], wait_time[i], tat[i]);
}
printf("Average waiting time = %f\n", (float)total_wt / (float)n);
printf("Average turn around time = %f\n", (float)total_tat / (float)n);
return 0;
}
// main function
int main() {
//process id's
int proc[] = { 1, 2, 3};
int n = sizeof proc / sizeof proc[0];
//Burst time of all processes
int burst_time[] = {5, 8, 12};
avgtime(proc, n, burst_time);
return 0;
}
OUTPUT:

ROUND ROBIN
ALGORITHM:
Step 1) Start
Step 2) We first have a queue where the processes are arranged in first come first serve
order.
Step 3) A quantum value is allocated to execute each process.
Step 4) The first process is executed until the end of the quantum value. After this, an
interrupt is generated and the state is saved.
Step 5) The CPU then moves to the next process and the same method is followed.
Step 6) Same steps are repeated till all the processes are over.
Step 7) Stop.

CODE:

#include<stdio.h>

int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
float average_wait_time, average_turnaround_time;
printf("\nEnter Total Number of Processes:\t");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("\nEnter Details of Process[%d]\n", i + 1);

printf("Arrival Time:\t");

scanf("%d", &arrival_time[i]);

printf("Burst Time:\t");

scanf("%d", &burst_time[i]);

temp[i] = burst_time[i];
}

printf("\nEnter Time Quantum:\t");


scanf("%d", &time_quantum);
printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
for(total = 0, i = 0; x != 0;)
{
if(temp[i] <= time_quantum && temp[i] > 0)
{
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - time_quantum;
total = total + time_quantum;
}
if(temp[i] == 0 && counter == 1)
{
x--;
printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, burst_time[i], total -
arrival_time[i], total - arrival_time[i] - burst_time[i]);
wait_time = wait_time + total - arrival_time[i] - burst_time[i];
turnaround_time = turnaround_time + total - arrival_time[i];
counter = 0;
}
if(i == limit - 1)
{
i = 0;
}
else if(arrival_time[i + 1] <= total)
{
i++;
}
else
{
i = 0;
}
}
average_wait_time = wait_time * 1.0 / limit;
average_turnaround_time = turnaround_time * 1.0 / limit;
printf("\n\nAverage Waiting Time:\t%f", average_wait_time);
printf("\nAvg Turnaround Time:\t%f\n", average_turnaround_time);
return 0;
}

OUTPUT:

SHORTEST JOB FIRST (SJF)


ALGORITHM:

Step 1) Start
Step 2) Sort all the process according to the arrival time.
Step 3) Then select that process which has minimum arrival time and minimum Burst time.
Step 4) After completion of process make a pool of process which after till the completion of
previous process and select that process among the pool which is having minimum Burst
time.
Step 5) Stop.

CODE:

#include<stdio.h>

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

printf("\nEnter Burst Time:\n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}
//sorting burst time in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

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

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

wt[0]=0; //waiting time for first process will be zero

//calculate waiting time


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

total+=wt[i];
}

avg_wt=(float)total/n; //average waiting time


total=0;

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


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
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=(float)total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
OUTPUT:

PRIORITY SCHEDULING
ALGORITHM:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Sort the ready queue according to the priority number.
Step 5: Set the waiting of the first process as ‘0’ and its burst time as its turn around time
Step 6: For each process in the Ready Q calculate
(i) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(ii) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Step 7: Calculate
(i)Average waiting time = Total waiting Time / Number of process
(iv) Average Turnaround time = Total Turnaround Time / Number of process
Step 8: Stop the process

CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,bt[10],p[10],compt[10], wt[10],tat[10],temp1,temp2;
float sumwt=0.0,sumtat=0.0,avgwt,avgtat;

printf("Enter number of processes: ");


scanf("%d",&n);
printf("Enter the burst time of %d process\n", n);
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
printf("Enter the priority of %d process\n", n);
for(i=0;i<n;i++)
scanf("%d",&p[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(p[i]>p[j])
{
temp1=bt[i];
bt[i]=bt[j];
bt[j]=temp1;
temp2=p[i];
p[i]=p[j];
p[j]=temp2;
}
compt[0]=bt[0]; wt[0]=0;
for(i=1;i<n;i++)
compt[i]=bt[i]+compt[i-1];
for(i=0;i<n;i++)
{
tat[i]=compt[i];
wt[i]=tat[i]-bt[i];
sumtat+=tat[i];
sumwt+=wt[i];
}
avgwt=sumwt/n; avgtat=sumtat/n;
printf("------------------------------\n");
printf("Bt\tCt\tTat\tWt\n");
printf("------------------------------\n");
for(i=0;i<n;i++)
{
printf("%2d\t%2d\t%2d\t%2d\n",bt[i],compt[i],tat[i],wt[i]);
}
printf("------------------------------\n");
printf(" Avgwt = %.2f\tAvgtat = %.2f\n",avgwt,avgtat);
printf("-------------------------------\n");
getch();
}
OUTPUT:

You might also like