Os Notes
Os Notes
1 2
1 2
3 4
3 4
1
2/16/23
5 6
5 6
7 8
7 8
2
2/16/23
9 10
11 12
11 12
3
2/16/23
13 14
15 16
15 16
4
2/16/23
17 18
17 18
19 20
19 20
5
2/16/23
21 22
21 22
– boolean flag[2];
• Hardware Approach: /* which process is ready to enter critical section */
23 24
6
2/16/23
25 26
25 26
27 28
27 28
7
2/16/23
29 30
31 32
31 32
8
2/16/23
33 34
33 34
35 36
35 36
9
2/16/23
OS and PL Approaches
• Semaphore – Disadvantages
– Deadlock
• Say, there are two semaphores, S and Q
USAGE OF SEMAPHORES
IN
CLASSICAL PROBLEMS OF SYNCHRONIZATION
– Starvation
• Processes are stored in LIFO order
37 38
37 38
39 40
10
2/16/23
41 42
41 42
43 44
11
2/16/23
45 46
47 48
47 48
12
2/16/23
Monitors Monitors
• Monitor is a software module • Chief characteristics
– Same as semaphore – simple to implement. – Local data variables are accessible only by the monitor
• Abstract Data Type – Process enters monitor by invoking one of its
– Class procedures
– Hide (Restrict the access to) the shared data – Only one process may be executing in the monitor at a
time
– Access through procedures / functions
49 50
49 50
Monitors Monitors
• monitor class Account {
• Syntax private int balance := 0
monitor monitor-name public method boolean withdraw(int amount) precondition amount >= 0
{ { BUSY WAITING ?
shared variable declarations if balance < amount then
procedure body P1 (…) { return false
...
else {
}
balance := balance - amount ;
procedure body P2 (…) {
... return true
} }
procedure body Pn (…) { }
... public method deposit(int amount) precondition amount >= 0
} {
{ balance := balance + amount
initialization code
}
}
} 51
} 52
51 52
13
2/16/23
53 54
53 54
55 56
14
2/16/23
57 58
59 60
59 60
15
2/16/23
61 62
63 64
16
2/16/23
65 66
65 66
17