You are on page 1of 33

222:

( Operating Systems: Internals and


Design Principles, 6/E, William Stallings)

.
:

.
.
.
.

.

.
.. UNIX, Linux,
Solaris Windows.
2


(deadlock)

.

,
,
.
M
(resources)
, , KME, .
,
3
.



C
&B


D
&A


B
&C


A
&B

P Q


A B

.



P


,


.
8

1
M
,

:
(preemptable)

, .. .
(nonpreemptable)

, ..
( ).
10

2
M
, :
(reusable), .. KME,
/, ,
,
.
(consumable), .. , ,
, ,
.

K

11
.





D and
T.


.

12

13




200Kbytes
:
P1

P2

...

...

Request 80 Kbytes;

Request 70 Kbytes;

Request 60 Kbytes;

Request 80 Kbytes;

...

...


,
.
14





( Receive
).

15


(resource
allocation graph)


.

16

H .

.

H (hold and
wait).


.

H .


.

17


,
, :
H (circular wait).

,
.

O
.
H
. H


,
.
18

19

20




:
.
(prevention),
.
(avoidance)
.
(detection)
(recovery).
21


.
:

.
.
.

.

.
.. UNIX, Linux,
Solaris Windows.
22

.
.

. H

(crash)
.
O





.
T


Unix
.
23

.
:
.

.
.
.

.


.
.. UNIX,
Linux, Solaris Windows.

24




.

:
:

.
: .
25


(.. KME,
)
.
,

,

,
.

26



.
A ,
, ,

.
H :

.
O

.
A
,
. 27

(
):

.. ,

.
,
, ..
.


/ (.. KME) ..
/ (.. ,
!).

28




(
).
P1
R3 R4 P2
R4 R3,
R4>R3. .


.

29

.
:
.
.

.
.

.

.
.. UNIX, Linux,
Solaris Windows.
30

H





.
O ,
,
.

.

.
31




.



.

32





.
H


.
33

(bankers algorithm),
Dijkstra 1965. O

.

.
M

.
.

per se
.
34




.
,
, ;

Amount of
Existing
Resources

Resources
available
after
35
allocation


,


;


,
:
Cij - Aij Vj, j

P1,
1 R1 2
R1, 2 R2 2
R3.
36

P2

R3 P2,


.
P2, P1.
Note P2 is
completed

37


P1

P3.

38


P3

39

P1
1
R1 1

R3,

40

P1

,
1 R1
.
P1
.

.
P1
R1 R3
.

,

.

41



:
.
.


:
, .
,
.
42

struct
int
int
int
int
}

state {
resource[m];
available[m];
claim[n][m];
alloc[n][m];

/* global data structures */

/* resource allocation algorithm */


if (alloc [i,*] + request [*] > claim [i,*])
< error >; /* total request > claim*/
else if (request [*] > available [*])
< suspend process >;
else { /* simulate alloc */
< define newstate by:
alloc [i,*] = alloc [i,*] + request [*];
available [*] = available [*] - request [*] >;
}
if (safe (newstate))
< carry out allocation >;
else {
< restore original state >;
< suspend process >;
}

43

:

boolean safe (state S)
{
int currentavail[m];
process rest[<number of processes>];
currentavail = available;
rest = {all processes};
possible = true;
while (possible) {
<find a process Pk in rest such that
claim [k,*] alloc [k,*] <= currentavail;>
if (found) { /* simulate execution of Pk */
currentavail = currentavail + alloc [k,*];
rest = rest - {Pk};
}
else possible = false;
}
return (rest == null);
}
44

(

)
:
O
.
O
.
O ,
.
O
.



.
45


.
:
.
.
.

.

.

.
.. UNIX, Linux,
Solaris Windows.
46


O

.
O


.
H

KME.

47




.
, Q:
Qij
j i.


.
48

1
1.
0
.
2. W
.
3. i i
ith
Q W:
Qik Wk 1 k m.
, .

49

2
4. :
i

W.
, Wk = Wk + Aik, 1 k m

5. 3.

,

50
.




.

,
.

.

,
.

, .
51

52



P4
.
W = (0,0,0,0,1).
P3 W,
P3 :
W = W +(0,0,0,1,0) = (0,0,0,1,1).


Q W,
.

P1 P2
.
53

1
A,
.


.


(checkpoints).
H

.
54

2

. H

.

.


. H

. K

.
55

3

,


:

KME,
,
,
,
.
56

57


.
:

.
.
.
.


.
.
.. UNIX, Linux,
Solaris Windows.
58

M
,

. :
.
X .
X .

O (
) :
(
).
,
().
( ).
E/E ( ).

59

.
:

.
.
.
.

.


.
.. UNIX, Linux,
Solaris Windows.

60

61


T

. Dijkstra
1965.

.

( ).

( ).

( (!)
62
).


void philosopher (int i)
{
while (1) {
think();
semWait(fork[i]);
semWait(fork [(i+1) mod 5]);
eat();
semSignal(fork [(i+1) mod 5]);
semSignal(fork[i]);
}
}

semaphore fork[5] = {1,1,1,1,1};


int i;
void main()
{
parbegin
philosopher(0);
philosopher(1);
philosopher(2);
philosopher(3);
philosopher(4);
parend
}

.
, .

.

.

63


void philosopher (int i)
{
while (1) {
think();
semWait(room);
semWait(fork[i]);
semWait(fork [(i+1) mod 5]);
eat();
semSignal(room);
semSignal(fork [(i+1) mod 5]);
semSignal(fork[i]);
}
}

semaphore fork[5] = {1,1,1,1,1};


room = 4;
int i;
void main()
{
parbegin
philosopher(0);
philosopher(1);
philosopher(2);
philosopher(3);
philosopher(4);
parend
}


,
.

room
.

64

1
monitor dining_controller;
{
cond ForkReady[5]; /* condition variable for synchronization */
boolean fork[5] = {true}; /* availability status of each fork */
void get_forks(int pid) /* pid is the philosopher id number */
{
int left = pid;
int right = (++pid) % 5;
/*grant the left fork*/
if (!fork(left)
cwait(ForkReady[left]); /* queue on condition variable */
fork(left) = false;
/*grant the right fork*/
if (!fork(right)
cwait(ForkReady(right); /* queue on condition variable */
fork(right) = false:
}
65

2
void release_forks(int pid)
{
int left = pid;
int right = (++pid) % 5;
/*release the left fork*/
if (empty(ForkReady[left]) /*no one is waiting for this fork */
fork(left) = true;
else /* awaken a process waiting on this fork */
csignal(ForkReady[left]);
/*release the right fork*/
if (empty(ForkReady[right]) /*no one is waiting for this fork */
fork(right) = true;
else /* awaken a process waiting on this fork */
csignal(ForkReady[right]);
}
}
/* end of monitor code */

66

3
/* main program */
void philosopher(i) /* the five philosopher clients */
{
while (1)
{
think();
dining_controller.get_forks(i); /* client requests two forks */
eat();
dining_controller.release_forks(i); /* client releases forks */
}
}
void main()
{
parbegin
philosopher(0);
philosopher(1);
philosopher(2);
philosopher(3);
philosopher(4);
parend
}

67


,
,
.

boolean
.
get_forks

.
,

.
, release_forks

.

68

.
:

.
.
.
.

.

.. UNIX,
Linux, Solaris Windows.
69


UNIX


UNIX :
(pipes).
.
.
.
.
70



,
---.
:
:
.
:
.
71

bytes
.
UNIX
msgsnd
msgrcv .


.
72




.


,
.

.
73

SVR4
semWait
semSignal.


.

74


.
,
,
.

.
bit (
)
.

( )
.
,
.
75


UNIX SVR4

76


Linux

UNIX, :
.
(spinlocks).
(
SVR4).
(barriers).

77



.
:

.
bits.

78


Linux

79

bits Linux

80


.


.
, ,

.
0, 1
.
0,
.



(
).
81

Linux

82

SVR4,


.

,

.
Linux
:
.
.
-.

83

Linux

84

85


Solaris

SVR4, :
(mutex).
.

.
.

86


Solaris

87

mutex


.

mutex .

88


-
Solaris
.

-




.
89






.


mutex.
90


Windows
Windows


:

(executive dispatcher objects).
.
-.
.
91

wait
wait
.

wait

.
wait,

, .
.

92

93



mutex.


.





.
94


Windows Vista
.

.

95

Windows Vista .

CONDITION_VARIABLE
InitializeConditionvariable.

-.
,

:

SleepConditionVariableCS.
SleepConditionVariableSRW.
WakeConditionVariableCS.
WakeConditionVariableSRW.
96

Windows Linux 1

97

Windows Linux 2

98

You might also like