OPERATING SYSTEMS AND
SCI LAB
V.PADMA
Associate Professor
IT Dept.
Course Outcomes:
Implementation of CPU scheduling algorithms, page
replacement techniques.
Understand and analyze the various file organization
techniques.
Understand the need for simulation/implementation
for the verification of mathematical functions.
Implement simple mathematical functions/equations
in numerical computing environment such as SCILAB.
Interpret and visualize simple mathematical functions
and operations thereon using plots/display
Syllabus
PART I
Task-1:Simulate the following CPU scheduling
algorithms
a) Round Robin b) SJF c) FCFS d) Priority
Task-2:Simulate all file allocation strategies
a) Sequential b) Indexed c) Linked
Task-3:Simulate MVT and MFT
Task-4:Simulate all File Organization Techniques
a) Single level directory b) Two level directory
Task-5:Simulate all page replacement algorithms
a) FIFO b) LRU c) LFU
Task-6:Simulate Paging Technique of memory
management.
PART II
Task-7: Scilab environment
Task-8:The Workspace and Working Directory
Task-9: Matrix Operations
Task-10:Sub- matrices
Task-11:Statistics
Task-12:Plotting Graphs
Task-13:Plotting 3D Graphs
Task-14:Scilab Programming Language
Task-15:Script Files and Function Files
Task-16:Functions in Scilab
Task-17:File Operations
Task-18:Reading Micros
Task-1:Simulate the following CPU scheduling algorithms
a) Round Robin b) SJF c) FCFS d) Priority
CPU scheduling is a process that allows one process
to use the CPU while the execution of another process
is on hold.
The aim of CPU scheduling is to make the system
efficient, fast, and fair
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible
Throughput – no of processes that complete their
execution per time unit
Turnaround time – amount of time to execute a particular
process
Waiting time – amount of time a process has been waiting
in the ready queue
Response time – amount of time it takes from when a
request was submitted until the first response is produced,
not output (for time-sharing environment)
Burst time is the total time taken by the process for its
execution on the CPU.
Scheduling Algorithm Optimization Criteria
Max CPU utilization
Max throughput
Min turnaround time
Min waiting time
Min response time
1. First-Come, First-Served (FCFS) Scheduling
Process Burst Time
P1 24
P2 3
P3 6
Suppose that the processes arrive in the order: P , P , P
1 2 3
The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 33
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27) / 3 = 17
Process Burst Time Waiting Time Turnaround
Time
P1 24 0 24
P2 3 24 27
P3 6 27 33
Suppose that the processes arrive in the order: P , P , P
1 2 3
The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 33
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27) / 3 = 17
Suppose that the processes arrive in the order
P2 , P3 , P1
The Gantt chart for the schedule is:
P2 P3 P1
0 3 9 33
Waiting time for P1 = 9; P2 = 0; P3 = 3
Average waiting time: (9 + 0 + 3)/3 = 4
Much better than previous case
//FCFS program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,p[10],bt[10],w[10],t[10],wt=0,tat=0;
float awt,atat;
clrscr();
printf("enter the no of processes:");
scanf("%d",&n);
printf("enter the process id's:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("enter the burst time of the processes:");
for(i=0;i<n;i++)
w[0]=0;
for(i=1;i<n;i++)
w[i]=w[i-1]+bt[i-1];
for(i=0;i<n;i++)
wt=wt+w[i];
awt=(wt)/n;
t[0]=bt[0];
for(i=1;i<n;i++)
t[i]=w[i]+bt[i];
for(i=0;i<n;i++)
tat=tat+t[i];
atat=(tat)/n;
printf("\n GANTT CHART \n");
for(i=0;i<n;i++)
{
printf("p[%d]\t",p[i]);
}
printf(" \n FIRST COME FIRST SERVE IS \n");
printf("processid\t bursttime\t waitingtime\t turnaroundtime\n");
for(i=0;i<n;i++)
{
printf("p[%d]\t\t%d\t\t%d\t\t%d\t\n",p[i],bt[i],w[i],t[i]);
}
printf("average waiting time is %f\n",awt);
printf("average turnaroundtime is %f\n",atat);
getch();
}
OUTPUT:
2. Shortest-Job-First (SJF) Scheduling
Associate with each process the length of its next CPU burst
Use these lengths to schedule the process with the shortest
time
SJF is optimal – gives minimum average waiting time for a
given set of processes
The difficulty is knowing the length of the next CPU
request
Example of SJF
Process Burst Time Waiting Time Turnaround
Time
P1 6
P2 8
P3 7
P4 3
P4 P1 P3 P2
0 3 9 16 24
SJF scheduling chart
Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
// SJF Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,p[10],bt[10],w[10],t[10],wt=0,tat=0,t1,t2;
float awt,atat;
clrscr();
printf("enter the no of processes:");
scanf("%d",&n);
printf("enter the process id's:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("enter the burst time of the processes:");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
t1=bt[i];
bt[i]=bt[j];
bt[j]=t1;
t2=p[i];
p[i]=p[j];
p[j]=t2;
}
}
}
w[0]=0;
for(i=1;i<n;i++)
w[i]=w[i-1]+bt[i-1];
for(i=0;i<n;i++)
wt=wt+w[i];
awt=(float)(wt)/n;
t[0]=bt[0];
for(i=1;i<n;i++)
t[i]=w[i]+bt[i];
for(i=0;i<n;i++)
tat=tat+t[i];
atat=(float)(tat)/n;
printf("\n GANTT CHART \n");
for(i=0;i<n;i++)
{
printf("p[%d]\t",p[i]);
}
printf(" \n SHORTEST JOB FIRST IS \n");
printf("processid\tbursttime\twaitingtime\tturnaroundtime\n");
for(i=0;i<n;i++)
{
printf("p[%d]\t\t%d\t\t%d\t\t%d\t\n",p[i],bt[i],w[i],t[i]);
}
printf("average waiting time is %f\n",awt);
printf("average turnaroundtime is %f\n",atat);
getch();
}
OUTPUT:
3. Priority Scheduling
A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest
priority (smallest integer highest priority)
Problem Starvation – low priority processes may never
execute
Solution Aging – as time progresses increase the priority
of the process
Example for Priority Scheduling
Process Priority Burst Time
P1 3 10
P2 1 1
P3 4 2
P4 5 1
P5 2 5
The Gantt chart for the schedule is:
P2 P5 P1 P3 P4
0 1 6 16 18 19
Average waiting time = (6 + 0 + 16 +18 + 1)/5 = 8.2 ms
// Priority Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,p[10],bt[10],pr[10],w[10],t[10],wt=0,tat=0,t1,t2,t3;
float awt=0,atat=0;
clrscr();
printf("enter the no of processes:");
scanf("%d",&n);
printf("enter the process id's:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("enter the burst time of the processes:");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
printf("enter the priority of the processes:");
for(i=0;i<n;i++)
scanf("%d",&pr[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(pr[i]>pr[j])
{
t1=pr[i];
pr[i]=pr[j];
pr[j]=t1;
t2=bt[i];
bt[i]=bt[j];
bt[j]=t2;
t3=p[i];
p[i]=p[j];
p[j]=t3;
}
}
}
w[0]=0;
for(i=1;i<n;i++)
w[i]=w[i-1]+bt[i-1];
for(i=0;i<n;i++)
wt=wt+w[i];
awt=(float)(wt)/n;
t[0]=bt[0];
for(i=1;i<n;i++)
t[i]=w[i]+bt[i];
for(i=0;i<n;i++)
tat=tat+t[i];
atat=(float)(tat)/n;
printf("\n GANTT CHART \n");
for(i=0;i<n;i++)
{
printf("p[%d]\t",p[i]);
}
printf(" \n PRIORITY IS \n");
printf("processid\tbursttime\tpriority\twaitingtime\
tturnaroundtime\n");
for(i=0;i<n;i++)
{
printf("p[%d]\t\t%d\t\t%d\t\t%d\t\t%d\t\
n",p[i],bt[i],pr[i],w[i],t[i]);
}
printf("average waiting time is %f\n",awt);
printf("average turnaroundtime is %f\n",atat);
getch();
}
OUTPUT:
4. Round Robin (RR)
Each process gets a small unit of CPU time (time
quantum), usually 10-100 milliseconds.
After this time has elapsed, the process is preempted and
added to the end of the ready queue.
If there are n processes in the ready queue and the time
quantum is q, then each process gets 1/n of the CPU
time in chunks of at most q time units at once.
Performance
q large FIFO
q small q must be large with respect to context
switch, otherwise overhead is too high
Example of RR with Time Quantum = 20
Process Burst Time
P1 53 -> 20+20+13
P2 17 -> 20
P3 68 -> 20+20+20+8
P4 24
The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
Typically, higher average turnaround than SJF, but better
response
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
// RoundRobinProgram:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,p[10],bt[10],cbt[10],i,sum=0,ts,temp=0,tat[10],wt[10];
float awt=0,atat=0;
clrscr();
printf("enter the no of processes:");
scanf("%d",&n);
printf("enter the process id's:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("enter the burst time of the processes:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
cbt[i]=bt[i];
}
printf("enter the time stamp:");
scanf("%d",&ts);
for(i=0;i<n;i++)
{
sum=sum+bt[i];
}
while(sum>0)
{
for(i=0;i<n;i++)
{
if(bt[i]>ts)
{
temp=temp+ts;
tat[i]=temp;
bt[i]=bt[i]-ts;
sum=sum-ts;
printf("p[%d]\t",p[i]);
}
else if(bt[i]<=ts&&bt[i]>0)
{
temp=temp+bt[i];
tat[i]=temp;
sum=sum-bt[i];
bt[i]=0;
printf("p[%d]\t",p[i]);
}
else
continue;
}
}
for(i=0;i<n;i++)
wt[i]=tat[i]-cbt[i];
printf("\nprocessid\tbursttime\twaitingtime\
tturnaroundtime\n");
for(i=0;i<n;i++)
{
printf("\np[%d]\t\t%d\t\t%d\t\t%d\
n",p[i],cbt[i],wt[i],tat[i]);
awt=awt+wt[i];
atat=atat+tat[i];
}
printf("\n average waiting time is %f\n",awt/n);
printf("\n average turn around time is %f\n",atat/n);
getch();
}
OUTPUT: