0% found this document useful (0 votes)
4 views21 pages

Scheduling Algorithms in C: FCFS, SJF, Priority

The document contains implementations of various CPU scheduling algorithms including FCFS, SJF, Priority, Preemptive SJF, Preemptive Priority, and Round Robin, along with paging algorithms like FCFS and LRU. Each algorithm is implemented in C, with functions to calculate completion times, turnaround times, and waiting times for processes. The document provides user input prompts for process attributes and displays the scheduling results in a tabular format.

Uploaded by

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

Scheduling Algorithms in C: FCFS, SJF, Priority

The document contains implementations of various CPU scheduling algorithms including FCFS, SJF, Priority, Preemptive SJF, Preemptive Priority, and Round Robin, along with paging algorithms like FCFS and LRU. Each algorithm is implemented in C, with functions to calculate completion times, turnaround times, and waiting times for processes. The document provides user input prompts for process attributes and displays the scheduling results in a tabular format.

Uploaded by

Rushikesh Jagtap
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// 1.

FCFS Scheduling Algorithm

#include<limits.h>

#include<stdio.h>

int fcfs(int n, int at[], int bt[])

int current_time=0,completed=0,complet[n];

int i=0,ind, gantt_p[n], gantt_num[n], gantt=0;

float toatal_tat=0, toatal_wt=0;

int ct[n], tat[n], wt[n];

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

complet[i]=0;

while(completed<n)

ind=-1;

int min_at= INT_MAX;

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

if(!complet[i] && at[i]<=current_time)

if(min_at>at[i])

min_at=at[i];

ind= i;

if(ind==-1) current_time++;

else
{

current_time+=bt[ind];

ct[ind]= current_time;

tat[ind]=ct[ind]-at[ind];

wt[ind]= tat[ind]-bt[ind];

complet[ind]=1;

completed++;

gantt_num[gantt]=current_time;

gantt_p[gantt]= ind+1;

gantt++;

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

toatal_tat+=tat[i];

toatal_wt+=wt[i];

printf("Pro\tAT\tBT\tCt\tTAT\tWT\n");

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

printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i+1, at[i],bt[i],ct[i],tat[i],wt[i]);

int main()

int n;

printf("Enter number of process : ");

scanf("%d", &n);

int at[n], bt[n];

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

{
printf("\nEnter AT and BT for the process %d : ", i+1);

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

fcfs(n,at,bt);

// 2. SJF Scheduling Algorithm

#include<limits.h>

#include<stdio.h>

int sjf(int n, int at[], int bt[])

int current_time=0,completed=0,complet[n];

int i=0,ind, gantt_p[n], gantt_num[n], gantt=0;

float toatal_tat=0, toatal_wt=0;

int ct[n], tat[n], wt[n];

for(int i=0;i<n; i++) complet[i]=0;

while(completed<n)

ind=-1;

int min_bt= INT_MAX;

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

if(!complet[i] && at[i]<=current_time)

if(min_bt>bt[i]) { min_bt=bt[i]; ind= i; }

if(ind==-1) current_time++;

else

{
current_time+=bt[ind];

ct[ind]= current_time;

tat[ind]=ct[ind]-at[ind];

wt[ind]= tat[ind]-bt[ind];

complet[ind]=1;

completed++;

gantt_num[gantt]=current_time;

gantt_p[gantt]= ind+1;

gantt++;

for(int i=0;i<n;i++) { toatal_tat+=tat[i]; toatal_wt+=wt[i]; }

printf("Pro\tAT\tBT\tCt\tTAT\tWT\n");

for(int i=0;i<n;i++) printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i+1, at[i],bt[i],ct[i],tat[i],wt[i]);

int main()

int n;

printf("Enter number of process : ");

scanf("%d", &n);

int at[n], bt[n];

for(int i=0; i<n; i++) { printf("\nEnter AT and BT for the process %d : ", i+1); scanf("%d%d", &at[i], &bt[i]); }

sjf(n,at,bt);

// 3. Priority Scheduling Algorithm

#include<limits.h>

#include<stdio.h>

int priority(int n, int at[], int bt[],int pr[])

{
int current_time=0,completed=0,complet[n];

int i=0,ind, gantt_p[n], gantt_num[n], gantt=0;

float toatal_tat=0, toatal_wt=0;

int ct[n], tat[n], wt[n];

for(int i=0;i<n; i++) complet[i]=0;

while(completed<n)

ind=-1;

int priority= INT_MAX;

for(int i=0;i<n;i++) { if(!complet[i] && at[i]<=current_time) { if(priority>pr[i]) { priority=pr[i]; ind=i; } } }

if(ind==-1) current_time++;

else { current_time+=bt[ind]; ct[ind]= current_time; tat[ind]=ct[ind]-at[ind]; wt[ind]= tat[ind]-bt[ind]; complet[ind]=1;


completed++; gantt_num[gantt]=current_time; gantt_p[gantt]= ind+1; gantt++; }

for(int i=0;i<n;i++) { toatal_tat+=tat[i]; toatal_wt+=wt[i]; }

printf("Pro\tAT\tBT\tPR\tCt\tTAT\tWT\n");

for(int i=0;i<n;i++) printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n", i+1, at[i],bt[i],pr[i],ct[i],tat[i],wt[i]);

int main()

int n;

printf("Enter number of process : ");

scanf("%d", &n);

int at[n], bt[n], pr[n];

for(int i=0; i<n; i++) { printf("\nEnter AT, BT, Priority(1 high) for the process %d : ", i+1); scanf("%d%d%d", &at[i], &bt[i],
&pr[i]); }

priority(n,at,bt,pr);

// 4. Preemptive SJF Scheduling Algorithm


#include <stdio.h>

#include <limits.h>

int preemptiveSJF(int n, int at[], int bt[])

int remaining_bt[n], ct[n], tat[n], wt[n];

int completed = 0, current_time = 0, prev = -1;

int gantt_p[200], gantt_num[200], gantt = 0;

float total_tat = 0, total_wt = 0;

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

remaining_bt[i] = bt[i];

while (completed < n)

int idx = -1;

int min_bt = INT_MAX;

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

if (at[i] <= current_time && remaining_bt[i] > 0)

if (remaining_bt[i] < min_bt)

min_bt = remaining_bt[i];

idx = i;

if (idx == -1)
{

current_time++;

continue;

if (idx != prev)

gantt_p[gantt] = idx + 1;

gantt_num[gantt] = current_time;

gantt++;

prev = idx;

remaining_bt[idx]--;

current_time++;

if (remaining_bt[idx] == 0)

ct[idx] = current_time;

tat[idx] = ct[idx] - at[idx];

wt[idx] = tat[idx] - bt[idx];

completed++;

gantt_num[gantt] = current_time;

gantt++;

printf("\nPro\tAT\tBT\tCT\tTAT\tWT\n");

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

{
printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i + 1, at[i], bt[i], ct[i], tat[i], wt[i]);

total_tat += tat[i];

total_wt += wt[i];

printf("\nAverage TAT = %.2f", total_tat / n);

printf("\nAverage WT = %.2f\n", total_wt / n);

return 0;

int main()

int n;

printf("Enter number of processes: ");

scanf("%d", &n);

int at[n], bt[n];

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

printf("Enter AT and BT for Process %d: ", i + 1);

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

preemptiveSJF(n, at, bt);

return 0;

// 5. Preemptive Priority Scheduling Algorithm

#include <stdio.h>

#include <limits.h>
int preemptivePriority(int n, int at[], int bt[], int prio[])

int remaining_bt[n], ct[n], tat[n], wt[n];

int completed = 0, current_time = 0, prev = -1;

int gantt_p[200], gantt_num[200], gantt = 0;

float total_tat = 0, total_wt = 0;

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

remaining_bt[i] = bt[i];

while (completed < n)

int idx = -1;

int highest_priority = INT_MAX;

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

if (at[i] <= current_time && remaining_bt[i] > 0)

if (prio[i] < highest_priority)

highest_priority = prio[i];

idx = i;

else if (prio[i] == highest_priority)

if (at[i] < at[idx])

idx = i;

}
}

if (idx == -1)

current_time++;

continue;

if (idx != prev)

gantt_p[gantt] = idx + 1;

gantt_num[gantt] = current_time;

gantt++;

prev = idx;

remaining_bt[idx]--;

current_time++;

if (remaining_bt[idx] == 0)

ct[idx] = current_time;

tat[idx] = ct[idx] - at[idx];

wt[idx] = tat[idx] - bt[idx];

completed++;

gantt_num[gantt] = current_time;

gantt++;
printf("\nPro\tAT\tBT\tPR\tCT\tTAT\tWT\n");

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

printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n", i + 1, at[i], bt[i], prio[i], ct[i], tat[i], wt[i]);

total_tat += tat[i];

total_wt += wt[i];

printf("\nAverage TAT = %.2f", total_tat / n);

printf("\nAverage WT = %.2f\n", total_wt / n);

return 0;

int main()

int n;

printf("Enter number of processes: ");

scanf("%d", &n);

int at[n], bt[n], prio[n];

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

printf("Enter AT, BT and Priority for Process %d: ", i + 1);

scanf("%d%d%d", &at[i], &bt[i], &prio[i]);

preemptivePriority(n, at, bt, prio);

return 0;

}
// 6. Round Robin Scheduling Algorithm

#include <stdio.h>

int roundRobin(int n, int at[], int bt[], int tq)

int rem_bt[n], ct[n], tat[n], wt[n];

int current_time = 0, completed = 0;

float total_tat = 0, total_wt = 0;

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

rem_bt[i] = bt[i];

while (completed < n)

int done = 1;

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

if (at[i] <= current_time && rem_bt[i] > 0)

done = 0;

if (rem_bt[i] > tq)

current_time += tq;

rem_bt[i] -= tq;

else

current_time += rem_bt[i];

ct[i] = current_time;

tat[i] = ct[i] - at[i];


wt[i] = tat[i] - bt[i];

rem_bt[i] = 0;

completed++;

if (done)

current_time++;

printf("\nPro\tAT\tBT\tCT\tTAT\tWT\n");

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

printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i + 1, at[i], bt[i], ct[i], tat[i], wt[i]);

total_tat += tat[i];

total_wt += wt[i];

printf("\nAverage TAT = %.2f", total_tat / n);

printf("\nAverage WT = %.2f\n", total_wt / n);

return 0;

int main()

int n, tq;

printf("Enter number of processes: ");

scanf("%d", &n);
int at[n], bt[n];

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

printf("Enter AT and BT for Process %d: ", i + 1);

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

printf("Enter Time Quantum: ");

scanf("%d", &tq);

roundRobin(n, at, bt, tq);

return 0;

// 7. FCFS Paging Algorithm

#include <stdio.h>

void fcfsPage(int n, int refStr[], int m)

int frames[10];

int index = 0;

int pageFaults = 0;

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

frames[i] = -1;

for (int i = 0; i < m; i++)

int page = refStr[i];

int found = 0;
for (int j = 0; j < n; j++)

if (frames[j] == page)

found = 1;

break;

if (found == 0)

frames[index] = page;

index = (index + 1) % n;

pageFaults++;

printf("Step %2d: ", i + 1);

for (int j = 0; j < n; j++)

if (frames[j] == -1)

printf(" - ");

else

printf(" %d ", frames[j]);

printf("\n");

printf("\nTotal Page Faults = %d\n", pageFaults);

}
int main()

int n;

int refStr[16] = {12, 15, 12, 18, 6, 8, 11, 12, 19, 12, 6, 8, 12, 15, 19, 8};

int m = 16;

printf("Enter number of frames: ");

scanf("%d", &n);

fcfsPage(n, refStr, m);

return 0;

// 8. LRU Paging Algorithm

#include <stdio.h>

void lruPage(int n, int refStr[], int m)

int frames[10];

int counter[10];

int time = 0;

int pageFaults = 0;

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

frames[i] = -1;

counter[i] = 0;

for (int i = 0; i < m; i++)

{
int page = refStr[i];

int found = 0;

for (int j = 0; j < n; j++)

if (frames[j] == page)

found = 1;

counter[j] = ++time;

break;

if (found == 0)

int lruIndex = 0;

for (int j = 1; j < n; j++)

if (counter[j] < counter[lruIndex])

lruIndex = j;

frames[lruIndex] = page;

counter[lruIndex] = ++time;

pageFaults++;

printf("Step %2d: ", i + 1);

for (int j = 0; j < n; j++)

if (frames[j] == -1)

printf(" - ");
else

printf(" %d ", frames[j]);

printf("\n");

printf("\nTotal Page Faults = %d\n", pageFaults);

int main()

int n;

int refStr[16] = {12, 15, 12, 18, 6, 8, 11, 12, 19, 12, 6, 8, 12, 15, 19, 8};

int m = 16;

printf("Enter number of frames: ");

scanf("%d", &n);

lruPage(n, refStr, m);

return 0;

// 9. Optimal Paging Algorithm

#include <stdio.h>

void optimalPage(int n, int refStr[], int m)

int frames[10];

int pageFaults = 0;

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


frames[i] = -1;

for (int i = 0; i < m; i++)

int page = refStr[i];

int found = 0;

for (int j = 0; j < n; j++)

if (frames[j] == page)

found = 1;

break;

if (found == 0)

int replaceIndex = -1, farthest = -1;

for (int j = 0; j < n; j++)

if (frames[j] == -1)

replaceIndex = j;

break;

int k;

for (k = i + 1; k < m; k++)

if (frames[j] == refStr[k])
break;

if (k == m)

replaceIndex = j;

break;

if (k > farthest)

farthest = k;

replaceIndex = j;

frames[replaceIndex] = page;

pageFaults++;

printf("Step %2d: ", i + 1);

for (int j = 0; j < n; j++)

if (frames[j] == -1)

printf(" - ");

else

printf(" %d ", frames[j]);

printf("\n");

printf("\nTotal Page Faults = %d\n", pageFaults);

}
int main()

int n;

int refStr[16] = {12, 15, 12, 18, 6, 8, 11, 12, 19, 12, 6, 8, 12, 15, 19, 8};

int m = 16;

printf("Enter number of frames: ");

scanf("%d", &n);

optimalPage(n, refStr, m);

return 0;

You might also like