Simulate the following CPU scheduling algorithms FCFS b) SJF c) Priority d) Round Robin
FCFS:
// C program for implementation of FCFS
// scheduling
#include<stdio.h>
// Function to find the waiting time for all
// processes
void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{
// waiting time for first process is 0
wt[0] = 0;
// calculating waiting time
for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}
// Function to calculate turn around time
void findTurnAroundTime( int processes[], int n,
int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
//Function to calculate average time
void findavgTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
//Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt);
//Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
//Display processes along with all details
printf("Processes Burst time Waiting time Turn around time\n");
// Calculate total waiting time and total turn
// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
}
float s=(float)total_wt / (float)n;
float t=(float)total_tat / (float)n;
printf("Average waiting time = %f",s);
printf("\n");
printf("Average turn around time = %f ",t);
}
// Driver code
int main()
{
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
//Burst time of all processes
int burst_time[] = {10, 5, 8};
findavgTime(processes, n, burst_time);
return 0;
}
OUTPUT:
DO IT ON YOUR OWN
SJF:
#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:
DO IT ON YOUR OWN
Priority:
/*
* C program to implement priority scheduling
*/
#include <stdio.h>
//Function to swap two variables
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n;
printf("Enter Number of Processes: ");
scanf("%d",&n);
// b is array for burst time, p for priority and index for process id
int b[n],p[n],index[n];
for(int i=0;i<n;i++)
{
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&b[i],&p[i]);
index[i]=i+1;
}
for(int i=0;i<n;i++)
{
int a=p[i],m=i;
//Finding out highest priority element and placing it at its desired position
for(int j=i;j<n;j++)
{
if(p[j] > a)
{
a=p[j];
m=j;
}
}
//Swapping processes
swap(&p[i], &p[m]);
swap(&b[i], &b[m]);
swap(&index[i],&index[m]);
}
// T stores the starting time of process
int t=0;
//Printing scheduled process
printf("Order of process Execution is\n");
for(int i=0;i<n;i++)
{
printf("P%d is executed from %d to %d\n",index[i],t,t+b[i]);
t+=b[i];
}
printf("\n");
printf("Process Id Burst Time Wait Time TurnAround Time\n");
int wait_time=0;
for(int i=0;i<n;i++)
{
printf("P%d %d %d %d\n",index[i],b[i],wait_time,wait_time + b[i]);
wait_time += b[i];
}
return 0;
}
OUTPUT:
DO IT ON YOUR OWN
Round Robin:
OUTPUT:
DO IT ON YOUR OWN
**************************************
FCFS:
#include <stdio.h>
int main() {
int n, i;
int bt[10], wt[10], tat[10];
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter burst time for each process:\n");
for(i = 0; i < n; i++) {
scanf("%d", &bt[i]);
}
wt[0] = 0; // first process waiting time
for(i = 1; i < n; i++) {
wt[i] = wt[i-1] + bt[i-1];
}
for(i = 0; i < n; i++) {
tat[i] = wt[i] + bt[i];
}
printf("\nProcess\tBT\tWT\tTAT\n");
for(i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\n", i+1, bt[i], wt[i], tat[i]);
}
return 0;
}
FCFS (First Come First Serve)
🔹 Sample Input
🔹 Execution Order
Processes execute in arrival order.
🔹 Gantt Chart
🔹 Waiting Time
-
P1 = 0
-
P2 = 5
-
P3 = 8
🔹 Turnaround Time
-
P1 = 5
-
P2 = 8
-
P3 = 10
SJF (Shortest Job First – Non-Preemptive)
🔹 Sample Input
🔹 Execution Order
Shortest burst time first → P3 → P2 → P1
🔹 Gantt Chart
🔹 Waiting Time
-
P3 = 0
-
P2 = 2
-
P1 = 5
🔹 Turnaround Time
-
P3 = 2
-
P2 = 5
-
P1 = 10
Priority Scheduling (Lower number = Higher priority)
🔹 Sample Input
🔹 Execution Order
Priority order → P2 → P1 → P3
🔹 Gantt Chart
🔹 Waiting Time
-
P2 = 0
-
P1 = 3
-
P3 = 7
🔹 Turnaround Time
-
P2 = 3
-
P1 = 7
-
P3 = 9
Round Robin Scheduling
🔹 Sample Input
🔹 Execution Order (Time Slice = 2)
| Step | Process | Time |
|---|---|---|
| 1 | P1 | 0–2 |
| 2 | P2 | 2–4 |
| 3 | P3 | 4–6 |
| 4 | P1 | 6–8 |
| 5 | P2 | 8–9 |
| 6 | P1 | 9–10 |
🔹 Gantt Chart
🔹 Waiting Time
-
P1 = 5
-
P2 = 6
-
P3 = 4
🔹 Turnaround Time
-
P1 = 10
-
P2 = 9
-
P3 = 6
No comments:
Post a Comment