Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views2 pages

CPU Scheduling Algorithms

The document provides C code implementations for three CPU scheduling algorithms: Priority Scheduling (Non-Preemptive), Round Robin, and Shortest Remaining Time First (Preemptive with Arrival Time). Each algorithm includes a structure for processes and functions to calculate waiting time, turnaround time, and display the results. The code is structured to take user input for process details and time quantum where applicable.

Uploaded by

yadavmayank0712
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)
11 views2 pages

CPU Scheduling Algorithms

The document provides C code implementations for three CPU scheduling algorithms: Priority Scheduling (Non-Preemptive), Round Robin, and Shortest Remaining Time First (Preemptive with Arrival Time). Each algorithm includes a structure for processes and functions to calculate waiting time, turnaround time, and display the results. The code is structured to take user input for process details and time quantum where applicable.

Uploaded by

yadavmayank0712
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/ 2

CPU Scheduling Algorithms in C

3a) Priority Scheduling Algorithm (Non-Preemptive)


#include <stdio.h>

struct Process {
int id, bt, wt, tat, pr;
};

int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
struct Process p[n];
for(int i=0;i<n;i++) {
p[i].id=i+1;
printf("Enter burst time and priority for P%d: ", i+1);
scanf("%d %d", &p[i].bt, &p[i].pr);
}
for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++) {
if(p[i].pr > p[j].pr) {
struct Process t=p[i];
p[i]=p[j];
p[j]=t;
}
}
}
p[0].wt=0;
for(int i=1;i<n;i++) p[i].wt=p[i-1].wt+p[i-1].bt;
for(int i=0;i<n;i++) p[i].tat=p[i].wt+p[i].bt;
printf("\nPID\tBT\tPR\tWT\tTAT\n");
for(int i=0;i<n;i++)
printf("%d\t%d\t%d\t%d\t%d\n",p[i].id,p[i].bt,p[i].pr,p[i].wt,p[i].tat);
return 0;
}

3b) Round Robin Algorithm


#include <stdio.h>

struct Process {
int id, bt, rt, wt, tat;
};

int main() {
int n, tq, t=0, done;
printf("Enter number of processes: ");
scanf("%d", &n);
struct Process p[n];
for(int i=0;i<n;i++) {
p[i].id=i+1;
printf("Enter burst time for P%d: ", i+1);
scanf("%d",&p[i].bt);
p[i].rt=p[i].bt;
p[i].wt=p[i].tat=0;
}
printf("Enter time quantum: ");
scanf("%d",&tq);
do {
done=1;
for(int i=0;i<n;i++) {
if(p[i].rt>0) {
done=0;
if(p[i].rt>tq) {
t+=tq;
p[i].rt-=tq;
} else {
t+=p[i].rt;
p[i].wt=t-p[i].bt;
p[i].rt=0;
}
}
}
} while(!done);
for(int i=0;i<n;i++) p[i].tat=p[i].wt+p[i].bt;
printf("\nPID\tBT\tWT\tTAT\n");
for(int i=0;i<n;i++)
printf("%d\t%d\t%d\t%d\n",p[i].id,p[i].bt,p[i].wt,p[i].tat);
return 0;
}

3c) Shortest Remaining Time First (Preemptive with Arrival Time)


#include <stdio.h>
#include <limits.h>

struct Process {
int id, at, bt, rt, wt, tat, ct;
};

int main() {
int n, t=0, complete=0, minm, shortest, finish_time;
printf("Enter number of processes: ");
scanf("%d",&n);
struct Process p[n];
for(int i=0;i<n;i++) {
p[i].id=i+1;
printf("Enter arrival time and burst time for P%d: ", i+1);
scanf("%d %d",&p[i].at,&p[i].bt);
p[i].rt=p[i].bt;
}
while(complete!=n) {
minm=INT_MAX;
shortest=-1;
for(int i=0;i<n;i++) {
if(p[i].at<=t && p[i].rt>0 && p[i].rt<minm) {
minm=p[i].rt;
shortest=i;
}
}
if(shortest==-1) {
t++;
continue;
}
p[shortest].rt--;
if(p[shortest].rt==0) {
complete++;
finish_time=t+1;
p[shortest].wt=finish_time-p[shortest].bt-p[shortest].at;
if(p[shortest].wt<0) p[shortest].wt=0;
p[shortest].tat=p[shortest].bt+p[shortest].wt;
p[shortest].ct=finish_time;
}
t++;
}
printf("\nPID\tAT\tBT\tCT\tWT\tTAT\n");
for(int i=0;i<n;i++)
printf("%d\t%d\t%d\t%d\t%d\t%d\n",p[i].id,p[i].at,p[i].bt,p[i].ct,p[i].wt,p[i].tat);
return 0;
}

You might also like