Operating System 16 Programs
Operating System 16 Programs
OBJECTIVE:-
Write a C program to demonstrate Longest Job First ( LJF).
SOURCE CODE : -
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++) {
printf("Process executing at
time %d is: P%d\t", totaltime, // Input the number of
p[index].processno); processes
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) {
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;
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;
{
} 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);
int arr[size];
return 0;
}
OUTPUT:-
PROGRAM-15
OBJECTIVE:-
Write a C program to implement Scan disc scheduling algorithm.
SOURCE CODE : -
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++) {
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]);
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");
return 0;
printf("Output:\n");
}
OUTPUT:-