Practical no.
Implementation of CPU scheduling algorithms to find turn around time
and waiting time
FCFS (first come first serve)
#include <stdio.h>
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[n], wt[n], tat[n];
for (int i = 0; i < n; i++) {
printf("Enter burst time of process %d: ", i + 1);
scanf("%d", &bt[i]);
wt[0] = 0;
for (int i = 1; i < n; i++) {
wt[i] = wt[i - 1] + bt[i - 1];
for (int i = 0; i < n; i++) {
tat[i] = wt[i] + bt[i];
}
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
return 0;
Output:
2. SJF (Shortest job first)
#include <stdio.h>
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[n], wt[n], tat[n], p[n];
for (int i = 0; i < n; i++) {
p[i] = i + 1;
printf("Enter burst time of process %d: ", i + 1);
scanf("%d", &bt[i]);
// Sorting by burst time
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (bt[i] > bt[j]) {
int temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
wt[0] = 0;
for (int i = 1; i < n; i++) {
wt[i] = wt[i - 1] + bt[i - 1];
for (int i = 0; i < n; i++) {
tat[i] = wt[i] + bt[i];
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\n", p[i], bt[i], wt[i], tat[i]);
return 0;
Output:
3. Round robin scheduling:
#include <stdio.h>
int main() {
int n, tq;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[n], rem_bt[n], wt[n], tat[n];
for (int i = 0; i < n; i++) {
printf("Enter burst time of process %d: ", i + 1);
scanf("%d", &bt[i]);
rem_bt[i] = bt[i];
wt[i] = 0;
printf("Enter time quantum: ");
scanf("%d", &tq);
int time = 0;
while (1) {
int done = 1;
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = 0;
if (rem_bt[i] > tq) {
time += tq;
rem_bt[i] -= tq;
} else {
time += rem_bt[i];
wt[i] = time - bt[i];
rem_bt[i] = 0;
if (done == 1)
break;
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
return 0;
OUTPUT:
4. Priority scheduling:
#include <stdio.h>
Int main() {
Int n;
Printf(“Enter number of processes: “);
Scanf(“%d”, &n);
Int bt[n], pr[n], wt[n], tat[n], p[n];
For (int i = 0; i < n; i++) {
P[i] = i + 1;
Printf(“Enter burst time of process %d: “, i + 1);
Scanf(“%d”, &bt[i]);
Printf(“Enter priority of process %d (lower number = higher priority): “, i +
1);
Scanf(“%d”, &pr[i]);
For (int i = 0; i < n – 1; i++) {
For (int j = i + 1; j < n; j++) {
If (pr[i] > pr[j]) {
Int temp = pr[i];
Pr[i] = pr[j];
Pr[j] = temp;
Temp = bt[i];
Bt[i] = bt[j];
Bt[j] = temp;
Temp = p[i];
P[i] = p[j];
P[j] = temp;
For (int i = 1; i < n; i++) {
Wt[i] = wt[i – 1] + bt[i – 1];
}
For (int i = 0; i < n; i++) {
Tat[i] = wt[i] + bt[i];
Printf(“\nProcess\tPriority\tBurst Time\tWaiting Time\tTurnaround Time\n”);
For (int i = 0; i < n; i++) {
Printf(“P%d\t%d\t\t%d\t\t%d\t\t%d\n”,
P[i], pr[i], bt[i], wt[i], tat[i]);
return 0;
Output: