Os Lab Programs
Os Lab Programs
Shortest Job First (SJF) is an algorithm in which the process having the smallest execution time is chosen for
the next execution. This scheduling method can be preemptive or non-preemptive. It significantly reduces the
average waiting time for other processes awaiting execution. The full form of SJF is Shortest Job First.
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}
//sorting burst time in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process will be zero
OUTPUT
Priority Based Scheduling
#include<stdio.h>
#include<conio.h>
void main()
int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("\nProcess no %d : ",i+1);
scanf("%d %d",&pt[i],&pp[i]);
p[i]=i+1;
for(i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(pp[i]<pp[j])
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=pt[i];
pt[i]=pt[j];
pt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+pt[i];
atat+=t[i];
}
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n");
for(i=0;i<n;i++)
awt/=n;
atat/=n;
getch();
OUTPUT
FCFS SCHEDULING
We are given with the n number of processes i.e. P1, P2, P3,.......,Pn and their corresponding burst times. The
task is to find the average waiting time and average turnaround time using FCFS CPU Scheduling algorithm.
What is Waiting Time and Turnaround Time?
Turnaround Time is the time interval between the submission of a process and its completion.
Turnaround Time = completion of a process – submission of a process
Waiting Time is the difference between turnaround time and burst time
Waiting Time = turnaround time – burst time
P1 3 15
P2 1 3
P3 2 3
Gantt chart showing the waiting time of processes P1, P2 and P3 in the system
As shown above,
As we have taken arrival time to be 0 therefore turn around time and completion time will be same.
IMPLEMENTATION
#include <stdio.h>
// Function to find the waiting time for all processes
int waitingtime(int proc[], int n,
int burst_time[], int wait_time[]) {
// waiting time for first process is 0
wait_time[0] = 0;
// calculating waiting time
for (int i = 1; i < n ; i++ )
wait_time[i] = burst_time[i-1] + wait_time[i-1] ;
return 0;
}
// Function to calculate turn around time
int turnaroundtime( int proc[], int n,
int burst_time[], int wait_time[], int tat[]) {
// calculating turnaround time by adding
// burst_time[i] + wait_time[i]
int i;
for ( i = 0; i < n ; i++)
tat[i] = burst_time[i] + wait_time[i];
return 0;
}
//Function to calculate average time
int avgtime( int proc[], int n, int burst_time[]) {
int wait_time[n], tat[n], total_wt = 0, total_tat = 0;
int i;
//Function to find waiting time of all processes
waitingtime(proc, n, burst_time, wait_time);
//Function to find turn around time for all processes
turnaroundtime(proc, n, burst_time, wait_time, tat);
//Display processes along with all details
printf("Processes Burst Waiting Turn around \n");
// Calculate total waiting time and total turn
// around time
for ( i=0; i<n; i++) {
total_wt = total_wt + wait_time[i];
total_tat = total_tat + tat[i];
printf(" %d\t %d\t\t %d \t%d\n", i+1, burst_time[i], wait_time[i], tat[i]);
}
printf("Average waiting time = %f\n", (float)total_wt / (float)n);
printf("Average turn around time = %f\n", (float)total_tat / (float)n);
return 0;
}
// main function
int main() {
//process id's
int proc[] = { 1, 2, 3};
int n = sizeof proc / sizeof proc[0];
//Burst time of all processes
int burst_time[] = {5, 8, 12};
avgtime(proc, n, burst_time);
return 0;
}
The multi-level feedback queue job scheduling algorithm primarily includes multiple job queues in the system.
It scans the job queue and separates the jobs into different categories based on their need for the processor.
The algorithm allocates the jobs or processes to different queues based on their CPU execution time. If a process
has a large burst-time, then it is automatically moved to a lower-priority queue. This technique helps to prevent
starvation of lower priority processes too.
The algorithm prefers shorter jobs with low burst times and it prefers input/output bound processes. A process
known as aging promotes lower priority jobs to a higher priority queue at regular intervals of time.
An important thing to note is that there is a difference between multi-level feedback queue scheduling algorithm
and multi-level queue scheduling algorithm.
In the multi level feedback queue scheduling algorithm, the processes are permanently assigned to a queue
whereas, in a multilevel feedback scheduling algorithm, the processes can move between multiple queues
according to their requirements.
The multilevel feedback queue scheduling algorithm makes use of both first come first serve algorithm and
shortest job first algorithm.
IMPLEMENTATION
#include<stdio.h>
#define N 10
typedef struct
{
int process_id, arrival_time, burst_time, priority;
int q, ready;
}process_structure;
int Queue(int t1)
{
if(t1 == 0 || t1 == 1 || t1 == 2 || t1 == 3)
{
return 1;
}
else
{
return 2;
}
}
int main()
{
int limit, count, temp_process, time, j, y;
process_structure temp;
printf("Enter Total Number of Processes:\t");
scanf("%d", &limit);
process_structure process[limit];
for(count = 0; count < limit; count++)
{
printf("\nProcess ID:\t");
scanf("%d", &process[count].process_id);
printf("Arrival Time:\t");
scanf("%d", &process[count].arrival_time);
printf("Burst Time:\t");
scanf("%d", &process[count].burst_time);
printf("Process Priority:\t");
scanf("%d", &process[count].priority);
temp_process = process[count].priority;
process[count].q = Queue(temp_process);
process[count].ready = 0;
}
time = process[0].burst_time;
for(y = 0; y < limit; y++)
{
for(count = y; count < limit; count++)
{
if(process[count].arrival_time < time)
{
process[count].ready = 1;
}
}
for(count = y; count < limit - 1; count++)
{
for(j = count + 1; j < limit; j++)
{
if(process[count].ready == 1 && process[j].ready == 1)
{
if(process[count].q == 2 && process[j].q == 1)
{
temp = process[count];
process[count] = process[j];
process[j] = temp;
}
}
}
}
for(count = y; count < limit - 1; count++)
{
for(j = count + 1; j < limit; j++)
{
if(process[count].ready == 1 && process[j].ready == 1)
{
if(process[count].q == 1 && process[j].q == 1)
{
if(process[count].burst_time > process[j].burst_time)
{
temp = process[count];
process[count] = process[j];
process[j] = temp;
}
else
{
break;
}
}
}
}
}
printf("\nProcess[%d]:\tTime:\t%d To %d\n", process[y].process_id, time, time +
process[y].burst_time);
time = time + process[y].burst_time;
for(count = y; count < limit; count++)
{
if(process[count].ready == 1)
{
process[count].ready = 0;
}
}
}
return 0;
}
C program:
#include<stdio.h>
#include<conio.h>
void create(int,int);
void del(int);
void compaction();
void display();
int fname[10],fsize[10],fstart[10],freest[10],freesize[10],m=0,n=0,start;
int main()
{
int name,size,ch,i;
int *ptr;
// clrscr();
ptr=(int *)malloc(sizeof(int)*100);
start=freest[0]=(int)ptr;
freesize[0]=500;
printf("\n\n");
printf(" Free start address Free Size \n\n");
for(i=0;i<=m;i++)
printf(" %d %d\n",freest[i],freesize[i]);
printf("\n\n");
while(1)
{
printf("1.Create.\n");
printf("2.Delete.\n");
printf("3.Compaction.\n");
printf("4.Exit.\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the name of file: ");
scanf("%d",&name);
printf("\nEnter the size of the file: ");
scanf("%d",&size);
create(name,size);
break;
case 2:
printf("\nEnter the file name which u want to delete: ");
scanf("%d",&name);
del(name);
break;
case 3:
compaction();
printf("\nAfter compaction the tables will be:\n");
display();
break;
case 4:
exit(1);
default:
printf("\nYou have entered a wrong choice.\n");
}
}
}
void compaction()
{
int i,j,size1=0,f_size=0;
if(fstart[0]!=start)
{
fstart[0]=start;
for(i=1;i<n;i++)
fstart[i]=fstart[i-1]+fsize[i-1];
}
else
{
for(i=1;i<n;i++)
fstart[i]=fstart[i-1]+fsize[i-1];
}
f_size=freesize[0];
for(j=0;j<=m;j++)
size1+=freesize[j];
freest[0]=freest[0]-(size1-f_size);
freesize[0]=size1;
m=0;
}
void display()
{
int i;
printf("\n *** MEMORY MAP TABLE *** \n");
printf("\n\nNAME SIZE STARTING ADDRESS \n\n");
for(i=0;i<n;i++)
printf(" %d%10d%10d\n",fname[i],fsize[i],fstart[i]);
printf("\n\n");
printf("\n\n*** FREE SPACE TABLE ***\n\n");
printf("FREE START ADDRESS FREE SIZE \n\n");
for(i=0;i<=m;i++)
printf(" %d %d\n",freest[i],freesize[i]);
}
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(){
int pages[50], p, a;
recursivePart(pages);
getch();
return 0;
}
Description of Indexed allocation: Indexed allocation supports both sequential and direct access files. The file
indexes are not physically stored as a part of the file allocation table. Whenever the file size increases, we can
easily add some more blocks to the index. In this strategy, the file allocation table contains a single entry for
each file. The entry consisting of one index block, the index blocks having the pointers to the other blocks. No
external fragmentation.
Program to simulate indexed file allocation strategy
Program Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
Program Output:
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk :
4
1234
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk :
2
78
A5llocated
File Indexed
6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)0
First Fit
In the first fit approach is to allocate the first free partition or hole large enough which can accommodate the
process. It finishes after finding the first suitable free partition.
Best Fit
The best fit deals with allocating the smallest free partition which meets the requirement of the requesting
process. This algorithm first searches the entire list of free partitions and considers the smallest hole that is
adequate. It then tries to find a hole which is close to actual process size needed.
Worst fit
In worst fit approach is to locate largest available free portion so that the portion left will be big enough to be
useful. It is the reverse of best fit.
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files:-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragment");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files:-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile_no \tFile_size \tBlock_no \tBlock_size \tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}