ls chapters 123 new
TRANSCRIPT
-
8/14/2019 LS Chapters 123 New
1/143
,
/ ,
&
2004
-
8/14/2019 LS Chapters 123 New
2/143
! ! , . !
76
-
8/14/2019 LS Chapters 123 New
3/143
1 ............................................................................................................ 8 1.1 & ............................... 9 1.2 .....................................................................10 1.3 ...................................................................................................................12
1.3.1 (Buffering)....................................................................13 1.3.2 (Spooling) ........................................................................................................13 1.3.3 - (Multiprogramming)........................................................................14 1.3.4 (Time Sharing)...................................................................................15
1.4 .................................................................................................................................16 1.4.1 / .......................................................................................................................16 1.4.2 ...................................................................................................................17 1.4.3 .................................................................................17
1.5 ..................................................................18
1.6
&
..................................21 1.7 .........................................................................................................21 1.8 ..........................................24
1.8.1 ............................................................................................24 1.8.2 ..........................................................................................................24 1.8.3 ..................................................................................................................................25 1.8.4 .................................................................................................................27 1.8.5 goto.........................................................................................................................28 1.8.6 ..................................................................................................................28 1.8.7 Typedef ..................................................................................................................................29 1.8.8 ...........................................................................................................................29 1.8.9 ..........................................................................................................................29
2 ............................30 2.1 .........................................................................................................31 2.2 ..........................................................................................................31 2.3 .............................................................................................32 2.4 .....................................................................................................33 2.5 ...................................................................................................................................34 2.6 ................................................................................................................39 2.7 .........................................................................................43
2.7.1 (First Come First Served, FCFS) .............43 2.7.2 (Round Robin, RR) ........................................................................................44 2.7.3 (Priority).....................................................................................................45 2.7.4 ..............................................................46
3 & ..........................58
3.1 ........................................................................................59 3.2 ........................................................................................................69 3.3 .......................................................................73 3.4 ........................................................................................75 3.5 ......................................................................................82
3.5.1 ......................................................................................83 3.5.2 Peterson..................................................................................................................90
3.6 ................................................................................................................................99 3.7 ....................................................................105
3.7.1 ................................................................................................................105 3.7.2 ( 6, , 2003).............113 3.7.3 ( 3 , , 2001) ..............................117 3.7.4 - ........................................................................119
-
8/14/2019 LS Chapters 123 New
4/143
3.7.5 ...................................................................................................121 3.7.6 ..........................................................................................................125
3.8 ................................................125
3.9 ..................................................................................................................133 3.9.1 Lamport (Bakery Algorithm)................................................................................133 3.9.2 Dekker ..................................................................................................................135 3.9.3 ...........................................................................................................................136 3.9.4 ....................................................................................................................139
-
8/14/2019 LS Chapters 123 New
5/143
1: ............................................... 9 2: ............................................................................................12 3: .....18 4: . ..........................................................................................32 5: PCB ..............................................................................................................33 6: . ................................................................................................................37 7: . ......................................................................................................40 8: . ...............................................41 9:
.............................................................................................................................................43 10: RR........................................................52 11: GANNT 6.
.............................................................................................................................................................54 12: 1 ..............................................................61 13: 1 : .......62 14: 2 ..............................................................63 15: . .......................................................................64 16: TALLY . ...68 17: ......................................................................................70 18: 1000 500....72 19: . ........................................74 20: . ..........................................75 21: TEST &S ET()...............................................................................................................76 22: TEST &S ET()...76 23: 0 1 TEST &S ET()
...............................................................77 24: TEST &S ET()
...........................................................................................................................78 25: DEPOSIT ()................................................................................................79 26: WITHDRAW ()...........................................................................................79 27: 1
TEST &S ET(). .........................................................................................................80 28: FETCH &A DD ()...........................................................................................................81 29: SWAP ().......................................................................................................................81 30: RMW()......................................................................................................................82 31:
......................................................................................................................................83 32: 1
...........................................................................................................................84 33:
......................................................................................................................................84 34: 2 .....85 35: ( )
. .......................................................................................86 36: 3 ...87 37:
......................................................................................................87 38: PETERSON . ..............................................................................................................90 39:
......................................................................................................92 40:
......................................................................................................................................93
-
8/14/2019 LS Chapters 123 New
6/143
41: 5 ...........................................................................................................................95
42: PETERSON ................................................95
43: PETERSON ......................................................................................................96 44:
......................................................................................................................................97 45:
......................................................................................................................................97 46: . 98 47:
............................................................................................................................................98 48: UP () DOWN () ........................................100 49: . ....................................100 50: .............................................................................101 51: . ...............................................................102 52:
TEST &S ET() ...........................................104 53: . ..................107 54: - . .......................................................109 55: - . .................................110 56: - ........................................................110 57: - . ....................................................................112 58: PING -PONG ................................................................................................................................113 59: ..114 60: ............................................................114 61: . ..................................115 62: .................................116 63: . ..................................................117 64: ....................118 65: . .........................................................................................119 66: - . .....................119 67: - .....................................................................120 68: . ........................................................................121 69: 44. ..............123 70:
45...................................................................................................................125 71: HANSEN . ...........................................................................................126 72: DEPOSIT () WITHDRAW ()
HANSEN . ...................................................................................................... 127 73: . ......................................................127 74:
( 60)........................................................................................129 75:
( 61)...............................................129 76:
( 62)...............................................130 77: 76
. .............................................................................................................................131 78: -
...................................................................................................131 79: -
. ...................................................................................................................................132 80: . .........................................................................................134 81: DEKKER . ...............................................................................................................135 82: TEST &S ET()...........137
-
8/14/2019 LS Chapters 123 New
7/143
83: - . ................................................139 84: SEM _DOWN () GSEM _UP ()...............................141 85: . ................................................141
86: . ....................................................................................................................................143
-
8/14/2019 LS Chapters 123 New
8/143
1
-
8/14/2019 LS Chapters 123 New
9/143
1
9
1.1 &
() (user interface) (hardware) . (1) , (2) ,(3) , (4) . 1.
1: .
, ( CPU), / ( / ) . , , , , , ., , , .
. (. ., , , , , , / , .) (. ., , , , .). , (. ., ).
. , .
1 2 3
&
-
8/14/2019 LS Chapters 123 New
10/143
1
10
. , . . .
. . , .
, , . , , . , .
, . , .
1.2
( ) . . . , . . (1 ) . . .
(system clock)
. .
. 32 64 bit. . (Program Counter, PC). .
chip (
-
8/14/2019 LS Chapters 123 New
11/143
1
11
), . , bytes. , , . , . ( ). , . . , .
( ) (system bus).
(. ., ).
. . , . . , . 2.
, . ., , , , . . . ( ) ( ). , (main memory). , . ( ). , ( , ). . .
, . . , . , . 2. , ( ).
-
8/14/2019 LS Chapters 123 New
12/143
1
12
2:
( ) , . , ( ). , . , ( - ). , . , , .
1.3
. ( 100%). , , , .
/ . / . / , (. ., ). . / . .
( , , )
( )
-
8/14/2019 LS Chapters 123 New
13/143
1
13
1.3.1 (Buffering)
. . / , ( ) (buffer) , . , / . . . / . , / ,.
bytes. . / . . / ( ). , / . , . 2.
/ . . . ., , ( / ), ( / ). ,
/ . , , / . ( ) .
1.3.2 (Spooling) (Spooling Simultaneous Peripheral Operation On Line) . .
-
8/14/2019 LS Chapters 123 New
14/143
1
14
, / . , / .
, / (offline). . ., . . , . ., . / . , /
. / .
. .
1.3.3 - (Multiprogramming) 100% . ,
- . . / , . ( ). , . / , , . .
. . , - ( ), .
. , . , . ( ).
-
8/14/2019 LS Chapters 123 New
15/143
1
15
. .
1.3.4 (Time Sharing) (interactive) online . . , , , , . / , , , , , ..
, , . (switching) . , , . , , .
,
. , . , , ( -) .
:
/ . (. .,
) .
, . ( , batch systems) . ( ) , .
. , ( )
-
8/14/2019 LS Chapters 123 New
16/143
1
16
. , .
1.4
. . , (
). , , , ( / ) , . , .
1.4.1 / / , . , , . / . , (supervisor mode), (usermode), . , bit. bit 0 , ( ). / ,
. . (privileged instructions).
/ , . . , / , ,
-
8/14/2019 LS Chapters 123 New
17/143
1
17
. , . .
, . , , . , , . , /
. / , , . , - ( ). , , .
1.4.2 , . . . , , .
,
.
1.4.3 . ( ), , . , , .
-
8/14/2019 LS Chapters 123 New
18/143
1
18
. , . , . , . ., , , .. .
, , (base register) (limitregister). ,
, . , . ( ) . , , . , . , (trap). . 3. ,
.
3: .
1.5
. 2. . :
( , ).
+
< <
-
8/14/2019 LS Chapters 123 New
19/143
1
19
, . .
.
. , , , , . , , , , . , ,
, . , . ( , , ). , . 2 3.
. :
, .
. ,
, .
4.
, , . , .
. (. ., ). , . ., , CDs, , . . , . , , , ,
-
8/14/2019 LS Chapters 123 New
20/143
1
20
, , ..
/ . , / . , , (device driver). / . , , .
. , . . , .
. , , . . Windows Windows. , . .
. / . :
. . .
. . / . , ..
(assembly). ( , ).
-
8/14/2019 LS Chapters 123 New
21/143
1
21
1.6 &
. , . , . , ( ), ( , ,
), , ..
. . . , .
.
. , . ., ( ), ( ), ..
1.7
1 ( 1, 4 . 2001-2002)
1. :
)
)
)
2. (i, ii iii) . .
) .
) .
-
8/14/2019 LS Chapters 123 New
22/143
1
22
) .
) .
3. ;
) .
) .
)
)
)
) / .
1. :
) .
/ .
.
.
) .
. . ,
.
) .
.
2. , . , . , . . , , ,
-
8/14/2019 LS Chapters 123 New
23/143
1
23
, ( ). , :
. , .
. , .
.
, . . .
. ( . editor, help browser, , ), .
3.
.)
( ).
) .
) / , , .
) . , .
) , . .
-
8/14/2019 LS Chapters 123 New
24/143
1
24
) / / .
1.8
. . .
1.8.1
:
int num;
num num . , boolean. boolean flag; flag boolean. boolean , TRUE FALSE. boolean , 0 FALSE TRUE.
. . .,
int num = 5;
num, num 5.
1.8.2 . . .,
num = 1;
1 num.
num = num +1;
num 1.
num++;
num += 1;
-
8/14/2019 LS Chapters 123 New
25/143
1
25
+ , , * / . +.
1.8.3 repeat . (repeat, begin, end, until), until, :
repeatbegin
;end
until < >;
begin, end repeat. repeat . , until . , . , repeat ( until). boolean , == , != ( ), , , .
while.while (< >) do
begin;
end
while TRUE, . while . , . , .. .
repeat while , . ., ,
, . ., repeat while. ( ).
repeat:
repeatbegin
;end
forever;
-
8/14/2019 LS Chapters 123 New
26/143
1
26
( , repeat ). repeat:
repeatbegin
;end
until FALSE;
FALSE ( ), repeat repeat .
while:
while (TRUE) dobegin
;end
H repeat, while . , TRUE. , FALSE. , not < >. , TRUE, 0 FALSE .
, begin, end .
, repeat, while:
repeat noop; until < >;
while (< >) do noop;
noop ( ). , , . , . , repeat while (
), TRUE FALSE, .
1
:
int num = 0;
while (num < 3)num = num+1;
print num;
-
8/14/2019 LS Chapters 123 New
27/143
1
27
. num 0. , (num < 3) TRUE while, num 1. , . 1 < 3 TRUE. , num 2. TRUE. num 3 . , FALSE ( 3 < 3). , while , print num;. 3 .
, repeat while;
int num = 0;
repeatnum = num + 1
until (num >= 3);print num;
.
repeat, while. while TRUE, repeat TRUE ( FALSE). repeat, while , . ,
repeat , while ( , , , while FALSE, ).
, , for,break, continue, .. ( ).
1.8.4 if . :
if (< >) thenbegin
end
, ( ) if. , . if repeat while .
if .
-
8/14/2019 LS Chapters 123 New
28/143
1
28
if else :
if (< >) thenbegin;
endelse
begin;
end
. TRUE 1, 2.
1.8.5 goto goto , goto :
goto part1;
, part1. part1, :.
part1: num = num +1;
goto part1. , goto .
1.8.6
int array[4];
4 array. array 4 . array[0], o array[1], array[2], 4 array[3]. , n , k- , 0< k n, array[k-1].
(structure) . ( , ). . :
struct 2fieldsbegin
int a;boolean flag;
end
-
8/14/2019 LS Chapters 123 New
29/143
1
29
2fields , a boolean flag. 2fields :
struct 2fields b;
b 2fields. b , b.a boolean b.flag.
1.8.7 Typedef typedef . 2fields. :
typedef struct 2fields 2FIELDS;
2FIELDS struct2fields. , :
2FIELDS b;
b 2fields.
, :
typedef int boolean;
boolean int .
1.8.8 . . 3 .
1.8.9 3 ( ).
/* */ , .
-
8/14/2019 LS Chapters 123 New
30/143
2
-
8/14/2019 LS Chapters 123 New
31/143
2
31
2.1
. ( ).
, . , . . , (
), , . . . , , , , . , , , ( , , ). .
, . , . , , .
2.2
. , ,
. . , , , (. ., / ) . , : , .
4 . . , . , / , ( / ). ,
-
8/14/2019 LS Chapters 123 New
32/143
2
32
/ , . , , , . , . .
4: .
2.3
, . ( ). , ( ) ,
, . , , . ( , , . , ).
(Process Control Block, PCB).
-
8/14/2019 LS Chapters 123 New
33/143
2
33
PCB. PCB :
. . . . ,
.
, .
(. ., , / , ).
PCB 5.
... ( )
5: PCB .
2.4
:
. . (foreground) , (background). (daemons). , . . , .
-
8/14/2019 LS Chapters 123 New
34/143
2
34
.
. , , . ., . , PCB .
- . . ., / . . .
. ,
. . , .
2.5
. :
. .
. , . ., .
, , ..
/ . / , / , , , .
, . ., restart INT.
( ). , . .
2
.
int b = 3, c = 4, p = 5;int tmp, flag = 0;
-
8/14/2019 LS Chapters 123 New
35/143
2
35
tmp = 1;while (p > 0)
begin
if (flag == 0)tmp = tmp * b;
elsetmp = tmp * c;
flag = 1 flag;p = p - 1;
endprint tmp;
. ( ) b*c*b*c*b.
, . , 28 ( ).
1. tmp = 12. while ( TRUE)3. if ( TRUE)4. tmp = 1 * 3 = 35. flag = 16. p = 47. while ( TRUE)8. if ( FALSE)9. tmp = 3 * 4 = 1210. flag = 011. p = 312. while ( TRUE)13. if ( TRUE)14. tmp = 12 * 3 = 3615. flag = 116. p = 217. while ( TRUE)18. if ( FALSE)19. tmp = 36 * 4 = 14420. flag = 021. p = 122. while ( TRUE)23. if ( TRUE)24. tmp = 144 * 3 = 432
-
8/14/2019 LS Chapters 123 New
36/143
2
36
25. flag = 126. p = 0
27. while ( FALSE)28. print 432
, . , , , .
. . ,
. ., if else while. while 2 27. 25 while. 2 3, 3 4, .. ., 26 27. , . . ., 17, , , if else.
, while TRUE, , if else ( if else 18). , , while, if. . ., 18. if else, KME.
, . (interrupt handlers). . , :
( PCB ).
.
, .
-
8/14/2019 LS Chapters 123 New
37/143
2
37
, .
6
.
6: .
3
( -) 2. ( , . ., while if else). , . :
2012 b = 3;2016 c = 4;2020 p = 5;2024 flag = 0;2028 tmp = 1;2032 label1:2036 if (p
-
8/14/2019 LS Chapters 123 New
38/143
2
38
, 2 ( 2). ( 2).
2064, . :
PC (program counter) = 2064, tmp = 3, flag = 0, p = 5, b = 3, c = 4.
, . ,
PCB . , . PCB , , . , . , .
, 2072. :
PC = 2072 , tmp = 12, flag = 0, p = 3 , b = 3, c = 4.
PC ( ) . PCB , .
4 ( , , , -11: )
. count-processes (). 100 ( 100 count-processes()) , . .
( , . ., while, repeat, for, .)
1004 print Start;
-
8/14/2019 LS Chapters 123 New
39/143
2
39
1008 i = 0;1012 old_proc = 0;1016 label1:
1020 if (i == 100)1024 goto label2;1028 new_proc = count_processes ();1032 if (new_proc > old_proc)1036 print Up;1040 else if (old_proc > new_proc)1044 print Down;1048 print new_proc;1052 old_proc = new_proc;1056 i ++;1060 goto label1:1064 label2:
1068 print End; count_processes 37, 41, 21, .
1004 ( ) 1032. 1036, . , , . , PCB :
= 1036, i = 0, new_proc = 37, old_proc = 0 , , 1052. PCB :
= 1052 , i = 0, new_proc = 37, old_proc = 0
1056,
= 1056 , i = 0, new_proc = 37, old_proc = 37
. , , .
2.6
(ready queue) . PCB
-
8/14/2019 LS Chapters 123 New
40/143
2
40
. 5 , , , 7.
7: .
. , PCB . , FIFO .
. :
/ . . .
( ) . , / . PCB / .
( / ), ( ), . , PCB / . , , , PCB . 8 .
, - . - , , (. ., / ) .
PCB A PCB B PCB PCB PCB
-
8/14/2019 LS Chapters 123 New
41/143
-
8/14/2019 LS Chapters 123 New
42/143
2
42
. t1 t2, o = t 2 t 1 ( t2-t1 ). . n p1, ..., p n,
= ( 1 + 2 + ... + n) / n ,
1, ..., n p1, ..., p n, .
. , t1, t2
d , XA = t 2t1d. n p1, ..., p n,
= ( 1 + 2 + ... + n) / n ,
1, ..., n p1, ..., p n, .
, . , , ,
. . ( , , ), .
, ( ) . , ,
. . . . , . 9 .
-
8/14/2019 LS Chapters 123 New
43/143
2
43
9: .
2.7
.
2.7.1 (First Come First Served, FCFS) . FIFO. . . . FCFS - .
/ . . .
FCFS . ( , , ). ( ) ( ) ( ).
, , 100 / . 1 / . , , 100 . , , .
-
8/14/2019 LS Chapters 123 New
44/143
2
44
. . . , , , / , . , / . , 100 . , , . . , , . ., . .
2.7.2 (Round Robin, RR) RR FCFS. FCFS, RR FIFO . , . , RR . . / , RR . RR .
RR , , ( ), . , RR . RR , ( ).
. , . , , 10 . 1 sec,
-
8/14/2019 LS Chapters 123 New
45/143
2
45
10 . . ( ) . . (process switch) (context switch) , , . , ( , ). , , .
2.7.3 (Priority) . . , . ( , .) , . . - , / , . , . , , .
. . , .
. . ., , , , .. .
, , , .
( ) . , . , .
-
8/14/2019 LS Chapters 123 New
46/143
2
46
, , , . , . . . , , ( , , ). , .
(Shortest Job First, SJF),
. SJF . (Shortest RemainingTime First, SRTF). , , SRTF . , .
SJF . , ( ) . ( / ). . , ( ).
2.7.4 .
5
.
A 3 70 ms
B 1 10 ms
3 20 ms
-
8/14/2019 LS Chapters 123 New
47/143
-
8/14/2019 LS Chapters 123 New
48/143
2
48
0 10 80 100 150 160
ms
. , . , , . ( . , , , . , , .
Gantt = 80 ms, = 160 ms, = 100ms, = 10 ms, = 150 ms. , = ( + + + + ) / 5 =(80+160+100+10+150) / 5 ms = 100 ms.
, = 10 ms, = 150 ms, = 80 ms, = 0 ms, = 100 ms. , = ( + + + + ) / 5 = (10+150+80+0+100) / 5 ms = 68ms.
3. SJF
0 10 20 40 90 160
ms
( ). , .
Gantt = 160 ms, = 10 ms, = 40ms, = 20 ms, = 90 ms. , = ( + + + + ) / 5 =(160+10+40+20+90) / 5 ms = 64 ms.
, = 90 ms, = 0 ms, = 20 ms, = 10 ms, = 40 ms. , = ( + + + + ) / 5 = (90+0+20+10+40) / 5 ms = 32 ms.
4. RR
0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160
-
8/14/2019 LS Chapters 123 New
49/143
2
49
ms
10 ms
.
Gantt = 160 ms, = 20 ms, = 70ms, = 40 ms, = 140 ms. , = ( + + + + ) / 5 =(160+20+70+40+130) / 5 ms = 86 ms.
, = (160-70) = 90 ms, = 10 ms, = (70-20) = 50 ms, = 30ms, = (140-50) = 90 ms. , = ( + + + + ) / 5 =(90+10+50+30+90) / 5 ms = 54 ms.
, SJF .
FCFS .
, , .. . , , = + ( + + ...+ )/5. , ( + + ... + )/5 . , .
( ), -
Priority . SJF SRTF ( Priority).
6
7 4 . 2003-04.
:
1 8
2 4
3 9
4 5
:1. FCFS
-
8/14/2019 LS Chapters 123 New
50/143
2
50
2. SJF 3. SRTF
4. RR 2 0.
Gantt 11. , .
1. FCFS . , , , , . 1 Gantt 11 = (9-1) = 8 , = (13-2) = 11 , = (22-3) = 19 , = (27-4) = 23 . , = ( + + + ) / 4 =
(8+11+19+23) / 4 = 15,25 . , = (9-8-1) = 0 , = (13-4-2) = 7 , = (22-9-3) = 10 , = (27-5-4) = 18 . , = ( + + + ) / 4 = (0+7+10+18) / 4 = 8,75 .
2. SJF , 1. 9, , . SJF , . .
2 Gantt 11 = (9-1) = 8 , = (13-2) = 11 , = (27-3) = 24 , = (18-4) = 14 . , = ( + + + )
/ 4 = (8+11+24+14) / 4 = 14,25 .
, = (9-8-1) = 0 , = (13-4-2) = 7 , = (27-9-3) = 15 , = (18-5-4) = 9 . , = ( + + + ) / 4 = (0+7+15+9) / 4 = 7,75 .
SJF FCFS . , .
SJF , . , ( ).
3. SRTF 1. 2 , SRTF ( 7 ) ( 4 ) , . 3 SRTF
-
8/14/2019 LS Chapters 123 New
51/143
2
51
. 7 , 3 9 . , . 4, . ( ) SRTF , . 6, , ( ).
3 Gantt 11 = (18-1) = 17 , = (6-2) = 4 , = (27-3) = 24 , = (11-4) = 7 . , = ( + + + ) / 4 = (17+4+24+7) / 4 = 13 .
, = (18-8-1) = 9 , = (6-4-2) = 0 , = (27-9-3) = 15 , = (11-5-4) = 2 . , = ( + + + ) / 4 = (9+0+15+2) / 4 = 6,5 .
4. RR . 0 , 1 . 2 . . , . , , , . . .
, , , , , . 10. i
i (i+1). i. , 4 , , , ( 4). 4 5.
1 . 2 . 3 . .
-
8/14/2019 LS Chapters 123 New
52/143
2
52
. 4 . 14 15 15 ( 14). , , 22, 23 27, .
0 - - - -
1 - -
2 -
3 , -
4 , , -
5 - , , -6 - , , -
7 - , , -
8 - , , -
9 - , , -
10 - , , -
11 - , , -
12 - , , -
13 - , , -14 - , , -
15 - ,
16 - , -
17 - , -
18 - , -
19 - , -
20 - , -
21 - , -
22 -
23 - -
24 - - -
25 - - -
26 - - -
27 - - -
10: RR.
-
8/14/2019 LS Chapters 123 New
53/143
-
8/14/2019 LS Chapters 123 New
54/143
-
8/14/2019 LS Chapters 123 New
55/143
-
8/14/2019 LS Chapters 123 New
56/143
2
56
. : SJF ( );, , SJF. ( ) . Gantt SJF .
3 ( 3, , . 2001-2002)
:
0 3
1 4
3 2
4 4
6 3
. , KME .
: Gantt . 6. :
0 -
... ... ... ...
.
4 ( 2 , 2002)
0 .
-
8/14/2019 LS Chapters 123 New
57/143
2
57
3
1
2
1
1
1. RR 1 , . .
2. 4 . ( Gantt) . .
: () . (). , , ( , ,,,,). , , .
-
8/14/2019 LS Chapters 123 New
58/143
3 &
-
8/14/2019 LS Chapters 123 New
59/143
3 &
59
3.1
, .
2 2 . , , . ( , ).
28 2. , , , .. . , . . , ( ) . , . 12 ( ). 13. 13, . , ( ). 13 12 . 14.
. , ( ) . , , ( ). , - . . , , .
: tmp = 1 ( . 1)
-
8/14/2019 LS Chapters 123 New
60/143
3 &
60
: tmp = 1 while if tmp = 1 * 3 = 3flag = 1p = 4 while if tmp = 3 * 4 = 12flag = 0p = 3 while
: while if tmp = 1 * 3 = 3
flag = 1p = 4 while if tmp = 3 * 4 = 12flag = 0p = 3 while if tmp = 12 * 3 = 36flag = 1p = 2
while if tmp = 36 * 4 = 144flag = 0
: if : p = 1
while if tmp = 144 * 3 = 432flag = 1p = 0
: tmp = 12 * 3 = 36
flag = 1p = 2 while if tmp = 36 * 4 = 144flag = 0p = 1 while if tmp = 144 * 3 = 432
: while: flag = 1
(. 1)(. 2)(. 3)(. 4)(. 5)(. 6)(. 7)(. 8)(. 9)(. 10)(. 11)(. 12)(. 2)(. 3)(. 4)
(. 5)(. 6)(. 7)(. 8)(. 9)(. 10)(. 11)(. 12)(. 13)(. 14)(. 15)(. 16)
(. 17)(. 18)(. 19)(. 20)(. 13)(. 21)(. 22)(. 23)(. 24)(. 25)(. 26)(. 14)
(. 15)(. 16)(. 17)(. 18)(. 19)(. 20)(. 21)(. 22)(. 23)(. 24)(. 27)(. 25)
-
8/14/2019 LS Chapters 123 New
61/143
3 &
61
p = 0 whileprint 432
: print 432
(. 26)(. 27)(. 28)(. 28)
12: 1
tmp = 1 while if tmp = 1 * 3 = 3flag = 1p = 4 while if tmp = 3 * 4 = 12flag = 0p = 3 while
if
tmp = 12 * 3 = 36flag = 1p = 2
(. 1)(. 2)(. 3)(. 4)(. 5)(. 6)(. 7)(. 8)(. 9)(. 10)(. 11)(. 12)
(. 13)
(. 14)(. 15)(. 16)
tmp = 1
while if tmp = 1 * 3 = 3flag = 1p = 4 while if tmp = 3 * 4 = 12flag = 0p = 3 while if tmp = 12 * 3 = 36flag = 1p = 2 while if tmp = 36 * 4 = 144flag = 0
p = 1 while if tmp = 144 * 3 = 432flag = 1p = 0
(. 1)
(. 2)(. 3)(. 4)(. 5)(. 6)(. 7)(. 8)(. 9)(. 10)(. 11)(. 12)(. 13)(. 14)(. 15)(. 16)(. 17)(. 18)(. 19)(. 20)
(. 21)(. 22)(. 23)(. 24)(. 25)(. 26)
-
8/14/2019 LS Chapters 123 New
62/143
3 &
62
while if tmp = 36 * 4 = 144flag = 0p = 1 while if tmp = 144 * 3 = 432
flag = 1p = 0 whileprint 432
(. 17)(. 18)(. 19)(. 20)(. 21)(. 22)(. 23)(. 24)
(. 25)(. 26)(. 27)(. 28)
while
print 432
(. 27)
(. 28)
13: 1 :
tmp = 1
while
if
tmp = 1 * 3 = 3
flag = 1
p = 4
while
if
tmp = 3 * 4 = 12
flag = 0
p = 3
while if tmp = 12 * 3 = 36flag = 1p = 2 while if
(. 1)
(. 2)
(. 3)
(. 4)
(. 5)
(. 6)
(. 7)
(. 8)
(. 9)
(. 10)
(. 11)
(. 12)(. 13)(. 14)(. 15)(. 16)(. 17)(. 18)
tmp = 1
while
if
tmp = 1 * 3 = 3
flag = 1
p = 4
while
if
tmp = 3 * 4 = 12
flag = 0
p = 3
while
(. 1)
(. 2)
(. 3)
(. 4)
(. 5)
(. 6)
(. 7)
(. 8)
(. 9)
(. 10)
(. 11)
(. 12)
-
8/14/2019 LS Chapters 123 New
63/143
3 &
63
tmp = 36 * 4 = 144flag = 0p = 1
while
if
tmp = 144 * 3 = 432flag = 1p = 0 whileprint 432
(. 19)(. 20)(. 21)
(. 22)
(. 23)
(. 24)(. 25)(. 26)(. 27)(. 28)
if tmp = 12 * 3 = 36flag = 1p = 2 while if tmp = 36 * 4 = 144flag = 0p = 1
while
if
tmp = 144 * 3 = 432flag = 1p = 0 while
print 432
(. 13)(. 14)(. 15)(. 16)(. 17)(. 18)(. 19)(. 20)(. 21)
(. 22)
(. 23)
(. 24)(. 25)(. 26)(. 27)
(. 28)
14: 2
( )
cobegin s 1; s 2; ... s n; coend
parbegin s 1; s 2; ... sn; parend
s1, s2, ..., sn . s1, s2, ..., sn .
7
:
shared int tally;
begintally := 0;cobegin
increase();increase();
coend
-
8/14/2019 LS Chapters 123 New
64/143
3 &
64
end
increase() ( ).
; , cobegin coend , increase(). . tally (shared) . . . ( , increase() .)
tally . tally, .
8
increase() 7 :
void increase()int tmp, i; /* */ begin
tmp = tally;tmp = tmp+1;tally = tmp;
end , . 15 ( tmp , 15 , tmpA, tmpB, ):
A B
1. tmp = tally;2. tmp = tmp +1;3. tally = tmp ;
1. tmpB = tally;2. tmpB = tmpB +1;3. tally = tmpB;
15: .
tally, tally.
tally , 0;
, tally ( 1 2).
-
8/14/2019 LS Chapters 123 New
65/143
3 &
65
:
tmp = tally;tmp = tmp +1;
tally = tmp ;
tmpB = tally;tmpB = tmpB +1;
tally = tmpB;
, tmpA tally ( 0). tmpA 1, tally, . , tmp tally (
tally 0). tmpA 1 . tmpA ( 1) tally . , tmpB ( 1) tally . , tally 1.
1 tally. , . ., ; - ;
9
increase() 7 :
void increase()int tmp, i; /* */ begin
for i=0 to 5 dobegin
tmp = tally;tmp = tmp+1;tally = tmp;
endend
, . for , tally, tally.
:
A B
for i = 1 to 5 do begin
for iB = 1 to 5 do begin
-
8/14/2019 LS Chapters 123 New
66/143
3 &
66
1. tmp = tally;2. tmp = tmp +1;
3. tally = tmp ;end
1. tmpB = tally;2. tmpB = tmpB +1;
3. tally = tmpB;end
tmpA, tmpB, iA iB ( ) .
tally . , . ( ) tally 5. , , 5 tally tally 1 ( for). , , tally
10. tally, ( - ). , tally 5. , 13 14 tally .
5 tally. ,
tally. 2. tally 2 . . , ( tally 2).
:
tally 0.
A 0 tmpA B.
B tmpB 0. 4 B . tally 4. for B, A.
A , for. tmpA 0, tally 4,
-
8/14/2019 LS Chapters 123 New
67/143
3 &
67
, ( 3) tally 1. 4 , B.
B . tally ( 1) tmpB. , tally 1 ( A ). B (4 tally) . B 2 3 , .
, tally 5. 2 3 B,
. tmpB 1, tally
5, 1 , tmpB 2 ( 2), tally 2 ( 3) .
tally 2.
( ) 16.
tmp = tally; /* tmpA 0 */
tmp = tmp +1; tally = tmp ; /* tally 1 */
tmp = tally; tmp = tmp +1; tally = tmp ; /* tally 2 */ tmp = tally; tmp = tmp +1; tally = tmp ;
/* tally 3 */ tmp = tally; tmp = tmp +1; tally = tmp ;
/* tally 4 */ tmp = tally; tmp = tmp +1; tally = tmp ;
/* tally 5 */
tmpB = tally; tmpB = tmpB +1; tally = tmpB; /* tally 1 */ tmpB = tally; tmpB = tmpB +1; tally = tmpB;
/* tally 2 */ tmpB = tally; tmpB = tmpB +1; tally = tmpB;
/* tally 3 */ tmpB = tally; tmpB = tmpB +1; tally = tmpB;
/* tally 4 */
tmpB = tally; /* tmpB 1 */
tmpB = tmpB +1; tally = tmpB;
-
8/14/2019 LS Chapters 123 New
68/143
3 &
68
/* tally 2 */
16: tally.
:tmp = tally;tmp = tmp+1;tally = tmp;
:
tally = tally + 1;
H tally . tally = tally +1; tally. , tally , tally. , tally= tally +1; , , tmp = tally; tmp =tmp +1; tally = tmp;.
, , (. ., ). , tally =tally + 1; ( ).
5
1. increase() 9. x, 2 < x 10, tally x.
2. increase() 9. x, 1 < x 10, tally x; .
3. increase() :void increase()int tmp, i; /* */ begin
for i=0 to 50 dotally = tally + 1;
end
tally, ; tally;
4. increase() 9 3 2 . tally ;
-
8/14/2019 LS Chapters 123 New
69/143
3 &
69
: tally 2. To 16. , (
tally ).5. tally
3;
6. 4 5 4 , 5 n .
: , tally 2.
3.2
. . , . , . , -11 .
. , Word Excel. (.doc, .xls) . - . ;
.
, . . . , :
. , / 2 1,80 , 3,60 1,5 2,75 3 4,12 4 0,25
1 . 1 0,5 0,5 , .
, , . , .
( ) ( ). , / , , ,
-
8/14/2019 LS Chapters 123 New
70/143
3 &
70
. , . , . ( ) , , . , 17. front , back .
17: .
. back
. back 10. , back 10 ( 11). , 10 back 11. , 10. , . , front back.
. 500 , (. ., ).
, . , , .
:
file1.txtfile2.txt
89
10
front
back
.
.
.
.
.
.
-
8/14/2019 LS Chapters 123 New
71/143
3 &
71
shared int balance;
boolean withdraw(int amount)begin
if (amount > balance)begin
print !;return FALSE;
endelse
beginbalance = balance amount;print ;print balance;return TRUE;
endend
, :
1. amount.
2. balance.
3. amount > balance " " .
4. , amount balance balance.
5. amount .
6. " " .
( ATM-1 ATM-2) 500 . ;
. , 500 . , . ( ). . 18.
1000 500 !
-
8/14/2019 LS Chapters 123 New
72/143
3 &
72
( balance).
- . . .
ATM-1 ( ) ATM-2 ( )
amount = 500
balance = 500
if ( TRUE)
balance = balance amount
print print balance
amount = 500
balance = 500
if ( TRUE)
balance = balance amount
print print balance
18: 1000 500.
6
500
, (. ., ).
1500 , 500 , 1000 . 500.
, . , .
:
shared int balance;
boolean deposit(int amount)begin
balance = balance + amount;print ;print balance;return TRUE;
end
-
8/14/2019 LS Chapters 123 New
73/143
3 &
73
. , 500 , 1000 ( 1500 ). !
3.3
(. ., ) . (race conditions ).
(. ., ) ( ). - . , . , . , . .
, ( ), , ( , ). , , :
;;;;
:
1. . ( ).
2. . ( ) , -
-
8/14/2019 LS Chapters 123 New
74/143
3 &
74
, ( , ).
. , , - (. ., , ). - . ,
, . . .
( ) . , , , .. .
:
1. . .
2. . , .
- ( ). , 19 ( , 1 ).
1
2
3
repeat
begin;;;;
endforever;
repeatbegin
;;;;
enduntil FALSE;
while (TRUE)
begin;;;;
end
19: .
-
8/14/2019 LS Chapters 123 New
75/143
3 &
75
, . . , , . , . 20 .
;
; /* */
;
;
; /*
*/ ...
; ;
;
;
;
.
.
.
20: .
20 .
3.4 , , . , , ( , ).
Test&Set() 21.
-
8/14/2019 LS Chapters 123 New
76/143
3 &
76
boolean Test&Set(boolean lock)
int tmp; /* */
begintmp = lock;lock = TRUE;return(tmp);
end
21: Test&Set().
H Test&Set() boolean lock. Test&Set() TRUE lock lock. , lock FALSE Test&Set(), Test&Set() FALSE, TRUE. , lock Test&Set() TRUE. Test&Set() lock. .
, Test&Set() ( Test&Set() Test&Set()).
22.
shared boolean lock; /* , , FALSE */
int tmp; /* */ repeatbegin
tmp = Test&Set(lock);while (tmp == TRUE) do tmp = Test&Set(lock);;lock = FALSE;;
endforever;
22: Test&Set().
lock FALSE, Test&Set() . , , Test&Set() FALSE, lock TRUE. FALSE tmp while FALSE. . Test&Set() TRUE ( , , lock TRUE). , while . ,
-
8/14/2019 LS Chapters 123 New
77/143
3 &
77
lock FALSE .
7 ( 1, 5, 4, . 2001-2002 )
22 Test&Set() ;
Test&Set() . ( 0 1), . , 23 ( ).
0 1repeat
begintmp0 = lock;lock = TRUE;while (tmp0 == TRUE) do
tmp0 = Test&Set(lock);
;
lock = FALSE;
;
endforever;
repeatbegin
tmp1 = lock;lock = TRUE;while (tmp1 == TRUE) do
tmp1 = Test&Set(lock);
;
lock = FALSE;
;
endforever;
23: 0 1 Test&Set() .
23, tmp , tmp . , tmp. 24.
0 1tmp0 = lock;
lock = 1; while /* FALSE */ ;
tmp1 = lock;lock = 1; while /* FALSE */ ;
-
8/14/2019 LS Chapters 123 New
78/143
3 &
78
24: Test&Set() .
0 lock ( FALSE) tmp0. 0 1. Test&Set() , 1 Test&Set() ( Test&Set() ). FALSE tmp1, lock FALSE . , 1 0. 0 lock TRUE, while FALSE ( tmp0 FALSE) .
8 ( 2, 4, . 2001-2002 ) ! , 3.2;
:
boolean deposit(int amount),
boolean withdraw(int amount),
:
deposit() amount , withdraw() amount .
, (. ., ), . .
Test&Set() .
- deposit() withdraw() 3.2. deposit() withdraw(), .
deposit() balance. deposit() 25.
shared int balance;
boolean deposit(int amount)
-
8/14/2019 LS Chapters 123 New
79/143
3 &
79
int tmp; /* */ begin
tmp = Test&Set(lock);
while (tmp == TRUE) do tmp = Test&Set(lock);balance = balance + amount;print ;print balance;lock = FALSE;return TRUE;
end
25: deposit().
withdraw(). ( ) balance
. withdraw() 26.shared int balance;
boolean withdraw(int amount)begin
tmp = Test&Set(lock);while (tmp == TRUE) do tmp = Test&Set(lock);
if (amount > balance)begin
print !;lock = FALSE;return FALSE;
endelse
beginbalance = balance amount;print ;print balance;lock = FALSE;return TRUE;
endend
26: withdraw().
3.2 ( ) deposit() withdraw() 25 26.
9
15 ;
-
8/14/2019 LS Chapters 123 New
80/143
3 &
80
.
, . . 27.
0 1
tmp = Test&Set(lock); while /* FALSE */
;lock = FALSE;;tmp = Test&Set(lock); while /* FALSE */
;lock = FALSE;;tmp = Test&Set(lock); while /* FALSE */
.
.
.
tmp = Test&Set(lock); while /* TRUE */
while /* TRUE */ while /* TRUE */ while /* TRUE */
while /* TRUE */ while /* TRUE */ ...
27: 1 Test&Set().
27 0. Test&Set() . 1 . 1 while . 0 , lock FALSE. 1, 0 - , Test&Set() lock TRUE. H 1 , . . 0 , 1 . 15 .
-
8/14/2019 LS Chapters 123 New
81/143
3 &
81
, . , , . .
10 ( 3.19 )
Fetch&Add(), 28 . Fetch&Add(). ( Fetch&AddA() Test&Set() .)
int Fetch&Add(int a, int b)
int tmp; /* */
begintmp = a;a = a + b;return(tmp);
end
28: Fetch&Add().
;
: lock Fetch&Add() (. ., 1) Fetch&Add(), .
11
Swap(), 29 . Swap().
int Swap(int a, int b)
int tmp; /* */
begintmp = a;a = b;b = tmp;return(tmp);
end
29: Swap().
;
-
8/14/2019 LS Chapters 123 New
82/143
3 &
82
: lock Swap() Swap(), .
12
RMW() (Read-Modify-Write()), 30 . RMW().
int RMW(int a, function f)
int tmp; /* */
begintmp = a;a = f(a);
return(tmp);end
30: RMW().
RMW() , a f(). a f ( f a a), a.
;
: f().
13 ( 2, 4, . 2001-2002 ) :
boolean deposit(int amount),
boolean withdraw(int amount),
8. :
1. Fetch&Add() ,
2. Swap() .
3. RMW() .
3.5
, ( . ., Test&Set()).
-
8/14/2019 LS Chapters 123 New
83/143
3 &
83
3.5.1 ,
, . , , , , .
. 31, .
shared boolean in; /* , FALSE */
0 1
repeatbegin
while (in == TRUE) do noop;in = TRUE;;in = FALSE;;
endforever;
repeatbegin
while (in == TRUE) do noop;in = TRUE;;in = FALSE;;
endforever;
31: .
in, . in FALSE, TRUE . in TRUE, while. , in FALSE, noop ( while).
; ; , .
, 0 while in FALSE. 1 in TRUE . , 1. 1 while, in FALSE, 0 in TRUE
-
8/14/2019 LS Chapters 123 New
84/143
3 &
84
. . in TRUE . 32.
while; /* FALSE */
free = TRUE; ;
while; /* FALSE */
free = TRUE;;
32: 1 .
, 31 .
. in FALSE in TRUE. , ( while) ( in TRUE). , . 33.
shared boolean flag[2]; /* , FALSE */
0 1
repeatbegin
flag[0] = TRUE;while (flag[1] == TRUE) do noop;
;flag[0] = FALSE;;
endforever;
repeatbegin
flag[1] = TRUE;while (flag[0] == TRUE) do noop;
;flag[1] = FALSE;;
endforever;
33: .
33, , . flag[i] i . , i flag[i] TRUE .
-
8/14/2019 LS Chapters 123 New
85/143
3 &
85
( , 1-i) . , i . 1-i flag FALSE.
; noop while; .
0 1
flag[0] = RUE;
while; /* TRUE */
while; /* TRUE */ while; /* TRUE */ while; /* TRUE */ ...
/* while FALSE 0 while */
flag[1] = TRUE;
while; /* TRUE */
while; /* TRUE */ while; /* TRUE */ ...
/* while FALSE 1 while */
34: 2 .
34, while . 0 1 flag[1] FALSE, 1 0 flag[0] FALSE. . ,
( ), .
35.
shared int turn; /* , 0 1, */
0 1
repeatbegin
while (turn == 1) do noop;
repeatbegin
while (turn == 0) do noop;
-
8/14/2019 LS Chapters 123 New
86/143
3 &
86
;turn = 1;;
endforever;
;turn = 0;;
endforever;
35: ( ) .
. . , turn 0 1 , while . . ., 1 . 1 , turn 1. , 0 , while . 1 , turn 0 0 . 1 , 0 turn 1. H 35 .
. , , - .
. 36 ( turn 1).
0 1
while; /* FALSE */ ;turn = 1;;
while; /* TRUE */ while; /* TRUE */
while; /* TRUE */ while; /* TRUE */ while; /* TRUE */
while; /* TRUE */ ...
while; /* FALSE */ ;turn = 0; - ;
- ;
- ;
.
.
.
-
8/14/2019 LS Chapters 123 New
87/143
-
8/14/2019 LS Chapters 123 New
88/143
-
8/14/2019 LS Chapters 123 New
89/143
3 &
89
5. .
6.
.7. , .
8. .
:
1. .
2. .
3. , .
4. .
;
17
( ;). . . . , - . , .
:
5. , .
6. .
7. , , ( ).
:
1. , .
2. .
3. , , ( ).
; ( ) ;
-
8/14/2019 LS Chapters 123 New
90/143
3 &
90
3.5.2 Peterson ,
. Peterson.
shared in turn; /* 0 1 */ shared boolean flag[2]; /* FALSE */
0 1
repeatbegin
1. flag[0] = TRUE;2. turn = 0;3. while (turn == 0 AND flag[1] == TRUE) do
noop;4. < >;5. flag[0] = FALSE; 6. < - >;
endforever;
repeatbegin
1. flag[1] = TRUE; 2. turn = 1;3. while (turn == 1 AND flag[0] == TRUE) do
noop;4. < >; 5. flag[1] = FALSE; 6. < - >;
endforever;
38: Peterson.
Peterson turn , flag , . i, , flag[i] TRUE turn i. . . , flag FALSE.
Peterso