Operating Systems Lab - DR - Maheswari R
Operating Systems Lab - DR - Maheswari R
Solutions provided by
Dr Maheswari R
Computer Engineering
Vit Chennai
2
List of Experiments
Solution 1.0 First Come First Serve Non Preemptive CPU Schedul-
ing using Scilab . . . . . . . . . . . . . . . . . . . 6
Solution 2.0 Shortest Job First . . . . . . . . . . . . . . . . . 8
Solution 3.0 Graphical Analysis WT and AWT . . . . . . . . . 10
Solution 4.0 Analysis TAT and ATAT . . . . . . . . . . . . . . 14
Solution 5.0 Round Robin Scheduling . . . . . . . . . . . . . . 18
Solution 6.0 Comparison of Various Partition Allocation Algo-
rithms using Scilab . . . . . . . . . . . . . . . . . 20
Solution 7.0 Banker Algorithm Deadlock Avoidance using Scilab 26
Solution 8.0 Dekker Process Synchronization Techniques using
Scilab . . . . . . . . . . . . . . . . . . . . . . . . 28
Solution 9.0 Memory Management using Scilab First Fit Best
Fit and Worst Fit . . . . . . . . . . . . . . . . . . 30
Solution 10.0 Optimal Page Replacement Algorithm using Scilab 35
AP 1 Optimal page replacement . . . . . . . . . . . . 40
AP 2 Worst Fit Memory Allocation . . . . . . . . . . . 41
AP 3 First Fit Memory Allocation . . . . . . . . . . . . 42
AP 4 Display Function . . . . . . . . . . . . . . . . . . 43
AP 5 Best Fit Memory Allocation . . . . . . . . . . . . 44
AP 6 Dekker Algorithm . . . . . . . . . . . . . . . . . 47
AP 7 Deadlock Bankers Algorithm . . . . . . . . . . . 49
AP 8 Round Robin Scheduling New . . . . . . . . . . . 51
AP 9 Display Function of Round Robin . . . . . . . . . 51
AP 10 SJF algorithm for Turn Around Time and Average
Turn Around Time calculation . . . . . . . . . . 53
AP 11 Round Robin Scheduling for Turn Around Time
and Average Turn Around Time calculation . . . 56
3
AP 12 FCFS Turn Around and Average Turn Around Cal-
culation . . . . . . . . . . . . . . . . . . . . . . . 57
AP 13 SJF for Turn Around Time and Average Turn Around
Time calculation . . . . . . . . . . . . . . . . . . 58
AP 14 Round Robin Scheduling for Waiting and Average
Waiting Time calculation . . . . . . . . . . . . . 61
AP 15 FCFS Waiting and Average Waiting Calculation . 62
AP 16 SJF New . . . . . . . . . . . . . . . . . . . . . . . 64
AP 17 Display Function SJF new . . . . . . . . . . . . . 66
AP 18 First Come First Serve CPU Scheduling . . . . . 67
4
List of Figures
5
Experiment: 1
Scilab code Solution 1.0 First Come First Serve Non Preemptive CPU
Scheduling using Scilab
1 clear ;
2 clc ;
3 //WINDOWS 10 64−BIT OS , S c i l a b and t o o l b o x versions
6.1.0.
4
5 // S c h e d u l i n g i s a m a t t e r o f managing q u e u e s and t o
d e c i d e which o f t h e p r o c e s s have t o be e x e c u t e d
next to achieve high e f f i c i e n c y l e v e l .
6 // F i r s t Come F i r s t S e r v e ( FCFS ) Non Pre−e m p t i v e :
J o b s a r e a l w a y s e x e c u t e d on a f i r s t −come , f i r s t −
serve basis .
6
7
8 // F u n c t i o n s t o be l o a d e d
9 exec ( ” f c f s . s c i ” ) ; // f c f s . s c i d e p e n d e n c y f i l e
10
11
12 num =4; // no o f p r o c e s s e s P1 , P2 , P3 , P4
13
14 bt =[10 2 8 6]; // Sample b u r s t t i m e
15 wt = zeros (1 , num ) ; // w a i t i n g t i m e
16 tat = zeros (1 , num ) ; // t u r n a r o u n d t i m e
17
18 disp ( ” F i r s t Come F i r s t S e r v e ( FCFS ) Non Pre−
e m p t i v e CPU S c h e d u l i n g ” ) ;
19 disp ( ” B u r s t t i m e o f t h e g i v e n P r o c e s s P1 =10 , P2
=2 , P3=8 , P4=6” ) ;
20
21 disp ( ’ W a i t i n g Time o f e a c h P r o c e s s ’ ) ; // d i s p l a y i n g
the waiting time
22
23 fcfs = firstcomefirstserve ( num , bt , wt , tat ) //
C a l l i n g f i r s t come f i r s t s e r v e f u n c t i o n
7
Experiment: 2
display_sjf_new.sci
1 clear ;
2 clc ;
3
4 //WINDOWS 10 64−BIT OS , S c i l a b and t o o l b o x versions
6.1.0.
5
6 // SJF s c h e d u l i n g i s employed when s e v e r a l p r o c e s s e s
a r r i v e a l m o s t a t t h e same time , s o a s t o a v o i d
c o n f l i c t , e n s u r e maximum CPU u t i l i z a t i o n w i t h
minimum w a i t i n g time , t u r n a r o u n d t i m e t o m i n i m i z e
s t a r v a t i o n . The a l g o r t h m can be u s e d f o r b o t h
8
c a s e s i . e . , when a r r i v a l t i m e i s t h e same f o r a l l
o r most p r o c e s s e s and when t h e r e a r e s l i g h t l y
d i f f e r e n t a r r i v a l t i m e s . I n c a s e o f same a r r i v a l
time , t h e v a l u e s amy be s e t t o 0 by d e f a u l t by
the user
7
8 // l o a d i n g t h e n e c e s s a r y f u n c t i o n s
9
10 exec ( ” s j f n e w . s c i ” ) ;
11 exec ( ” d i s p l a y s j f n e w . s c i ” ) ;
12
13 num =4; // no o f p r o c e s s e s P1 , P2 , P3 , P4
14 pt =[10 2 8 6 ]; // p r o c e s s t i m e o r b u r s t t i m e
15 pid =[1 2 3 4]; // p r o c e s s i d
16 wt = zeros (1 , num ) ; // w a i t i n g t i m e
17 tat = zeros (1 , num ) ; // t u r n a r o u n d t i m e
18 total =0; // t o t a l w a i t i n g t i m e
19 total2 =0; // t o t a l t u r n a r o u n d t i m e
20
21 disp ( ” S h o r t e s t Job F i r s t ( SJF ) Pre−e m p t i v e CPU
S c h e d u l i n g ”);
22 disp ( ” B u r s t t i m e o f t h e g i v e n P r o c e s s P1 =10 , P2=2 ,
P3=8 , P4=6” ) ;
23
24
25 disp ( ” S o r t e d P r o c e s s b a s e d on i t s S h o r t e s t Job ” ) ;
26
27 sjf = shorestjobfirst ( pid , num , pt , wt , tat ) ; //
Calling shorest job f i r s t function
sjf_new.sci
9
Experiment: 3
1 clear ;
2 clc ;
3 //WINDOWS 10 64−BIT OS , S c i l a b and t o o l b o x versions
6.1.0.
4
10
Figure 3.1: Graphical Analysis WT and AWT
11
5 // S c h e d u l i n g a l g o r i t h m s d e a l s t o m i n i m i z e q u e u i n g
d e l a y and t o o p t i m i z e p e r f o r m a n c e o f q u e u i n g
e n v i r o n m e n t . I n t h i s a n a l y s i s , some common
s c h e d u l i n g a l g o r i t h m s l i k e F i r s t Come F i r s t S e r v e
( FCFS ) , S h o r t e s t Job F i r s t ( SJF ) and Round Robin
(RR) S c h e d u l i n g a r e s t u d i e d and r e v i e w e d on t h e
b a s i s of t h e i r working s t r a t e g y
6
7
8 // F u n c t i o n s t o be l o a d e d
9 exec ( ” f c f s w t a w t . s c i ” ) ; // f c f s w t a w t . s c i
dependency f i l e
10 exec ( ” s j f w t a w t . s c i ” ) ; // s j f w t a w t . s c i d e p e n d e n c y
file
11 exec ( ” r r w t a w t . s c i ” ) ; // r r w t a w t . s c i d e p e n d e n c y
file
12
13 num =4; // no o f p r o c e s s e s P1 , P2 , P3 , P4
14 bt =[10 2 8 6]; // Sample b u r s t t i m e
15 wt = zeros (1 , num ) ; // w a i t i n g t i m e
16 tat = zeros (1 , num ) ; // t u r n a r o u n d t i m e
17
18 disp ( ” G r a p h i c a l A n a l y s i s − W a i t i n g Time v s A v e r a g e
W a i t i n g Time o f S c h e d u l i n g A l g o r i t h m s ” ) ;
19
20 disp ( ” B u r s t t i m e o f t h e g i v e n P r o c e s s P1 =10 , P2=2 ,
P3=8 , P4=6” ) ;
21
22 disp ( ’ W a i t i n g Time o f e a c h P r o c e s s i n FCFS ’ ) ; //
d i s p l a y i n g t h e w a i t i n g t i m e i n FCFS
23
24 fcfs = firstcomefirstserve ( num , bt , wt , tat ) //
C a l l i n g f i r s t come f i r s t s e r v e f u n c t i o n
25
26
27 disp ( ’ W a i t i n g t i m e o f e a c h P r o c e s s i n SJF ’ ) ; //
d i s p l a y i n g the Waiting time o f each P r o c e s s in
SJF
12
28 sjf = shortestjobfirst ( num , bt , wt , tat ) // C a l l i n g
s h o r t e s t job f i r s t function
29
30
31 disp ( ’ W a i t i n g Time o f e a c h P r o c e s s i n Round Robin ’ ) ;
// d i s p l a y i n g t h e W a i t i t i n g Time o f e a c h P r o c e s s
i n Round Robin
32
33 rr = roundrobin ( num , bt , wt , tat ) // C a l l i n g
Round Robin f u n c t i o n
34
35 /* constructing a rows for graphical representation */
36
37 scf (1) ;
38
39 y = [0 ,10 ,20 ,30 ,40];
40
41 x =[1 ,2 ,3 ,4 ,5]
42 avg =
[0 ,16 ,12;10 ,0 ,5;12 ,8 ,15;20 ,2 ,15;10.5 ,6.5 ,11.75];
43
44 /* Matrix avg is set of values obtained from waiting
time for each algorithm FCFS , SJF , RR
respectively */
45
46 xtitle ( ’ G r a p h i c a l A n a l y s i s : W a i t i n g Time Vs A v e r a g e
W a i t i n g Time ’ , ’ P r o c e s s ’ , ’ W a i t i n g t i m e ’ ) ;
47
48 bar (x , avg ) ;
49
50 legend ( ”FCFS” ,” SJF ” ,”RR” ) ;
13
Experiment: 4
1 clear ;
2 clc ;
3 //WINDOWS 10 64−BIT OS , S c i l a b and t o o l b o x versions
6.1.0.
4
14
Figure 4.1: Analysis TAT and ATAT
15
5 // I n t h i s a n a l y s i s , some common s c h e d u l i n g
a l g o r i t h m s l i k e F i r s t Come F i r s t S e r v e ( FCFS ) ,
S h o r t e s t Job F i r s t ( SJF ) and Round Robin (RR)
S c h e d u l i n g a r e s t u d i e d and r e v i e w e d on t h e b a s i s
o f t h e i r Turn Around Time and A v e r a g e Turn Around
Time
6
7
8 // F u n c t i o n s t o be l o a d e d
9 exec ( ” f c f s t a t a t a t . s c i ” ) ; // f c f s t a t a t a t . s c i
d e p e n d e n c y f i l e f o r FCFS S c h e d u l i n g
10 exec ( ” s j f t a t a t a t . s c i ” ) ; // s j f t a t a t a t . s c i
d e p e n d e n c y f i l e f o r SJF S c h e d u l i n g
11 exec ( ” r r t a t a t a t . s c i ” ) ; // r r t a t a t a t . s c i
d e p e n d e n c y f i l e f o r RR S c h e d u l i n g
12
13 num =4; // no o f p r o c e s s e s P1 , P2 , P3 , P4
14 bt =[10 2 8 6]; // Sample b u r s t t i m e
15 wt = zeros (1 , num ) ; // w a i t i n g t i m e
16 tat = zeros (1 , num ) ; // t u r n a r o u n d t i m e
17
18 disp ( ” G r a p h i c a l A n a l y s i s w i t h Turn−Around Time &
A v e r a g e Turn−Around Time o f CPU u s i n g S c i L a b ” ) ;
19 disp ( ” B u r s t t i m e o f t h e g i v e n P r o c e s s P1 =10 , P2=2 ,
P3=8 , P4=6” ) ;
20
21 disp ( ’ Turn Around Time o f e a c h P r o c e s s i n FCFS ’ ) ; //
d i s p l a y i n g t h e Turn Around t i m e i n FCFS
22
23 fcfs = firstcomefirstserve ( num , bt , wt , tat ) //
C a l l i n g f i r s t come f i r s t s e r v e f u n c t i o n
24
25
26 disp ( ’ Turn Around Time o f e a c h P r o c e s s i n SJF ’ ) ; //
d i s p l a y i n g t h e Turn Around t i m e o f e a c h P r o c e s s
i n SJF
27 sjf = shortestjobfirst ( num , bt , wt , tat ) // C a l l i n g
s h o r t e s t job f i r s t function
16
28
29
30 disp ( ’ Turn Around Time o f e a c h P r o c e s s i n Round
Robin ’ ) ; // d i s p l a y i n g t h e Turn Around Time o f
e a c h P r o c e s s i n Round Robin
31
32 rr = roundrobin ( num , bt , wt , tat ) // C a l l i n g
Round Robin f u n c t i o n
33
34 /* constructing a rows for graphical representation */
35
36 scf (1) ;
37
38 y = [0 ,10 ,20 ,30 ,40];
39
40 x =[1 ,2 ,3 ,4 ,5]
41 avg =
[10 ,26 ,22;12 ,2 ,7;20 ,16 ,23;26 ,8 ,21;17 ,13 ,18.25];
42
43 /* Matrix avg is set of values obtained from Turn
Around time for each algorithm FCFS , SJF , RR
respectively */
44
45 xtitle ( ’ G r a p h i c a l A n a l y s i s w i t h Turn−Around Time &
A v e r a g e Turn−Around Time o f CPU u s i n g S c i L a b ’ ,”
P r o c e s s ” ,” Turn Around Time ” ) ;
46
47
48 bar (x , avg ) ;
49
50 legend ( ”FCFS” ,” SJF ” ,”RR” ) ;
17
Experiment: 5
display_rr.sci
1 clear ;
2 clc ;
3
4 //WINDOWS 10 64−BIT OS , S c i l a b and t o o l b o x versions
6.1.0.
5
6 // Round Robin (RR) i s a pre −e m p t i v e s c h e d u l i n g
a l g o r i t h m . The CPU i s s h i f t e d t o t h e n e x t p r o c e s s
a f t e r f i x e d i n t e r v a l time , which i s c a l l e d t i m e
quantum / t i m e s l i c e .
7
18
8 // F u n c t i o n s t o be l o a d e d
9 exec ( ” r o u n d r o b i n n e w . s c i ” ) ; // d e p e n d e n c y f i l e
roundrobin new . s c i
10 exec ( ” d i s p l a y r r . s c i ” ) ; // d e p e n d e n c y f i l e f o r d i s p l a y
function
11
12 disp ( ” ROUND ROBIN SCHEDULING
”)
13
14 at = [0 1 2 3]; // D e f i n i n g s a m p l e A r r i v a l Time
15 bt = [9 5 3 4]; // D e f i n i n g s a m p l e B u r s t Time
16 n = size ( at ) ;
17
18 disp ( ” Sample Quantum Time= 5 ” )
19 mprintf ( ” \n ” )
20 q = input ( ” E n t e r Quantum Time : ” ) ;
21 disp ( ” P r o c e s s Turnaround t i m e
Waiting time ”);
22
23 // C a l l i n g Round Robin f u n c t i o n
24 rr = roundrobin (q ,n , at , bt ) ;
25
26 // d i s p ( ” P r o c e s s Turnaround t i m e
Waiting time ”) ;
roundrobin_new.sci
19
Experiment: 6
Comparison of Various
Partition Allocation
Algorithms using Scilab
best_fit_func.sci
display_func.sci
first_fit_func.sci
1 clear ;
20
Figure 6.1: Comparison of Various Partition Allocation Algorithms using
Scilab
21
2 clc ;
3 //WINDOWS 10 64−BIT OS , S c i l a b and t o o l b o x versions
6.1.0.
4
5 // T h i s e x p e r i m e n t i s compare t h e v a r i o u s p a r t i t i o n
a l l o c a t i o n a l g o r i t h m s u s e d by t h e o p e r a t i n g
s y s t e m f o r memory a l l o c a t i o n
6 // 1 . F i r s t −F i t Memory A l l o c a t i o n 2 . Best −F i t
Memory A l l o c a t i o n 3 . Worst−F i t Memory A l l o c a t i o n
7
8 // l o a d i n g a l l t h e n e c e s s a r y f u n c t i o n s
9 exec ( ” f i r s t f i t f u n c . s c i ” ) ;
10 exec ( ” b e s t f i t f u n c . s c i ” ) ;
11 exec ( ” w o r s t f i t f u n c . s c i ” ) ;
12 exec ( ” d i s p l a y f u n c . s c i ” ) ;
13
14 // Example p r o b l e m
15 p = [90 20 50 200]; // D e f i n i n g s a m p l e P r o c e s s S i z e
16 b = [50 100 90 200 60]; // D e f i n i n g s a m p l e B l o c k S i z e
17
18
19 // D e t e r m i n i n g t h e number o f p r o c e s s e s and b l o c k s
20
21 size_process = size ( p ) ; // S i z e o f t h e p r o c e s s
array i s calculated using s i z e () function
22 size_process = size_process (2) ;
23
24 size_block = size ( b ) ; // S i z e o f t h e b l o c k
array i s calculated using s i z e () function
25 size_block = size_block (2) ;
26
27 /* calling the function , defined in first fit . sci ,
for first fit allocation */
28 ff_allot = firstFit (p ,b , size_process , size_block )
29
30 /* calling the function , defined in best fit . sci , for
best fit allocation */
31 bf_allot = bestFit (p , b , size_process , size_block )
22
32
33 /* calling the function , defined in worst fit . sci ,
for worst fit allocation */
34 wf_allot = worstFit (p ,b , size_process , size_block ) ;
35
36
37 ff_allotsize = zeros (1 , size_process ) ; //
ff allotsize − s i z e of the s e l e c t e d blocks f o r
first fit
38 bf_allotsize = zeros (1 , size_process ) ; //
bf allotsize − s i z e of the s e l e c t e d blocks f o r
best f i t
39 wf_allotsize = zeros (1 , size_process ) ; //
wf allotsize − s i z e of the s e l e c t e d blocks f o r
worst f i t
40
41 // s t o r i n g t h e a l l o c a t e d b l o c k s i z e f o r e a c h p r o c e s s
according to the r e s p e c t i v e f i t s
42
43 for i =1: size_process
44 if ff_allot ( i ) ~=0 then //
c h e c k i n g i f any b l o c k i s s e l e c t e d
45 ff_allotsize ( i ) = b ( ff_allot ( i ) ) ; //
s t o r i n g the s i z e of the s e l e c t e d block
for f i r s t f i t
46 else
47 ff_allotsize ( i ) = 0 //
s t o r e s i z e a s 0 i f no b l o c k i s s e l e c t e d
48 end ,
49
50 if bf_allot ( i ) ~=0 then //
c h e c k i n g i f any b l o c k i s s e l e c t e d
51 bf_allotsize ( i ) = b ( bf_allot ( i ) ) ; //
s t o r i n g the s i z e of the s e l e c t e d block
for best f i t
52 else
53 bf_allotsize ( i ) = 0 //
s t o r e s i z e a s 0 i f no b l o c k i s s e l e c t e d
23
54 end ,
55
56 if wf_allot ( i ) ~=0 then //
c h e c k i n g i f any b l o c k i s s e l e c t e d
57 wf_allotsize ( i ) = b ( wf_allot ( i ) ) ; //
s t o r i n g the s i z e of the s e l e c t e d block
f o r worst f i t
58 else
59 wf_allotsize ( i ) = 0; //
s t o r e s i z e a s 0 i f no b l o c k i s s e l e c t e d
60 end ,
61 end
62
63
64 // c o n s t r u c t i n g a m a t r i x ( y ) f o r graphical
representation of f i r s t f i t , b e s t f i t and w o r s t
fit
65
66 y = zeros ( size_process ,3) ;
67
68 y (: ,1) = ff_allotsize ; // First f i t allotment
size
69 y (: ,2) = bf_allotsize ; // Best f i t allotment
size
70 y (: ,3) = wf_allotsize ; // Worst f i t a l l o t m e n t
size
71
72 bar ( y ) // p l o t t i n g a b a r g r a p h
73
74 xtitle ( ” Comparison o f V a r i o u s P a r t i t i o n Allocation
A l g o r i t h m ” ,” P r o c e s s Number” ,” B l o c k S i z e ” ) ;
75 legend ( ” F i r s t F i t ” , ” B e s t F i t ” , ” Worst F i t ” ) ;
76
77
78 // p r i n t i n g first f i t , b e s t f i t and w o r s t f i t a r r a y
79
80 mprintf ( ” Comparison o f V a r i o u s P a r t i t i o n A l l o c a t i o n
A l g o r i t h m s u s i n g S c i l a b \n\n ” ) // d i s p l a y i n g t h e
24
t i t l e of experiment
81 mprintf ( ”Ex : P r o c e s s s i z e P1 =90 , P2 =20 , P3 =50 , P4
=200 ” ) ; // d i s p l a y i n g
the sample P r o c e s s s i z e c o n s i d e r e d f o r the o f
experiment
82 mprintf ( ” \n B l o c k o r h o l e s i z e B1= 5 0 , B2= 1 0 0 ,
B3=90 , B4=200\n ” ) ; // d i s p l a y i n g
the sample Block s i z e c o n s i d e r e d f o r the o f
experiment
83
84 mprintf ( ” \nFIRST FIT : \ n ” )
85 mprintf ( ” P r o c e s s no . \ t P r o c e s s s i z e \ t B l o c k no . B l o c k
s i z e \n ” )
86 display ( ff_allot , ff_allotsize , size_process , p )
// d i s p l a y i n g t h e p r o c e s s and b l o c k
a l l o c a t i o n by f i r s t f i t a r r a y
87
88 mprintf ( ” \nBEST FIT : \ n ” )
89 mprintf ( ” P r o c e s s no . \ t P r o c e s s s i z e \ t B l o c k no . B l o c k
s i z e \n ” )
90 display ( bf_allot , bf_allotsize , size_process , p )
// d i s p l a y i n g t h e p r o c e s s and b l o c k
a l l o c a t i o n by b e s t f i t a r r a y
91
92
93 mprintf ( ” \nWORST FIT : \ n ” )
94 mprintf ( ” P r o c e s s no . \ t P r o c e s s s i z e \ t B l o c k no . B l o c k
s i z e \n ” )
95 display ( wf_allot , wf_allotsize , size_process , p )
// d i s p l a y i n g t h e p r o c e s s and b l o c k
a l l o c a t i o n by w o r s t f i t a r r a y
worst_fit_func.sci
25
Experiment: 7
Deadlock.sci
Scilab code Solution 7.0 Banker Algorithm Deadlock Avoidance using Scilab
1 clear ;
2 clc ;
3
4 // The b a n k e r s algorithm i s a resource a l l o c a t i o n
and d e a d l o c k a v o i d a n c e a l g o r i t h m t h a t t e s t s f o r
s a f e t y by s i m u l a t i n g t h e a l l o c a t i o n f o r
p r e d e t e r m i n e d maximum p o s s i b l e amounts o f a l l
resources
5
6 // Load d e p e n d e n c y f i l e
7 exec ( ” D e a d l o c k . s c i ” ) ;
8
26
9 //WINDOWS 10 64−BIT OS , S c i l a b and t o o l b o x v e r s i o n s
6.1.0.
10 close ;
11 n =5; // Number o f p r o c e s s e s
12 m =3; // Number o f r e s o u r c e s
13
14
15 disp ( ” B a n k e r s A l g o r i t h m f o r D e a d l o c k A v o i d a n c e ” )
16 mprintf ( ” \ n C o n s i d e r Number o f P r o c e s s e s N=5 , Number
o f r e s o u r c e s M=3\n ” ) ; // Number o f p r o c e s s e s and
Number o f r e s o u r c e s
17
18 mprintf ( ” \n A l l o c a t i o n a r e [ ] 0 1 0 ; 2 0 0 ; 3 0 2 ; 2 1
2 ; 0 0 2 ] ” ) ; // G i v i n g t h e p r o c e s s a l l o c a t i o n
values
19 mprintf ( ” \nM=[7 5 3 ; 3 2 2 ; 9 0 2 ; 2 2 2 ; 4 3 3 ] \ n ” ) ; //
G i v i n g t h e maximum v a l u e s
20
21 disp ( ’ F o l l o w i n g i s t h e SAFE S e q u e n c e s a t i s f i e s t h e
s a f e t y r e q u i r e m e n t : ’ ) ; // D i s p command p r i n t s t h e
safe sequence
22
23 Deadlock (n , m ) // F u n c t i o n c a l l o f D e a d l o c k −
Bankers Algorithm
27
Experiment: 8
Process Synchronization
Techniques using Scilab
1 // //DEKKER’ S ALGORITHM/ / / /
2
3 clear ;
4 clc ;
5 //WINDOWS 10 64−BIT OS , S c i l a b and t o o l b o x versions
6.1.0.
6
7 exec ( ” P r o c e s s S y n c h . s c i ” ) ;
8
9
10 // Dekker ’ s a l g o r i t h m g u a r a n t e e s mutual e x c l u s i o n ,
f r e e d o m from d e a d l o c k , and f r e e d o m from
starvation .
28
11 // I n t h i s a l g o r i t h m , t h e p o s i t i o n o f e a c h p r o c e s s i s
i n d i c a t e d w i t h t h e v a r i a b l e s t u r n and f l a g .
12
13
14 // INITIALIZING THE VALUES
15 c1 =1 , c2 =1 , turn =1;
16 count0 =0 , count1 =0 , count =0;
17 i =0;
18 // D e a f u l t v a l u e s
19 limit =10; // L i m i t o f CS
20 a =1 , b =1; // Time t a k e n i n CS by b o t h p r o c e s s
21
22
23 disp ( ” P r o c e s s S y n c h r o n i z a t i o n u s i n g D e k k e r s
Algorithm i n S c i l a b ”);
24 mprintf ( ” \ n I n p u t Ex : Time i n CS f o r P1 i s 2 , P2 i s
3 , T o t a l Time i s 9\ n ” )
25 //INSTRUCTIONS TO PROCEED :
26 mprintf ( ” 1 ) P r o c e s s 1 e n t e r s \n ” ) ;
27 mprintf ( ” 2 ) P r o c e s s 2 e n t e r s \n ” ) ;
28 mprintf ( ” 3 ) Both p r o c e s s e n t e r s \n ” ) ;
29 mprintf ( ” 4 ) E x i t \n ” ) ;
30 mprintf ( ” \n E n t e r t o t a l t i m e r e q u i e d by P r o c e s s 1 i n
CS : ” )
31 a = input ( ” ” )
32 mprintf ( ” E n t e r t o t a l t i m e r e q u i e d by P r o c e s s 2 i n
CS : ” )
33 b = input ( ” ” )
34 mprintf ( ” E n t e r t o t a l t i m e l i m i t o f CS : ” )
35 limit = input ( ” ” )
36 // Psyn ( a , b , l i m i t ) ;
37 Psyn (a ,b , limit )
38 //FUNCTION TO IDENTIFY WHICH PROCESS ENTERS NOW
29
Experiment: 9
best_fit_func.sci
display_func.sci
first_fit_func.sci
Scilab code Solution 9.0 Memory Management using Scilab First Fit Best
Fit and Worst Fit
1 clear ;
30
2 clc ;
3 //WINDOWS 10 64−BIT OS , S c i l a b and t o o l b o x versions
6.1.0.
4 // The memory t o t h e p r o c e s s o r w i l l be a l l o c a t e d i n
s e v e r a l b l o c k s o f memory , i n o r d e r t o make a
p e r f e c t l y o r g a n i z e d a l l o c a t i o n b e t w e e n t h e memory
b l o c k s and p r o c e s s , t h r e e d i f f e r e n t p a r t i t i o n
and a l l o c a t i o n a l g o r i t h m s a r e u s e d 1 . F i r s t −F i t
Memory A l l o c a t i o n 2 . Best −F i t Memory A l l o c a t i o n
3 . Worst−F i t Memory A l l o c a t i o n
5
6 /* loading all the necessary functions */
7 exec ( ” f i r s t f i t f u n c . s c i ” ) ;
8 exec ( ” b e s t f i t f u n c . s c i ” ) ;
9 exec ( ” w o r s t f i t f u n c . s c i ” ) ;
10 exec ( ” d i s p l a y f u n c . s c i ” ) ;
11
12 mprintf ( ”Memory Management F i r s t f i t , B e s t F i t and
Worst F i t A l l o c a t i o n \n ” ) ;
13 mprintf ( ”
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\
n ”);
14
15 mprintf ( ”Ex : P r o c e s s s i z e P1 =212 , P2 =417 , P3 =112 , P4
=426 ” ) ;
16 mprintf ( ” \n B l o c k o r h o l e s i z e B1= 1 0 0 , B2= 5 0 0 ,
B3 =200 , B4 =300 , B5=600\n ” ) ;
17
18 /* Example problem */
19 p = [212 ,417 ,112 ,426]; // p r o c e s s s i z e
20 b = [100 ,500 ,200 ,300 ,600]; // b l o c k o r h o l e s i z e
21
22 disp ( ” S e l e c t t h e O p t i o n : ” ) ;
23 mprintf ( ” 1− F i r s t F i t \n ” ) ;
24 mprintf ( ” 2− B e s t F i t \n ” )
25 mprintf ( ” 3−Worst F i t \n ” ) ;
26
27 /*
31
28 Determining the number of processes and blocks
29 */
30 size_process = size ( p ) ;
31 size_process = size_process (2) ;
32
33 size_block = size ( b ) ;
34 size_block = size_block (2) ;
35
36 n1 = input ( ” ” ) ;
37 if ( n1 ==1) then
38 // f i r s t f i t () ;
39 /* calling the function , defined in first fit . sci ,
for first fit allocation */
40 ff_allot = firstFit (p ,b , size_process , size_block )
41 end
42 if ( n1 ==2) then
43 /* calling the function , defined in best fit . sci , for
best fit allocation */
44 bf_allot = bestFit (p , b , size_process ,
size_block )
45
46 end
47 if ( n1 ==3) then
48 // worstfit () ;
49 /* calling the function , defined in worst fit . sci ,
for worst fit allocation */
50 wf_allot = worstFit (p ,b , size_process , size_block ) ;
51
52 end
53
54 /*
55 ff_allotsize - size of the selected blocks for first
fit
56 bf_allotsize - size of the selected blocks for best
fit
57 wf_allotsize - size of the selected blocks for worst
fit
58 */
32
59 ff_allotsize = zeros (1 , size_process ) ;
60 bf_allotsize = zeros (1 , size_process ) ;
61 wf_allotsize = zeros (1 , size_process ) ;
62
63 /* storing the allocated block size for each process
according to the respective fits */
64 for i =1: size_process
65 if ( n1 ==1) then
66
67 if ff_allot ( i ) ~=0 then // c h e c k i n g i f any
block i s s el ec t ed
68 ff_allotsize ( i ) = b ( ff_allot ( i ) ) ; //
s t o r i n g the s i z e of the s e l e c t e d
block for f i r s t f i t
69 else
70 ff_allotsize ( i ) = 0 // s t o r e s i z e a s 0
i f no b l o c k i s s e l e c t e d
71 end ,
72 end
73
74 if ( n1 ==2) then
75 if bf_allot ( i ) ~=0 then // c h e c k i n g i f any
block i s s el ec te d
76 bf_allotsize ( i ) = b ( bf_allot ( i ) ) ; //
s t o r i n g the s i z e of the s e l e c t e d
block for best f i t
77 else
78 bf_allotsize ( i ) = 0 // s t o r e s i z e a s 0
i f no b l o c k i s s e l e c t e d
79 end ,
80 end
81 if ( n1 ==3) then
82 if wf_allot ( i ) ~=0 then // c h e c k i n g i f any
block i s s el ec te d
83 wf_allotsize ( i ) = b ( wf_allot ( i ) ) ; //
s t o r i n g the s i z e of the s e l e c t e d
block f o r worst f i t
84 else
33
85 wf_allotsize ( i ) = 0; // s t o r e s i z e a s 0
i f no b l o c k i s s e l e c t e d
86 end ,
87 end
88 end
89 /*
90 printing first fit , best fit and worst fit array
91 */
92 if ( n1 ==1) then
93 mprintf ( ” \nFIRST FIT : \ n ” )
94 mprintf ( ” P r o c e s s no . \ t P r o c e s s s i z e \ t B l o c k no . B l o c k
s i z e \n ” )
95 display ( ff_allot , ff_allotsize , size_process , p )
96 end
97 if ( n1 ==2) then
98
99 mprintf ( ” \nBEST FIT : \ n ” )
100 mprintf ( ” P r o c e s s no . \ t P r o c e s s s i z e \ t B l o c k no .
B l o c k s i z e \n ” )
101 display ( bf_allot , bf_allotsize , size_process , p )
102 end
103 if ( n1 ==3) then
104
105 mprintf ( ” \nWORST FIT : \ n ” )
106 mprintf ( ” P r o c e s s no . \ t P r o c e s s
s i z e \ t B l o c k no .
B l o c k s i z e \n ” )
107 display ( wf_allot , wf_allotsize , size_process , p )
108 end
worst_fit_func.sci
34
Experiment: 10
PageReplacement.sci
1 clear ;
2 clc ;
3 //WINDOWS 10 64−BIT OS , S c i l a b and t o o l b o x versions
6.1.0.
4
5 // I n Optimal p a g e r e p l a c e m e n t a l g o r i t h m , t h e p a g e
t h a t w i l l n o t be u s e d f o r t h e l o n g e s t p e r i o d o f
t i m e i s r e p l a c e d t o make s p a c e f o r t h e r e q u e s t e d
page .
6
7 // F u n c t i o n s t o be l o a d e d
35
8 exec ( ” P a g e R e p l a c e m e n t . s c i ” ) ; // P a g e R e p l a c e m e n t . s c i
dependency f i l e
9
10 mprintf ( ” Optimal Page R e p l a c e m e n t A l g o r i t h m u s i n g
S c i l a b \n ” )
11
12 mprintf ( ” Sample I n p u t : No . o f f r a m e s =4 , No . Page =13
”)
13 mprintf ( ” \n ” )
14 mprintf ( ” Sample Page R e f e r e n c e S t r i n g
7 , 0 , 1 , 2 , 0 , 3 , 0 , 4 , 2 , 3 , 0 , 3 , 2 ”)
15 frames = cell (10) ; // Read Frames
16 pages = cell (30) ; // Read P a g e s
17 temp = cell (10) ; // Read temp
18 noOfFrames =0;
19 noOfPages =0;
20
21 mprintf ( ” \n ” )
22 mprintf ( ’ E n t e r No Of Frames ’ ) ; // W r i t e t o command
window
23 mprintf ( ” \n ” )
24 noOfFrames = input ( ” ” ) ; // G i v i n g t h e no . o f . Frames
Value
25
26 mprintf ( ’ E n t e r No Of P a g e s ’ ) ; // W r i t e t o command
window
27 noOfPages = input ( ” ” ) ; // G i v i n g t h e no . o f . P a g e s
Value
28 flag1 =0; // S e t t i n g a F l a g
29 flag2 =0; // S e t t i n g a F l a g
30 flag3 =0; // S e t t i n g a F l a g
31 faults =0; // Read f a u l t s
32 maximum =0; // S e t t i n g i t t o a marker v a l u e
33
34 mprintf ( ” \n ” )
35 mprintf ( ’ E n t e r Page R e f e r e n c e V a l u e s ’ ) ; // W r i t e t o
command window
36
36
37 for n =1: noOfPages // G i v i n g t h e first reference value
in f i r s t frame
38 // m p r i n t f ( ” E n t e r Page r e f e r e n c e %d” , n )
39 pages { n }= input ( ” ” ) ; // P r i n t i n g t h e v a l u e i n
page
40 end
41
42 pagereplacement ( noOfFrames , noOfPages , pages ) //
Calling pagereplacement function
37
Appendix
Scilab code AP 1
1 function []= pagereplacement ( noOfFrames , noOfPages
, pages ) // // Page r e p l a c e m e n t f u n c t i o n
2
3 for n =1: noOfFrames // G i v i n g t h e no . o f Frames
4 frames { n }= -1;
5 end
6
7 for i =1: noOfPages // No . o f P a g e s
8 flag1 =0;
9 flag2 =0;
10 for j =1: noOfFrames
11 if ( frames { j }== pages { i })
12 flag1 =1;
13 flag2 =1;
14 break ;
15 end
16 end
17 if ( flag1 ==0)
18 for j =1: noOfFrames
19 if ( frames { j }== -1)
20 faults = faults +1;
21 frames { j }= pages { i };
22 flag2 =1;
23 break ;
24 end
25 end
38
26 end
27 if ( flag2 ==0)
28 flag3 =0;
29 for j =1: noOfFrames
30 temp { j }= -1;
31 for k = i +1: noOfPages
32 if ( frames { j }== pages { k }) // Check t h e
f r a m e s and p a g e s a r e f i l l e d
33 temp { j }= k
34 break ;
35 end
36 end
37 end
38
39 // For l o o p f o r i d e n t i f y i n g p a g e f a u l t s
40 for j =1: noOfFrames
41 if ( temp { j }== -1)
42 pos = j ;
43 flag3 =1;
44 break ;
45 end
46 end
47 if ( flag3 ==0)
48 maximum = temp {0};
49 pos =0;
50 for j =1: noOfFrames
51 if ( temp { j } > maximum )
52 maximum = temp { j }; // Check t h e
value i s long period use or
not
53 pos = j ;
54 end
55 end
56 end
57 frames { pos }= pages { i }
58 faults = faults +1; // Check n e x t v a l u e i s
Equal o r t o c h a n g e t h e p o s i t i o n
59 end
39
60 end
61 faults = faults -1;
62 mprintf ( ”No Of Page F a l u t s %d\n ” , faults ) ; // W r i t e
Page F a u l t s t o command window
63
64 endfunction
Optimal page replacement
Scilab code AP 2
1 // Worst−F i t Memory A l l o c a t i o n , t h e o p e r a t i n g
s y s t e m s e a r c h e s t h e e n t i r e l i s t and a l l o c a t e s t h e
l a r g e s t a v a i l a b l e hole to the pro cess .
2 // I f a l a r g e p r o c e s s comes a t a l a t e r s t a g e , t h e n
memory may n o t have s p a c e t o accommodate i t .
3
4 function [ wf_allot ]= worstFit (p ,b , size_process ,
size_block )
5
6 // D e c l a r i n g w o r s t f i t f l a g a r r a y ( w f f l a g ) which i s
used f o r maintaining the s t a t u s o f each block (
f r e e o r busy )
7
8 wf_allot = zeros (1 , size_process ) ;
// D e c l a r i n g w o r s t f i t a r r a y ( w f a l l o t )
9 wf_flag = zeros (1 , size_block ) ;
10
11 // For l o o p f o r a l l o c a t i n g b l o c k s a c c o r d i n g t o w o r s t
fit
12
13 for i =1: size_process
14 k = -1; // k − i n d e x
p o s i t i o n o f t h e l a r g e s t b l o c k which can
accommodate a p r o c e s s , i n i t i a l l y s e t t o
−1
15 for j =1: size_block
16 if p ( i ) <= b ( j ) && wf_flag ( j ) == 0 then //
i f p r o c e s s s i z e i s l e s s than b l o c k
40
s i z e and b l o c k i s f r e e
17 if k == -1 then
18 k = j; // u p d a t e k w i t h
index p o s i t i o i n of the block
19 elseif ( b ( k ) <b ( j ) ) // i f t h e r e i s a
l a r g e r b l o c k which can
accommodate t h e p r o c e s s
20 k = j ; // u p d a t e k w i t h t h e
index p o s i t i o n of the l a r g e r
block
21 end ,
22 end ,
23 end
24 if ( k == -1)
25 wf_allot ( i ) =0; // i f no b l o c k can
accomodate the p r o c e s s , s e t a l l o t t e d
b l o c k number a s 0
26 else
27 wf_allot ( i ) = k ; // s t o r e t h e s e l e c t e d
index in the worst f i t array in the
i n d e x p o s i t i o n i ( p r o c e s s number )
28 wf_flag ( k ) = 1; // s e t t h e s t a t u s o f t h e
s e l e c t e d b l o c k a s busy
29 end ,
30 end
31 endfunction
Worst Fit Memory Allocation
41
6
7 // D e c l a r i n g f i r s t f i t f l a g a r r a y ( f f f l a g ) which i s
used f o r maintaining the s t a t u s o f each block (
f r e e o r busy )
8
9 ff_allot = zeros (1 , size_process ) ; //
Declaring f i r s t f i t array ( f f a l l o t )
10 ff_flag = zeros (1 , size_block ) ;
11
12 // For l o o p f o r a l l o c a t i n g b l o c k s a c c o r d i n g t o
first fit
13
14 for i =1: size_process
15 for j =1: size_block
16 if p ( i ) <= b ( j ) && ff_flag ( j ) ==0 then
// i f p r o c e s s s i z e i s l e s s t h a n b l o c k
s i z e and b l o c k i s f r e e
17 ff_allot ( i ) = j ;
// s t o r e i n d e x p o s i t i o n o f t h e b l o c k
in f f a l l o t in the index p o s i t i o n i (
p r o c e s s number )
18 ff_flag ( j ) = 1;
// s e t s t a t u s a s busy
19 break
20 end ,
21 end
22 end
23 endfunction
First Fit Memory Allocation
Scilab code AP 4
1 // D i s p l a y F u n c t i o n : I t p r i n t s a l l r e q u i r e d d e t a i l s
s u c h a s P r o c e s s no . , P r o c e s s s i z e , B l o c k no . ,
2 // B l o c k s i z e f o r F i r s t F i t , B e s t F i t and Worst F i t .
3
4 function display ( allot , allotsize , size_process , p )
5 for i =1: size_process
42
6 if allot ( i ) ==0 then
7 mprintf ( ”P%d\ t \t%d\ t Not a l l o c a t e d
−\n ” ,i , p ( i ) ) // D i s p l a y
t h e P r o c e s s number t h a t c o u l d n o t
allocated
8 else
9 mprintf ( ”P%d\ t \t%d\ t \tB%d\ t %d\n ” ,i , p ( i
) , allot ( i ) , allotsize ( i ) ) // D i s p l a y
t h e P r o c e s s number w i t h a l l o c a t e d
B l o c k number
10 end ,
11 end
12 endfunction
Display Function
43
15 k = -1; // k −
index p o s i t i o n of the s m a l l e s t block
which can accommodate a p r o c e s s ,
i n i t i a l l y s e t t o −1
16 for j =1: size_block
17 if p ( i ) <= b ( j ) && bf_flag ( j ) == 0 then //
i f p r o c e s s s i z e i s l e s s than b l o c k
s i z e and b l o c k i s f r e e
18 if k == -1 then
19 k = j; // u p d a t e k
with index p o s i t i o n o f the
block
20 elseif ( b ( j ) <b ( k ) ) // i f t h e r e
i s a s m a l l e r b l o c k which can
accommodate t h e p r o c e s s
21 k = j; // u p d a t e k
with the index p o s i t i o n o f
the smaller block
22 end ,
23 end ,
24 end
25 if ( k == -1)
26 bf_allot ( i ) =0; // i f no
b l o c k can a c c o m o d a t e t h e p r o c e s s , s e t
a l l o t t e d b l o c k number a s 0
27 else
28 bf_allot ( i ) = k ; // s t o r e t h e
s e l e c t e d index in the best f i t array
in the index p o s i t i o n i ( process
number )
29 bf_flag ( k ) = 1; // s e t t h e
s t a t u s o f t h e s e l e c t e d b l o c k a s busy
30 end ,
31 end
32 endfunction
Best Fit Memory Allocation
44
Scilab code AP 6
1 // I n t h i s program D e k k e r s A l g o r i t h m i s used ,
t o e n s u r e one p r o c e s s e n t e r s t h e c r i t i c a l s e c t i o n
at a time w h i l e the o t h e r p r o c e s s e s need to wait
f o r t h e f i r s t one t o l e a v e t h e c r i t i c a l s e c t i o n .
2
3 function [] = Psyn (a ,b , limit )
4 while (i <= limit )
5 printf ( ” \n ” )
6 x = input ( ”SELECT THE OPTION : ” )
7
8 //PROCESS 1 ENTERS
9 if x ==1 then // O p t i o n 1
10 c1 =0;
11 while c2 ==0
12 if turn ==2 then
13 c1 =1;
14 while turn ==2 // do n o t h i n g
15 end
16 c1 =0;
17 end
18 end
19 // c r i t i c a l s e c t i o n
20 count0 = count0 + a ;
21 i=i+a;
22 // y i e l d
23 c1 =1;
24 turn =2;
25 // r e m a i n d e r s e c t i o n
26 if ( count0 > limit ) | (i > limit ) then
27 printf ( ” E x c e e d s t h e l i m i t o f CS\n ” )
28 printf ( ”END\n ” ) ;
29 i = 100;
30 else
31 if (a >0)
32 printf ( ” P r o c e s s P1 E n t e r s t h e
C r i t i c a l s e c t i o n ”);
33 printf ( ” \ n T o t a l Time o f P1 i n
45
C r i t i c a l S e c t i o n : %d\n ” , count0 ) ;
34 end
35 printf ( ” \ n I t i s t h e t u r n p r o c e s s P2\n
”);
36 end
37 end
38
39 //PROCESS 2 ENTERS
40 if x ==2 then // O p t i o n 2
41 c2 =0;
42 while c1 ==0
43 if turn ==1
44 c2 =1;
45 while turn ==1 // do n o t h i n g
46 end
47 c2 =0;
48 end
49 end
50 // c r i t i c a l s e c t i o n
51 count1 = count1 + b ;
52 i=i+b;
53 // y i e l d
54 c2 =1;
55 turn =1;
56 // r e m a i n d e r s e c t i o n
57 if ( count1 > limit ) | (i > limit ) then
58 printf ( ” E x c e e d s t h e l i m i t o f CS\n ” )
59 printf ( ”END\n ” ) ;
60 i = 100;
61 else
62 if (b >0)
63 printf ( ” P r o c e s s P2 E n t e r s t h e
C r i t i c a l s e c t i o n ”);
64 printf ( ” \ n T o t a l Time o f P2 i n
C r i t i c a l S e c t i o n : %d\n ” , count1 ) ;
65 end
66 printf ( ” \ n I t i s t h e t u r n p r o c e s s P1\n
”);
46
67 end
68 end
69
70 //BOTH PROCESS ENTER AT SAME TIME O p t i o n 3
71 if x ==3 then
72 printf ( ” \ nBoth p r o c e s s c a n t e n t e r a t same
t i m e i n C r i t i c a l S e c t i o n \n ” ) ;
73 if i > limit then
74 printf ( ”END\n ” ) ;
75 i = 100;
76 end
77 end
78
79 //END f o r OTHER CONDITIONS Option 4
80 if x ==4 then
81 printf ( ” \nEND\n ” ) ;
82 i = 100;
83 end
84 end
85 endfunction
Dekker Algorithm
47
14 for k =1:5 // I t e r a t i n g the values f o r a l l the 5
processes
15 f ( k ) =0;
16 end
17
18 for i =1:5 // I t e r a t i n g the values f o r a l l the
processes
19 for j =1:3 // I t e r a t i n g t h e v a l u e f o r a l l t h e
processes
20 need (i , j ) = M (i , j ) - A (i , j ) ; // Need i s
c a l c u l a t e d by s u b t a c t i n g t h e maximum and
availabe resources
21
22 end ;
23
24 end
25 y =0;
26 for k =1:5 // I t e r a t i n g f o r a l l t h e 5 p r o c e s s e s
27 for i =1:5
28 if ( f ( i ) ==0) then
29 flag = 0; // F l a g v a l u e i s s e t z e r o
30 for j =1:3 // I t e r a t i n g f o r a l l t h e r e s o u r c e s
31 if ( need (i , j ) > L ( j ) ) then // I f t h e
n e e d v a l u e i s more t h a n t h e A v a i l a b l e
r e s o u r c e s , t h e r e q u e s t c a n n o t be
granted
32 flag =1; // Then t h e f l a g i s s e t t o 1
33 break ; // The l o o p b r e a k s h e r e
34 end
35 end
36 if ( flag ==0) then // I f t h e c o n d i t i o n i s
s a t i s f i e d the next p r o c e s s i s checked
similarly
37 ans1 ( z ) = i ;
38 z = z +1;
39
40 for y =1:3
41 L ( y ) = L ( y ) + A (i , y ) ; // I f t h e c o n d i t i o n i s
48
s a t i s f i e d the a v a i l a b l e v a l u e i s
u p d a t e d by a d d i n g a v a i l a b l e v a l u e and
the a l l o c a t e d r e s o u r c e s of the
particular process
42 end
43 f ( i ) =1;
44 end
45 end
46 end
47 end
48
49 // For l o o p f o r d i s p l a y i n g SAFE S e q u e n c e which
s a t i s f i e s the s a f e t y
50 for i =1:5
51 ans1 ( i ) = ans1 ( i ) -1;
52 end
53 for i =1:5
54
55 mprintf ( ’ <P%d> , ’ , ans1 ( i ) ) ;
56
57 end ;
58 endfunction
Deadlock Bankers Algorithm
49
12
13 time =0; // c o m p l e t i o n t i m e i s i n i t i a l l y s e t t o z e r o
14
15 for i =1:4
16 rt ( i ) = bt ( i ) ;
17 end ;
18
19 // r u n n i n g t h e p r o c e s s e s f o r s p e c i f i e d quantum
20 while remain ~=0
21 for i =1:4
22
23 if rt ( i ) <= quantum_time & rt ( i ) >0 then //
e x e c u t e s i f b u r s t time i s g r e a t e r than 0
and l e s s e r t h a n quantum t i m e
24 time = time + rt ( i ) ; // u p d a t e c o m p l e t i o n
time
25 rt ( i ) =0;
26 flag =1;
27 elseif rt ( i ) >0 then
28 rt ( i ) = rt ( i ) - quantum_time ; // u p d a t e
burst time
29 time = time + quantum_time ;
30 end ;
31 if rt ( i ) ==0 & flag ==1 then // e x e c u t e s i f
b u r s t t i m e i s e q u a l t o 0 and f l a g =1
32 remain = remain -1;
33 mprintf ( ’ \n P%i\ t \ t \ t %i \ t \ t \ t %i ’ ,i
, time - at ( i ) , time - at ( i ) - bt ( i ) ) ;
34 tat = tat + time - at ( i ) ; // Turnaround t i m e
= c o m p l e t i o n time − a r r i v a l t i m e
35 wait_time = wait_time + time - at ( i ) - bt (
i ) ; // W a i t i n g t i m e = t u r n a r o u n d −
burst time
36 flag =0;
37 end ;
38 if i == n -1
39 i =1;
40 elseif at ( i ) <= time then // e x e c u t e s when
50
a r r i v a l time i s l e s s e r than / e q u a l to
completion time
41 i =1;
42 else
43 i =1;
44 end ;
45 end ;
46 end ;
47
48 // d i s p l a y f u n c n c t i o n c a l l
49 d = displayfunc ( tat , wait_time ) ; // A v e r a g e
D i s p l a y i n g t u r n a r o u n d t i m e and A v e r a g e w a i t i n g
time
50
51 endfunction
Round Robin Scheduling New
Scilab code AP 9
1 function [ atat , awt ] = displayfunc ( tat , wait_time )
2 awt = wait_time *1.0/4; // T o t a l w a i t t i m e / no o f
p r o c e s s e s g i v e s a v e r a g e w a i t i n g time ,
s i m i l a r l y avg t u r n a r o u n d t i m e i s c a l c u l a t e d
3 atat = tat *1.0/4;
4
5 mprintf ( ” \n ” )
6
7 disp ( ” A v e r a g e W a i t i n g Time u s i n g RR= ” ) ; //
D i s p l a y i n g Average w a i t i n g time
8 disp ( awt ) ;
9
10 disp ( ” A v e r a g e Turnaround Time u s i n g RR= ” ) ; //
D i s p l a y i n g A v e r a g e Turnaround t i m e
11 disp ( atat ) ;
12 endfunction
Display Function of Round Robin
51
Scilab code AP 10
1 // SJF S c h e d u l i n g f u n c t i o n
2
3 function [ tat , wait_time ]= shortestjobfirst ( num , btime ,
wtime , tatime )
4 total =0; // t o t a l w a i t i n g t i m e
5 total2 =0;
6 n = num ;
7 ptime = btime ;
8 process =[1 2 3 4]; // p r o c e s s i d
9 fd = %io (2) ;
10
11 for i =1:1: n -1 // s o r t i n g t h e p r o c e s s e s i n t e r m s o f
process times
12 for j = i +1:1: n
13 if ( ptime ( i ) > ptime ( j ) )
14 temp = ptime ( i ) ;
15 ptime ( i ) = ptime ( j ) ;
16 ptime ( j ) = temp ;
17 temp = process ( i ) ;
18 process ( i ) = process ( j ) ;
19 process ( j ) = temp ;
20 end
21 end
22
23 end
24
25 wtime (1) = 0;
26 for i =2:1: n
27 wtime ( i ) = wtime (i -1) + ptime (i -1) ; // w a i t t i m e
o f a p r o c e s s i s sum o f w a i t t i m e o f p r o c e s s
b e f o r e i t and p r o c e s s t i m e o f p r o c e s s b e f o r e
it
28 total = total + wtime ( i ) ; // f i n d i n g
t o t a l waiting time
29 end
30
31 tatime (1) = 0;
32 for i =1:1: n
52
33 tatime ( i ) = ptime ( i ) + wtime ( i ) ; // t u r n a r o u n d
t i m e=b u r s t t i m e +w a i t t i m e
34 total2 = total2 + tatime ( i ) ; // t o t a l
turn around time
35 end
36
37 avg1 = total2 / n ; // f i n d i n g
average time
38
39 for i =1:1: n
40 mfprintf ( fd , ’ P%d i s %d ’ , process ( i ) , tatime ( i ) )
;
41 end
42
43 mfprintf ( fd , ’ \n A v e r a g e Turn−Around Time i n SJF
%. 2 f ’ , avg1 ) ;
44
45 endfunction
SJF algorithm for Turn Around Time and Average Turn Around Time cal-
culation
Scilab code AP 11
1 // Round Robin S c h e d u l i n g f u n c t i o n
2
3 function [ tat , wait_time ]= roundrobin ( num , btime , wtime ,
tatime )
4 b =0;
5 t =0;
6 n = num
7 q =5; // quantum t i m e
8 wtime = zeros (1 , n ) ; // w a i t i n g t i m e
9 fd = %io (2) ;
10 rtime = btime // b u r s t t i m e
11
12 // For l o o p : r u n n i n g t h e p r o c e s s e s f o r s p e c i f i e d
quantum
13
14 for i =1:1: n // r u n n i n g t h e p r o c e s s e s f o r 1
53
quantum
15 if ( rtime ( i ) >= q )
16 for j =1:1: n
17 if ( j == i )
18 rtime ( i ) = rtime ( i ) -q ; // s e t t i n g
the remaining time i f i t i s the
process scheduled
19 else if ( rtime ( j ) >0)
20 wtime ( j ) = wtime ( j ) + q ; //
incrementing wait time i f i t
i s not the p r o c e s s s c h ed u l ed
21 end
22 end
23 end
24 else if ( rtime ( i ) >0)
25 for j =1:1: n
26 if ( j == i )
27 rtime ( i ) =0; // a s t h e
remaining time i s l e s s than
quantum i t w i l l run t h e p r o c e s s
and end i t
28 else if ( rtime ( j ) >0)
29 wtime ( j ) = wtime ( j ) + rtime ( i ) ;
// i n c r e m e n t i n g w a i t t i m e i f
i t i s not the p r o c e s s
scheduled
30 end
31 end
32 end
33 end
34 end
35 end
36 for i =1:1: n
37 if ( rtime ( i ) >0) // i f r e m a i n i n g t i m e i s l e f t
set flag
38 flag =1;
39 end
40 end
54
41 while ( flag ==1) // i f f l a g i s s e t run t h e
above p r o c e s s a g a i n
42 flag =0;
43 for i =1:1: n
44 if ( rtime ( i ) >= q )
45 for j =1:1: n
46 if ( j == i )
47 rtime ( i ) = rtime ( i ) -q ;
48 else if ( rtime ( j ) >0)
49 wtime ( j ) = wtime ( j ) + q ;
50 end
51 end
52 end
53 else if ( rtime ( i ) >0)
54 for j =1:1: n
55 if ( j == i )
56 rtime ( i ) =0;
57 else if ( rtime ( j ) >0)
58 wtime ( j ) = wtime ( j ) + rtime (
i);
59 end
60 end
61 end
62 end
63 end
64 end
65 for i =1:1: n
66 if ( rtime ( i ) >0)
67 flag =1;
68 end
69 end
70 end
71
72 // For l o o p : c a l c u l a t i n g t u r n a r o u n d t i m e f o r e a c h
process
73 for i =1:1: n
74 tatime ( i ) = wtime ( i ) + btime ( i ) ; //By a d d i n g
w a i t i n g t i m e and b u r s t t i m e
55
75 end
76 for i =1:1: n
77 b = b + wtime ( i ) ;
78 t = t + tatime ( i ) ;
79 end
80
81 for i =1:1: n
82 mfprintf ( fd , ’ P%d i s %d ’ ,i , tatime ( i ) ) ;
83 end
84
85 // d i s p l a y i n g t h e A v e r a g e Turn−Around Time i n RR
86
87 mfprintf ( fd , ’ \n A v e r a g e Turn−Around Time i n RR
%. 2 f ’ ,t / n ) ;
88
89 endfunction
Round Robin Scheduling for Turn Around Time and Average Turn Around
Time calculation
56
13 for i =2:1: n
14 wtime ( i ) = btime (i -1) + wtime (i -1) ; // w a i t i n g t i m e
w i l l be sum o f b u r s t t i m e o f p r e v i o u s p r o c e s s
and w a i t i n g t i m e o f p r e v i o u s p r o c e s s
15 t1 = t1 + wtime ( i ) ; // c a l c u l a t i n g
t o t a l time
16 end
17
18 for i =1:1: n
19 tatime ( i ) = btime ( i ) + wtime ( i ) ; // t u r n a r o u n d
t i m e=b u r s t t i m e +w a i t t i m e
20 t2 = t2 + tatime ( i ) ; // t o t a l t u r n
around time
21 end
22
23 for i =1:1: n
24 mfprintf ( fd , ’ P%d i s %d ’ ,i , tatime ( i ) ) ;
25 end
26
27
28 mfprintf ( fd , ’ \n A v e r a g e Turn−Around Time i n FCFS
%. 2 f ’ , t2 / n ) ;
29
30 endfunction
FCFS Turn Around and Average Turn Around Calculation
Scilab code AP 13
1 // SJF S c h e d u l i n g
2
3 function [ tat , wait_time ]= shortestjobfirst ( num , btime ,
wtime , tatime )
4 total =0; // t o t a l w a i t i n g t i m e
5 n = num ;
6 ptime = btime ;
7 process =[1 2 3 4]; // p r o c e s s i d
8 fd = %io (2) ;
9
57
10 for i =1:1: n -1 // s o r t i n g t h e p r o c e s s e s i n t e r m s o f
process times
11 for j = i +1:1: n
12 if ( ptime ( i ) > ptime ( j ) )
13 temp = ptime ( i ) ; // a s s i g n i n g Temporary
variable for sorting
14 ptime ( i ) = ptime ( j ) ;
15 ptime ( j ) = temp ;
16 temp = process ( i ) ;
17 process ( i ) = process ( j ) ;
18 process ( j ) = temp ;
19 end
20 end
21
22 end
23
24 wtime (1) = 0;
25 for i =2:1: n
26 wtime ( i ) = wtime (i -1) + ptime (i -1) ; // w a i t t i m e
o f a p r o c e s s i s sum o f w a i t t i m e o f p r o c e s s
b e f o r e i t and p r o c e s s t i m e o f p r o c e s s b e f o r e
it
27 total = total + wtime ( i ) ; // f i n d i n g
t o t a l waiting time
28 end
29
30 avg = total / n ; // f i n d i n g
average time
31
32 for i =1:1: n
33 mfprintf ( fd , ’ P%d i s %d ’ , process ( i ) , wtime ( i ) ) ;
34 end
35
36 mfprintf ( fd , ’ \n A v e r a g e W a i t i n g Time i n SJF i s
%. 2 f ’ , avg ) ;
37
38 endfunction
58
SJF for Turn Around Time and Average Turn Around Time calculation
59
27 wtime ( j ) = wtime ( j ) + rtime ( i ) ;
// i n c r e m e n t i n g w a i t t i m e i f
i t i s not the p r o c e s s
scheduled
28 end
29 end
30 end
31 end
32 end
33 end
34 for i =1:1: n
35 if ( rtime ( i ) >0) // i f r e m a i n i n g t i m e i s left
set flag
36 flag =1;
37 end
38 end
39
40 // T o t a l w a i t i n g t i m e , processes average waiting
time c a l c u l a t i o n
41 while ( flag ==1) // i f f l a g i s s e t run t h e
above p r o c e s s a g a i n
42 flag =0;
43 for i =1:1: n
44 if ( rtime ( i ) >= q )
45 for j =1:1: n
46 if ( j == i )
47 rtime ( i ) = rtime ( i ) -q ;
48 else if ( rtime ( j ) >0)
49 wtime ( j ) = wtime ( j ) + q ;
50 end
51 end
52 end
53 else if ( rtime ( i ) >0)
54 for j =1:1: n
55 if ( j == i )
56 rtime ( i ) =0;
57 else if ( rtime ( j ) >0)
58 wtime ( j ) = wtime ( j ) + rtime (
60
i ) ; // U p d a t i o n o f
waiting time f o r each
process
59 end
60 end
61 end
62 end
63 end
64 end
65 for i =1:1: n
66 if ( rtime ( i ) >0)
67 flag =1;
68 end
69 end
70 end
71
72 // d i s p l a y i n g t h e w a i t i n g t i m e i n RR
73
74 for i =1:1: n
75 mfprintf ( fd , ’ P%d i s %d ’ ,i , wtime ( i ) ) ;
76 end
77
78 // C a l c u l a t i n g A v e r a g e W a i t i n g Time
79 for i =1:1: n
80 b = b + wtime ( i ) ;
81 t = t + tatime ( i ) ;
82
83 end
84
85 // d i s p l a y i n g t h e A v e r a g e w a i t i n g t i m e i n RR
86
87 mfprintf ( fd , ’ \n A v e r a g e W a i t i n g Time i n Round
Robin i s %. 2 f ’ ,b / n ) ;
88
89 endfunction
Round Robin Scheduling for Waiting and Average Waiting Time calculation
61
Scilab code AP 15 1 function [ tat , wait_time ]=
firstcomefirstserve ( num , btime , wtime , tatime ) //
F u n c t i o n d e f i n t i o n o f f i r s t come f i r s t s e r v e
2
3 t1 =0; // i n t i a l i z i n g t i m e t 1 =0 f o r
t o t a l waiting time c a l c u l a t i o n
4 t2 =0; // i n t i a l i z i n g t i m e t 2 =0 f o r
t o t a l t u r n round t i m e c a l c u l a t i o n
5
6 btime = bt ; // assigning burst time
7 wtime = wt ; // assigning waiting time
8 tatime = tat // assigning turn around time
9 n = num ; // assigning number o f p r o c e s s n
=4 h e r e
10 fd = %io (2) ;
11
12 // For l o o p f o r c a l c u l a t i n g t o t a l w a i t i n g t i m e o f
each Process
13 for i =2:1: n
14 wtime ( i ) = btime (i -1) + wtime (i -1) ; // w a i t i n g t i m e
w i l l be sum o f b u r s t t i m e o f p r e v i o u s
p r o c e s s and w a i t i n g t i m e o f p r e v i o u s p r o c e s s
15 t1 = t1 + wtime ( i ) ; // c a l c u l a t i n g
t o t a l waiting time
16 end
17
18
19 // d i s p l a y i n g t h e w a i t i n g t i m e o f e a c h P r o c e s s
20 for i =1:1: n
21 mfprintf ( fd , ’ P%d i s %d ’ ,i , wtime ( i ) ) ;
22 end
23
24 mfprintf ( fd , ’ \n A v e r a g e W a i t i n g Time i n FCFS i s
%. 2 f ’ , t1 / n ) ;
25
26 endfunction
FCFS Waiting and Average Waiting Calculation
62
Scilab code AP 16
1 //WINDOWS 10 64−BIT OS , S c i l a b and
toolbox versions 6 . 1 . 0 .
2
3 // l o a d i n g t h e n e c e s s a r y f u n c t i o n s
4 function [ tat , wait_time ]= shorestjobfirst ( pid , num , pt ,
wt , tat ) // F u n c t i o n d e f i n t i o n o f f i r s t come
f i r s t serve
5
6 process = pid ; // p r o c e s s i d
7 n = num ; // number o f p r o c e s s e s
8 ptime = pt ; // p r o c e s s t i m e o r b u r s t t i m e
9 tatime = tat ; // t u r n a r o u n d t i m e
10 wtime = wt ; // w a i t i n g t i m e
11 fd = %io (2) ;
12
13 // D e t e r m i n i n g t h e number o f p r o c e s s e s and b l o c k s
14 size_process = size ( process ) ;
15 size_process = size_process (2) ;
16 size_ptime = size ( ptime ) ;
17 size_ptime = size_ptime (2) ;
18
19 // marks t h e p o s i t i o n o f p r o c e s s w i t h minimum b u r s t
t i m e i n t h e s p e c i f i e d r a n g e . T h i s may be u s e d t o
rearrange the order of the p r o c e s s e s to achieve
p r o p e r SJF s c h e d u l i n g . . .
20 for i =1:1: n -1 // For l o o p f o r s o r t i n g t h e p r o c e s s e s
in terms of p r o c e s s times
21 for j = i +1:1: n
22 if ( ptime ( i ) > ptime ( j ) )
23 temp = ptime ( i ) ; // t e m p o r a r y
v a r i a b l e used to enable e f f i c i e n t
swapping o f v a l u e s . .
24 ptime ( i ) = ptime ( j ) ;
25 ptime ( j ) = temp ;
26 temp = process ( i ) ;
27 process ( i ) = process ( j ) ;
28 process ( j ) = temp ;
29 end
63
30 end
31
32 end
33
34 wtime (1) = 0;
35 // w a i t i n g t i m e c a l c u l a t i o n
36 for i =2:1: n
37 wtime ( i ) = wtime (i -1) + ptime (i -1) ; // w a i t t i m e
o f a p r o c e s s i s sum o f w a i t t i m e o f p r o c e s s
b e f o r e i t and p r o c e s s t i m e o f p r o c e s s b e f o r e
it
38 total = total + wtime ( i ) ; // f i n d i n g
t o t a l waiting time
39 end
40
41 // t o t a l t u r n a r o u n d t i m e c a l c u l a t i o n
42 for i =1:1: n
43 tatime ( i ) = ptime ( i ) + wtime ( i ) ; // t u r n a r o u n d
t i m e=b u r s t t i m e +w a i t t i m e
44 total2 = total2 + tatime ( i ) ; // t o t a l
turn around time
45 end
46
47 avg = total / n ; // f i n d i n g
a v e r a g e time , a v e r a g e w a i t i n g t i m e c a l c u l a t e d by
d i v i d i n g t o t a l w a i t i n g t i m e by number o f p r o c e s e s
48 avg1 = total2 / n ; // a v e r a g e
t u r n a r o u n d t i m e c a l c u l a t e d by d i v i d i n g t o t a l
t u r n a r o u n d t i m e by number o f p r o c e s s e s
49
50 display ( process , size_process , wtime , tatime , avg , avg1 ) ;
// d i s p l a y i n g t h e p r o c e s s and b l o c k
a l l o c a t i o n by f i r s t f i t a r r a y
51 endfunction
SJF New
64
Scilab code AP 17 1 //WINDOWS 10 64−BIT OS , S c i l a b and
toolbox versions 6 . 1 . 0 .
2 // D i s p l a y F u n c t i o n : I t p r i n t s a l l r e q u i r e d d e t a i l s
s u c h a s P r o c e s s no .
3 // W a i t i n g time , Turn−Around time , A v e r a g e W a i t i n g
t i m e and A v e r a g e Turn−Around t i m e
4
5 function display ( process , size_process , wtime , tatime ,
avg , avg1 )
6 // d i s p l a y o f f i n a l v a l u e s
7
8 for i =1:1: n
9 mfprintf ( fd , ’ P%d ’ , process ( i ) ) ; //
D i s p l a y i n g s o r t e d P r o c e s s b a s e d on i t s
S h o r t e s t Job
10 end
11
12 disp ( ’ W a i t i n g t i m e o f e a c h P r o c e s s u s i n g SFJ ’ ) ; //
d i s p l a y i n g the Waiting time
13 for i =1:1: n
14 mfprintf ( fd , ’ P%d i s %d ’ , process ( i ) , wtime ( i ) ) ;
15 end
16
17 disp ( ’ Turn−Around t i m e o f e a c h P r o c e s s u s i n g SFJ ’ ) ;
// d i s p l a y i n g t h e Turn−Around t i m e
18 for i =1:1: n
19 mfprintf ( fd , ’ P%d i s %d ’ , process ( i ) , tatime ( i ) )
;
20 end
21
22 mfprintf ( fd , ’ \n A v e r a g e W a i t i n g Time u s i n g SJF i s
%. 2 f ’ , avg ) ; // d i s p l a y i n g t h e A v e r a g e W a i t i n g
time
23 mfprintf ( fd , ’ \n A v e r a g e Turn−Around Time u s i n g SFJ
i s %. 2 f ’ , avg1 ) ; // d i s p l a y i n g t h e A v e r a g e Turn−
Around t i m e
24
25 endfunction
65
Display Function SJF new
66
24
25 // d i s p l a y i n g t h e w a i t i n g t i m e o f e a c h P r o c e s s
26 for i =1:1: n
27 mfprintf ( fd , ’ P%d i s %d ’ ,i , wtime ( i ) ) ;
28 end
29
30 disp ( ’ Turn−Around Time o f e a c h P r o c e s s ’ ) ;
// d i s p l a y i n g t h e f i n a l Turn−Around t i m e o f
each Process
31 for i =1:1: n
32 mfprintf ( fd , ’ P%d i s %d ’ ,i , tatime ( i ) ) ;
33 end
34
35 mfprintf ( fd , ’ \n A v e r a g e W a i t i n g Time i s %. 2 f ’
, t1 / n ) ; // d i s p l a y i n g t h e A v e r a g e
waiting time
36 mfprintf ( fd , ’ \n A v e r a g e Turn−Around Time i s %
. 2 f ’ , t2 / n ) ; // d i s p l a y i n g t h e A v e r a g e Turn
Around t i m e
37
38 endfunction
First Come First Serve CPU Scheduling
67