0% found this document useful (0 votes)
16 views14 pages

EXNO3_OS_Up_date (1) (1)

The document outlines the implementation of four scheduling algorithms in C: First Come First Serve (FCFS), Shortest Job First (SJF), Round Robin, and Priority Scheduling. Each section includes the aim, algorithm steps, C program code, sample output, and results confirming successful implementation. The algorithms compute waiting times, turnaround times, and average times for processes based on user input.

Uploaded by

vishal.cs23
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)
16 views14 pages

EXNO3_OS_Up_date (1) (1)

The document outlines the implementation of four scheduling algorithms in C: First Come First Serve (FCFS), Shortest Job First (SJF), Round Robin, and Priority Scheduling. Each section includes the aim, algorithm steps, C program code, sample output, and results confirming successful implementation. The algorithms compute waiting times, turnaround times, and average times for processes based on user input.

Uploaded by

vishal.cs23
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/ 14

EX.NO.

3A: IMPLEMENTATION OF FCFS SCHEDULING ALGORITHM


DATE:
AIM
To write a C program to implement First Come First Serve scheduling algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the input process and their burst time.
Step 3: Sort the processes based on order in which it requests CPU.
Step 4: Compute the waiting time and turnaround time for each process.
Step 5: Calculate the average waiting time and average turnaround time.
Step 6: Print the details about all the processes.
Step 7: Stop the program.
PROGRAM :
#include<stdio.h>
Void main()
{
int bt[50],wt[80],at[80],wat[30],ft[80],tat[80]; int i,n;
float awt,att,sum=0,sum1=0;char
p[10][5];
printf("\nenter the number of process ");
scanf("%d",&n);
printf("\nEnter the process name and burst-time:");
for(i=0;i<n;i++)scanf("%s%d",p[i],&bt[i]);
printf("\nEnter the arrival- time:");
for(i=0;i<n;i++)
scanf("%d",&at[i]);
wt[0]=0;
for(i=1;i<=n;i++)
wt[i]=wt[i-1]+bt[i-1];
ft[0]=bt[0]; for(i=1;i<=n;i++)
ft[i]=ft[i-1]+bt[i];
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n \n");
for(i=0;i<n;i++)
printf("|\t%s\t",p[i]);
printf("|\t\n");
printf("\n \n");
printf("\n");
for(i=0;i<n;i++)
printf("%d\t\t",wt[i]);
printf("%d",wt[n]+bt[n]); printf("\n \n");
printf("\n"); for(i=0;i<n;i++)
wat[i]=wt[i]-at[i]; for(i=0;i<n;i++)
tat[i]=wat[i]-at[i];
printf("\n FIRST COME FIRST SERVE\n");
printf("\n Process Burst-time Arrival-time Waiting-time Finish-time Turnaround-time\n");
for(i=0;i<n;i++)
printf("\n\n %d%s \t %d\t\t %d \t\t %d\t\t %d
\t\t%d",i+1,p[i],bt[i],at[i],wat[i],ft[i],tat[i]);
for(i=0;i<n;i++)
sum=sum+wat[i];
awt=sum/n;
for(i=0;i<n;i++)
sum1=sum1+bt[i]+wt[i];
att=sum1/n;
printf("\n\nAverage waiting time:%f",awt);
printf("\n\nAverage turnaround time:%f",att);
}
}
OUTPUT:
enter the number of process 3
Enter the process name and burst-time:p1 2 p2 3
p3 4
Enter the arrival-time:0 1 2
GANTT CHART
| p1 | p2 | p3 |
0 2 5 9

FIRST COME FIRST SERVE


Process Burst-time Arrival-time Waiting-time Finish-time
Turnaround-time
p1 2 0 0 2 2
p2 3 1 1 5 4
p3 4 2 3 9 7
Average waiting time:1.333333 Average turnaround time:5.333333

RESULT:
The FCFS scheduling algorithm has been implemented in C.
EX.NO.3B : IMPLEMENTATION OF SJF SCHEDULING ALGORITHM
DATE:

AIM
To write a C program to implement shortest job first (non-pre-emptive) scheduling
algorithm.

ALGORITHM:
Step 1: Start the program.
Step 2: Get the input process and their burst time. Step 3: Sort the processes based on burst
time.
Step 4: Compute the waiting time and turnaround time for each process. Step 5: Calculate
the average waiting time and average turnaround time. Step 6: Print the details about all the
processes.
Step 7: Stop the program.
PROGRAM:
#include <stdio.h>
int main()
{
// Matrix for storing Process Id, Burst
// Time, Average Waiting Time & Average
// Turn Around Time.
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
// User Input Burst Time and alloting Process Id.
for (i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}
// Sorting process according to their Burst Time.
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;

temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
// Calculation of Waiting Times
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
// Calculation of Turn Around Time and printing the
// data.
for (i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
printf("P%d %d %d %d\n", A[i][0],
A[i][1], A[i][2], A[i][3]);
}
avg_tat = (float)total / n;
printf("Average Waiting Time= %f", avg_wt);
printf("\nAverage Turnaround Time= %f", avg_tat);
}

Output:

RESULT:
The SJF scheduling algorithm has been implemented in C.
EX.NO.3C IMPLEMENTATION OF ROUND ROBIN SCHEDULING ALGORITHM
DATE:

AIM:
To write a C program to implement Round Robin scheduling algorithm.

ALGORITHM:
Step 1: Start the program.
Step 2: Get the input process and their burst time.Step

3: Sort the processes based on priority.


Step 4: Compute the waiting time and turnaround time for each process. Step 5: Calculate
the average waiting time and average turnaround time. Step 6: Print the details about all the
processes.
Step 7: Stop the program.
PROGRAM:
//Write a C Program to implement Round Robin Schedulling algorithm
#include<stdio.h>
#include<conio.h>

void main()
{
// initlialize the variable name
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
float avg_wt, avg_tat;
clrscr();
printf("\n\n Round Robin Schedulling algorithm");
printf("\n *************************************\n");
printf(" \n Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP; // Assign the number of process to variable y
// Use for loop to enter the details of the process like Arrival time and the Burst Time
for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t"); // Accept arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time
scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}
// Accept the Time qunat
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
// Display the process No, burst time, Turn Around Time and the waiting time
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0) // define the conditions
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--; //decrement the process no.
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-
bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
// represents the average waiting time and Turn Around time
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}
Output:

RESULT
The Round Robin scheduling algorithm has been implemented in C.

EX.NO.3D: IMPLEMENTATION OF PRIORITY SCHEDULING ALGORITHM


DATE:

AIM
To write a C program to implement Priority Scheduling algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the input process and their burst time. Step 3: Sort the processes based on
priority.
Step 4: Compute the waiting time and turnaround time for each process.
Step 5: Calculate the average waiting time and average turnaround time. Step 6: Print the
details about all the processes.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h> #include<string.h>
void main()
{
int bt[30],pr[30],np; intwt[30],tat[30],wat[30],at[30],ft[30];int i,j,x,z,t; float
sum1=0,sum=0,awt,att;char
p[5][9],y[9];
printf("\nenter the number of process"); scanf("%d",&np);
printf("\nEnter the process,burst-time and priority:"); for(i=0;i<np;i++)
scanf("%s%d%d",p[i],&bt[i],&pr[i]); printf("\nEnter the arrival-time:"); for(i=0;i<np;i++)
scanf("%d",&at[i]); for(i=0;i<np;i++)
for(j=i+1;j<np;j++)
{
if(pr[i]>pr[j])
{
x=pr[j]; pr[j]=pr[i]; pr[i]=x; strcpy(y,p[j]);
strcpy(p[j],p[i]);
strcpy(p[i],y);

z=bt[j]; b t[j]=bt[i]; bt[i]=z;


}
} wt[0]=0;
for(i=1;i<=np;i++)
wt[i]=wt[i-1]+bt[i-1];
ft[0]=bt[0]; for(i=1;i<np;i++)
ft[i]=ft[i-1]+bt[i]; printf("\n\n\t\tGANTT CHART\n");printf("\n
\n"); for(i=0;i<np;i++)
printf("|\t%s\t",p[i]);
printf("|\t\n");
printf("\n \n");
printf("\n"); for(i=0;i<=np;i++)
printf("%d\t\t",wt[i]);
printf("\n \n");
printf("\n"); for(i=0;i<np;i++)
wat[i]=wt[i]-at[i]; for(i=0;i<np;i++)
tat[i]=wat[i]-at[i]; printf("\nPRIORITY SCHEDULING:\n");
printf("\nProcess Priority Burst-time Arrival-time Waiting-time Turnaround-time");
for(i=0;i<np;i++)
printf("\n\n%d%s\t%d\t\t%d\t\t%d\t%d\t\t%d",i+1,p[i],pr[i],bt[i],a t[i],wt[i],tat[i]);
for(i=0;i<np;i++)
sum=sum+wat[i]; awt=sum/np; for(i=0;i<np;i++)
sum1=sum1+tat[i]; att=sum1/np;
printf("\n\nAverage waiting time:%f",awt); printf("\n\nAverageturn aroundtime is:%f",att);
}
OUTPUT:
Enter the number of process3
Enter the process, burst-time and priority:p1 3 3 p2 4 2
p3 5 1
Enter the arrival-time: 0 1 2

GANTT CHART

| p3 | p2 | p1 |
0
5
9
12

PRIORITY SCHEDULING:
Process PriorityBurst-time Arrival-time Waiting-time Turnaround-time
p3 1 5 0 0 0
p2 2 4 1 5 3
p1 3 3 2 9 5
Average waiting time: 3.666667 Average turnaround time is: 2.666667
RESULT
The Priority scheduling algorithm has been implemented in C.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy