PROGRAM-8
OBJECTIVE:-
Write a C program to demonstrate Longest Job First ( LJF).
SOURCE CODE : -
#include <stdio.h> if (processes[j].burstTime
< processes[j + 1].burstTime) {
#include <stdlib.h>
// Swap the processes
struct Process { struct Process temp =
processes[j];
int processID;
processes[j] =
int arrivalTime; processes[j + 1];
int burstTime; processes[j + 1] =
int completionTime; temp;
int turnaroundTime; }
int waitingTime; }
}; }
// Calculate completion time,
turnaround time, and waiting
void LJF(struct Process
time
processes[], int n) {
int currentTime = 0;
// Sort processes based on
burst time in descending order for (int i = 0; i < n; i++) {
for (int i = 0; i < n - 1; i++) {
processes[i].completionTime =
for (int j = 0; j < n - i - 1;
currentTime +
j++) {
processes[i].burstTime;
// Calculate average waiting
processes[i].turnaroundTime = time and turnaround time
processes[i].completionTime -
int waitingTime = 0;
processes[i].arrivalTime;
int turnaroundTime = 0;
processes[i].waitingTime =
processes[i].turnaroundTime - for (int i = 0; i < n; i++) {
processes[i].burstTime; turnaroundTime +=
currentTime = processes[i].turnaroundTime;
processes[i].completionTime; waitingTime +=
} processes[i].waitingTime;
// Display the table header }
printf("\nProcess\t Arrival // Calculate average waiting
Time\t Burst Time\t Completion time and turnaround time
Time\t Turnaround Time\t float avgWaitingTime =
Waiting Time\n"); (float)waitingTime / n;
// Display process details, float avgTurnaroundTime =
completion time, turnaround (float)turnaroundTime / n;
time, and waiting time
// Display average waiting
for (int i = 0; i < n; i++) { time and turnaround time
printf("%d\t %d\t\t %d\t\t printf("\nAverage Waiting
%d\t\t\t %d\t\t\t %d\n", Time: %.2f", avgWaitingTime);
processes[i].processID,
printf("\nAverage Turnaround
processes[i].arrivalTime,
Time: %.2f\n",
processes[i].burstTime, avgTurnaroundTime);
processes[i].completionTime,
}
processes[i].turnaroundTime,
processes[i].waitingTime); int main() {
} int n;
// Get the number of
processes
printf("Enter the number of
processes: ");
scanf("%d", &n);
struct Process processes[n];
// Get arrival time and burst
time for each process
for (int i = 0; i < n; i++) {
processes[i].processID = i +
1;
printf("Enter arrival time
for Process %d: ", i + 1);
scanf("%d",
&processes[i].arrivalTime);
printf("Enter burst time for
Process %d: ", i + 1);
scanf("%d",
&processes[i].burstTime);
}
// Apply Longest Job First
scheduling algorithm
LJF(processes, n);
return 0;
}
OUTPUT:-
PROGRAM-9
OBJECTIVE:-
Write a C program to demonstrate Longest remaining time first
(LRF).
SOURCE CODE : -
#include <stdio.h> return ((struct process *)a)-
>AT - ((struct process *)b)->AT;
#include <stdlib.h>
}
// Creating a structure for a
process // Function to find the process
with the largest burst time
struct process {
int findlargest(struct process
int processno;
p[], int n, int at) {
int AT;
int max = 0;
int BT;
for (int i = 0; i < n; i++) {
int BTbackup;
if (p[i].AT <= at && p[i].BT >
int WT; p[max].BT) {
int TAT; max = i;
int CT; }
}; }
return max;
// Function to compare }
processes for sorting
int compare(const void *a,
const void *b) {
// Function to find the printf(" Process P%d is
completion time of each completed at %d",
process p[index].processno, totaltime);
void findCT(struct process p[], }
int n, int totaltime, int
printf("\n");
prefinaltotal) {
int index;
// Loop termination
int i = p[0].AT; condition
while (1) { if (totaltime ==
if (i <= prefinaltotal) { prefinaltotal)
index = findlargest(p, n, break;
i);
}
} else {
}
index = findlargest(p, n,
prefinaltotal);
int main() {
}
int n;
printf("Process executing at
time %d is: P%d\t", totaltime, // Input the number of
p[index].processno); processes
p[index].BT -= 1; printf("Enter the number of
processes: ");
totaltime += 1;
scanf("%d", &n);
i++;
struct process p[n];
if (p[index].BT == 0) {
p[index].CT = totaltime;
// Input arrival time and
burst time for each process int totaltime = p[0].AT;
for (int i = 0; i < n; i++) {
int prefinaltotal = totaltime;
p[i].processno = i + 1;
printf("Enter arrival time // Calculating to terminate
for P%d: ", p[i].processno);
the loop
scanf("%d", &p[i].AT); for (int i = 0; i < n; i++) {
printf("Enter burst time for
prefinaltotal += p[i].BT;
P%d: ", p[i].processno);
}
scanf("%d", &p[i].BT);
p[i].BTbackup = p[i].BT;
// Finding completion time of
}
each process
findCT(p, n, totaltime,
// Displaying the processes prefinaltotal);
before executing
printf("PNo\tAT\tBT\n");
// Calculating total waiting
for (int i = 0; i < n; i++) { time and total turn around time
printf("%d\t%d\t%d\n", int totalWT = 0;
p[i].processno, p[i].AT, p[i].BT);
int totalTAT = 0;
} for (int i = 0; i < n; i++) {
printf("\n");
p[i].TAT = p[i].CT - p[i].AT;
p[i].WT = p[i].TAT -
// Sorting processes p[i].BTbackup;
according to arrival time
totalWT += p[i].WT;
qsort(p, n, sizeof(struct
totalTAT += p[i].TAT;
process), compare);
} // Displaying total TAT,
average TAT, total WT, and
average WT
// Displaying the results after
all processes execute printf("\nTotal TAT = %d\n",
totalTAT);
printf("\nAfter execution of
all processes ... \n"); printf("Average TAT =
%.2f\n", (float)totalTAT / n);
printf("PNo\tAT\tBT\tCT\tTAT\t printf("Total WT = %d\n",
totalWT);
WT\n");
printf("Average WT =
for (int i = 0; i < n; i++) {
%.2f\n", (float)totalWT / n);
printf("%d\t%d\t%d\t%d\t%d\t
%d\n", p[i].processno, p[i].AT, return 0;
p[i].BTbackup, p[i].CT, p[i].TAT,
}
p[i].WT);
}
OUTPUT:-
PROGRAM-10
OBJECTIVE:-
Write a C program to demonstrate Preemptive priority cpu
scheduling.
SOURCE CODE : -
#include <stdio.h> if (Heap[*heapsize].intime ==
-1)
#include <stdlib.h>
Heap[*heapsize].intime =
*currentTime;
struct Process {
++(*heapsize);
int processID;
int burstTime;
while (start != 0 &&
int tempburstTime; Heap[(start - 1) / 2].priority >
int responsetime; Heap[start].priority) {
int arrivalTime; struct Process temp =
Heap[(start - 1) / 2];
int priority;
Heap[(start - 1) / 2] =
int outtime;
Heap[start];
int intime;
Heap[start] = temp;
};
start = (start - 1) / 2;
}
void insert(struct Process
}
Heap[], struct Process value,
int* heapsize, int* currentTime)
{ void order(struct Process
int start = *heapsize, i; Heap[], int* heapsize, int start)
{
Heap[*heapsize] = value;
int smallest = start; struct Process min = Heap[0];
int left = 2 * start + 1; if ([Link] == -1)
int right = 2 * start + 2; [Link] =
*currentTime - [Link];
if (left < *heapsize &&
Heap[left].priority < --(*heapsize);
Heap[smallest].priority)
if (*heapsize >= 1) {
smallest = left; Heap[0] =
if (right < *heapsize && Heap[*heapsize];
Heap[right].priority <
order(Heap, heapsize, 0);
Heap[smallest].priority)
}
smallest = right;
return min;
}
if (smallest != start) {
struct Process temp =
Heap[smallest]; int compare(const void* a,
const void* b) {
Heap[smallest] =
Heap[start]; return ((struct Process*)a)-
>arrivalTime - ((struct
Heap[start] = temp;
Process*)b)->arrivalTime;
order(Heap, heapsize, }
smallest);
}
void scheduling(struct Process
}
Heap[], struct Process array[],
int n, int* heapsize, int*
currentTime) {
struct Process
extractminimum(struct Process if (*heapsize == 0)
Heap[], int* heapsize, int* return;
currentTime) {
void priority(struct Process
struct Process min = array[], int n) {
extractminimum(Heap, qsort(array, n, sizeof(struct
heapsize, currentTime); Process), compare);
[Link] = *currentTime
+ 1; int totalwaitingtime = 0,
--[Link]; totalbursttime = 0,
printf("process id = %d totalturnaroundtime = 0, i,
insertedprocess = 0,
current time = %d\n",
[Link], *currentTime); heapsize = 0, currentTime =
array[0].arrivalTime,
totalresponsetime = 0;
if ([Link] > 0) {
insert(Heap, min, heapsize,
struct Process Heap[4 * n];
currentTime);
return;
for (int i = 0; i < n; i++) {
}
totalbursttime +=
array[i].burstTime;
for (int i = 0; i < n; i++)
array[i].tempburstTime =
if (array[i].processID == array[i].burstTime;
[Link]) {
}
array[i] = min;
break;
do {
}
if (insertedprocess != n) {
}
for (i = 0; i < n; i++) {
if (array[i].arrivalTime
== currentTime) {
++insertedprocess; }
array[i].intime = -1;
printf("Average waiting time
array[i].responsetime = -1; = %f\n", ((float)totalwaitingtime
/ (float)n));
insert(Heap, array[i],
&heapsize, ¤tTime); printf("Average response
time = %f\n",
}
((float)totalresponsetime /
} (float)n));
} printf("Average turn around
scheduling(Heap, array, n, time = %f\n",
&heapsize, ¤tTime); ((float)(totalwaitingtime +
totalbursttime) / (float)n));
++currentTime;
}
if (heapsize == 0 &&
insertedprocess == n)
break; int main() {
} while (1); int n;
for (int i = 0; i < n; i++) { printf("Enter the number of
processes: ");
totalresponsetime +=
array[i].responsetime; scanf("%d", &n);
totalwaitingtime +=
(array[i].outtime - struct Process array[n];
array[i].intime -
array[i].tempburstTime);
for (int i = 0; i < n; i++) {
totalbursttime +=
array[i].burstTime; array[i].processID = i + 1;
printf("Enter arrival time scanf("%d",
for P%d: ", array[i].processID); &array[i].burstTime);
scanf("%d", }
&array[i].arrivalTime);
printf("Enter priority for
priority(array, n);
P%d: ", array[i].processID);
scanf("%d",
&array[i].priority); return 0;
printf("Enter burst time for }
P%d: ", array[i].processID);
Output:-
PROGRAM-11
OBJECTIVE:-
Write a C program to implement Banker’s Algorithm.
SOURCE CODE : -
#include <stdio.h> {
int main() f[k] = 0;
{ }
int n, m, i, j, k; int need[n][m];
n = 5; for (i = 0; i < n; i++)
m = 3; {
int alloc[5][3] = {{0, 1, 0}, for (j = 0; j < m; j++)
{2, 0, 0}, need[i][j] = max[i][j] - alloc[i][j];
{3, 0, 2}, }
{2, 1, 1}, int y = 0;
{0, 0, 2}}; for (k = 0; k < 5; k++)
int max[5][3] = {{7, 5, 3}, {
{3, 2, 2}, for (i = 0; i < n; i++)
{9, 0, 2}, {
{2, 2, 2}, if (f[i] == 0)
{4, 3, 3}}; {
int avail[3] = {3, 3, 2}; int flag = 0;
int f[n], ans[n], ind = 0; for (j = 0; j < m; j++)
for (k = 0; k < n; k++) {
if (need[i][j] > avail[j]) {
{ if (f[i] == 0)
flag = 1; {
break; flag = 0;
} printf("The following system is
not safe");
}
break;
if (flag == 0)
{ }
ans[ind++] = i; }
if (flag == 1)
for (y = 0; y < m; y++)
{
avail[y] += alloc[i][y];
f[i] = 1; printf("Following is the SAFE
Sequence\n");
}
for (i = 0; i < n - 1; i++)
}
printf(" P%d ->", ans[i]);
}
printf(" P%d", ans[n - 1]);
}
}
int flag = 1;
return (0);}
for (int i = 0; i < n; i++)
OUTPUT:-
PROGRAM-12
OBJECTIVE:-
Write a C program to implement FCFS page replacement algorithm.
SOURCE CODE : -
#include<stdio.h> }
int fsize; for(i=0;i< fsize;i++)
int frm[15]; frm[i]=-1;
void display(); printf("\n page | \t Frame
void main() content ");
printf("\n-----------------------------
{
---------");
int pg[100],nPage,i,j,pf=0,top=-
for(j=0;j< nPage;j++)
1,temp,flag=0;
{
//clrscr();
printf("\n Enter frame size:"); flag=0;
for(i=0;i< fsize;i++)
scanf("%d",&fsize);
{
printf("\n Enter number of
pages:"); if(frm[i]==pg[j])
scanf("%d",&nPage); {
flag=1;
for(i=0;i< nPage;i++) break;
{ }
printf("\n Enter }
page[%d]:",i+1);
scanf("%d",&pg[i]);
if(flag==0) }
{ printf("\n----------------------------
----------");
if(top==fsize-1)
printf("\n total page
{
fault:%d",pf);
top=-1;
//getch();
}
}
pf++;
void display()
top++;
{
frm[top]=pg[j];
int i;
}
for(i=0;i< fsize;i++)
printf("\n %d |",pg[j]);
printf("\t %d",frm[i]);
display();
}
OUTPUT:
PROGRAM-13
OBJECTIVE:-
Write a C program to implement Least Recently Used (LRU) Page
Replacement algorithm.
SOURCE CODE : -
#include<stdio.h> for(n = 0; n < total_pages; n++)
{
int main() printf("%d: ", pages[n]);
{ a = 0, b = 0;
int m, n, position, k, l; for(m = 0; m < total_frames;
m++)
int a = 0, b = 0, page_fault = 0;
{
int total_frames = 3; if(frames[m] == pages[n])
{
int frames[total_frames];
a = 1;
int temp[total_frames];
b = 1;
int pages[] = {1, 2, 3, 2, 1, 5, 2,
1, 6, 2, 5, 6, 3, 1, 3, 6, 1, 2, 4, 3}; break;
int total_pages = }
sizeof(pages)/sizeof(pages[0]); }
for(m = 0; m < total_frames;
if(a == 0)
m++){
{
frames[m] = -1;
for(m = 0; m < total_frames;
}
m++)
{ temp[m] = 1;
if(frames[m] == -1) }
{ }
frames[m] = pages[n]; }
b = 1; for(m = 0; m < total_frames;
m++)
page_fault++;
{
break;
} if(temp[m] == 0)
} position = m;
}
}
frames[position] = pages[n];
if(b == 0)
{ page_fault++;
}
for(m = 0; m < total_frames;
m++)
{ for(m = 0; m < total_frames;
m++)
temp[m] = 0;
} {
printf("%d\t", frames[m]);
for(k = n - 1, l = 1; l <=
total_frames - 1; l++, k--) }
{ printf("\n");
for(m = 0; m < total_frames; }
m++) printf("\nTotal Number of Page
{ Faults:\t%d\n", page_fault);
if(frames[m] == pages[k])
{ return 0;}
OUTPUT:-
PROGRAM-14
OBJECTIVE:-
Write a C program to implement Fcfs disc scheduling algorithm.
SOURCE CODE : -
#include <stdio.h> seek_count +=
#include <stdlib.h> distance;
// accessed track is
#define SIZE 8
now the new head
head = cur_track;
void FCFS(int arr[], int size, int
head) }
{
printf("Total number of
int seek_count = 0;
seek operations = %d\n",
int distance, cur_track; seek_count);
for (int i = 0; i < size; i++) { // Seek sequence would
cur_track = arr[i]; be the same
// as the request array
sequence
// calculate absolute
distance printf("Seek Sequence
is\n");
distance =
abs(cur_track - head);
for (int i = 0; i < size; i++) {
// increase the total printf("%d\n",
count arr[i]);
} printf("Enter the request
} array elements:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
int main()
{ }
int size, head;
printf("Enter the initial
head position: ");
printf("Enter the size of
scanf("%d", &head);
the request array: ");
scanf("%d", &size);
FCFS(arr, size, head);
int arr[size];
return 0;
}
OUTPUT:-
PROGRAM-15
OBJECTIVE:-
Write a C program to implement Scan disc scheduling algorithm.
SOURCE CODE : -
#include <stdio.h> // Dividing the requests into
two arrays based on direction
#include <stdlib.h>
for (int i = 0; i < size; i++) {
// Function to perform SCAN if (arr[i] < head)
Disk Scheduling left[left_size++] = arr[i];
void SCAN(int arr[], int head, int if (arr[i] > head)
size, char direction) {
right[right_size++] =
int seek_count = 0; arr[i];
int distance, cur_track; }
int left[size], right[size];
int left_size = 0, right_size = // Sorting the left and right
0; arrays
int seek_sequence[size + 1]; for (int i = 0; i < left_size - 1;
i++) {
for (int j = 0; j < left_size - i
// Appending end values
which have to be visited before - 1; j++) {
reversing the direction if (left[j] < left[j + 1]) {
left[left_size++] = 0; int temp = left[j];
right[right_size++] = 199; left[j] = left[j + 1];
left[j + 1] = temp;
} for (int i = left_size - 1; i
} >= 0; i--) {
cur_track = left[i];
}
seek_sequence[sequence_inde
for (int i = 0; i < right_size - 1; x++] = cur_track;
i++) {
for (int j = 0; j < right_size -
distance =
i - 1; j++) {
abs(cur_track - head);
if (right[j] > right[j + 1]) {
seek_count +=
int temp = right[j]; distance;
right[j] = right[j + 1]; head = cur_track;
right[j + 1] = temp; }
} direction = 'r';
} } else if (direction == 'r') {
} for (int i = 0; i <
right_size; i++) {
// Run the while loop two cur_track = right[i];
times, one by one scanning
right and left of the head seek_sequence[sequence_inde
int run = 2; x++] = cur_track;
int sequence_index = 0; //
Index to track the seek distance =
sequence abs(cur_track - head);
while (run--) { seek_count +=
if (direction == 'l') { distance;
head = cur_track;
} printf("Enter the size of the
direction = 'l'; request array: ");
scanf("%d", &size);
}
}
// Dynamic allocation of
memory for the request array
// Display the seek sequence
int *arr = (int *)malloc(size *
printf("\nSeek Sequence is: sizeof(int));
");
for (int i = 0; i <
sequence_index; i++) { // Get the request array
printf("Enter the request
printf("%d ",
seek_sequence[i]); array:\n");
for (int i = 0; i < size; i++) {
}
scanf("%d", &arr[i]);
// Display the total number }
of seek operations
printf("\nTotal number of // Get the initial head
seek operations = %d\n", position
seek_count); printf("Enter the initial head
} position: ");
scanf("%d", &head);
int main() {
int size, head; // Get the direction (l for left,
r for right)
// Get the size of the request char direction;
array
printf("Enter the direction (l
for left, r for right): "); // Free the dynamically
scanf(" %c", &direction); allocated memory
free(arr);
// Apply SCAN Disk
Scheduling algorithm
return 0;
SCAN(arr, head, size, }
direction);
OUTPUT:-
PROGRAM-16
OBJECTIVE:-
Write a C program to implement C-Scan disc scheduling algorithm.
SOURCE CODE : -
#include <stdio.h> if (!left || !right) {
#include <stdlib.h> printf("Memory allocation
failed.\n");
void CSCAN(int arr[], int size, int exit(EXIT_FAILURE);
head, int disk_size, const char }
*direction) {
int seek_count = 0;
// appending end values
int distance, cur_track; which have to be visited before
reversing the direction
int *left = NULL, *right =
NULL; left[left_count++] = 0;
int left_count = 0, right[right_count++] =
right_count = 0; disk_size - 1;
int i, j;
// tracks on the left of the
left = (int*)malloc(size * head will be serviced when
once the head comes back to
sizeof(int));
the beginning (left end).
right = (int*)malloc(size *
for (i = 0; i < size; i++) {
sizeof(int));
if (arr[i] < head)
left[left_count++] = int temp = right[j];
arr[i]; right[j] = right[j + 1];
if (arr[i] > head)
right[j + 1] = temp;
right[right_count++] =
}
arr[i];
}
}
}
// sorting left and right
vectors // first service the requests
on the right side of the head.
for (i = 0; i < left_count - 1;
i++) { for (i = 0; i < right_count; i++)
{
for (j = 0; j < left_count - i -
1; j++) { cur_track = right[i];
if (left[j] < left[j + 1]) { // appending current track
to seek sequence
int temp = left[j];
printf("%d, ", cur_track);
left[j] = left[j + 1];
// calculate absolute
left[j + 1] = temp; distance
}
distance = abs(cur_track -
} head);
} // increase the total count
seek_count += distance;
for (i = 0; i < right_count - 1; // accessed track is now
i++) { new head
for (j = 0; j < right_count - i head = cur_track;
- 1; j++) { }
if (right[j] > right[j + 1]) {
}
// once reached the right end
jump to the beginning.
printf("\nTotal number of
head = 0; seek operations = %d\n",
seek_count);
}
// adding seek count for head
returning from (disk_size - 1) to
0
int main() {
seek_count += (disk_size - 1);
int size = 8;
int arr[] = { 176, 79, 34, 60,
// Now service the requests 92, 11, 41, 114 };
again which are left. int head = 50;
for (i = 0; i < left_count; i++) {
int disk_size;
cur_track = left[i];
// appending current track printf("Request sequence =
to seek sequence
{");
printf("%d, ", cur_track); for (int i = 0; i < size; i++) {
// calculate absolute
printf("%d", arr[i]);
distance
if (i < size - 1)
distance = abs(cur_track -
head); printf(", ");
// increase the total count }
seek_count += distance; printf("}\n");
// accessed track is now
the new head printf("Initial head position =
head = cur_track; %d\n", head);
printf("Direction = right (We printf("Initial position of
are moving from left to head: %d\n", head);
right)\n");
printf("Seek Sequence: ");
printf("Enter the size of the
CSCAN(arr, size, head,
disk: "); disk_size, "right");
scanf("%d", &disk_size);
return 0;
printf("Output:\n");
}
OUTPUT:-