Practical 1
Aim: WAP to implement CPU Scheduling for First-Come First-Served (FCFS).
Program:
#include <stdio.h>
int main() {
int n, i;
// Declare arrays to store burst time, waiting time, and turnaround time
int bt[20], wt[20], tat[20];
float avg_wt = 0, avg_tat = 0;
printf("Enter total number of processes (maximum 20): ");
scanf("%d", &n);
printf("\nEnter Process Burst Time:\n");
for (i = 0; i < n; i++) {
printf("P[%d]: ", i + 1);
scanf("%d", &bt[i]);
// Calculate waiting time for each process
// The first process's waiting time is 0
wt[0] = 0;
for (i = 1; i < n; i++)
{ wt[i] = 0;
for (int j = 0; j < i; j++) {
wt[i] += bt[j]; // Sum of burst times of previous processes
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time\n");
// Calculate turnaround time and total average times
for (i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i]; // Turnaround time = Burst time + Waiting time
avg_wt += wt[i];
avg_tat += tat[i];
printf("P[%d]\t\t%d\t\t%d\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
avg_wt /= n;
avg_tat /= n;
printf("\nAverage Waiting Time: %.2f", avg_wt); printf("\
nAverage Turnaround Time: %.2f\n", avg_tat); return 0;
Output:
Practical 2
Aim: WAP to implement the shortest job first (SJF) CPU scheduling.
Program:
#include <stdio.h>
int main()
// Matrix A stores:
// A[i][0] = Process ID
// A[i][1] = Burst Time
// A[i][2] = Waiting Time
// A[i][3] = Turnaround Time
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
// Step 1: Take input for number of processes
printf("Enter number of process: ");
scanf("%d", &n);
// Step 2: Take input for Burst Times
printf("Enter Burst Time:\n");
for (i = 0; i < n; i++) { printf("P
%d: ", i + 1);
scanf("%d", &A[i][1]); // store burst time
A[i][0] = i + 1; // store process id
}
// Step 3: Sort processes by Burst Time (Shortest Job First)
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++) {
if (A[j][1] < A[index][1])
{ index = j;
// Swap burst times
temp = A[i][1]; A[i]
[1] = A[index][1];
A[index][1] = temp;
// Swap process IDs (so ID matches correct burst time)
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
// Step 4: First process has 0 waiting time
A[0][2] = 0;
// Step 5: Calculate Waiting Time for each process
for (i = 1; i < n; i++) {
A[i][2] = 0;
// Waiting time = sum of all previous burst times
for (j = 0; j < i; j++) {
A[i][2] += A[j][1];
total += A[i][2]; // add all waiting times for average
// Step 6: Calculate Average Waiting Time
avg_wt = (float)total / n;
total = 0; // reset total to reuse for TAT
// Print table header
printf("P BT WT TAT\n");
// Step 7: Calculate Turnaround Time (TAT) and print table
for (i = 0; i < n; i++) {
// TAT = Burst Time + Waiting Time A[i]
[3] = A[i][1] + A[i][2];
total += A[i][3]; // add TAT to total
printf("P%d %d %d %d\n",
A[i][0], A[i][1], A[i][2], A[i][3]);
// Step 8: Calculate Average Turnaround Time
avg_tat = (float)total / n;
// Step 9: Print averages
printf("Average Waiting Time= %f", avg_wt); printf("\
nAverage Turnaround Time= %f", avg_tat);
return 0;
Output:
Practical 3
Aim: Write a program to perform priority scheduling
Program:
#include <stdio.h>
// Structure to represent a process
struct Process {
int pid; // Process ID
int bt; // Burst Time
int priority; // Priority (lower value = higher priority)
int wt; // Waiting Time
int tat; // Turnaround Time
};
int main()
{ int n, i, j;
struct Process p, temp;
int total_wt = 0, total_tat = 0;
// Input number of processes
printf("Enter number of processes: ");
scanf("%d", &n);
// Input burst time and priority
for(i = 0; i < n; i++) {
p[i].pid = i+1;
printf("Enter Burst Time and Priority for Process %d: ", p[i].pid);
scanf("%d %d", &p[i].bt, &p[i].priority);
}
// Sort processes by priority (ascending order → higher priority first)
for(i = 0; i < n-1; i++)
{ for(j = i+1; j < n; j++)
if(p[i].priority > p[j].priority)
{ temp = p[i];
p[i] = p[j];
p[j] = temp;
// First process waiting time is 0
[Link] = 0;
// Calculate Waiting Time (WT)
for(i = 1; i < n; i++) {
p[i].wt = 0;
for(j = 0; j < i; j++)
{ p[i].wt += p[j].bt;
// Calculate Turnaround Time (TAT)
for(i = 0; i < n; i++) {
p[i].tat = p[i].wt + p[i].bt;
total_wt += p[i].wt;
total_tat += p[i].tat;
}
// Print results printf("\nProcess\tBT\tPriority\
tWT\t\tTAT\n"); for(i = 0; i < n; i++) {
printf("P%-7d%-8d%-12d%-8d%-8d\n",
p[i].pid, p[i].bt, p[i].priority, p[i].wt, p[i].tat);
printf("\nAverage Waiting Time = %.2f", (float)total_wt/n); printf("\
nAverage Turnaround Time = %.2f\n", (float)total_tat/n);
return 0;
Output:
Practical 4
Aim: Write a program to implement CPU scheduling for Round Robin.
Program:
#include <stdio.h>
int main() {
int n, i, tq, total = 0, count = 0;
int bt, rt, wt, tat;
float avg_wt = 0, avg_tat = 0;
// Input number of processes
printf("Enter number of processes: ");
scanf("%d", &n);
// Input burst times
printf("Enter Burst Time for each process:\n");
for(i = 0; i < n; i++) {
printf("P%d: ", i+1);
scanf("%d", &bt[i]);
rt[i] = bt[i]; // remaining time = burst time initially
// Input Time Quantum
printf("Enter Time Quantum: ");
scanf("%d", &tq);
int t = 0; // current time
// Loop until all processes are done
while(1) {
int done = 1; // assume all finished
for(i = 0; i < n; i++) {
if(rt[i] > 0) {
done = 0; // at least one process left
if(rt[i] > tq) {
t += tq;
rt[i] -= tq;
} else {
t += rt[i];
wt[i] = t - bt[i]; // waiting time
rt[i] = 0;
if(done == 1) break; // all finished
// Calculate Turnaround Time
for(i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
avg_wt += wt[i];
avg_tat += tat[i];
// Print results printf("\nProcess\tBT\t\
tWT\t\tTAT\n"); for(i = 0; i < n; i++) {
printf("P%d\t\t%d\t\t%d\t\t%d\n", i+1, bt[i], wt[i], tat[i]);
}
printf("\nAverage Waiting Time = %.2f", avg_wt/n);
printf("\nAverage Turnaround Time = %.2f\n", avg_tat/n);
return 0;
Output:
Practical 5 (a)
Aim: Write a program to illustrate the Least Recently Used Page
Program:
#include <stdio.h>
#include <stdlib.h>
int findLRU(int time[], int n)
{ int i, min = time[0], pos =
0; for (i = 1; i < n; ++i) {
if (time[i] < min)
{ min = time[i];
pos = i;
return pos; // return index of least recently used page
int main() {
int no_of_frames, no_of_pages, frames[10], pages[30];
int counter = 0, time[10], flag1, flag2, i, j, pos, faults = 0;
// Input number of pages
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
// Input page reference string
printf("Enter reference string: ");
for (i = 0; i < no_of_pages; ++i) {
scanf("%d", &pages[i]);
// Input number of frames
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
// Initialize frames
for (i = 0; i < no_of_frames; ++i)
{ frames[i] = -1; // -1 means
empty
// Process each page
for (i = 0; i < no_of_pages; ++i)
{ flag1 = flag2 = 0;
// Check if page is already in frame
for (j = 0; j < no_of_frames; ++j) {
if (frames[j] == pages[i]) {
counter++;
time[j] = counter; // update last used time
flag1 = flag2 = 1;
break;
// If not present, insert page
if (flag1 == 0) {
for (j = 0; j < no_of_frames; ++j) {
if (frames[j] == -1) {
counter++; faults+
+;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
// If no empty frame, replace using LRU
if (flag2 == 0) {
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
// Print current frame contents
printf("\nPage %d -> ", pages[i]);
for (j = 0; j < no_of_frames; ++j) {
if (frames[j] != -1)
printf("%d ", frames[j]);
else
printf("- ");
}
printf("\n\nTotal Page Faults = %d\n", faults);
return 0;
Output:
Practical 5 (b)
Aim: Write a program to illustrate First In First Out Page replacement
Program:
#include <stdio.h>
int main() {
int frames, pages;
int n, f, i, j, k, flag, fault = 0;
// Step 1: Input number of pages
printf("Enter number of pages: ");
scanf("%d", &n);
// Step 2: Input page reference string
printf("Enter page reference string:\n");
for(i = 0; i < n; i++) {
scanf("%d", &pages[i]);
// Step 3: Input number of frames
printf("Enter number of frames: ");
scanf("%d", &f);
// Initialize all frames to -1 (empty)
for(i = 0; i < f; i++) {
frames[i] = -1;
j = 0; // points to the oldest frame
// Step 4: Process each page
for(i = 0; i < n; i++) {
flag = 0;
// Check if page already in frame (hit)
for(k = 0; k < f; k++) {
if(frames[k] == pages[i])
{ flag = 1; // page hit
break;
// If page fault occurs
if(flag == 0) {
frames[j] = pages[i]; // replace oldest page
j = (j + 1) % f; // move to next frame
(circular) fault++;
// Print current frame status
printf("For page %d: ", pages[i]);
for(k = 0; k < f; k++) {
if(frames[k] != -1)
printf("%d ", frames[k]);
else
printf("- ");
printf("\n");
}
}
// Step 5: Print total page faults printf("\
nTotal Page Faults = %d\n", fault);
return 0;
Output:
Practical 5 (c)
Aim: Write a program to illustrate Optimal Page replacement algorithm.
Program:
#include <stdio.h>
int main() {
int frames, pages;
int n, f, i, j, k, pos, fault = 0, flag1, flag2, farthest, next_use;
// Step 1: Input number of pages
printf("Enter number of pages: ");
scanf("%d", &n);
// Step 2: Input page reference string
printf("Enter page reference string:\n");
for(i = 0; i < n; i++) {
scanf("%d", &pages[i]);
// Step 3: Input number of frames
printf("Enter number of frames: ");
scanf("%d", &f);
// Initialize all frames to -1 (empty)
for(i = 0; i < f; i++) {
frames[i] = -1;
// Step 4: Process each page
for(i = 0; i < n; i++)
{ flag1 = flag2 = 0;
// Check if page already exists in frame (hit)
for(j = 0; j < f; j++) {
if(frames[j] == pages[i]) {
flag1 = flag2 = 1;
break;
// If free frame available
if(flag1 == 0) {
for(j = 0; j < f; j++)
{ if(frames[j] == -1) {
frames[j] = pages[i];
flag2 = 1;
fault++;
break;
// If all frames are full and page not present (page fault)
if(flag2 == 0) {
farthest = -1;
pos = -1;
// Find page to replace
for(j = 0; j < f; j++)
{ next_use = -1;
// Find the next use of frames[j]
for(k = i+1; k < n; k++) {
if(frames[j] == pages[k])
{ next_use = k;
break;
// If not used again, replace immediately...
if(next_use == -1) {
pos = j;
break;
// Otherwise choose farthest next use
if(next_use > farthest) {
farthest = next_use;
pos = j;
// Replace page
frames[pos] = pages[i];
fault++;
}
// Print current frame status
printf("For page %d: ", pages[i]);
for(j = 0; j < f; j++) {
if(frames[j] != -1)
printf("%d ", frames[j]);
else
printf("- ");
printf("\n");
// Step 5: Print total page faults printf("\
nTotal Page Faults = %d\n", fault);
return 0;
Output: