CS-502 - ITM Universe

advertisement
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
EXPERIMENT NO.1
CPU Scheduling Algorithm
Date of conduction:-
Date of submission:-
Submitted by other members:1.
2.
3.
4.
5.
6.
7.
8.
Group no:-
Signature
Name of faculty incharge: Kuldeep Singh Jadon
Page 1 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Name of Technical Assistant:
Objective: -Write a program to implement FCFS CPU scheduling
algorithm.
Appratus:- C or C++ compiler which either support windows or
unix platform.
Theory: ALGORITHM
1. Start the process
2. Declare the array size
3. Get the number of processes to be inserted
4. Get the value
5. Start with the first process from it’s initial position let other process to be in queue
6. Calculate the total number of burst time
7. Display the values
8. Stop the process
Procedure: #include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
Page 2 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
int n,a[10],b[10],t[10],w[10],g[10],i,m;
float att=0,awt=0;
for(i=0;i<10;i++)
{
a[i]=0; b[i]=0; w[i]=0; g[i]=0;
}
printf("enter the number of process");
scanf("%d",&n);
printf("enter the burst times");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
/*printf("\nenter the arrival times");
for(i=0;i<n;i++)
scanf("%d",&a[i]);*/
g[0]=0;
for(i=0;i<10;i++)
g[i+1]=g[i]+b[i];
for(i=0;i<n;i++)
{
w[i]=g[i]-a[i];
t[i]=g[i+1]-a[i];
awt=awt+w[i];
Page 3 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
att=att+t[i];
}
awt =awt/n;
att=att/n;
printf("\n\tprocess\twaiting time\tturn arround time\n");
for(i=0;i<n;i++)
{
printf("\tp%d\t\t%d\t\t%d\n",i,w[i],t[i]);
}
printf("the average waiting time is %f\n",awt);
printf("the average turn around time is %f\n",att);
getch();
}
Observation Table:-N/A
Calculation:- N/A
Results: -
Page 4 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Conclusion:- With this we can calculate waiting time
,turnaround time, average waiting time and average
turnaround time
Precautions:Suggestions:-
Lab Quiz :1. Which module gives control of the CPU to the process selected by the short-term scheduler?
a) dispatcher
b) interrupt
Page 5 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
c) scheduler
d) none of the mentioned
Answer:a
2. The processes that are residing in main memory and are ready and waiting to execute are kept
on a list called
a) job queue
b) ready queue
c) execution queue
d) process queue
Answer: b
3. The interval from the time of submission of a process to the time of completion is termed as
a) waiting time
b) turnaround time
c) response time
d) throughput
Answer: b
4. Which scheduling algorithm allocates the CPU first to the process that requests the CPU first?
a) first-come, first-served scheduling
b) shortest job scheduling
c) priority scheduling
d) none of the mentioned
Answer: a
5. In priority scheduling algorithm
a) CPU is allocated to the process with highest priority
b) CPU is allocated to the process with lowest priority
c) equal priority processes cannot be scheduled
d) none of the mentioned
Answer: a
6. In priority scheduling algorithm, when a process arrives at the ready queue, its priority is
compared with the priority of
a) all process
b) currently running process
c) parent process
Page 6 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
d) init process
Answer: b
Book: Lab experiment related theory available in following
books:
Book Name
Author
Page No.
1. Operating System and Concept by Galvin at page no 188
Web resources:
1. NPTEL lectures notes and their exercise.
Page 7 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
EXPERIMENT NO.2
CPU Scheduling Algorithm
Date of conduction:-
Date of submission:-
Submitted by other members:1.
2.
3.
4.
5.
6.
7.
8.
Group no:-
Signature
Name of faculty incharge: Kuldeep Singh Jadon
Page 8 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Name of Technical Assistant:
Objective: - Write a program to implement SJF CPU scheduling algorithm.
Appratus:- C or C++ compiler for windows and unix
Theory: Associate with each process the length of its next CPU burst. Use these lengths to
schedule the process with the shortest time.
Two schemes:
1. non pre- emptive – once CPU given to the process it cannot be preempted until
completes its CPU burst.
2. preemptive – if a new process arrives with CPU burst length less than remaining
time of current executing process, preempt. This scheme is know as the ShortestRemaining-Time-First (SRTF).
SJF is optimal – gives minimum average waiting time for a given set of processes.
Process
Arrival Time
P1
0.0
P2
2.0
P3
4.0
P4
5.0
SJF (non-preemptive)
Burst Time
7
4
1
4
P1
0
P2
P3
7
8
P4
12
Average waiting time = [0 +(8-2)+(7-4) +(12-5)] /4 =4
Example of Preemptive SJF
Page 9 of 60
16
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Proces
Arrival Time
Burst Time
P1
0.0
7
P2
2.0
4
P3
4.0
1
P4
5.0
4
SJF (preemptive)
P1
0
P2
2
P3
4
P2
5
P4
7
P1
11
16
Average waiting time = (9 + 1 + 0 +2)/4 =3
Determining Length of Next CPU Burst .Can only estimate the length.
Can be done by using the length of previous CPU bursts, using exponential
averaging.
Procedure: #include <stdio.h>
#include <conio.h>
#define SIZE 15
void main()
{
clrscr();
int size, count=0, time=0, avg_wt=0, avg_tat=0;
printf("\n Enter number of processes : ");
scanf("%d",&size);
int bt[SIZE], at[SIZE], wt[SIZE], temp[SIZE];
for(int i=0 ; i<size ; i++)
Page 10 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
{
printf("\n\t\tFor Process : P%d",i+1);
printf("\n\t\tBurst Time : ");
scanf("%d",&bt[i]);
printf("\t\tArrival Time : ");
scanf("%d",&at[i]);
time += temp[i] = bt[i];
}
i=0;
while(count!=time)
{
int min = 32767;
int index;
for(int j=0 ; j<size ; j++)
{
if(min > bt[j] && at[j] <= count && bt[j] != 0)
{
min = bt[j];
index = j;
}
}
count += bt[index];
wt[index] = count - bt[index] - at[index];
avg_wt += wt[index];
avg_tat += bt[index] + wt[index];
bt[index] = 0;
i++;
if(i == size)
i=0;
}
printf("\n\n Processes");
printf(" Burst_Time");
printf(" Arrival_Time");
printf(" Waiting_Time");
printf(" Turn_Around_Time\n\n");
for(i=0 ; i<size ; i++)
{
printf(" P%d",i+1);
printf("\t\t %d",temp[i]);
printf("\t\t %d",at[i]);
printf("\t\t %d",wt[i]);
printf("\t\t %d",temp[i] + wt[i]);
Page 11 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
printf("\n");
}
printf("\n\n Average Waiting_Time : %.2f",(float)avg_wt/size);
printf("\n Average Turn_Around_Time : %.2f",(float)avg_tat/size);
getch();
}
Observation Table:Calculation:-
Results: -
Conclusion:- By this we can calculate the wating_time , turnaround_time,
average waiting_time , average turnaround_time
Precautions:-N/A
Page 12 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Suggestions:- N/A
Lab Quiz :1) Round robin scheduling falls under the category of :
a) Non preemptive scheduling
b) Preemptive scheduling
c) None of these
Answer :-b
2) The portion of the process scheduler in an operating system that dispatches processes is
concerned with :
a) assigning ready processes to CPU
b) assigning ready processes to waiting queue
c) assigning running processes to blocked queue
d) All of these
Answer :-a
3) Complex scheduling algorithms :
a) are very appropriate for very large computers
b) use minimal resources
c) use many resources
d) All of these
Answer :-a
4) The strategy of making processes that are logically runnable to be temporarily suspended is
called :
a) Non preemptive scheduling
b) Preemptive scheduling
c) Shortest job first
d) First come First served
Answer: b
5) Scheduling is :
a) allowing a job to use the processor
b) making proper use of processor
c) Both a and b
d) None of these
Answer: c
Page 13 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
6) There are 10 different processes running on a workstation. Idle processes are waiting for an
input event in the input queue. Busy processes are scheduled with the Round-Robin timesharing
method. Which out of the following quantum times is the best value for small response times, if
the processes have a short runtime, e.g. less than 10ms ?
a) tQ = 15ms
b) tQ = 40ms
c) tQ = 45ms
d) tQ = 50ms
Answer: a
7) Orders are processed in the sequence they arrive if _______ rule sequences the jobs.
a) earliest due date
b) slack time remaining
c) first come, first served
d) critical ratio
Answer: c
8) Which of the following algorithms tends to minimize the process flow time ?
a) First come First served
b) Shortest Job First
c) Earliest Deadline First
d) Longest Job First
Answer: b
9) Under multiprogramming, turnaround time for short jobs is usually ________ and that for
long jobs is slightly ___________.
a) Lengthened; Shortened
b) Shortened; Lengthened
c) Shortened; Shortened
d) Shortened; Unchanged
Answer: b
10) Which of the following statements are true ? (GATE 2010)
I. Shortest remaining time first scheduling may cause starvation
II. Preemptive scheduling may cause starvation
III. Round robin is better than FCFS in terms of response time
a) I only
b) I and III only
c) II and III only
d) I, II and III
Answer: d
Page 14 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Further reading resources:
Book: Lab experiment related theory available in following
books:
Book Name
Author
Page No.
1. Operating System Concept by Peter B. Galvin at page no. 189
Web resources:
1. NPTEL Lecture notes and question set
Page 15 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
EXPERIMENT NO.3
CPU Scheduling Algorithm
Date of conduction:-
Date of submission:-
Submitted by other members:1.
2.
3.
4.
5.
6.
7.
8.
Group no:-
Signature
Name of faculty incharge: Kuldeep Singh Jadon
Page 16 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Name of Technical Assistant:
Objective: - Write a program to implement Priority CPU Scheduling algorithm
Appratus:- C or C++ compiler for windows and unix
Theory: 


Priority scheduling is a more general case of SJF, in which each job is
assigned a priority and the job with the highest priority gets scheduled first.
( SJF uses the inverse of the next expected burst time as its priority - The
smaller the expected burst, the higher the priority. )
Note that in practice, priorities are implemented using integers within a fixed
range, but there is no agreed-upon convention as to whether "high" priorities
use large numbers or small numbers. This book uses low number for high
priorities, with 0 being the highest possible priority.
For example, the following Gantt chart is based upon these process burst
times and priorities, and yields an average waiting time of 8.2 ms:
P2 P5
0 1
Process
Burst Time
Priority
P1
10
3
P2
1
1
P3
2
4
P4
1
5
P5
5
2
P1
P3
16
6
Page 17 of 60
P4
18
19
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE



Priorities can be assigned either internally or externally. Internal priorities
are assigned by the OS using criteria such as average burst time, ratio of
CPU to I/O activity, system resource use, and other factors available to the
kernel. External priorities are assigned by users, based on the importance of
the job, fees paid, politics, etc.
Priority scheduling can be either preemptive or non-preemptive.
Priority scheduling can suffer from a major problem known as indefinite
blocking, or starvation, in which a low-priority task can wait forever
because there are always some other jobs around that have higher priority.
o If this problem is allowed to occur, then processes will either run
eventually when the system load lightens ( at say 2:00 a.m. ), or will
eventually get lost when the system is shut down or crashes. ( There
are rumors of jobs that have been stuck for years. )
o One common solution to this problem is aging, in which priorities of
jobs increase the longer they wait. Under this scheme a low-priority
job will eventually get its priority raised high enough that it gets run.
Procedure: /*NON PREEMTIVE */
#include <stdio.h>
#include <conio.h>
#define SIZE 15
void main()
{
clrscr();
int size, count=0, time=0, avg_wt=0, avg_tat=0;
printf("\n Enter number of processes : ");
scanf("%d",&size);
int bt[SIZE], at[SIZE], wt[SIZE], temp[SIZE], pr[SIZE];
for(int i=0 ; i<size ; i++)
{
printf("\n\t\tFor Process : P%d",i+1);
printf("\n\t\tBurst Time : ");
scanf("%d",&bt[i]);
printf("\t\tArrival Time : ");
Page 18 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
scanf("%d",&at[i]);
printf("\t\tPriority : ");
scanf("%d",&pr[i]);
temp[i] = bt[i];
time += bt[i];
}
i=0;
while(count!=time)
{
int min = 32767;
int index;
for(int j=0;j<size;j++)
{
if(at[j] <= count && min > pr[j] && bt[j] != 0)
{
min = pr[j];
index = j;
}
}
bt[index]--;
count++;
if(bt[index] == 0)
{
wt[index] = count - temp[index] - at[index];
avg_wt += wt[index];
avg_tat += wt[index] + temp[index];
}
i++;
if(i == size)
i=0;
}
printf("\n\n Processes");
printf(" Burst_Time");
printf(" Arrival_Time");
printf(" Priority");
printf(" Waiting_Time");
printf(" Turn_Around_Time\n\n");
for(i=0 ; i<size ; i++)
{
printf("\t\b\b\bP%d",i+1);
printf("\t\t %d",temp[i]);
printf("\t\t\b\b\b%d",at[i]);
Page 19 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
printf("\t\t %d",pr[i]);
printf("\t\t\b\b\b\b\b%d",wt[i]);
printf("\t\t\t\b\b\b%d",temp[i] + wt[i]);
printf("\n");
}
printf("\n\n Average Waiting_Time : %.2f",(float)avg_wt/size);
printf("\n Average Turn_Around_Time : %.2f",(float)avg_tat/size);
getch();
}
Observation Table:Calculation:-
Results: -
Page 20 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Conclusion:- By this we can calculate the wating_time , turnaround_time,
average waiting_time , average turnaround_time.
Precautions:Suggestions:-
Lab Quiz :1) The high paging activity is called ________.
a) Inter process communication
b) Thrashing
c) Context Switch
d) None of the above
Answer:-b
2) The Hardware mechanism that enables a device to notify the CPU is called __________.
a) Polling
b) Interrupt
c) System Call
d) None of the above
Answer:-b
3) In the running state
a) only the process which has control of the processor is found
b) all the processes waiting for I/O to be completed are found
c) all the processes waiting for the processor are found
d) none of the above
Answer:-a
4) Which of the following is crucial time while accessing data on the disk?
a) Seek time
b) Rotational time
c) Transmission time
d) Waiting time
Answer:-a
Page 21 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
5) Process State is a part of
a) Process Control block
b) Inode
c) File Allocation Table
d)None of the above
Answer:- a
6) Who is called a supervisor of computer acitvity ?
a) CPU
b) Operating system
c) Control unit
d) Application Program
Answer:- b
7) Virtual memory is __________.
a) An extremely large main memory
b) An extremely large secondary memory
c) An illusion of extremely large main memory
d) A type of memory used in super computers.
Answer: - c
8) The kernel keeps track of the state of each task by using a data structure called __
a) Process control block
b) User control block
c) Memory control block
d) None of the above
Answer: - a
9) _________ does the job of allocating a process to the processor.
a) Long term scheduler
b) Short term scheduler
c) Medium term scheduler
d) Dispatcher
Answer: - d
10) A major problem with priority scheduling is _________.
a) Definite blocking
b) Starvation
c) Low priority
d) None of the above
Answer: - b
Page 22 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Book: Lab experiment related theory available in following
books:
Book Name
Author
1. Operating System and Concept by Galvin
Web resources:
1. NPTEL lectures notes and their exercise.
Page 23 of 60
Page No.
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
EXPERIMENT NO.4
CPU Scheduling Algorithm
Date of conduction:-
Date of submission:-
Submitted by other members:1.
2.
3.
4.
5.
6.
7.
8.
Group no:-
Signature
Name of faculty incharge: Kuldeep Singh Jadon
Page 24 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Name of Technical Assistant:
Objective: - Write a program to implement Round Robin CPU scheduling
algorithm.
Appratus:- C or C++ compiler for windows and unix
Theory: 




Round robin scheduling is similar to FCFS scheduling, except that CPU
bursts are assigned with limits called time quantum.
When a process is given the CPU, a timer is set for whatever value has been
set for a time quantum.
o If the process finishes its burst before the time quantum timer expires,
then it is swapped out of the CPU just like the normal FCFS
algorithm.
o If the timer goes off first, then the process is swapped out of the CPU
and moved to the back end of the ready queue.
The ready queue is maintained as a circular queue, so when all processes
have had a turn, then the scheduler gives the first process another turn, and
so on.
RR scheduling can give the effect of all processors sharing the CPU equally,
although the average wait time can be longer than with other scheduling
algorithms. In the following example the average wait time is 5.66 ms.
Process
Burst Time
P1
24
P2
3
P3
3
The performance of RR is sensitive to the time quantum selected. If the
quantum is large enough, then RR reduces to the FCFS algorithm; If it is
Page 25 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE

very small, then each process gets 1/nth of the processor time and share the
CPU equally.
BUT, a real system invokes overhead for every context switch, and the
smaller the time quantum the more context switches there are. ( See Figure
5.4 below. ) Most modern systems use time quantum between 10 and 100
milliseconds, and context switch times on the order of 10 microseconds, so
the overhead is small relative to the time quantum.
Procedure: #include <stdio.h>
#include <conio.h>
#define SIZE 15
void main()
{
clrscr();
int size, qunt, time=0, count=0, avg_tat=0, avg_wt=0;
printf("\n Enter number of processes : ");
scanf("%d",&size);
printf(" Enter Quantum : ");
scanf("%d",&qunt);
int bt[SIZE], at[SIZE], wt[SIZE], temp[SIZE];
for(int i=0 ; i<size ; i++)
{
printf("\n\t\tFor Process : P%d",i+1);
printf("\n\t\tBurst Time : ");
scanf("%d",&bt[i]);
printf("\t\tArrival Time : ");
scanf("%d",&at[i]);
time += temp[i] = bt[i];
}
i=0;
while(count != time)
{
if(at[i] <= count && bt[i] != 0)
{
for(int j=0 ; j<qunt ; j++)
{
Page 26 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
bt[i]--;
count++;
if(bt[i] == 0)
{
wt[i] = count - temp[i] - at[i];
avg_wt += wt[i];
avg_tat += wt[i] + temp[i];
break;
}
}
}
i++;
if(i == size)
i=0;
}
printf("\n\n Processes");
printf(" Burst_Time");
printf(" Arrival_Time");
printf(" Waiting_Time");
printf(" Turn_Around_Time\n\n");
for(i=0 ; i<size ; i++)
{
printf(" P%d",i+1);
printf("\t\t %d",temp[i]);
printf("\t\t %d",at[i]);
printf("\t\t %d",wt[i]);
printf("\t\t %d",temp[i] + wt[i]);
printf("\n");
}
printf("\n\n Average Waiting_Time : %.2f",(float)avg_wt/size);
printf("\n Average Turn_Around_Time : %.2f",(float)avg_tat/size);
getch();
}
Observation Table:Calculation:-
Page 27 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Results: -
Conclusion:- By this we can calculate the wating_time , turnaround_time,
average waiting_time , average turnaround_time.
Precautions:Suggestions:Lab Quiz :1. Which module gives control of the CPU to the process selected by the short-term scheduler?
a) dispatcher
b) interrupt
c) scheduler
d) none of the mentioned
Answer:-a
2. The processes that are residing in main memory and are ready and waiting to execute are kept
on a list called
Page 28 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
a) job queue
b) ready queue
c) execution queue
d) process queue
Answer:-a
3. The interval from the time of submission of a process to the time of completion is termed as
a) waiting time
b) turnaround time
c) response time
d) throughput
Answer:-b
4. Which scheduling algorithm allocates the CPU first to the process that requests the CPU first?
a) first-come, first-served scheduling
b) shortest job scheduling
c) priority scheduling
d) none of the mentioned
Answer:-b
5. In priority scheduling algorithm
a) CPU is allocated to the process with highest priority
b) CPU is allocated to the process with lowest priority
c) equal priority processes can not be scheduled
d) none of the mentioned
Answer:-a
6. In priority scheduling algorithm, when a process arrives at the ready queue, its priority is
compared with the priority of
a) all process
b) currently running process
c) parent process
d) init process
Answer:-b
7. Time quantum is defined in
a) shortest job scheduling algorithm
b) round robin scheduling algorithm
c) priority scheduling algorithm
d) multilevel queue scheduling algorithm
Answer:-b
Page 29 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
8. Process are classified into different groups in
a) shortest job scheduling algorithm
b) round robin scheduling algorithm
c) priority scheduling algorithm
d) multilevel queue scheduling algorithm
Answer:-d
9. In multilevel feedback scheduling algorithm
a) a process can move to a different classified ready queue
b) classification of ready queue is permanent
c) processes are not classified into groups
d) none of the mentioned
Answer:-a
10. Which one of the following can not be scheduled by the kernel?
a) kernel level thread
b) user level thread
c) process
d) none of the mentioned
Answer:-b
Further reading resources:
Book: Lab experiment related theory available in following
books:
Book Name
Author
1. Operating System and Concept by Galvin
Web resources:
1. NPTEL lectures notes and their exercise.
Date of conduction:Page 30 of 60
Page No.
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
EXPERIMENT NO.5
Page Replacement Algorithm
Date of submission:-
Submitted by other members:1.
2.
3.
4.
5.
6.
7.
8.
Group no:-
Signature
Name of faculty incharge:Kuldeep Singh Jadon
Name of Technical Assistant:
Page 31 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Objective: - Write a program to implement the first in first out page
replacement algorithm.
Appratus:- C or C++ compiler for windows and unix
Theory: -
Page 32 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Procedure: #include <stdio.h>
#include <conio.h>
#include <alloc.h>
void main()
{
clrscr();
int size,page,pgft=0,flag;
printf("\nEnter frame size : ");
scanf("%d",&size);
int *fr = (int*) malloc (size*sizeof(int));
for(int i=0;i<size;i++)
*(fr+i)=-1;
printf("\nEnter pages [Enter -1 for exit] : \n\n");
Page 33 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
while(1)
{
printf("\t ¯ ");
scanf("%d",&page);
if(page==-1)
break;
flag=0;
for(int j=0;j<size;j++)
if(*(fr+j)==page)
flag=1;
if(!flag)
{
*(fr+(pgft%size))=page;
pgft++;
}
for(int k=0;k<size;k++)
if(*(fr+k)!=-1)
printf("\n\t\t -> %d",*(fr+k));
printf("\n");
}
printf("\n\nTotal page fault : %d",pgft);
getch();
free(fr);
}
Observation Table:-
Calculation:-
Results: -
Page 34 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Conclusion:-In this we calculate the total page fault.
Precautions:Suggestions:-
Lab Quiz :1. Which of the memory allocation schemes are subject to external
fragmentation?
a. Multiple Contiguous Fixed Partitions
b. Multiple Contiguous Variable Partitions
c. Paging
d. None of above
Answer: - b
Page 35 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
2. Which of the memory allocation schemes are subject to internal
fragmentation?
a. Multiple Contiguous Fixed Partitions
b. Multiple Contiguous Variable Partitions
c. Segmentation
d. None of above
Answer: - a
3. In paging system where page size is 2048 words, and the available
physical memory is equal to 2 ^ 17 = 128 K words, the length of the
physical address is equal to:
a. 16 bits
b. 28 bits
c. 17 bits
d. None of the above
Answer: - c
4. If normal memory access time is 100 nanoseconds and the cache search
time is 20 nanoseconds and all 80% of the page table entries are
found in the cache memory the paged memory access time is equal to:
a. 100 ns
Answer: - c
b. 120 ns
c. 140 ns
d. 200 ns
5. When inverted paging is used, there is/are:
a. One global page table sorted by process id
b. One global page table sorted by the frame number
c. One global page table sorted by the virtual address
d. Page tables for each process sorted by the frame number
Answer: - b
6. With paging, the internal fragmentation is possible when:
a. Page does not quite fit the frame
b. The last page of the job is less than maximum page size
c. The cache memory assigned to the page table entry is not
the same as normal memory assigned to the page table entry
d. There is no such thing as internal fragmentation with paging
Page 36 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Answer: - b
7. If there are 64 segments, and the maximum segment size is 1024
words, the length of logical address is:
a. 12 bits
b. 14 bits
c. 16 bits
d. 18 bits
Answer: - c
8. The modified (dirty) bit is used for the purpose of:
a. Dynamic allocation of memory used by one process to another
b. Implementing FIFO page replacement algorithm
c. To reduce the average time required to service page faults
d. None of the above
Answer: - c
9. Which is not the advantages of virtual memory?
a. Reduced I/O since only a portion of process may need to run
b. Support for higher degree of multiprogramming
c. Jobs (processes) not getting aborted for insufficient memory
d. None of the above.
Answer: - d
10. The modified (dirty) bit is used for the purpose of:
a. Dynamic allocation of memory used by one process to another
b. Implementing FIFO page replacement algorithm
c. To reduce the average time required to service page faults
d. None of the above
Answer: - c
Page 37 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Further reading resources:
Book: Lab experiment related theory available in following
books:
Book Name
Author
1. Operating System and Concept by Galvin
Web resources:
1. NPTEL lectures notes and their exercise.
Page 38 of 60
Page No.
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
EXPERIMENT NO.6
Page Replacement Algorithm
Date of conduction:-
Date of submission:-
Submitted by other members:1.
2.
3.
4.
5.
6.
7.
8.
Group no:-
Signature
Name of faculty incharge: Kuldeep Singh Jadon
Page 39 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Name of Technical Assistant:
Objective: - Write a program to implement the Least Recent Used page
replacement algorithm.
Appratus:- C or C++ compiler for windows and unix
Theory: -
Page 40 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Page 41 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Procedure: #include <stdio.h>
#include <conio.h>
#include <alloc.h>
void main()
{
clrscr();
int size,page,pgft=0,flag,temp=0;
printf("\nEnter frame size : ");
scanf("%d",&size);
int *fr = (int*) malloc (size*sizeof(int));
for(int i=0;i<size;i++)
*(fr+i)=-1;
printf("\nEnter pages [Enter -1 for exit] : \n\n");
while(1)
{
printf("\t ¯ ");
scanf("%d",&page);
Page 42 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
if(page==-1)
break;
flag=-1;
for(int i=0;i<temp;i++)
if(*(fr+i)==page)
flag=i;
if(flag==-1 && temp<size)
temp++;
if(pgft<size)
if(flag!=-1)
for(int j=flag;j<temp-1;j++)
*(fr+j) = *(fr+j+1);
else
for(int j=flag;j<temp-1;j++)
*(fr+j)=*(fr+j+1);
*(fr+temp-1)=page;
if(flag==-1)
pgft++;
}
for(int j=0;j<size;j++)
printf("\n\t -> %d",*(fr+j));
printf("\n\nTotal page fault : %d",pgft);
getch();
free(fr);
}
Observation Table:Calculation:Page 43 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Results: -
Conclusion:- In this we calculate the total page fault.
Precautions:Suggestions:-
Page 44 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Lab Quiz :1. Which of the following is the reason that the least recently used (LRU) algorithm is usually
not used as a page replacement algorithm?
A. Other practical schemes such as MIN do a better job.
B. LRU requires knowledge of the future to work correctly.
C. LRU is too inefficient to implement in practice.
D. The Clock algorithm always outperforms LRU.
Answer:-C
2. Which of the following are shared between threads in the same process?
A. registers
B. page table
C. stack
D. stack pointer
E. None of these are shared
Answer:-B
3. Which of the following is true about two threads running in the same process?
A. One thread can both read and write another thread's registers
B. One thread can change the other thread's program counter
C. One thread can neither read nor write the other thread's stack
D. One thread can both read and write the other thread's stack (there is no address space
protection between threads in the same process)
Answer:-D
4. What is the primary reason that a translation lookaside buffer (TLB) is used?
A. A TLB ensures that a process does not access memory outside of its address space
B. A TLB makes translating virtual addresses to physical addresses faster
C. A TLB allows multiple processes to share the L1 cache
D. A TLB makes translating virtual addresses to physical addresses possible
E. None of the above
Answer:-B
5. Which of the following is not a solution to thrashing?
A. Running fewer processes
B. Increasing the speed of the CPU
C. Increasing the size of physical memory
D. Rewriting programs to have better locality
E. These all solve the problem of thrashing
Answer:-B
Page 45 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Suppose a system has only three physical pages. Given the following sequence of virtual page
references, determine the number of page faults that are required. Initially, assume that the
physical pages are not being used by any virtual page.
121132143112415621
(Next two question is related to this)
6. Using the first in first out (FIFO) replacement policy
A. 9
B. 10
C. 11
D. 12
E. None of the above
Answer:-A
7. Using the least recently used (LRU) replacement policy
A. 9
B. 10
C. 11
D. 12
E. None of the above
Answer:-C
8. Which of the following is not a solution to thrashing?
A. Running fewer processes
B. Increasing the speed of the CPU
C. Increasing the size of physical memory
D. Rewriting programs to have better locality
E. These all solve the problem of thrashing
Answer:-B
9.Which disk block allocation scheme will require the most I/O operations for random access to
a large file?
A. Indexed allocation
B. Linked allocation
C. Contiguous allocation
D. I-node allocation
E. Each scheme requires approximately the same number of I/O operations
Answer:-B
10. Which of the following is true about two threads running in the same process?
A. One thread can both read and write another thread's registers
B. One thread can change the other thread's program counter
Page 46 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
C. One thread can neither read nor write the other thread's stack
D. One thread can both read and write the other thread's stack (there is no address space
protection between threads in the same process)
Answer:-D
Further reading resources:
Book: Lab experiment related theory available in following
books:
Book Name
Author
1. Operating System and Concept by Galvin
Web resources:
1. NPTEL lectures notes and their exercise.
Page 47 of 60
Page No.
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
EXPERIMENT NO.7
Date of conduction:-
Date of submission:-
Submitted by other members:1.
2.
3.
4.
5.
6.
7.
8.
Group no:-
Signature
Name of faculty incharge: Kuldeep Singh Jadon
Page 48 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Name of Technical Assistant:
Objective: - Write a program to implement Banker’s algorithms
Appratus:- C or C++ compiler for windows and unix
Theory: The following examples are adapted from


Deitel: An Introduction to Operating Systems and
Tanenbaum: Operating Systems: Design and Implementation.
Introduction:
Assume we have nine tape drives. Consider whether or not the following states are safe
or unsafe.
State
Current Loan Maximum Need
Process A
0
3
Process B
3
5
Process C
4
7






Since only 7 (3+4) tape drives are currently on loan (allocated), two (2) tape
drives are still available.
Process B can finish with only two additional tape drives.
Once Process B is done, it will release all 5 tape drives, making the number of
available tape drives = 5.
With only three of these tape drives, either Process A or Process C may complete
and release its tape drives.
This means that there are two possible safe sequences: <Process B, Process A,
Process C> and <Process B, Process C, Process A>.
Thus, we say that this is a safe state.
Again assume we have nine tape drives. Consider whether or not the following states are
safe or unsafe.
State
Current Loan Maximum Need
Process A
5
7
Process B
2
5
Page 49 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Process C




1
3
Since 8 (5+2+1) tape drives are currently on loan (allocated), only one tape drive
is still available.
None of the three processes can complete with only one additional tape drive.
This means that there are no safe sequences possible.
Thus, we say that this is an unsafe state.
Now return to the first example.
Suppose that Process C requests one tape drive.
If this request is granted, will we still be in a safe state?
State
Current Loan Maximum Need
Process A
0
3
Process B
3
5
Process C
5
7




The number of available tape drives is reduced to one (1).
No process can be granted enough tape drives to complete.
This means that there will be no safe sequences possible, if we grant Process C's
request.
Thus, granting this request will take us from a safe state to an unsafe state.
According to Deitel:
"An unsafe state does not imply the existence of deadlock. What an unsafe state does imply is
simply that some unfortunate sequence of events might lead to deadlock."
The Banker's algorithm:
Allows:



mutual exclusion
wait and hold
no preemption
Prevents:

circular wait
Page 50 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
User process may only request one resource at a time.
System grants request only if the request will result in a safe state.
The Banker's algorithm: An Example
Assume we have the following resources:




5 tape drives
2 graphic displays
4 printers
3 disks
We can create a vector representing our total resources: Total = (5, 2, 4, 3).
Consider we have already allocated these resources among four processes as
demonstrated by the following matrix named Allocation.
Process Name Tape Drives Graphics Printers Disk Drives
Process A
2
0
1
1
Process B
0
1
0
0
Process C
1
0
1
1
Process D
1
1
0
1
The vector representing the allocated resources is the sum of these columns:
Allocated = (4, 2, 2, 3).
We also need a matrix to show the number of each resource still needed for each process;
we call this matrix Need.
Process Name Tape Drives Graphics Printers Disk Drives
Process A
1
1
0
0
Process B
0
1
1
2
Process C
3
1
0
0
Process D
0
0
1
0
The vector representing the available resources will be the sum of these columns
subtracted from the Allocated vector: Available = (1, 0, 2, 0).
The Banker's algorithm:
1. Find a row in the Need matrix which is less than the Available vector. If such a row
exists, then the process represented by that row may complete with those additional
resources. If no such row exists, eventual deadlock is possible.
2. You want to double check that granting these resources to the process for the chosen row
will result in a safe state. Looking ahead, pretend that that process has acquired all its
Page 51 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
needed resources, executed, terminated, and returned resources to the Available vector.
Now the value of the Available vector should be greater than or equal to the value it was
previously.
3. Repeat steps 1 and 2 until
a. all the processes have successfully reached pretended termination (this implies
that the initial state was safe); or
b. deadlock is reached (this implies the initial state was unsafe).
Following the algorithm sketched above,

Iteration 1:
1. Examine the Need matrix. The only row that is less than the Available vector is
the one for Process D.
Need(Process D) = (0, 0, 1, 0) < (1, 0, 2, 0) = Available
2. If we assume that Process D completes, it will turn over its currently allocated
resources, incrementing the Available vector.
(1, 0, 2, 0)
+ (1, 1, 0, 1)
````````````````
(2, 1, 2, 1)

Current value of Available
Allocation (Process D)
Updated value of Available
Iteration 2:
1. Examine the Need matrix, ignoring the row for Process D. The only row that is
less than the Available vector is the one for Process A.
Need(Process A) = (1, 1, 0, 0) < (2, 1, 2, 1) = Available
2. If we assume that Process A completes, it will turn over its currently allocated
resources, incrementing the Available vector.
(2, 1, 2, 1)
+ (2, 0, 1, 1)
````````````````
(4, 1, 3, 2)
Current value of Available
Allocation (Process A)
Updated value of Available
Page 52 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE

Iteration 3:
1. Examine the Need matrix without the row for Process D and Process A. The only
row that is less than the Available vector is the one for Process B.
Need(Process B) = (0, 1, 1, 2) < (4, 1, 3, 2) = Available
2. If we assume that Process B completes, it will turn over its currently allocated
resources, incrementing the Available vector.
(4, 1, 3, 2)
+ (0, 1, 0, 0)
````````````````
(4, 2, 3, 2)

Current value of Available
Allocation (Process B)
Updated value of Available
Iteration 4:
1. Examine the Need matrix without the rows for Process A, Process B, and Process
D. The only row left is the one for Process C, and it is less than the Available
vector.
Need(Process C) = (3, 1, 0, 0) < (4, 2, 3, 2) = Available
2. If we assume that Process C completes, it will turn over its currently allocated
resources, incrementing the Available vector.
(4, 2, 3, 3)
+ (1, 0, 1, 1)
````````````````
(5, 2, 4, 3)
Current value of Available
Allocation (Process C)
Updated value of Available
Notice that the final value of the Available vector is the same as the original Total vector,
showing the total number of all resources:
Total = (5, 2, 4, 2) < (5, 2, 4, 2) = Available
This means that the initial state represented by the Allocation and Need matrices is a safe state.
The safe sequence that assures this safe state is <D, A, B, C>.
Note: The Banker's algorithm can also be used in the detection of deadlock.
Disadvantages of the Banker's Algorithm
Page 53 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE





It requires the number of processes to be fixed; no additional processes can start while it
is executing.
It requires that the number of resources remain fixed; no resource may go down for any
reason without the possibility of deadlock occurring.
It allows all requests to be granted in finite time, but one year is a finite amount of time.
Similarly, all of the processes guarantee that the resources loaned to them will be repaid
in a finite amount of time. While this prevents absolute starvation, some pretty hungry
processes might develop.
All processes must know and state their maximum resource need in advance.
Procedure: #include<stdio.h>
#include<conio.h>
void main()
{
int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7];
int first,p,r,i,j,prc,count,t;
clrscr();
count=0;
for(i=1;i<=7;i++)
comp[i]=0;
printf("Enter the no of processes:\n");
scanf("%d",&p);
printf("Enter the no of resources:\n");
scanf("%d",&r);
printf("Enter the claim for each process:");
for(i=1;i<=p;i++)
{
printf("\nFor process %d",i);
for(j=1;j<=r;j++)
{
scanf("%d",&clm[i][j]);
}
}
printf("Enter the allocation for each process:\n");
for(i=1;i<=p;i++)
{
printf("\nFor process ",i);
for(j=1;j<=r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
Page 54 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
printf("Enter total no of each resource:");
for(j=1;j<=r;j++)
scanf("%d",&rsrc[j]);
for(j=1;j<=r;j++)
{
int total=0;
avail[j]=0;
for(i=1;i<=p;i++)
{total+=alloc[i][j];}
avail[j]=rsrc[j]-total;
}
do
{
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
req[i][j]=clm[i][j]-alloc[i][j];
}
}
printf("\n\nAvailable resorces is:");
for(j=1;j<=r;j++)
{ printf(" ",avail[j]); }
printf("\nClaim matrix:\t\tAllocation matrix:\n");
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
printf("%d",clm[i][j]);
}
printf("\t\t\t");
for(j=1;j<=r;j++)
{
printf("%d",alloc[i][j]);
}
printf("\n");
}
prc=0;
for(i=1;i<=p;i++)
{
if(comp[i]==0)//if not completed
{
prc=i;
for(j=1;j<=r;j++)
{
Page 55 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
if(avail[j])
{
prc=0;
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\nProcess ",prc,"runs to completion!");
count++;
for(j=1;j<=r;j++)
{
avail[j]+=alloc[prc][j];
alloc[prc][j]=0;
clm[prc][j]=0;
comp[prc]=1;
}
}
}
while(count!=p&&prc!=0);
if(count==p)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");
getch();
}
Observation Table:Calculation:-
Page 56 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Results: -
Conclusion:-
By this we can find the state of system according to the available resources.
Precautions:Suggestions:-
Lab Quiz :1) Each request requires that the system consider the __________, _____________, ____________ to
decide whether the current request can be satisfied or must wait to avoid a future possible deadlock.
(choose three)
a) resources currently available
b) processes that have previously been in the system
c) resources currently allocated to each process
d) future requests and releases of each process
Page 57 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Answer: a, c and d
2) Given a priori information about the ________ number of resources of each type that maybe requested
for each process, it is possible to construct an algorithm that ensures that the system will never enter a
deadlock state.
a) minimum
b) average
c) maximum
d) approximate
Answer: c
3) A deadlock avoidance algorithm dynamically examines the __________, to ensure that a circular wait
condition can never exist.
a) resource allocation state
b) system storage state
c) operating system
d) resources
Answer: a
4) A state is safe, if :
a) the system does not crash due to deadlock occurrence
b) the system can allocate resources to each process in some order and still avoid a deadlock
c) the state keeps the system protected and safe
d) All of these
Answer: b
5) A system is in a safe state only if there exists a :
a) safe allocation
b) safe resource
c) safe sequence
d) All of these
Answer: c
6) All unsafe states are :
a) deadlocks
b) not deadlocks
c) fatal
d) None of these
Answer: b
7) A system has 12 magnetic tape drives and 3 processes : P0, P1, and P2. Process P0 requires 10 tape
drives, P1 requires 4 and P2 requires 9 tape drives.
Process
P0
P1
Page 58 of 60
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
P2
Maximum needs (process-wise : P0 through P2 top to bottom)
10
4
9
Currently allocated (process-wise)
5
2
2
Which of the following sequence is a safe sequence ?
a) P0, P1, P2
b) P1, P2, P0
c) P2, P0, P1
d) P1, P0, P2
Answer: d
8) If no cycle exists in the resource allocation graph :
a) then the system will not be in a safe state
b) then the system will be in a safe state
c) either a or b
d) None of these
Answer: b
9) The resource allocation graph is not applicable to a resource allocation system :
a) with multiple instances of each resource type
b) with a single instance of each resource type
c) Both a and b
Answer: a
10) The Banker’s algorithm is _____________ than the resource allocation graph algorithm.
a) less efficient
b) more efficient
c) None of these
Answer: a
Further reading resources:
Book: Lab experiment related theory available in following
books:
Book Name
Author
1. Operating System and Concept by Galvin
Page 59 of 60
Page No.
NAME OF LABORATORY: Operating System
LAB SUBJECT CODE: CS-502
NAME OF DEPARTMENT CSE
Web resources:
1. NPTEL lectures notes and their exercise.
http://www.cs.utexas.edu/users/witchel/372/lectures/16.Page
ReplacementAlgos.pdf
Page 60 of 60
Download