-
Notifications
You must be signed in to change notification settings - Fork 0
/
CS302-OS-Notes.html
518 lines (480 loc) · 520 KB
/
CS302-OS-Notes.html
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<!doctype html>
<html>
<head>
<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'>
<style type='text/css'>html {overflow-x: initial !important;}:root { --bg-color:#ffffff; --text-color:#333333; --select-text-bg-color:#B5D6FC; --select-text-font-color:auto; --monospace:"Lucida Console",Consolas,"Courier",monospace; --title-bar-height:20px; }
.mac-os-11 { --title-bar-height:28px; }
html { font-size: 14px; background-color: var(--bg-color); color: var(--text-color); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; }
body { margin: 0px; padding: 0px; height: auto; bottom: 0px; top: 0px; left: 0px; right: 0px; font-size: 1rem; line-height: 1.42857; overflow-x: hidden; background: inherit; tab-size: 4; }
iframe { margin: auto; }
a.url { word-break: break-all; }
a:active, a:hover { outline: 0px; }
.in-text-selection, ::selection { text-shadow: none; background: var(--select-text-bg-color); color: var(--select-text-font-color); }
#write { margin: 0px auto; height: auto; width: inherit; word-break: normal; overflow-wrap: break-word; position: relative; white-space: normal; overflow-x: visible; padding-top: 36px; }
#write.first-line-indent p { text-indent: 2em; }
#write.first-line-indent li p, #write.first-line-indent p * { text-indent: 0px; }
#write.first-line-indent li { margin-left: 2em; }
.for-image #write { padding-left: 8px; padding-right: 8px; }
body.typora-export { padding-left: 30px; padding-right: 30px; }
.typora-export .footnote-line, .typora-export li, .typora-export p { white-space: pre-wrap; }
.typora-export .task-list-item input { pointer-events: none; }
@media screen and (max-width: 500px) {
body.typora-export { padding-left: 0px; padding-right: 0px; }
#write { padding-left: 20px; padding-right: 20px; }
.CodeMirror-sizer { margin-left: 0px !important; }
.CodeMirror-gutters { display: none !important; }
}
#write li > figure:last-child { margin-bottom: 0.5rem; }
#write ol, #write ul { position: relative; }
img { max-width: 100%; vertical-align: middle; image-orientation: from-image; }
button, input, select, textarea { color: inherit; font: inherit; }
input[type="checkbox"], input[type="radio"] { line-height: normal; padding: 0px; }
*, ::after, ::before { box-sizing: border-box; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p, #write pre { width: inherit; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p { position: relative; }
p { line-height: inherit; }
h1, h2, h3, h4, h5, h6 { break-after: avoid-page; break-inside: avoid; orphans: 4; }
p { orphans: 4; }
h1 { font-size: 2rem; }
h2 { font-size: 1.8rem; }
h3 { font-size: 1.6rem; }
h4 { font-size: 1.4rem; }
h5 { font-size: 1.2rem; }
h6 { font-size: 1rem; }
.md-math-block, .md-rawblock, h1, h2, h3, h4, h5, h6, p { margin-top: 1rem; margin-bottom: 1rem; }
.hidden { display: none; }
.md-blockmeta { color: rgb(204, 204, 204); font-weight: 700; font-style: italic; }
a { cursor: pointer; }
sup.md-footnote { padding: 2px 4px; background-color: rgba(238, 238, 238, 0.7); color: rgb(85, 85, 85); border-radius: 4px; cursor: pointer; }
sup.md-footnote a, sup.md-footnote a:hover { color: inherit; text-transform: inherit; text-decoration: inherit; }
#write input[type="checkbox"] { cursor: pointer; width: inherit; height: inherit; }
figure { overflow-x: auto; margin: 1.2em 0px; max-width: calc(100% + 16px); padding: 0px; }
figure > table { margin: 0px; }
tr { break-inside: avoid; break-after: auto; }
thead { display: table-header-group; }
table { border-collapse: collapse; border-spacing: 0px; width: 100%; overflow: auto; break-inside: auto; text-align: left; }
table.md-table td { min-width: 32px; }
.CodeMirror-gutters { border-right: 0px; background-color: inherit; }
.CodeMirror-linenumber { user-select: none; }
.CodeMirror { text-align: left; }
.CodeMirror-placeholder { opacity: 0.3; }
.CodeMirror pre { padding: 0px 4px; }
.CodeMirror-lines { padding: 0px; }
div.hr:focus { cursor: none; }
#write pre { white-space: pre-wrap; }
#write.fences-no-line-wrapping pre { white-space: pre; }
#write pre.ty-contain-cm { white-space: normal; }
.CodeMirror-gutters { margin-right: 4px; }
.md-fences { font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; overflow: visible; white-space: pre; background: inherit; position: relative !important; }
.md-fences-adv-panel { width: 100%; margin-top: 10px; text-align: center; padding-top: 0px; padding-bottom: 8px; overflow-x: auto; }
#write .md-fences.mock-cm { white-space: pre-wrap; }
.md-fences.md-fences-with-lineno { padding-left: 0px; }
#write.fences-no-line-wrapping .md-fences.mock-cm { white-space: pre; overflow-x: auto; }
.md-fences.mock-cm.md-fences-with-lineno { padding-left: 8px; }
.CodeMirror-line, twitterwidget { break-inside: avoid; }
.footnotes { opacity: 0.8; font-size: 0.9rem; margin-top: 1em; margin-bottom: 1em; }
.footnotes + .footnotes { margin-top: 0px; }
.md-reset { margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: top; background: 0px 0px; text-decoration: none; text-shadow: none; float: none; position: static; width: auto; height: auto; white-space: nowrap; cursor: inherit; -webkit-tap-highlight-color: transparent; line-height: normal; font-weight: 400; text-align: left; box-sizing: content-box; direction: ltr; }
li div { padding-top: 0px; }
blockquote { margin: 1rem 0px; }
li .mathjax-block, li p { margin: 0.5rem 0px; }
li blockquote { margin: 1rem 0px; }
li { margin: 0px; position: relative; }
blockquote > :last-child { margin-bottom: 0px; }
blockquote > :first-child, li > :first-child { margin-top: 0px; }
.footnotes-area { color: rgb(136, 136, 136); margin-top: 0.714rem; padding-bottom: 0.143rem; white-space: normal; }
#write .footnote-line { white-space: pre-wrap; }
@media print {
body, html { border: 1px solid transparent; height: 99%; break-after: avoid; break-before: avoid; font-variant-ligatures: no-common-ligatures; }
#write { margin-top: 0px; padding-top: 0px; border-color: transparent !important; }
.typora-export * { -webkit-print-color-adjust: exact; }
.typora-export #write { break-after: avoid; }
.typora-export #write::after { height: 0px; }
.is-mac table { break-inside: avoid; }
.typora-export-show-outline .typora-export-sidebar { display: none; }
}
.footnote-line { margin-top: 0.714em; font-size: 0.7em; }
a img, img a { cursor: pointer; }
pre.md-meta-block { font-size: 0.8rem; min-height: 0.8rem; white-space: pre-wrap; background: rgb(204, 204, 204); display: block; overflow-x: hidden; }
p > .md-image:only-child:not(.md-img-error) img, p > img:only-child { display: block; margin: auto; }
#write.first-line-indent p > .md-image:only-child:not(.md-img-error) img { left: -2em; position: relative; }
p > .md-image:only-child { display: inline-block; width: 100%; }
#write .MathJax_Display { margin: 0.8em 0px 0px; }
.md-math-block { width: 100%; }
.md-math-block:not(:empty)::after { display: none; }
.MathJax_ref { fill: currentcolor; }
[contenteditable="true"]:active, [contenteditable="true"]:focus, [contenteditable="false"]:active, [contenteditable="false"]:focus { outline: 0px; box-shadow: none; }
.md-task-list-item { position: relative; list-style-type: none; }
.task-list-item.md-task-list-item { padding-left: 0px; }
.md-task-list-item > input { position: absolute; top: 0px; left: 0px; margin-left: -1.2em; margin-top: calc(1em - 10px); border: none; }
.math { font-size: 1rem; }
.md-toc { min-height: 3.58rem; position: relative; font-size: 0.9rem; border-radius: 10px; }
.md-toc-content { position: relative; margin-left: 0px; }
.md-toc-content::after, .md-toc::after { display: none; }
.md-toc-item { display: block; color: rgb(65, 131, 196); }
.md-toc-item a { text-decoration: none; }
.md-toc-inner:hover { text-decoration: underline; }
.md-toc-inner { display: inline-block; cursor: pointer; }
.md-toc-h1 .md-toc-inner { margin-left: 0px; font-weight: 700; }
.md-toc-h2 .md-toc-inner { margin-left: 2em; }
.md-toc-h3 .md-toc-inner { margin-left: 4em; }
.md-toc-h4 .md-toc-inner { margin-left: 6em; }
.md-toc-h5 .md-toc-inner { margin-left: 8em; }
.md-toc-h6 .md-toc-inner { margin-left: 10em; }
@media screen and (max-width: 48em) {
.md-toc-h3 .md-toc-inner { margin-left: 3.5em; }
.md-toc-h4 .md-toc-inner { margin-left: 5em; }
.md-toc-h5 .md-toc-inner { margin-left: 6.5em; }
.md-toc-h6 .md-toc-inner { margin-left: 8em; }
}
a.md-toc-inner { font-size: inherit; font-style: inherit; font-weight: inherit; line-height: inherit; }
.footnote-line a:not(.reversefootnote) { color: inherit; }
.md-attr { display: none; }
.md-fn-count::after { content: "."; }
code, pre, samp, tt { font-family: var(--monospace); }
kbd { margin: 0px 0.1em; padding: 0.1em 0.6em; font-size: 0.8em; color: rgb(36, 39, 41); background: rgb(255, 255, 255); border: 1px solid rgb(173, 179, 185); border-radius: 3px; box-shadow: rgba(12, 13, 14, 0.2) 0px 1px 0px, rgb(255, 255, 255) 0px 0px 0px 2px inset; white-space: nowrap; vertical-align: middle; }
.md-comment { color: rgb(162, 127, 3); opacity: 0.8; font-family: var(--monospace); }
code { text-align: left; vertical-align: initial; }
a.md-print-anchor { white-space: pre !important; border-width: initial !important; border-style: none !important; border-color: initial !important; display: inline-block !important; position: absolute !important; width: 1px !important; right: 0px !important; outline: 0px !important; background: 0px 0px !important; text-decoration: initial !important; text-shadow: initial !important; }
.md-inline-math .MathJax_SVG .noError { display: none !important; }
.html-for-mac .inline-math-svg .MathJax_SVG { vertical-align: 0.2px; }
.md-fences-math .MathJax_SVG_Display, .md-math-block .MathJax_SVG_Display { text-align: center; margin: 0px; position: relative; text-indent: 0px; max-width: none; max-height: none; min-height: 0px; min-width: 100%; width: auto; overflow-y: visible; display: block !important; }
.MathJax_SVG_Display, .md-inline-math .MathJax_SVG_Display { width: auto; margin: inherit; display: inline-block !important; }
.MathJax_SVG .MJX-monospace { font-family: var(--monospace); }
.MathJax_SVG .MJX-sans-serif { font-family: sans-serif; }
.MathJax_SVG { display: inline; font-style: normal; font-weight: 400; line-height: normal; text-indent: 0px; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; overflow-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin: 0px; zoom: 90%; }
#math-inline-preview-content { zoom: 1.1; }
.MathJax_SVG * { transition: none 0s ease 0s; }
.MathJax_SVG_Display svg { vertical-align: middle !important; margin-bottom: 0px !important; margin-top: 0px !important; }
.os-windows.monocolor-emoji .md-emoji { font-family: "Segoe UI Symbol", sans-serif; }
.md-diagram-panel > svg { max-width: 100%; }
[lang="flow"] svg, [lang="mermaid"] svg { max-width: 100%; height: auto; }
[lang="mermaid"] .node text { font-size: 1rem; }
table tr th { border-bottom: 0px; }
video { max-width: 100%; display: block; margin: 0px auto; }
iframe { max-width: 100%; width: 100%; border: none; }
.highlight td, .highlight tr { border: 0px; }
mark { background: rgb(255, 255, 0); color: rgb(0, 0, 0); }
.md-html-inline .md-plain, .md-html-inline strong, mark .md-inline-math, mark strong { color: inherit; }
.md-expand mark .md-meta { opacity: 0.3 !important; }
mark .md-meta { color: rgb(0, 0, 0); }
@media print {
.typora-export h1, .typora-export h2, .typora-export h3, .typora-export h4, .typora-export h5, .typora-export h6 { break-inside: avoid; }
}
.md-diagram-panel .messageText { stroke: none !important; }
.md-diagram-panel .start-state { fill: var(--node-fill); }
.md-diagram-panel .edgeLabel rect { opacity: 1 !important; }
.md-require-zoom-fix foreignobject { font-size: var(--mermaid-font-zoom); }
.md-fences.md-fences-math { font-size: 1em; }
.md-fences-math .MathJax_SVG_Display { margin-top: 8px; cursor: default; }
.md-fences-advanced:not(.md-focus) { padding: 0px; white-space: nowrap; border: 0px; }
.md-fences-advanced:not(.md-focus) { background: inherit; }
.typora-export-show-outline .typora-export-content { max-width: 1440px; margin: auto; display: flex; flex-direction: row; }
.typora-export-sidebar { width: 300px; font-size: 0.8rem; margin-top: 80px; margin-right: 18px; }
.typora-export-show-outline #write { --webkit-flex:2; flex: 2 1 0%; }
.typora-export-sidebar .outline-content { position: fixed; top: 0px; max-height: 100%; overflow: hidden auto; padding-bottom: 30px; padding-top: 60px; width: 300px; }
@media screen and (max-width: 1024px) {
.typora-export-sidebar, .typora-export-sidebar .outline-content { width: 240px; }
}
@media screen and (max-width: 800px) {
.typora-export-sidebar { display: none; }
}
.outline-content li, .outline-content ul { margin-left: 0px; margin-right: 0px; padding-left: 0px; padding-right: 0px; list-style: none; }
.outline-content ul { margin-top: 0px; margin-bottom: 0px; }
.outline-content strong { font-weight: 400; }
.outline-expander { width: 1rem; height: 1.42857rem; position: relative; display: table-cell; vertical-align: middle; cursor: pointer; padding-left: 4px; }
.outline-expander::before { content: ""; position: relative; font-family: Ionicons; display: inline-block; font-size: 8px; vertical-align: middle; }
.outline-item { padding-top: 3px; padding-bottom: 3px; cursor: pointer; }
.outline-expander:hover::before { content: ""; }
.outline-h1 > .outline-item { padding-left: 0px; }
.outline-h2 > .outline-item { padding-left: 1em; }
.outline-h3 > .outline-item { padding-left: 2em; }
.outline-h4 > .outline-item { padding-left: 3em; }
.outline-h5 > .outline-item { padding-left: 4em; }
.outline-h6 > .outline-item { padding-left: 5em; }
.outline-label { cursor: pointer; display: table-cell; vertical-align: middle; text-decoration: none; color: inherit; }
.outline-label:hover { text-decoration: underline; }
.outline-item:hover { border-color: rgb(245, 245, 245); background-color: var(--item-hover-bg-color); }
.outline-item:hover { margin-left: -28px; margin-right: -28px; border-left: 28px solid transparent; border-right: 28px solid transparent; }
.outline-item-single .outline-expander::before, .outline-item-single .outline-expander:hover::before { display: none; }
.outline-item-open > .outline-item > .outline-expander::before { content: ""; }
.outline-children { display: none; }
.info-panel-tab-wrapper { display: none; }
.outline-item-open > .outline-children { display: block; }
.typora-export .outline-item { padding-top: 1px; padding-bottom: 1px; }
.typora-export .outline-item:hover { margin-right: -8px; border-right: 8px solid transparent; }
.typora-export .outline-expander::before { content: "+"; font-family: inherit; top: -1px; }
.typora-export .outline-expander:hover::before, .typora-export .outline-item-open > .outline-item > .outline-expander::before { content: "−"; }
.typora-export-collapse-outline .outline-children { display: none; }
.typora-export-collapse-outline .outline-item-open > .outline-children, .typora-export-no-collapse-outline .outline-children { display: block; }
.typora-export-no-collapse-outline .outline-expander::before { content: "" !important; }
.typora-export-show-outline .outline-item-active > .outline-item .outline-label { font-weight: 700; }
.CodeMirror { height: auto; }
.CodeMirror.cm-s-inner { background: inherit; }
.CodeMirror-scroll { overflow: auto hidden; z-index: 3; }
.CodeMirror-gutter-filler, .CodeMirror-scrollbar-filler { background-color: rgb(255, 255, 255); }
.CodeMirror-gutters { border-right: 1px solid rgb(221, 221, 221); background: inherit; white-space: nowrap; }
.CodeMirror-linenumber { padding: 0px 3px 0px 5px; text-align: right; color: rgb(153, 153, 153); }
.cm-s-inner .cm-keyword { color: rgb(119, 0, 136); }
.cm-s-inner .cm-atom, .cm-s-inner.cm-atom { color: rgb(34, 17, 153); }
.cm-s-inner .cm-number { color: rgb(17, 102, 68); }
.cm-s-inner .cm-def { color: rgb(0, 0, 255); }
.cm-s-inner .cm-variable { color: rgb(0, 0, 0); }
.cm-s-inner .cm-variable-2 { color: rgb(0, 85, 170); }
.cm-s-inner .cm-variable-3 { color: rgb(0, 136, 85); }
.cm-s-inner .cm-string { color: rgb(170, 17, 17); }
.cm-s-inner .cm-property { color: rgb(0, 0, 0); }
.cm-s-inner .cm-operator { color: rgb(152, 26, 26); }
.cm-s-inner .cm-comment, .cm-s-inner.cm-comment { color: rgb(170, 85, 0); }
.cm-s-inner .cm-string-2 { color: rgb(255, 85, 0); }
.cm-s-inner .cm-meta { color: rgb(85, 85, 85); }
.cm-s-inner .cm-qualifier { color: rgb(85, 85, 85); }
.cm-s-inner .cm-builtin { color: rgb(51, 0, 170); }
.cm-s-inner .cm-bracket { color: rgb(153, 153, 119); }
.cm-s-inner .cm-tag { color: rgb(17, 119, 0); }
.cm-s-inner .cm-attribute { color: rgb(0, 0, 204); }
.cm-s-inner .cm-header, .cm-s-inner.cm-header { color: rgb(0, 0, 255); }
.cm-s-inner .cm-quote, .cm-s-inner.cm-quote { color: rgb(0, 153, 0); }
.cm-s-inner .cm-hr, .cm-s-inner.cm-hr { color: rgb(153, 153, 153); }
.cm-s-inner .cm-link, .cm-s-inner.cm-link { color: rgb(0, 0, 204); }
.cm-negative { color: rgb(221, 68, 68); }
.cm-positive { color: rgb(34, 153, 34); }
.cm-header, .cm-strong { font-weight: 700; }
.cm-del { text-decoration: line-through; }
.cm-em { font-style: italic; }
.cm-link { text-decoration: underline; }
.cm-error { color: red; }
.cm-invalidchar { color: red; }
.cm-constant { color: rgb(38, 139, 210); }
.cm-defined { color: rgb(181, 137, 0); }
div.CodeMirror span.CodeMirror-matchingbracket { color: rgb(0, 255, 0); }
div.CodeMirror span.CodeMirror-nonmatchingbracket { color: rgb(255, 34, 34); }
.cm-s-inner .CodeMirror-activeline-background { background: inherit; }
.CodeMirror { position: relative; overflow: hidden; }
.CodeMirror-scroll { height: 100%; outline: 0px; position: relative; box-sizing: content-box; background: inherit; }
.CodeMirror-sizer { position: relative; }
.CodeMirror-gutter-filler, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-vscrollbar { position: absolute; z-index: 6; display: none; }
.CodeMirror-vscrollbar { right: 0px; top: 0px; overflow: hidden; }
.CodeMirror-hscrollbar { bottom: 0px; left: 0px; overflow: hidden; }
.CodeMirror-scrollbar-filler { right: 0px; bottom: 0px; }
.CodeMirror-gutter-filler { left: 0px; bottom: 0px; }
.CodeMirror-gutters { position: absolute; left: 0px; top: 0px; padding-bottom: 30px; z-index: 3; }
.CodeMirror-gutter { white-space: normal; height: 100%; box-sizing: content-box; padding-bottom: 30px; margin-bottom: -32px; display: inline-block; }
.CodeMirror-gutter-wrapper { position: absolute; z-index: 4; background: 0px 0px !important; border: none !important; }
.CodeMirror-gutter-background { position: absolute; top: 0px; bottom: 0px; z-index: 4; }
.CodeMirror-gutter-elt { position: absolute; cursor: default; z-index: 4; }
.CodeMirror-lines { cursor: text; }
.CodeMirror pre { border-radius: 0px; border-width: 0px; background: 0px 0px; font-family: inherit; font-size: inherit; margin: 0px; white-space: pre; overflow-wrap: normal; color: inherit; z-index: 2; position: relative; overflow: visible; }
.CodeMirror-wrap pre { overflow-wrap: break-word; white-space: pre-wrap; word-break: normal; }
.CodeMirror-code pre { border-right: 30px solid transparent; width: fit-content; }
.CodeMirror-wrap .CodeMirror-code pre { border-right: none; width: auto; }
.CodeMirror-linebackground { position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; z-index: 0; }
.CodeMirror-linewidget { position: relative; z-index: 2; overflow: auto; }
.CodeMirror-wrap .CodeMirror-scroll { overflow-x: hidden; }
.CodeMirror-measure { position: absolute; width: 100%; height: 0px; overflow: hidden; visibility: hidden; }
.CodeMirror-measure pre { position: static; }
.CodeMirror div.CodeMirror-cursor { position: absolute; visibility: hidden; border-right: none; width: 0px; }
.CodeMirror div.CodeMirror-cursor { visibility: hidden; }
.CodeMirror-focused div.CodeMirror-cursor { visibility: inherit; }
.cm-searching { background: rgba(255, 255, 0, 0.4); }
span.cm-underlined { text-decoration: underline; }
span.cm-strikethrough { text-decoration: line-through; }
.cm-tw-syntaxerror { color: rgb(255, 255, 255); background-color: rgb(153, 0, 0); }
.cm-tw-deleted { text-decoration: line-through; }
.cm-tw-header5 { font-weight: 700; }
.cm-tw-listitem:first-child { padding-left: 10px; }
.cm-tw-box { border-style: solid; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-color: inherit; border-top-width: 0px !important; }
.cm-tw-underline { text-decoration: underline; }
@media print {
.CodeMirror div.CodeMirror-cursor { visibility: hidden; }
}
:root {
--primary-color: #2875d9;
}
* {
box-sizing: border-box;
}
html, body, #write {
font-family: 'Source Han Serif CN', serif;
}
pre > *, code {
font-family: 'Fira Code','Source Han Serif CN';
font-size: 16px;
}
h1, h2, h3, h4, h5, h6 {
font-family: 'Source Han Serif CN', serif;
}
h1 {
font-size: 2.2rem;
}
h2 {
font-size: 1.8rem;
}
h3 {
font-size: 1.6rem;
}
h4 {
font-size: 1.4rem;
}
h5 {
font-size: 1.2rem;
}
h6 {
font-size: 1rem;
}
html {
font-size: 18px;
}
body, #write {
max-width: 914px;
text-align: justify;
display: block;
margin: auto;
}
p {
font-size: 1rem;
}
a {
color: var(--primary-color);
}
/* Stuff */
sup.md-footnote {
background-color: transparent;
color: var(--primary-color);
padding: 1px;
margin: 0;
}
code, .md-fences, .sourceCode {
background: #f2f2f2;
}
.md-fences, pre.sourceCode {
font-size: .7em;
padding: .5rem;
}
.show-fences-line-number .md-fences {
padding-left: .5rem;
}
blockquote {
margin: 2rem .2rem;
padding: .3rem .5rem;
color: #6f6f6f;
/* border-left: .4rem solid gray; */
border-left: 4px solid gray;
}
/* blockquote p {
padding: .2rem 1.5rem;
} */
blockquote p {
padding: .0.5rem 0.5rem;
}
mark {
background: yellow;
}
figure {
margin: .5rem auto;
max-width: 100%;
display: block;
margin: auto;
}
figcaption {
text-align: center;
margin: .5rem auto;
}
img {
max-width: 100%;
display: block;
margin: auto;
}
/*
Print Mode and page breaks.
WkhtmlToPDF does not work on @print mode
*/
@media (max-width: 210mm) {
img {
max-height: 8cm;
}
html {
font-size: 16px;
}
div[title="pb"], page-break {
display: block;
page-break-after: always;
break-after: page;
}
nav {
font-size: .9rem;
page-break-after: always;
page-break-before: always;
}
nav a {
color: black;
}
/* Move top-level headings to a new page on the right-hand side: */
h1, h2 {
page-break-before: right;
break-before: recto;
}
/* Override the previous ruleset for the very first heading: */
h1:first-of-type, section>h1:first-child {
page-break-before: avoid;
break-before: avoid;
}
/* Headings should not be the last paragraph on a page: */
h1, h2, h3, h4, h5, h6 {
page-break-after: avoid;
}
/* Consecutive headings with deepening level should not be split across pages: */
h1+h2, h2+h3, h3+h4, h4+h5, h5+h6 {
page-break-before: avoid;
}
}
table {
padding: 0;
word-break: initial;
}
table tr {
border-top: 1px solid #dfe2e5;
margin: 0;
padding: 0;
}
table tr:nth-child(2n),
thead {
background-color: #f8f8f8;
}
table tr th {
font-weight: bold;
border: 1px solid #dfe2e5;
border-bottom: 0;
margin: 0;
padding: 6px 13px;
}
table tr td {
border: 1px solid #dfe2e5;
margin: 0;
padding: 6px 13px;
}
table tr th:first-child,
table tr td:first-child {
margin-top: 0;
}
table tr th:last-child,
table tr td:last-child {
margin-bottom: 0;
}
@media print {
/* body > #write {
font-size: 16px;
} */
pre > *, code {
font-size: 14px;
}
}
</style><title>CS302-OS-Notes</title>
</head>
<body class='typora-export os-windows'><div class='typora-export-content'>
<div id='write' class=''><h1 id='操作系统-operating-system'><span>操作系统 Operating System</span></h1><p><span>南方科技大学 计算机科学与工程系 11812804 董正</span></p><hr /><div class='md-toc' mdtype='toc'><p class="md-toc-content" role="list"><span role="listitem" class="md-toc-item md-toc-h1" data-ref="n0"><a class="md-toc-inner" href="#操作系统-operating-system">操作系统 Operating System</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n6"><a class="md-toc-inner" href="#前言-preface">前言 Preface</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n10"><a class="md-toc-inner" href="#第三章-进程-process">第三章 进程 Process</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n11"><a class="md-toc-inner" href="#31-基本概念">3.1 基本概念</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n20"><a class="md-toc-inner" href="#311-进程的概念">3.1.1 进程的概念</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n51"><a class="md-toc-inner" href="#312-进程的状态-process-states">3.1.2 进程的状态 Process States</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n89"><a class="md-toc-inner" href="#313-进程控制块-process-control-block">3.1.3 进程控制块 Process Control Block</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n122"><a class="md-toc-inner" href="#32-进程生命周期">3.2 进程生命周期</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n123"><a class="md-toc-inner" href="#321-进程标识符-process-identifier">3.2.1 进程标识符 Process Identifier</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n134"><a class="md-toc-inner" href="#322-进程创建-process-creation">3.2.2 进程创建 Process Creation</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n162"><a class="md-toc-inner" href="#323-进程执行-process-execution">3.2.3 进程执行 Process Execution</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n195"><a class="md-toc-inner" href="#324-进程等待">3.2.4 进程等待</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n227"><a class="md-toc-inner" href="#325-进程时间">3.2.5 进程时间</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n247"><a class="md-toc-inner" href="#326-进程终止-process-termination">3.2.6 进程终止 Process Termination</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n316"><a class="md-toc-inner" href="#327-进程生命周期-process-lifecycle">3.2.7 进程生命周期 Process Lifecycle</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n344"><a class="md-toc-inner" href="#第四章-线程-thread">第四章 线程 Thread</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n345"><a class="md-toc-inner" href="#41-线程的概念">4.1 线程的概念</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n372"><a class="md-toc-inner" href="#42-线程的组成">4.2 线程的组成</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n401"><a class="md-toc-inner" href="#43-线程和进程的区别">4.3 线程和进程的区别</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n440"><a class="md-toc-inner" href="#44-线程的生命周期-thread-lifecycle">4.4 线程的生命周期 Thread Lifecycle</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n443"><a class="md-toc-inner" href="#45-多线程进程-multithreaded-process">4.5 多线程进程 Multithreaded Process</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n453"><a class="md-toc-inner" href="#46-多线程调度">4.6 多线程调度</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n463"><a class="md-toc-inner" href="#47-multiprocessing-multithreading-and-multiprogramming">4.7 Multiprocessing, Multithreading and Multiprogramming</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n477"><a class="md-toc-inner" href="#第五章-进程调度-process-scheduling">第五章 进程调度 Process Scheduling</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n478"><a class="md-toc-inner" href="#51-基本概念">5.1 基本概念</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n479"><a class="md-toc-inner" href="#511-上下文切换-context-switch">5.1.1 上下文切换 Context Switch</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n505"><a class="md-toc-inner" href="#512-调度队列">5.1.2 调度队列</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n523"><a class="md-toc-inner" href="#513-调度程序-scheduler">5.1.3 调度程序 Scheduler</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n564"><a class="md-toc-inner" href="#514-dispatcher">5.1.4 Dispatcher</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n619"><a class="md-toc-inner" href="#52-调度准则">5.2 调度准则</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n640"><a class="md-toc-inner" href="#53-调度算法-scheduling-algorithm">5.3 调度算法 Scheduling Algorithm</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n641"><a class="md-toc-inner" href="#531-先到先服务调度-first-come-first-served-fcfs">5.3.1 先到先服务调度 First-Come-First-Served (FCFS)</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n643"><a class="md-toc-inner" href="#532-最短作业优先调度-shortest-job-first-sjf">5.3.2 最短作业优先调度 Shortest-Job-First (SJF)</a></span><span role="listitem" class="md-toc-item md-toc-h5" data-ref="n653"><a class="md-toc-inner" href="#5321-非抢占-non-preemptive-sjf">5.3.2.1 非抢占 (Non-Preemptive) SJF</a></span><span role="listitem" class="md-toc-item md-toc-h5" data-ref="n655"><a class="md-toc-inner" href="#5322-抢占-sjf">5.3.2.2 抢占 SJF</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n663"><a class="md-toc-inner" href="#533-轮转调度-round-robin-rr">5.3.3 轮转调度 Round Robin (RR)</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n689"><a class="md-toc-inner" href="#534-优先级调度-priority-scheduling">5.3.4 优先级调度 Priority Scheduling</a></span><span role="listitem" class="md-toc-item md-toc-h5" data-ref="n704"><a class="md-toc-inner" href="#5341-multiple-queue-priority-scheduling">5.3.4.1 Multiple Queue Priority Scheduling</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n719"><a class="md-toc-inner" href="#第六章-同步-synchronization">第六章 同步 Synchronization</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n720"><a class="md-toc-inner" href="#61-进程间通信-inter-process-communication-ipc">6.1 进程间通信 Inter-Process Communication (IPC)</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n747"><a class="md-toc-inner" href="#62-临界区-critical-section">6.2 临界区 Critical Section</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n748"><a class="md-toc-inner" href="#621-竞争条件-race-condition">6.2.1 竞争条件 Race Condition</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n754"><a class="md-toc-inner" href="#622-临界区问题-critical-section-problem">6.2.2 临界区问题 Critical Section Problem</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n776"><a class="md-toc-inner" href="#623-临界区问题的要求">6.2.3 临界区问题的要求</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n790"><a class="md-toc-inner" href="#63-临界区问题的解决方案-solutions-for-critical-section-problem">6.3 临界区问题的解决方案 Solutions for Critical Section Problem</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n811"><a class="md-toc-inner" href="#631-硬件同步-×-hardware-synchronization">6.3.1 硬件同步 (×) Hardware Synchronization</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n824"><a class="md-toc-inner" href="#632-基本自旋锁-×-basic-spin-lock">6.3.2 基本自旋锁 (×) Basic Spin Lock</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n839"><a class="md-toc-inner" href="#633-petersons-solution">6.3.3 Peterson's Solution</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n898"><a class="md-toc-inner" href="#634-信号量-semaphore">6.3.4 信号量 Semaphore</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n926"><a class="md-toc-inner" href="#64-经典同步问题">6.4 经典同步问题</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n927"><a class="md-toc-inner" href="#641-有界缓冲问题-bounded-buffer-problem">6.4.1 有界缓冲问题 Bounded-Buffer Problem</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n976"><a class="md-toc-inner" href="#642-读者-作者问题-reader-writer-problem">6.4.2 读者-作者问题 Reader-Writer Problem</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n987"><a class="md-toc-inner" href="#643-哲学家就餐问题-dining-philosophers-problem">6.4.3 哲学家就餐问题 Dining-Philosophers Problem</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n1034"><a class="md-toc-inner" href="#第七章-死锁-deadlock">第七章 死锁 Deadlock</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1035"><a class="md-toc-inner" href="#71-死锁的概念">7.1 死锁的概念</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1060"><a class="md-toc-inner" href="#72-死锁的特征">7.2 死锁的特征</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1061"><a class="md-toc-inner" href="#721-死锁的必要条件">7.2.1 死锁的必要条件</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1085"><a class="md-toc-inner" href="#722-资源分配图-resource-allocation-graph">7.2.2 资源分配图 Resource-Allocation Graph</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1112"><a class="md-toc-inner" href="#73-死锁的处理方法">7.3 死锁的处理方法</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1123"><a class="md-toc-inner" href="#74-死锁检测-deadlock-detection">7.4 死锁检测 Deadlock Detection</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1124"><a class="md-toc-inner" href="#741-死锁检测算法">7.4.1 死锁检测算法</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1134"><a class="md-toc-inner" href="#742-死锁恢复">7.4.2 死锁恢复</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1149"><a class="md-toc-inner" href="#75-死锁预防-deadlock-prevention">7.5 死锁预防 Deadlock Prevention</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1178"><a class="md-toc-inner" href="#76-死锁避免-deadlock-avoidance">7.6 死锁避免 Deadlock Avoidance</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1179"><a class="md-toc-inner" href="#761-安全状态">7.6.1 安全状态</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1189"><a class="md-toc-inner" href="#762-资源分配图算法">7.6.2 资源分配图算法</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1201"><a class="md-toc-inner" href="#763-银行家算法-bankers-algorithm">7.6.3 银行家算法 Banker's Algorithm</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n1232"><a class="md-toc-inner" href="#第八章-内存管理策略">第八章 内存管理策略</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1233"><a class="md-toc-inner" href="#81-背景">8.1 背景</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1235"><a class="md-toc-inner" href="#811-aspects-of-memory-multiplexing">8.1.1 Aspects of Memory Multiplexing</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1246"><a class="md-toc-inner" href="#812-地址绑定-address-binding">8.1.2 地址绑定 Address Binding</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1262"><a class="md-toc-inner" href="#813-逻辑地址空间与物理地址空间">8.1.3 逻辑地址空间与物理地址空间</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1279"><a class="md-toc-inner" href="#814-动态加载-dynamic-loading">8.1.4 动态加载 Dynamic Loading</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1285"><a class="md-toc-inner" href="#815-动态链接与共享库">8.1.5 动态链接与共享库</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1297"><a class="md-toc-inner" href="#82-交换-swap">8.2 交换 Swap</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1306"><a class="md-toc-inner" href="#83-连续内存分配-contiguous-memory-allocation">8.3 连续内存分配 Contiguous Memory Allocation</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1307"><a class="md-toc-inner" href="#831-uniprogramming">8.3.1 Uniprogramming</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1316"><a class="md-toc-inner" href="#832-内存保护-protection">8.3.2 内存保护 Protection</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1327"><a class="md-toc-inner" href="#833-多分区方法-multiple-partition-method">8.3.3 多分区方法 Multiple-Partition Method</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1373"><a class="md-toc-inner" href="#833-碎片-fragmentation">8.3.3 碎片 Fragmentation</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1392"><a class="md-toc-inner" href="#84-分段-segmentation">8.4 分段 Segmentation</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1463"><a class="md-toc-inner" href="#85-分页-paging">8.5 分页 Paging</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1471"><a class="md-toc-inner" href="#851-页表-page-table">8.5.1 页表 Page Table</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1525"><a class="md-toc-inner" href="#852-共享页">8.5.2 共享页</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1527"><a class="md-toc-inner" href="#853-分层分页-multilevel-paging">8.5.3 分层分页 Multilevel Paging</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1549"><a class="md-toc-inner" href="#854-分段分页">8.5.4 分段+分页</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n1571"><a class="md-toc-inner" href="#第九章-虚拟内存管理">第九章 虚拟内存管理</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1572"><a class="md-toc-inner" href="#91-缓存-cache">9.1 缓存 Cache</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1606"><a class="md-toc-inner" href="#92-转换表缓冲区-transition-look-aside-buffer-tlb">9.2 转换表缓冲区 Transition Look-aside Buffer (TLB)</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1656"><a class="md-toc-inner" href="#93-请求调页-demand-paging">9.3 请求调页 Demand Paging</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1657"><a class="md-toc-inner" href="#931-基本概念">9.3.1 基本概念</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1682"><a class="md-toc-inner" href="#932-请求调页的性能">9.3.2 请求调页的性能</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1771"><a class="md-toc-inner" href="#94-页面置换-page-replacement">9.4 页面置换 Page Replacement</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1772"><a class="md-toc-inner" href="#941-cache-miss-的分类">9.4.1 Cache Miss 的分类</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1810"><a class="md-toc-inner" href="#942-fifo-页面置换">9.4.2 FIFO 页面置换</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1827"><a class="md-toc-inner" href="#943-最优页面置换-min">9.4.3 最优页面置换 MIN</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1842"><a class="md-toc-inner" href="#944-lru-页面置换">9.4.4 LRU 页面置换</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1864"><a class="md-toc-inner" href="#945-近似-lru-页面置换">9.4.5 近似 LRU 页面置换</a></span><span role="listitem" class="md-toc-item md-toc-h5" data-ref="n1865"><a class="md-toc-inner" href="#9451-时钟算法-clock-algorithm">9.4.5.1 时钟算法 Clock Algorithm</a></span><span role="listitem" class="md-toc-item md-toc-h5" data-ref="n1908"><a class="md-toc-inner" href="#9452-second-chance-list-algorithm">9.4.5.2 Second Chance List Algorithm</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1930"><a class="md-toc-inner" href="#95-帧分配-frame-allocation">9.5 帧分配 Frame Allocation</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1931"><a class="md-toc-inner" href="#951-全局分配与局部分配">9.5.1 全局分配与局部分配</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1939"><a class="md-toc-inner" href="#952-分配算法">9.5.2 分配算法</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n1966"><a class="md-toc-inner" href="#第十--十一章--文件系统-file-system">第十 & 十一章 文件系统 File System</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1967"><a class="md-toc-inner" href="#101-文件系统概念">10.1 文件系统概念</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n1985"><a class="md-toc-inner" href="#102-文件和目录-files-and-directories">10.2 文件和目录 Files and Directories</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n1986"><a class="md-toc-inner" href="#1021-目录的组成">10.2.1 目录的组成</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2002"><a class="md-toc-inner" href="#1022-文件">10.2.2 文件</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2029"><a class="md-toc-inner" href="#103-磁盘管理策略">10.3 磁盘管理策略</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2064"><a class="md-toc-inner" href="#104-目录分配">10.4 目录分配</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2065"><a class="md-toc-inner" href="#1041-连续分配-contiguous-allocation">10.4.1 连续分配 Contiguous Allocation</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2089"><a class="md-toc-inner" href="#1042-链接分配-linked-allocation">10.4.2 链接分配 Linked Allocation</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2118"><a class="md-toc-inner" href="#1043-索引分配-index-allocation">10.4.3 索引分配 Index Allocation</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2135"><a class="md-toc-inner" href="#105-文件分配表-fat">10.5 文件分配表 FAT</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2136"><a class="md-toc-inner" href="#1051-fat-的原理">10.5.1 FAT 的原理</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2146"><a class="md-toc-inner" href="#1052-fat-文件系统的大小">10.5.2 FAT 文件系统的大小</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2159"><a class="md-toc-inner" href="#1053-fat-文件系统结构">10.5.3 FAT 文件系统结构</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2161"><a class="md-toc-inner" href="#1054-fat-文件遍历">10.5.4 FAT 文件遍历</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2168"><a class="md-toc-inner" href="#1055-fat-directory-entry">10.5.5 FAT Directory Entry</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2203"><a class="md-toc-inner" href="#1056-fat-读文件">10.5.6 FAT 读文件</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2208"><a class="md-toc-inner" href="#1057-fat-写文件">10.5.7 FAT 写文件</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2217"><a class="md-toc-inner" href="#1058-fat-删除文件">10.5.8 FAT 删除文件</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2235"><a class="md-toc-inner" href="#1059-总结">10.5.9 总结</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2264"><a class="md-toc-inner" href="#106-inode">10.6 iNode</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2265"><a class="md-toc-inner" href="#1061-inode-的原理">10.6.1 iNode 的原理</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2281"><a class="md-toc-inner" href="#1062-inode-的结构">10.6.2 iNode 的结构</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2294"><a class="md-toc-inner" href="#1063-inode-文件大小">10.6.3 iNode 文件大小</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2297"><a class="md-toc-inner" href="#107-可扩展文件系统-ext">10.7 可扩展文件系统 Ext</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2305"><a class="md-toc-inner" href="#1071-ext-文件系统的大小">10.7.1 Ext 文件系统的大小</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2315"><a class="md-toc-inner" href="#1072-ext-文件系统结构">10.7.2 Ext 文件系统结构</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2358"><a class="md-toc-inner" href="#1073-ext-的-inode-结构">10.7.3 Ext 的 iNode 结构</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2360"><a class="md-toc-inner" href="#1074-ext-删除文件">10.7.4 Ext 删除文件</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2363"><a class="md-toc-inner" href="#1075-硬链接-hard-link">10.7.5 硬链接 Hard Link</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2385"><a class="md-toc-inner" href="#1076-符号链接-软链接-symbolic-soft-link">10.7.6 符号链接 (软链接) Symbolic (Soft) Link</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2417"><a class="md-toc-inner" href="#108-ntfs">10.8 NTFS</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2430"><a class="md-toc-inner" href="#1081-ntfs-文件系统结构">10.8.1 NTFS 文件系统结构</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2454"><a class="md-toc-inner" href="#1082-ntfs-文件存储">10.8.2 NTFS 文件存储</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2467"><a class="md-toc-inner" href="#109-内存映射文件-memory-mapped-file">10.9 内存映射文件 Memory Mapped File</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2486"><a class="md-toc-inner" href="#1010-文件系统总结">10.10 文件系统总结</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n2540"><a class="md-toc-inner" href="#第十二章-大容量存储结构">第十二章 大容量存储结构</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2541"><a class="md-toc-inner" href="#121-大容量存储结构概述">12.1 大容量存储结构概述</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2542"><a class="md-toc-inner" href="#1211-磁盘-magnetic-disk-hard-disk">12.1.1 磁盘 Magnetic Disk (Hard Disk)</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2561"><a class="md-toc-inner" href="#1212-磁盘性能">12.1.2 磁盘性能</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2615"><a class="md-toc-inner" href="#1212-固态磁盘-solid-state-disk-ssd">12.1.2 固态磁盘 Solid State Disk (SSD)</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2618"><a class="md-toc-inner" href="#122-磁盘调度-disk-scheduling">12.2 磁盘调度 Disk Scheduling</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2630"><a class="md-toc-inner" href="#1221-fcfs-调度">12.2.1 FCFS 调度</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2634"><a class="md-toc-inner" href="#1222-sstf-调度">12.2.2 SSTF 调度</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2641"><a class="md-toc-inner" href="#1223-scan-调度">12.2.3 SCAN 调度</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2649"><a class="md-toc-inner" href="#1224-c-scan-调度">12.2.4 C-SCAN 调度</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2658"><a class="md-toc-inner" href="#1225-look-与-c-look-调度">12.2.5 LOOK 与 C-LOOK 调度</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2665"><a class="md-toc-inner" href="#1226-调度算法的选择">12.2.6 调度算法的选择</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n2678"><a class="md-toc-inner" href="#第十三章-io-系统">第十三章 I/O 系统</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2680"><a class="md-toc-inner" href="#131-io-硬件">13.1 I/O 硬件</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2751"><a class="md-toc-inner" href="#132-cpu-访问-io-设备">13.2 CPU 访问 I/O 设备</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2799"><a class="md-toc-inner" href="#133-控制器与-io-设备的数据传输">13.3 控制器与 I/O 设备的数据传输</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2819"><a class="md-toc-inner" href="#134-io-设备与-cpu-通信">13.4 I/O 设备与 CPU 通信</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2820"><a class="md-toc-inner" href="#1341-轮询-polling">13.4.1 轮询 Polling</a></span><span role="listitem" class="md-toc-item md-toc-h4" data-ref="n2846"><a class="md-toc-inner" href="#1342-io-中断-io-interrupt">13.4.2 I/O 中断 I/O Interrupt</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2894"><a class="md-toc-inner" href="#135-io-请求生命周期">13.5 I/O 请求生命周期</a></span><span role="listitem" class="md-toc-item md-toc-h3" data-ref="n2897"><a class="md-toc-inner" href="#136-io-性能">13.6 I/O 性能</a></span><span role="listitem" class="md-toc-item md-toc-h2" data-ref="n2963"><a class="md-toc-inner" href="#end">END</a></span></p></div><hr /><h2 id='前言-preface'><span>前言 Preface</span></h2><p><span>笔记结构基于《操作系统概念(第九版)》</span></p><p><span>Based on </span><em><span>Operating System Concepts Ninth Edition</span></em></p><hr /><h2 id='第三章-进程-process'><span>第三章 进程 Process</span></h2><h3 id='31-基本概念'><span>3.1 基本概念</span></h3><ul><li><p><span>CPU 活动</span></p><ul><li><span>批处理系统: 作业 (job)</span></li><li><span>分时系统: 用户程序 (user program) 或任务 (task)</span></li></ul></li></ul><h4 id='311-进程的概念'><span>3.1.1 进程的概念</span></h4><ul><li><p><strong><span>进程 (Process)</span></strong><span> 是执行的程序</span></p><p><span>Process is a program in execution</span></p></li><li><p><span>进程还包括:</span></p><ul><li><span>程序计数器 PC</span></li><li><span>寄存器 (Register) 的内容</span></li><li><span>堆 Heap</span></li><li><span>栈 Stack</span></li><li><span>数据段 Data Section</span></li><li><span>文本段 Text Section</span></li><li><span>...</span></li></ul></li><li><p><span>程序 (Program) 和进程</span></p><ul><li><span>程序是</span><strong><span>被动实体 (passive entity)</span></strong><span>,如存储在磁盘上包含一系列指令的文件,经常称为可执行文件 (executable file)</span></li><li><span>进程是</span><strong><span>活动实体 (active entity)</span></strong><span> 或称</span><strong><span>主动实体</span></strong><span>,具有一个程序计数器用来表示下个执行命令和一组相关资源</span></li><li><span>当一个可执行文件被加载到内存时,这个程序就成为进程</span></li></ul></li></ul><h4 id='312-进程的状态-process-states'><span>3.1.2 进程的状态 Process States</span></h4><figure><table><thead><tr><th style='text-align:center;' ><span>状态</span></th><th style='text-align:center;' ><span>英文</span></th><th style='text-align:center;' ><span>说明</span></th></tr></thead><tbody><tr><td style='text-align:center;' ><span>新的</span></td><td style='text-align:center;' ><span>new</span></td><td style='text-align:center;' ><span>进程正在创建</span></td></tr><tr><td style='text-align:center;' ><span>运行</span></td><td style='text-align:center;' ><span>running</span></td><td style='text-align:center;' ><span>指令正在执行</span></td></tr><tr><td style='text-align:center;' ><span>等待</span></td><td style='text-align:center;' ><span>waiting/blocked</span></td><td style='text-align:center;' ><span>进程等待发生某个事件,如 IO 完成或收到信号</span></td></tr><tr><td style='text-align:center;' ><span>就绪</span></td><td style='text-align:center;' ><span>ready</span></td><td style='text-align:center;' ><span>进程等待分配处理器</span></td></tr><tr><td style='text-align:center;' ><span>终止</span></td><td style='text-align:center;' ><span>terminated</span></td><td style='text-align:center;' ><span>进程已经完成执行</span></td></tr></tbody></table></figure><p><img src="D:\TyporaPictures\OS\2.png" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\1.jpg" referrerpolicy="no-referrer"></p><ul><li><p><span>等待状态又分为</span></p><ul><li><span>可中断 (Interruptable)</span></li><li><span>不可中断 (Un-interruptible)</span></li></ul></li><li><p><span>刚 fork 的进程都会变成 ready 状态</span></p></li></ul><h4 id='313-进程控制块-process-control-block'><span>3.1.3 进程控制块 Process Control Block</span></h4><p><span>进程控制块 PCB (任务控制块 Task Control Block)</span></p><p><span>在内存 (Main Memory) 里</span></p><p><img src="D:\TyporaPictures\OS\3.jpg" referrerpolicy="no-referrer"></p><p><span>PCB 是系统感知进程存在的唯一标志</span></p><ul><li><p><span>进程状态 Process State</span></p></li><li><p><span>程序计数器 PC</span></p></li><li><p><span>CPU 寄存器 CPU Register</span></p></li><li><p><span>CPU 调度信息 CPU-scheduling Infomation</span></p><p><span>进程优先级,调度队列的指针和其他调度参数</span></p></li><li><p><span>内存管理信息 Memory-management Infomation</span></p><p><span>基地址,界限寄存器的值,页表或段表等</span></p></li><li><p><span>记账信息 Accounting Infomation</span></p><p><span>CPU 时间,实际使用时间,时间期限,记账数据,作业或进程数量等</span></p></li><li><p><span>IO 状态信息 IO Status Infomation</span></p><p><span>分配给进程的 IO 设备列表,打开文件列表等</span></p><p><img src="D:\TyporaPictures\OS\16.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>几个概念</span></p><ul><li><span>PCB = 进程表 = </span><code>task_struct</code><span> in Linux</span></li><li><span>Task list = PCB 组成的双向链表</span></li></ul></li></ul><hr /><h3 id='32-进程生命周期'><span>3.2 进程生命周期</span></h3><h4 id='321-进程标识符-process-identifier'><span>3.2.1 进程标识符 Process Identifier</span></h4><ul><li><p><span>进程标识符 Process Identifier (PID)</span></p><p><span>系统的每个进程都有一个唯一的整数 PID</span></p><p><span>System call </span><code>getpid()</code><span>: return the PID of the calling process</span></p></li><li><p><code>init</code><span> 进程</span></p><p><span>PID = 1, 所有用户进程的父进程或根进程</span></p><p><span>代码位于 </span><code>/sbin /init</code></p><p><span>它的第一个任务是创建进程 </span><code>fork()+exec*()</code></p></li></ul><h4 id='322-进程创建-process-creation'><span>3.2.2 进程创建 Process Creation</span></h4><ul><li><p><span>System call </span><code>fork()</code><span>: creates a new process by duplicating the calling process.</span></p><p><span>fork 出的子进程从 fork 调用的下一行开始执行(因为 PC 也复制了)</span></p><p><img src="D:\TyporaPictures\OS\5.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\4.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>在代码中如何区分父进程和子进程</span></p><p><code>pid=fork()</code><span>, 则父进程中 </span><code>pid</code><span> 变量等于子进程的 PID,子进程中 </span><code>pid=0</code></p><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="c"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 33px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -29px; width: 29px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">if</span> (<span class="cm-operator">!</span><span class="cm-variable">pid</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">// 只有子进程执行</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">else</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-comment">// 只有父进程执行</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 132px;"></div><div class="CodeMirror-gutters" style="height: 132px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div></pre></li><li><p><span>父进程和子进程执行顺序不确定</span></p></li><li><p><code>fork</code><span> 的流程, 内核空间的动作</span></p><ol start='' ><li><p><span>复制 PCB</span></p><p><img src="D:\TyporaPictures\OS\12.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>更新 PCB 和 task list</span></p><p><img src="D:\TyporaPictures\OS\13.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>复制用户空间</span></p><p><img src="D:\TyporaPictures\OS\14.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>return</span></p><p><img src="D:\TyporaPictures\OS\15.PNG" referrerpolicy="no-referrer"></p></li></ol></li></ul><h4 id='323-进程执行-process-execution'><span>3.2.3 进程执行 Process Execution</span></h4><ul><li><p><span>System call </span><code>exec*()</code></p></li><li><p><span>Example: </span><code>ls -l</code></p><p><img src="D:\TyporaPictures\OS\6.PNG" referrerpolicy="no-referrer"></p><p><code>args[0]</code><span> 是程序的名字</span></p></li><li><p><span>The process is changing the code that is executing and never returns to the original code.</span></p><p><code>exec*()</code><span> 之后的代码不会执行了,因为调用之后该进程就去执行 </span><code>exec</code><span> 指定的程序了</span></p></li><li><p><span>User space 的信息被覆盖</span></p><ul><li><p><span>Program Code</span></p></li><li><p><span>Memory</span></p><ul><li><span>Local Variables</span></li><li><span>Global Variables</span></li><li><span>Dynamically Allocated Memory</span></li></ul></li><li><p><span>Register Value: 如 PC</span></p></li></ul></li><li><p><span>Kernel space 的信息保留: PID, 进程关系等</span></p></li><li><p><code>exec*()</code><span> 的内核执行过程</span></p><p><img src="D:\TyporaPictures\OS\17.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\18.PNG" referrerpolicy="no-referrer"></p></li></ul><h4 id='324-进程等待'><span>3.2.4 进程等待</span></h4><ol start='' ><li><p><span>System call </span><code>wait()</code></p><ul><li><p><span>Suspend the calling process to waiting state and return (wakes up) when</span></p><ul><li><span>one of its child processes changes from running to terminated</span></li><li><span>received a signal</span></li></ul></li><li><p><span>Return immediately (i.e., does nothing) if</span></p><ul><li><span>it has no children</span></li><li><span>a child terminates before the parent calls wait</span></li></ul></li><li><p><span>给子进程收尸 见 </span><a href='#3.2.6'><span>3.2.6</span></a></p></li></ul><p><img src="D:\TyporaPictures\OS\8.PNG" referrerpolicy="no-referrer"></p></li></ol><p><img src="D:\TyporaPictures\OS\7.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\19.PNG" referrerpolicy="no-referrer"></p><ol start='2' ><li><p><code>fork()+exec*()+wait()=system()</code></p><p><img src="D:\TyporaPictures\OS\9.PNG" referrerpolicy="no-referrer"></p><p><span>例: shell 里输入命令 -> 执行相应程序 -> 程序终止 -> 返回 shell</span></p></li></ol><ul><li><span>除了 </span><code>init</code><span>, 所有的进程都是 </span><code>fork()+exec*()</code><span> 来的</span></li></ul><h4 id='325-进程时间'><span>3.2.5 进程时间</span></h4><ul><li><p><span>实际时间 Real Time</span></p><p><span>Wall-clock time</span></p></li><li><p><span>用户时间 User Time</span></p><p><span>CPU 在用户空间花费的时间</span></p></li><li><p><span>系统时间 System Time</span></p><p><span>CPU 在内核空间花费的时间</span></p></li></ul><p><img src="D:\TyporaPictures\OS\11.PNG" referrerpolicy="no-referrer"></p><ul><li><p><span>User Time + Sys Time 决定了程序的性能 (Performance)</span></p><ul><li><span>User Time + Sys Time > Real Time: 单核</span></li><li><span>User Time + Sys Time < Real Time: 多核</span></li></ul></li></ul><h4 id='326-进程终止-process-termination'><span id="3.2.6"><span>3.2.6 进程终止 Process Termination</span></span></h4><p><img src="D:\TyporaPictures\OS\27.PNG" referrerpolicy="no-referrer"></p><ul><li><p><span>System call </span><code>exit()</code><span>: terminate the calling process</span></p></li><li><p><code>exit()</code><span> 的执行过程</span></p><ol start='' ><li><p><span>Clean up most of the allocated kernel space memory</span></p><p><img src="D:\TyporaPictures\OS\20.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>Clean up the exit process's user space memory</span></p><p><img src="D:\TyporaPictures\OS\21.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>Notify the parent with SIGCHLD.</span></p><p><img src="D:\TyporaPictures\OS\22.PNG" referrerpolicy="no-referrer"></p></li></ol></li><li><p><span>僵尸进程 Zombie Process</span></p><ul><li><span>进程的用户空间和内核空间被释放之后,PID 依然在进程表里,直到父进程调用 </span><code>wait()</code></li><li><span>当进程已经终止,但是其父进程尚未调用 </span><code>wait()</code><span>,这样的进程称为僵尸进程</span></li><li><span>所有进程终止时都会过渡到这个状态</span></li><li><span>一旦父进程调用 </span><code>wait()</code><span>,僵尸进程的进程标识符和它在进程表中的条目就会释放</span></li></ul><p><img src="D:\TyporaPictures\OS\23.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\24.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\25.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\26.PNG" referrerpolicy="no-referrer"></p><ul><li><p><span>子进程先终止,父进程再调用 </span><code>wait()</code><span> 也可以,SIGCHLD 不会消失,但是这段间隔内僵尸进程就一直存在、占用资源</span></p></li><li><p><span>Linux 系统中僵尸进程被标为 </span><code><defunct></code></p><p><span>查看: </span><code>ps aux | grep <defunct></code></p></li><li><p><code>exit()</code><span> system call turns a process into a zombie when</span></p><ul><li><p><span>The process calls </span><code>exit()</code></p></li><li><p><span>The process returns from </span><code>main()</code></p></li><li><p><span>The process terminates abnormally</span></p><p><span>这种情况下 kernel 会帮忙给他调用 </span><code>exit()</code></p></li></ul></li></ul></li><li><p><span>The fork bomb</span></p><ul><li><p><span>PID 是有限的,Linux 中 PID 最大值为 32768</span></p><p><code>cat / proc /sys/ pid_max</code></p></li><li><p><span>fork bomb (僵尸大军)</span></p><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="c"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 33px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>4</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -29px; width: 29px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">int</span> <span class="cm-def">main</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">while</span> (<span class="cm-variable">fork</span>());</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-number">0</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 88px;"></div><div class="CodeMirror-gutters" style="height: 88px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div></pre></li></ul></li><li><p><span>孤儿进程 Orphan Process</span></p><ul><li><span>父进程没有调用 </span><code>wait()</code><span> 就终止,子进程变成孤儿进程</span></li><li><span>Linux & UNIX: 将 </span><code>init</code><span> 进程作为孤儿进程的父进程 (Re-parent)</span></li><li><span>这个操作在 </span><code>exit()</code><span> 里完成,见下图</span></li></ul></li><li><p><code>init</code><span> 进程定期调用 </span><code>wait()</code><span> 以便收集任何孤儿进程的退出状态,并释放孤儿进程标识符和进程表条目</span></p><p><img src="D:\TyporaPictures\OS\28.PNG" referrerpolicy="no-referrer"></p></li></ul><h4 id='327-进程生命周期-process-lifecycle'><span>3.2.7 进程生命周期 Process Lifecycle</span></h4><ol start='' ><li><p><span>forked</span></p><p><img src="D:\TyporaPictures\OS\29.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>Ready</span></p><p><img src="D:\TyporaPictures\OS\30.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>Running</span></p><p><img src="D:\TyporaPictures\OS\31.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>Blocked</span></p><p><img src="D:\TyporaPictures\OS\32.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>Interruptable waiting</span></p><p><img src="D:\TyporaPictures\OS\33.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>Un-interruptable waiting</span></p><p><img src="D:\TyporaPictures\OS\34.PNG" referrerpolicy="no-referrer"></p><p><span>计网的程序里经常碰见,纯贵物,谁设计的抓紧埋了吧</span></p></li><li><p><span>Return back to ready</span></p><p><img src="D:\TyporaPictures\OS\35.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>Terminated</span></p><p><img src="D:\TyporaPictures\OS\36.PNG" referrerpolicy="no-referrer"></p></li></ol><hr /><h2 id='第四章-线程-thread'><span>第四章 线程 Thread</span></h2><h3 id='41-线程的概念'><span>4.1 线程的概念</span></h3><ul><li><p><span>Heavyweight Process</span></p><p><span>A process has a single thread of control</span></p><p><img src="D:\TyporaPictures\OS\49.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>线程 Thread</span></p><p><span>A sequential execution stream within process</span></p><p><span>又称 Lightweight Process</span></p><ul><li><span>Process still contains a single Address Space</span></li><li><span>No protection between threads</span></li></ul></li><li><p><span>多线程 Multithreading</span></p><p><span>A single program made up of a number of different concurrent (并发) activities</span></p></li><li><p><span>结构图</span></p><p><img src="D:\TyporaPictures\OS\50.PNG" referrerpolicy="no-referrer"></p><ul><li><span>Threads encapsulate </span><strong><span>concurrency</span></strong><span>: "Active" component</span></li><li><span>Address spaces encapsulate </span><strong><span>protection</span></strong><span>: "Passive" part</span></li></ul></li></ul><hr /><h3 id='42-线程的组成'><span>4.2 线程的组成</span></h3><ul><li><p><span>State shared by all threads in process/address space</span></p><ul><li><span>Content of memory (global variables, heap)</span></li><li><span>I/O state (file descriptors, network connections, etc)</span></li></ul></li><li><p><span>State "private" to each thread</span></p><ul><li><span>Kept in TCB (Thread Control Block)</span></li><li><span>CPU registers (</span><mark><span>including PC</span></mark><span>)</span></li><li><span>Execution stack</span></li></ul></li><li><p><span>栈</span></p><ul><li><span>Parameters, temporary variables</span></li><li><span>Return PCs are kept while called procedures are executing</span></li><li><span>回忆计组学的,不多说</span></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\51.PNG" referrerpolicy="no-referrer"></p><hr /><h3 id='43-线程和进程的区别'><span>4.3 线程和进程的区别</span></h3><figure><table><thead><tr><th style='text-align:left;' ><span>Process</span></th><th style='text-align:left;' ><span>Thread</span></th></tr></thead><tbody><tr><td style='text-align:left;' ><span>Process means any program is in execution.</span></td><td style='text-align:left;' ><span>Thread means segment of a process.</span></td></tr><tr><td style='text-align:left;' ><span>Process takes more time to terminate.</span></td><td style='text-align:left;' ><span>Thread takes less time to terminate.</span></td></tr><tr><td style='text-align:left;' ><span>It takes more time for creation.</span></td><td style='text-align:left;' ><span>It takes less time for creation.</span></td></tr><tr><td style='text-align:left;' ><span>It also takes more time for context switching.</span></td><td style='text-align:left;' ><span>It takes less time for context switching.</span></td></tr><tr><td style='text-align:left;' ><span>Process is less efficient in term of communication.</span></td><td style='text-align:left;' ><span>Thread is more efficient in term of communication.</span></td></tr><tr><td style='text-align:left;' ><span>Process consume more resources.</span></td><td style='text-align:left;' ><span>Thread consume less resources.</span></td></tr><tr><td style='text-align:left;' ><span>Process is isolated.</span></td><td style='text-align:left;' ><span>Threads share memory.</span></td></tr><tr><td style='text-align:left;' ><span>Process is called heavy weight process.</span></td><td style='text-align:left;' ><span>Thread is called light weight process.</span></td></tr><tr><td style='text-align:left;' ><span>Process switching uses interface in operating system.</span></td><td style='text-align:left;' ><span>Thread switching does not require to call a operating system and cause an interrupt to the kernel.</span></td></tr><tr><td style='text-align:left;' ><span>If one process is blocked then it will not effect the execution of other process</span></td><td style='text-align:left;' ><span>Second thread in the same task couldnot run, while one server thread is blocked.</span></td></tr><tr><td style='text-align:left;' ><span>Process has its own Process Control Block, Stack and Address Space.</span></td><td style='text-align:left;' ><span>Thread has Parents' PCB, its own Thread Control Block and Stack and common Address space.</span></td></tr></tbody></table></figure><hr /><h3 id='44-线程的生命周期-thread-lifecycle'><span>4.4 线程的生命周期 Thread Lifecycle</span></h3><p><img src="D:\TyporaPictures\OS\52.PNG" referrerpolicy="no-referrer"></p><hr /><h3 id='45-多线程进程-multithreaded-process'><span>4.5 多线程进程 Multithreaded Process</span></h3><ul><li><p><span>PCB 指向多个 TCB</span></p><p><img src="D:\TyporaPictures\OS\53.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>Switching threads within a block is a simple thread switch</span></p></li><li><p><span>Switching threads across blocks requires changes to memory and I/O address tables</span></p></li></ul><hr /><h3 id='46-多线程调度'><span>4.6 多线程调度</span></h3><p><img src="D:\TyporaPictures\OS\54.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\55.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\56.PNG" referrerpolicy="no-referrer"></p><ul><li><p><span>超线程 Hyper-Threading</span></p><p><span>超线程(hyper-threading)其实就是</span><strong><span>同时多线程(simultaneous multi-theading)</span></strong><span>, 是一项允许一个CPU执行多个控制流的技术。它的原理很简单,就是把一颗CPU当成两颗来用,将一颗具有超线程功能的物理CPU变成两颗逻辑CPU,而逻辑CPU对操作系统来说,跟物理CPU并没有什么区别。因此,操作系统会把工作线程分派给这两颗(逻辑)CPU上去执行,让(多个或单个)应用程序的多个线程,能够同时在同一颗CPU上被执行。注意:两颗逻辑CPU共享单颗物理CPU的所有执行资源。因此,我们可以认为,</span><strong><span>超线程技术就是对CPU的虚拟化</span></strong></p></li></ul><p><img src="D:\TyporaPictures\OS\57.PNG" referrerpolicy="no-referrer"></p><hr /><h3 id='47-multiprocessing-multithreading-and-multiprogramming'><span>4.7 Multiprocessing, Multithreading and Multiprogramming</span></h3><ul><li><p><span>多进程 Multiprocessing</span></p><p><span>Multiple CPUs</span></p><p><span>A computer using more than one CPU at a time.</span></p></li><li><p><span>多线程 Multithreading</span></p><p><span>Multiple threads per Process</span></p></li><li><p><span>多道程序设计 Multiprogramming</span></p><p><span>Multiple Jobs or Processes</span></p><p><span>A computer running more than one program at a time</span></p></li></ul><hr /><h2 id='第五章-进程调度-process-scheduling'><span>第五章 进程调度 Process Scheduling</span></h2><h3 id='51-基本概念'><span>5.1 基本概念</span></h3><h4 id='511-上下文切换-context-switch'><span>5.1.1 上下文切换 Context Switch</span></h4><ul><li><p><span>切换 CPU 到另一个进程需要</span><strong><span>保存</span></strong><span>当前进程状态和</span><strong><span>恢复</span></strong><span>另一个进程的状态,这个任务称为上下文切换</span></p></li><li><p><span>当进行上下文切换时,内核会将旧进程的状态保存在其 </span><strong><span>PCB</span></strong><span> 中,然后加载经调度而要执行的新进程的上下文</span></p></li><li><p><span>上下文切换是纯粹的时间开销 (Overhead),因为 CPU 在此期间没有做任何有用工作</span></p></li><li><p><span>上下文切换非常耗时</span></p></li><li><p><span>什么时候 Context Switch</span></p><ul><li><span>Get blocked,比如调用 </span><code>wait(), sleep()</code><span> 等</span></li><li><mark><span>System Call</span></mark></li><li><span>A signal arrives</span></li><li><span>An interrupt arrives</span></li><li><span>时间片用完</span></li><li><span>被抢占</span></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\37.png" referrerpolicy="no-referrer"></p><h4 id='512-调度队列'><span>5.1.2 调度队列</span></h4><ul><li><p><span>作业队列 Job Queue</span></p><p><span>包含所有进程</span></p></li><li><p><span>就绪队列 Ready Queue</span></p><p><span>等待运行的进程</span></p><p><span>PCB 构成的链表</span></p></li><li><p><span>设备队列 Device Queue</span></p><p><span>等待使用该 IO 设备的进程队列</span></p><p><span>每个设备都有</span></p></li><li><p><span>队列图 Queueing Diagram</span></p><p><span>圆圈代表服务队列的资源, 箭头代表系统内的进程流向</span></p><p><span> </span><span>↓执行或分派 (Dispatch)</span></p><p><img src="D:\TyporaPictures\OS\38.jpg" referrerpolicy="no-referrer"></p></li></ul><h4 id='513-调度程序-scheduler'><span>5.1.3 调度程序 Scheduler</span></h4><ul><li><p><span>缓冲池</span></p><p><span>通常来说,对于批处理系统,提交的进程多于可执行的,这些进程被保存到大容量存储设备(如磁盘)的缓冲池,以便以后执行</span></p></li><li><p><span>调度程序 (调度器)</span></p><figure><table><thead><tr><th style='text-align:center;' ><span>调度器</span></th><th style='text-align:center;' ><span>别名</span></th><th style='text-align:center;' ><span>作用</span></th></tr></thead><tbody><tr><td style='text-align:center;' ><span>长期调度程序 Long-term Scheduler</span></td><td style='text-align:center;' ><span>作业调度程序 Job Scheduler</span></td><td style='text-align:center;' ><span>从缓冲池中选择进程加载到内存</span></td></tr><tr><td style='text-align:center;' ><span>短期调度程序 Short-term Scheduler</span></td><td style='text-align:center;' ><span>CPU 调度程序</span></td><td style='text-align:center;' ><span>从 Ready Queue 中选择进程并分配 CPU</span></td></tr><tr><td style='text-align:center;' ><span>中期调度程序 Medium-term Scheduler</span></td><td style='text-align:center;' > </td><td style='text-align:center;' ><span>进程交换</span></td></tr></tbody></table></figure></li><li><p><span>进程分类</span></p><figure><table><thead><tr><th style='text-align:center;' ><span>中文</span></th><th style='text-align:center;' ><span>英文</span></th><th style='text-align:center;' ><span>特点</span></th></tr></thead><tbody><tr><td style='text-align:center;' ><span>I/O 密集型进程</span></td><td style='text-align:center;' ><span>I/O Bounded Process</span></td><td style='text-align:center;' ><span>执行 I/O 比执行计算耗时</span></td></tr><tr><td style='text-align:center;' ><span>CPU 密集型进程</span></td><td style='text-align:center;' ><span>CPU Bounded Process</span></td><td style='text-align:center;' ><span>很少I/O, 执行计算用时长</span></td></tr></tbody></table></figure><p><span>长期调度程序需要选择这两种进程的合理组合才能最大化 CPU 和 IO 设备的利用</span></p></li></ul><p><img src="D:\TyporaPictures\OS\38.PNG" referrerpolicy="no-referrer"></p><h4 id='514-dispatcher'><span>5.1.4 Dispatcher</span></h4><p><span>Dispatcher 是一个模块,用来将 CPU 控制交给由 CPU 调度程序选择的进程</span></p><ul><li><p><span>功能</span></p><ul><li><span>切换上下文</span></li><li><span>切换到用户模式</span></li><li><span>跳转到用户程序的合适位置,以便重新启动程序</span></li></ul></li><li><p><span>调度延迟 Dispatch Latency</span></p><p><span>Dispatcher 停止一个进程而启动另一个进程所需的时间</span></p></li><li><p><span>Dispatcher 和 Scheduler 的区别</span></p><p><span>中文书把 dispatcher 也翻译成调度程序,我真想一拳干碎你的眼镜</span></p><p><a href='https://www.differencebetween.com/difference-between-scheduler-and-vs-dispatcher' target='_blank' class='url'>https://www.differencebetween.com/difference-between-scheduler-and-vs-dispatcher</a></p><p><a href='https://www.geeksforgeeks.org/difference-between-dispatcher-and-scheduler' target='_blank' class='url'>https://www.geeksforgeeks.org/difference-between-dispatcher-and-scheduler</a></p><blockquote><p><span>The </span><strong><span>key difference</span></strong><span> between scheduler and dispatcher is that </span><strong><span>the scheduler selects a process out of several processes to be executed while the dispatcher allocates the CPU for the selected process by the scheduler.</span></strong></p></blockquote><p><img src="D:\TyporaPictures\OS\39.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\40.png" referrerpolicy="no-referrer"></p></li></ul><figure><table><thead><tr><th style='text-align:center;' ><span>Properties</span></th><th style='text-align:left;' ><span>DISPATCHER</span></th><th style='text-align:left;' ><span>SCHEDULER</span></th></tr></thead><tbody><tr><td style='text-align:center;' ><span>Definition</span></td><td style='text-align:left;' ><span>Dispatcher is a module that gives control of CPU to the process selected by short term scheduler</span></td><td style='text-align:left;' ><span>Scheduler is something which selects a process among various processes</span></td></tr><tr><td style='text-align:center;' ><span>Types</span></td><td style='text-align:left;' ><span>There are no different types in dispatcher. It is just a code segment.</span></td><td style='text-align:left;' ><span>There are 3 types of scheduler i.e. Long-term, Short-term, Medium-term</span></td></tr><tr><td style='text-align:center;' ><span>Dependency</span></td><td style='text-align:left;' ><span>Working of dispatcher is dependent on scheduler. Means dispatcher have to wait until scheduler selects a process.</span></td><td style='text-align:left;' ><span>Scheduler works independently. It works immediately when needed</span></td></tr><tr><td style='text-align:center;' ><span>Algorithm</span></td><td style='text-align:left;' ><span>Dispatcher has no specific algorithm for its implementation</span></td><td style='text-align:left;' ><span>Scheduler works on various algorithm such as FCFS, SJF, RR etc.</span></td></tr><tr><td style='text-align:center;' ><span>Time Taken</span></td><td style='text-align:left;' ><span>The time taken by dispatcher is called dispatch latency.</span></td><td style='text-align:left;' ><span>Time taken by scheduler is usually negligible. Hence we neglect it.</span></td></tr><tr><td style='text-align:center;' ><span>Functions</span></td><td style='text-align:left;' ><span>Dispatcher is also responsible for: Context Switching, Switch to user mode, Jumping to proper location when process again restarted</span></td><td style='text-align:left;' ><span>The only work of scheduler is selection of processes.</span></td></tr></tbody></table></figure><hr /><h3 id='52-调度准则'><span>5.2 调度准则</span></h3><ul><li><p><span>CPU 使用率</span></p><p><span>应该使 CPU 尽可能忙碌</span></p></li><li><p><span>吞吐量</span></p><p><span>一个时间单元内进程完成的数量</span></p></li><li><p><mark><span>周转时间</span></mark></p><p><span>从进程提交到完成的时间段称为周转时间 (Turnaround Time)</span></p></li><li><p><mark><span>等待时间</span></mark></p><p><span>在就绪队列中等待所花时间之和</span></p></li><li><p><span>响应时间</span></p><p><span>从提交请求到产生第一响应的时间</span></p></li><li><p><mark><span>Number of Context Switches</span></mark><span> (from 课件)</span></p><p><span>尽可能少做上下文切换</span></p></li></ul><hr /><h3 id='53-调度算法-scheduling-algorithm'><span>5.3 调度算法 Scheduling Algorithm</span></h3><h4 id='531-先到先服务调度-first-come-first-served-fcfs'><span>5.3.1 先到先服务调度 First-Come-First-Served (FCFS)</span></h4><p><span>字面意思</span></p><h4 id='532-最短作业优先调度-shortest-job-first-sjf'><span>5.3.2 最短作业优先调度 Shortest-Job-First (SJF)</span></h4><ul><li><span>选择最短 CPU 执行时间的进程</span></li><li><span>相同,可以使用 FCFS 规则选择</span></li><li><span>又称最短下次 CPU执行 (Shortest-Next-CPU-Burst) 算法</span></li><li><span>可能造成 starvation</span></li></ul><h5 id='5321-非抢占-non-preemptive-sjf'><span>5.3.2.1 非抢占 (Non-Preemptive) SJF</span></h5><p><img src="D:\TyporaPictures\OS\42.PNG" referrerpolicy="no-referrer"></p><h5 id='5322-抢占-sjf'><span>5.3.2.2 抢占 SJF</span></h5><ul><li><span>最短剩余时间优先 (Shortest-Remaining-Time-First)</span></li></ul><p><img src="D:\TyporaPictures\OS\43.PNG" referrerpolicy="no-referrer"></p><ul><li><span>缺点:上下文切换多</span></li></ul><h4 id='533-轮转调度-round-robin-rr'><span>5.3.3 轮转调度 Round Robin (RR)</span></h4><ul><li><p><span>每个进程都有一个时间量 (Time Quantum) 或时间片 (Time Slice)</span></p><p><span>通常 10~100ms</span></p></li><li><p><span>当时间片用完时,该进程就会释放 CPU (相当于抢占)</span></p></li><li><p><span>调度程序选择下一个时间片 > 0 的进程</span></p></li><li><p><span>如果所有进程都用完了时间片,它们的时间片同时被 recharge 到初始值</span></p></li><li><p><span>就绪队列为循环队列,进程被依次执行</span></p><ul><li><span>刚执行完的进队尾</span></li><li><span>新来的进队尾</span></li><li><span>新来的进程不会触发新的 Schedule,就按队列顺序来</span></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\44.PNG" referrerpolicy="no-referrer"></p><ul><li><span>缺点:性能较差</span></li><li><span>优点:公平</span></li></ul><h4 id='534-优先级调度-priority-scheduling'><span>5.3.4 优先级调度 Priority Scheduling</span></h4><ul><li><p><span>每个进程都有一个优先级</span></p></li><li><p><span>调度程序根据优先级选择进程</span></p></li><li><p><span>优先队列</span></p></li><li><p><span>分类</span></p><p><img src="D:\TyporaPictures\OS\45.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>新进程到来时,重新 schedule (这里会发生抢占)</span></p></li><li><p><span>如果当前进程被抢占,它先出队再入队</span></p></li></ul><h5 id='5341-multiple-queue-priority-scheduling'><span>5.3.4.1 Multiple Queue Priority Scheduling</span></h5><ul><li><span>依然是 priority scheduler</span></li><li><span>每个优先级有不同的调度方式</span></li><li><span>可以是静态优先级和动态优先级混合</span></li></ul><p><img src="D:\TyporaPictures\OS\46.PNG" referrerpolicy="no-referrer"></p><ul><li><p><span>Linux Scheduler</span></p><p><img src="D:\TyporaPictures\OS\47.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\48.PNG" referrerpolicy="no-referrer"></p></li></ul><hr /><h2 id='第六章-同步-synchronization'><span>第六章 同步 Synchronization</span></h2><h3 id='61-进程间通信-inter-process-communication-ipc'><span>6.1 进程间通信 Inter-Process Communication (IPC)</span></h3><ul><li><p><span>匿名管道 Pipe</span></p><ul><li><span>单向 Unidirectional</span></li><li><span>匿名管道只能在祖先相同的进程之间建立</span></li></ul><p><img src="D:\TyporaPictures\OS\58.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>信号 Signal</span></p><ul><li><span>More kernel-level</span></li><li><span>Limited (SIGKILL, SIGCHLD, ...)</span></li></ul></li><li><p><span>例: </span><code>ls | less</code></p><p><img src="D:\TyporaPictures\OS\59.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\60.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>IPC Models</span></p><p><img src="D:\TyporaPictures\OS\61.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>User space 里的所有东西都不能 share,所以 pipe 之类的都在 kernel 里</span></p></li></ul><hr /><h3 id='62-临界区-critical-section'><span>6.2 临界区 Critical Section</span></h3><h4 id='621-竞争条件-race-condition'><span>6.2.1 竞争条件 Race Condition</span></h4><ul><li><span>多个进程并发访问和操作同一数据并且执行结果与特定访问顺序有关,称为竞争条件 (Race Condition)</span></li><li><span>Shared Object + Multiple Process + Concurrency</span></li></ul><h4 id='622-临界区问题-critical-section-problem'><span>6.2.2 临界区问题 Critical Section Problem</span></h4><ul><li><p><span>临界区 Critical Section</span></p><p><span>每个进程有一段代码,进程在执行该段代码时可能修改公共变量、更新一个表、写一个文件等</span></p></li><li><p><span>进入区 Entry Section</span></p><p><span>进入临界区前,请求许可的代码段</span></p></li><li><p><span>退出区 Exit Section</span></p></li><li><p><span>剩余区 Remainder Section</span></p></li></ul><p><img src="D:\TyporaPictures\OS\62.PNG" referrerpolicy="no-referrer"></p><ul><li><span>临界区问题 (Critical-Section Problem) 指设计一个协议以便协作进程,使得没有两个进程可以在它们的临界区内同时执行</span></li><li><span>临界区要尽可能紧凑</span></li><li><span>一个临界区里可以访问多个 shared object</span></li><li><span>重点是进入区和退出区的实现</span></li></ul><h4 id='623-临界区问题的要求'><span>6.2.3 临界区问题的要求</span></h4><ol start='' ><li><p><span>互斥 Mutual Exclusion</span></p><p><span>如果一个进程在其临界区内执行,那么其他进程都不能在临界区内执行</span></p></li><li><p><span>进步 Progress</span></p><p><span>如果没有进程在临界区内执行,并且有进程需要进入临界区,那么只有那些不在剩余区内的进程可以参加选择,以便确定谁下次进入临界区,而且这种选择不能无限推迟</span></p><p><span style='color:blue'><span>别让执行临界区的进程空着,除非大家都不想进临界区</span></span></p></li><li><p><span>有限等待 Bounded Waiting</span></p><p><span>从一个进程做出进入临界区的请求直到这个请求允许为止,其他进程允许进入其临界区的次数有上限</span></p><p><span style='color:blue'><span>别让一个进程等一辈子</span></span></p></li></ol><hr /><h3 id='63-临界区问题的解决方案-solutions-for-critical-section-problem'><span>6.3 临界区问题的解决方案 Solutions for Critical Section Problem</span></h3><ul><li><p><span>Lock-based</span></p><ul><li><p><span>Spin-based Lock</span></p><ul><li><span>Basic spinning</span></li><li><span>Peterson's solution</span></li></ul></li><li><p><span>Sleep-based Lock</span></p><ul><li><span>POSIX semaphore</span></li><li><code>pthread_mutex_lock</code></li></ul></li></ul></li><li><p><span>Lock-free</span></p></li></ul><h4 id='631-硬件同步-×-hardware-synchronization'><span>6.3.1 硬件同步 (×) Hardware Synchronization</span></h4><ul><li><span>禁止中断</span></li></ul><p><img src="D:\TyporaPictures\OS\63.PNG" referrerpolicy="no-referrer"></p><ul><li><p><span>单核</span></p><p><span>正确,但是不能接受</span></p><p><span>如果有个进程在 CS 里写个死循环就全卡这了</span></p></li><li><p><span>多核</span></p><p><span>不正确,除非把所有核的中断全都禁止</span></p></li></ul><h4 id='632-基本自旋锁-×-basic-spin-lock'><span>6.3.2 基本自旋锁 (×) Basic Spin Lock</span></h4><ul><li><p><span>原理</span></p><p><span>设置一个公共变量 </span><code>turn</code><span> 来决定哪个进程可以进 CS</span></p></li></ul><p><img src="D:\TyporaPictures\OS\64.PNG" referrerpolicy="no-referrer"></p><ul><li><p><span>太浪费 CPU</span></p></li><li><p><span>违反 Progress</span></p><p><span>多个进程一定是交替执行</span></p><p><span>如果一个进程不打算进 CS 但是另一个进程交出了权限,那就要等很长时间 (</span><strong><span>no progress</span></strong><span>)</span></p><p><span>Example: 这种情况下没人在 CS 里。</span><mark><span>不能让执行 CS 的进程空着</span></mark></p><p><img src="D:\TyporaPictures\OS\65.PNG" referrerpolicy="no-referrer"></p></li></ul><h4 id='633-petersons-solution'><span>6.3.3 Peterson's Solution</span></h4><ul><li><p><span>在 </span><code>turn</code><span> 的基础上新加一个布尔数组 </span><code>interested</code></p><ul><li><p><span>If I don't show interest</span></p><p><span>I let you all go</span></p></li><li><p><span>If we both show interest</span></p><p><span>Take turns</span></p></li></ul></li></ul><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="c"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 43px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 39px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><span><span></span>x</span></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -39px; width: 39px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">int</span> <span class="cm-variable">turn</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">int</span> <span class="cm-variable">interested</span>[<span class="cm-number">2</span>] <span class="cm-operator">=</span> {<span class="cm-atom">false</span>, <span class="cm-atom">false</span>};</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">void</span> <span class="cm-def">lock</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">process</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">int</span> <span class="cm-variable">other</span> <span class="cm-operator">=</span> <span class="cm-number">1</span> <span class="cm-operator">-</span> <span class="cm-variable">process</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">interested</span>[<span class="cm-variable">process</span>] <span class="cm-operator">=</span> <span class="cm-atom">true</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">turn</span> <span class="cm-operator">=</span> <span class="cm-variable">other</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">while</span> (<span class="cm-variable">turn</span> <span class="cm-operator">==</span> <span class="cm-variable">other</span> <span class="cm-operator">&&</span> <span class="cm-variable">interested</span>[<span class="cm-variable">other</span>]); <span class="cm-comment">// busy waiting</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">void</span> <span class="cm-def">unlock</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">process</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">interested</span>[<span class="cm-variable">process</span>] <span class="cm-operator">=</span> <span class="cm-atom">false</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 286px;"></div><div class="CodeMirror-gutters" style="height: 286px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 38px;"></div></div></div></div></pre><ul><li><p><span>会产生优先级翻转问题 (Priority Invasion)</span></p><p><span>优先级 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="11.465ex" height="2.063ex" viewBox="0 -793.3 4936.1 888.4" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E66-MJMATHI-41" d="M208 74Q208 50 254 46Q272 46 272 35Q272 34 270 22Q267 8 264 4T251 0Q249 0 239 0T205 1T141 2Q70 2 50 0H42Q35 7 35 11Q37 38 48 46H62Q132 49 164 96Q170 102 345 401T523 704Q530 716 547 716H555H572Q578 707 578 706L606 383Q634 60 636 57Q641 46 701 46Q726 46 726 36Q726 34 723 22Q720 7 718 4T704 0Q701 0 690 0T651 1T578 2Q484 2 455 0H443Q437 6 437 9T439 27Q443 40 445 43L449 46H469Q523 49 533 63L521 213H283L249 155Q208 86 208 74ZM516 260Q516 271 504 416T490 562L463 519Q447 492 400 412L310 260L413 259Q516 259 516 260Z"></path><path stroke-width="0" id="E66-MJMAIN-3C" d="M694 -11T694 -19T688 -33T678 -40Q671 -40 524 29T234 166L90 235Q83 240 83 250Q83 261 91 266Q664 540 678 540Q681 540 687 534T694 519T687 505Q686 504 417 376L151 250L417 124Q686 -4 687 -5Q694 -11 694 -19Z"></path><path stroke-width="0" id="E66-MJMATHI-42" d="M231 637Q204 637 199 638T194 649Q194 676 205 682Q206 683 335 683Q594 683 608 681Q671 671 713 636T756 544Q756 480 698 429T565 360L555 357Q619 348 660 311T702 219Q702 146 630 78T453 1Q446 0 242 0Q42 0 39 2Q35 5 35 10Q35 17 37 24Q42 43 47 45Q51 46 62 46H68Q95 46 128 49Q142 52 147 61Q150 65 219 339T288 628Q288 635 231 637ZM649 544Q649 574 634 600T585 634Q578 636 493 637Q473 637 451 637T416 636H403Q388 635 384 626Q382 622 352 506Q352 503 351 500L320 374H401Q482 374 494 376Q554 386 601 434T649 544ZM595 229Q595 273 572 302T512 336Q506 337 429 337Q311 337 310 336Q310 334 293 263T258 122L240 52Q240 48 252 48T333 46Q422 46 429 47Q491 54 543 105T595 229Z"></path><path stroke-width="0" id="E66-MJMATHI-43" d="M50 252Q50 367 117 473T286 641T490 704Q580 704 633 653Q642 643 648 636T656 626L657 623Q660 623 684 649Q691 655 699 663T715 679T725 690L740 705H746Q760 705 760 698Q760 694 728 561Q692 422 692 421Q690 416 687 415T669 413H653Q647 419 647 422Q647 423 648 429T650 449T651 481Q651 552 619 605T510 659Q484 659 454 652T382 628T299 572T226 479Q194 422 175 346T156 222Q156 108 232 58Q280 24 350 24Q441 24 512 92T606 240Q610 253 612 255T628 257Q648 257 648 248Q648 243 647 239Q618 132 523 55T319 -22Q206 -22 128 53T50 252Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E66-MJMATHI-41" x="0" y="0"></use><use xlink:href="#E66-MJMAIN-3C" x="1027" y="0"></use><use xlink:href="#E66-MJMATHI-42" x="2083" y="0"></use><use xlink:href="#E66-MJMAIN-3C" x="3120" y="0"></use><use xlink:href="#E66-MJMATHI-43" x="4176" y="0"></use></g></svg></span><script type="math/tex">A<B<C</script></p><ol start='' ><li><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.742ex" height="1.955ex" viewBox="0 -793.3 750 841.8" role="img" focusable="false" style="vertical-align: -0.113ex;"><defs><path stroke-width="0" id="E72-MJMATHI-41" d="M208 74Q208 50 254 46Q272 46 272 35Q272 34 270 22Q267 8 264 4T251 0Q249 0 239 0T205 1T141 2Q70 2 50 0H42Q35 7 35 11Q37 38 48 46H62Q132 49 164 96Q170 102 345 401T523 704Q530 716 547 716H555H572Q578 707 578 706L606 383Q634 60 636 57Q641 46 701 46Q726 46 726 36Q726 34 723 22Q720 7 718 4T704 0Q701 0 690 0T651 1T578 2Q484 2 455 0H443Q437 6 437 9T439 27Q443 40 445 43L449 46H469Q523 49 533 63L521 213H283L249 155Q208 86 208 74ZM516 260Q516 271 504 416T490 562L463 519Q447 492 400 412L310 260L413 259Q516 259 516 260Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E72-MJMATHI-41" x="0" y="0"></use></g></svg></span><script type="math/tex">A</script><span> 获得锁, </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.765ex" height="2.063ex" viewBox="0 -793.3 760 888.4" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E77-MJMATHI-43" d="M50 252Q50 367 117 473T286 641T490 704Q580 704 633 653Q642 643 648 636T656 626L657 623Q660 623 684 649Q691 655 699 663T715 679T725 690L740 705H746Q760 705 760 698Q760 694 728 561Q692 422 692 421Q690 416 687 415T669 413H653Q647 419 647 422Q647 423 648 429T650 449T651 481Q651 552 619 605T510 659Q484 659 454 652T382 628T299 572T226 479Q194 422 175 346T156 222Q156 108 232 58Q280 24 350 24Q441 24 512 92T606 240Q610 253 612 255T628 257Q648 257 648 248Q648 243 647 239Q618 132 523 55T319 -22Q206 -22 128 53T50 252Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E77-MJMATHI-43" x="0" y="0"></use></g></svg></span><script type="math/tex">C</script><span> 来了, </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.765ex" height="2.063ex" viewBox="0 -793.3 760 888.4" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E77-MJMATHI-43" d="M50 252Q50 367 117 473T286 641T490 704Q580 704 633 653Q642 643 648 636T656 626L657 623Q660 623 684 649Q691 655 699 663T715 679T725 690L740 705H746Q760 705 760 698Q760 694 728 561Q692 422 692 421Q690 416 687 415T669 413H653Q647 419 647 422Q647 423 648 429T650 449T651 481Q651 552 619 605T510 659Q484 659 454 652T382 628T299 572T226 479Q194 422 175 346T156 222Q156 108 232 58Q280 24 350 24Q441 24 512 92T606 240Q610 253 612 255T628 257Q648 257 648 248Q648 243 647 239Q618 132 523 55T319 -22Q206 -22 128 53T50 252Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E77-MJMATHI-43" x="0" y="0"></use></g></svg></span><script type="math/tex">C</script><span> 申请锁</span></li><li><span>按理来说 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.765ex" height="2.063ex" viewBox="0 -793.3 760 888.4" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E77-MJMATHI-43" d="M50 252Q50 367 117 473T286 641T490 704Q580 704 633 653Q642 643 648 636T656 626L657 623Q660 623 684 649Q691 655 699 663T715 679T725 690L740 705H746Q760 705 760 698Q760 694 728 561Q692 422 692 421Q690 416 687 415T669 413H653Q647 419 647 422Q647 423 648 429T650 449T651 481Q651 552 619 605T510 659Q484 659 454 652T382 628T299 572T226 479Q194 422 175 346T156 222Q156 108 232 58Q280 24 350 24Q441 24 512 92T606 240Q610 253 612 255T628 257Q648 257 648 248Q648 243 647 239Q618 132 523 55T319 -22Q206 -22 128 53T50 252Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E77-MJMATHI-43" x="0" y="0"></use></g></svg></span><script type="math/tex">C</script><span> 应该抢占 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.742ex" height="1.955ex" viewBox="0 -793.3 750 841.8" role="img" focusable="false" style="vertical-align: -0.113ex;"><defs><path stroke-width="0" id="E72-MJMATHI-41" d="M208 74Q208 50 254 46Q272 46 272 35Q272 34 270 22Q267 8 264 4T251 0Q249 0 239 0T205 1T141 2Q70 2 50 0H42Q35 7 35 11Q37 38 48 46H62Q132 49 164 96Q170 102 345 401T523 704Q530 716 547 716H555H572Q578 707 578 706L606 383Q634 60 636 57Q641 46 701 46Q726 46 726 36Q726 34 723 22Q720 7 718 4T704 0Q701 0 690 0T651 1T578 2Q484 2 455 0H443Q437 6 437 9T439 27Q443 40 445 43L449 46H469Q523 49 533 63L521 213H283L249 155Q208 86 208 74ZM516 260Q516 271 504 416T490 562L463 519Q447 492 400 412L310 260L413 259Q516 259 516 260Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E72-MJMATHI-41" x="0" y="0"></use></g></svg></span><script type="math/tex">A</script><span>, 但是锁在 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.742ex" height="1.955ex" viewBox="0 -793.3 750 841.8" role="img" focusable="false" style="vertical-align: -0.113ex;"><defs><path stroke-width="0" id="E72-MJMATHI-41" d="M208 74Q208 50 254 46Q272 46 272 35Q272 34 270 22Q267 8 264 4T251 0Q249 0 239 0T205 1T141 2Q70 2 50 0H42Q35 7 35 11Q37 38 48 46H62Q132 49 164 96Q170 102 345 401T523 704Q530 716 547 716H555H572Q578 707 578 706L606 383Q634 60 636 57Q641 46 701 46Q726 46 726 36Q726 34 723 22Q720 7 718 4T704 0Q701 0 690 0T651 1T578 2Q484 2 455 0H443Q437 6 437 9T439 27Q443 40 445 43L449 46H469Q523 49 533 63L521 213H283L249 155Q208 86 208 74ZM516 260Q516 271 504 416T490 562L463 519Q447 492 400 412L310 260L413 259Q516 259 516 260Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E72-MJMATHI-41" x="0" y="0"></use></g></svg></span><script type="math/tex">A</script><span> 手里, </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.765ex" height="2.063ex" viewBox="0 -793.3 760 888.4" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E77-MJMATHI-43" d="M50 252Q50 367 117 473T286 641T490 704Q580 704 633 653Q642 643 648 636T656 626L657 623Q660 623 684 649Q691 655 699 663T715 679T725 690L740 705H746Q760 705 760 698Q760 694 728 561Q692 422 692 421Q690 416 687 415T669 413H653Q647 419 647 422Q647 423 648 429T650 449T651 481Q651 552 619 605T510 659Q484 659 454 652T382 628T299 572T226 479Q194 422 175 346T156 222Q156 108 232 58Q280 24 350 24Q441 24 512 92T606 240Q610 253 612 255T628 257Q648 257 648 248Q648 243 647 239Q618 132 523 55T319 -22Q206 -22 128 53T50 252Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E77-MJMATHI-43" x="0" y="0"></use></g></svg></span><script type="math/tex">C</script><span> 就只能等待</span></li><li><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.763ex" height="1.847ex" viewBox="0 -746.7 759 795.3" role="img" focusable="false" style="vertical-align: -0.113ex;"><defs><path stroke-width="0" id="E76-MJMATHI-42" d="M231 637Q204 637 199 638T194 649Q194 676 205 682Q206 683 335 683Q594 683 608 681Q671 671 713 636T756 544Q756 480 698 429T565 360L555 357Q619 348 660 311T702 219Q702 146 630 78T453 1Q446 0 242 0Q42 0 39 2Q35 5 35 10Q35 17 37 24Q42 43 47 45Q51 46 62 46H68Q95 46 128 49Q142 52 147 61Q150 65 219 339T288 628Q288 635 231 637ZM649 544Q649 574 634 600T585 634Q578 636 493 637Q473 637 451 637T416 636H403Q388 635 384 626Q382 622 352 506Q352 503 351 500L320 374H401Q482 374 494 376Q554 386 601 434T649 544ZM595 229Q595 273 572 302T512 336Q506 337 429 337Q311 337 310 336Q310 334 293 263T258 122L240 52Q240 48 252 48T333 46Q422 46 429 47Q491 54 543 105T595 229Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E76-MJMATHI-42" x="0" y="0"></use></g></svg></span><script type="math/tex">B</script><span> 不要锁,所以 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.763ex" height="1.847ex" viewBox="0 -746.7 759 795.3" role="img" focusable="false" style="vertical-align: -0.113ex;"><defs><path stroke-width="0" id="E76-MJMATHI-42" d="M231 637Q204 637 199 638T194 649Q194 676 205 682Q206 683 335 683Q594 683 608 681Q671 671 713 636T756 544Q756 480 698 429T565 360L555 357Q619 348 660 311T702 219Q702 146 630 78T453 1Q446 0 242 0Q42 0 39 2Q35 5 35 10Q35 17 37 24Q42 43 47 45Q51 46 62 46H68Q95 46 128 49Q142 52 147 61Q150 65 219 339T288 628Q288 635 231 637ZM649 544Q649 574 634 600T585 634Q578 636 493 637Q473 637 451 637T416 636H403Q388 635 384 626Q382 622 352 506Q352 503 351 500L320 374H401Q482 374 494 376Q554 386 601 434T649 544ZM595 229Q595 273 572 302T512 336Q506 337 429 337Q311 337 310 336Q310 334 293 263T258 122L240 52Q240 48 252 48T333 46Q422 46 429 47Q491 54 543 105T595 229Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E76-MJMATHI-42" x="0" y="0"></use></g></svg></span><script type="math/tex">B</script><span> 可以被调度上去</span></li><li><span>明明 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.763ex" height="1.847ex" viewBox="0 -746.7 759 795.3" role="img" focusable="false" style="vertical-align: -0.113ex;"><defs><path stroke-width="0" id="E76-MJMATHI-42" d="M231 637Q204 637 199 638T194 649Q194 676 205 682Q206 683 335 683Q594 683 608 681Q671 671 713 636T756 544Q756 480 698 429T565 360L555 357Q619 348 660 311T702 219Q702 146 630 78T453 1Q446 0 242 0Q42 0 39 2Q35 5 35 10Q35 17 37 24Q42 43 47 45Q51 46 62 46H68Q95 46 128 49Q142 52 147 61Q150 65 219 339T288 628Q288 635 231 637ZM649 544Q649 574 634 600T585 634Q578 636 493 637Q473 637 451 637T416 636H403Q388 635 384 626Q382 622 352 506Q352 503 351 500L320 374H401Q482 374 494 376Q554 386 601 434T649 544ZM595 229Q595 273 572 302T512 336Q506 337 429 337Q311 337 310 336Q310 334 293 263T258 122L240 52Q240 48 252 48T333 46Q422 46 429 47Q491 54 543 105T595 229Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E76-MJMATHI-42" x="0" y="0"></use></g></svg></span><script type="math/tex">B</script><span> 优先级低,却比 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.765ex" height="2.063ex" viewBox="0 -793.3 760 888.4" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E77-MJMATHI-43" d="M50 252Q50 367 117 473T286 641T490 704Q580 704 633 653Q642 643 648 636T656 626L657 623Q660 623 684 649Q691 655 699 663T715 679T725 690L740 705H746Q760 705 760 698Q760 694 728 561Q692 422 692 421Q690 416 687 415T669 413H653Q647 419 647 422Q647 423 648 429T650 449T651 481Q651 552 619 605T510 659Q484 659 454 652T382 628T299 572T226 479Q194 422 175 346T156 222Q156 108 232 58Q280 24 350 24Q441 24 512 92T606 240Q610 253 612 255T628 257Q648 257 648 248Q648 243 647 239Q618 132 523 55T319 -22Q206 -22 128 53T50 252Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E77-MJMATHI-43" x="0" y="0"></use></g></svg></span><script type="math/tex">C</script><span> 先执行</span></li></ol></li><li><p><span>为什么 </span><code>turn=other</code><span> 不是 </span><code>turn=process</code></p><p><span>我们假设是这样</span></p><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="c"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 33px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>2</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -29px; width: 29px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">turn</span><span class="cm-operator">=</span><span class="cm-variable">自己</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">while</span> (<span class="cm-variable">turn</span><span class="cm-operator">==</span><span class="cm-variable">自己</span> <span class="cm-operator">&&</span> <span class="cm-variable">interested</span>[<span class="cm-variable">别人</span>]);</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 44px;"></div><div class="CodeMirror-gutters" style="height: 44px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div></pre><p><span>如果现在有三个进程 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="9.699ex" height="2.388ex" viewBox="0 -746.7 4176 1028" role="img" focusable="false" style="vertical-align: -0.653ex;"><defs><path stroke-width="0" id="E79-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E79-MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path stroke-width="0" id="E79-MJMAIN-2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path><path stroke-width="0" id="E79-MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path stroke-width="0" id="E79-MJMAIN-33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E79-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E79-MJMAIN-31" x="907" y="-213"></use><use xlink:href="#E79-MJMAIN-2C" x="1095" y="0"></use><g transform="translate(1540,0)"><use xlink:href="#E79-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E79-MJMAIN-32" x="907" y="-213"></use></g><use xlink:href="#E79-MJMAIN-2C" x="2635" y="0"></use><g transform="translate(3080,0)"><use xlink:href="#E79-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E79-MJMAIN-33" x="907" y="-213"></use></g></g></svg></span><script type="math/tex">P_1,P_2,P_3</script></p><ol start='' ><li><p><span>我们脸比较黑,这三个进程经过调度,都该执行 </span><code>turn=自己</code><span> 这一行</span></p></li><li><p><span>那么最终 </span><code>turn</code><span> 是几,就取决于调度器了</span></p></li><li><p><span>假设调度器就是按 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="9.699ex" height="2.388ex" viewBox="0 -746.7 4176 1028" role="img" focusable="false" style="vertical-align: -0.653ex;"><defs><path stroke-width="0" id="E79-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E79-MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path stroke-width="0" id="E79-MJMAIN-2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path><path stroke-width="0" id="E79-MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path stroke-width="0" id="E79-MJMAIN-33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E79-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E79-MJMAIN-31" x="907" y="-213"></use><use xlink:href="#E79-MJMAIN-2C" x="1095" y="0"></use><g transform="translate(1540,0)"><use xlink:href="#E79-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E79-MJMAIN-32" x="907" y="-213"></use></g><use xlink:href="#E79-MJMAIN-2C" x="2635" y="0"></use><g transform="translate(3080,0)"><use xlink:href="#E79-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E79-MJMAIN-33" x="907" y="-213"></use></g></g></svg></span><script type="math/tex">P_1,P_2,P_3</script><span> 的顺序调度的,那么最后 </span><code>turn=3</code></p></li><li><p><span>现在我们检查 </span><code>while</code><span> 的条件</span></p><ul><li><span>对于 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.545ex" height="2.28ex" viewBox="0 -746.7 1095.6 981.5" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E88-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E88-MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E88-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E88-MJMAIN-31" x="907" y="-213"></use></g></svg></span><script type="math/tex">P_1</script><span>, </span><code>turn=3</code><span>, 前半句不成立, 不需要 wait</span></li><li><span>对于 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.545ex" height="2.28ex" viewBox="0 -746.7 1095.6 981.5" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E89-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E89-MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E89-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E89-MJMAIN-32" x="907" y="-213"></use></g></svg></span><script type="math/tex">P_2</script><span>, </span><code>turn=3</code><span>, 前半句不成立, 不需要 wait</span></li><li><span>对于 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.545ex" height="2.28ex" viewBox="0 -746.7 1095.6 981.5" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E82-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E82-MJMAIN-33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E82-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E82-MJMAIN-33" x="907" y="-213"></use></g></svg></span><script type="math/tex">P_3</script><span>, 条件成立, 需要 wait</span></li></ul></li><li><p><span>那么现在 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="6.122ex" height="2.388ex" viewBox="0 -746.7 2635.8 1028" role="img" focusable="false" style="vertical-align: -0.653ex;"><defs><path stroke-width="0" id="E83-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E83-MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path stroke-width="0" id="E83-MJMAIN-2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path><path stroke-width="0" id="E83-MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E83-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E83-MJMAIN-31" x="907" y="-213"></use><use xlink:href="#E83-MJMAIN-2C" x="1095" y="0"></use><g transform="translate(1540,0)"><use xlink:href="#E83-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E83-MJMAIN-32" x="907" y="-213"></use></g></g></svg></span><script type="math/tex">P_1,P_2</script><span> 都被许可进入 CS,违反了</span><strong><span>互斥</span></strong><span>原则</span></p></li></ol><p> </p><p><span>正确是这样:</span></p><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang=""><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang=""><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 33px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>4</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -29px; width: 29px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">turn=别人;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">while (turn==别人们 && interested[别人们]);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">// while ((turn==x && interested[x]) || (turn==y && interested[y]))</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">// while (turn!=自己 && interested[别人们])</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 88px;"></div><div class="CodeMirror-gutters" style="height: 88px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div></pre><p><span>还是这个例子,我们假设 </span><code>turn</code><span> 的赋值是 1 给 2,2 给 3,3 给 1</span></p><ol start='' ><li><span>还是都执行到 </span><code>turn=别人</code><span> 这一行,还是按 123 的顺序调度的</span></li><li><span>那最终 </span><code>turn=1</code></li><li><span>检查 </span><code>while</code><span> 的条件,只有 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.545ex" height="2.28ex" viewBox="0 -746.7 1095.6 981.5" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E88-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E88-MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E88-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E88-MJMAIN-31" x="907" y="-213"></use></g></svg></span><script type="math/tex">P_1</script><span> 可以进 CS</span></li></ol></li></ul><h4 id='634-信号量-semaphore'><span>6.3.4 信号量 Semaphore</span></h4><ul><li><p><span>信号量是一个 Structure</span></p><ul><li><span>一个 </span><code>int</code><span>,表示剩余多少资源可用</span></li><li><span>一个等待队列</span></li></ul><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="c"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 33px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>4</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -29px; width: 29px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">typedef</span> <span class="cm-keyword">struct</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">int</span> <span class="cm-variable">value</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">struct</span> <span class="cm-def">process</span> <span class="cm-operator">*</span><span class="cm-variable">list</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">} <span class="cm-variable">semaphore</span>;</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 88px;"></div><div class="CodeMirror-gutters" style="height: 88px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div></pre></li><li><p><span>Wait (P 操作)</span></p><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="c"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 33px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>7</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -29px; width: 29px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">wait</span>(<span class="cm-variable">semaphore</span> <span class="cm-operator">*</span><span class="cm-variable">s</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">s</span><span class="cm-operator">-></span><span class="cm-variable">value</span><span class="cm-operator">--</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> (<span class="cm-variable">s</span><span class="cm-operator">-></span><span class="cm-variable">value</span><span class="cm-operator"><</span><span class="cm-number">0</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">add</span> <span class="cm-variable">this</span> <span class="cm-variable">process</span> <span class="cm-variable">to</span> <span class="cm-variable">s</span><span class="cm-operator">-></span><span class="cm-variable">list</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">block</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 154px;"></div><div class="CodeMirror-gutters" style="height: 154px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div></pre></li><li><p><span>Post (V 操作)</span></p><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="c"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 33px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 29px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>7</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -29px; width: 29px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">post</span>(<span class="cm-variable">semaphore</span> <span class="cm-operator">*</span><span class="cm-variable">s</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">s</span><span class="cm-operator">-></span><span class="cm-variable">value</span><span class="cm-operator">++</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> (<span class="cm-variable">s</span><span class="cm-operator">-></span><span class="cm-variable">value</span><span class="cm-operator"><=</span><span class="cm-number">0</span>){</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">remove</span> <span class="cm-variable">a</span> <span class="cm-variable">process</span> <span class="cm-variable">p</span> <span class="cm-variable">from</span> <span class="cm-variable">s</span><span class="cm-operator">-></span><span class="cm-variable">list</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">wakeup</span>(<span class="cm-variable">p</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 20px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -29px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 20px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 154px;"></div><div class="CodeMirror-gutters" style="height: 154px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 28px;"></div></div></div></div></pre></li><li><p><span>分类</span></p><ul><li><p><span>二进制信号量 Binary Semaphore</span></p><p><span>只能 0 或 1</span></p></li><li><p><span>计数信号量 Counting Semaphore</span></p><p><span>可以 > 1</span></p></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\67.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\66.PNG" referrerpolicy="no-referrer"></p><hr /><h3 id='64-经典同步问题'><span>6.4 经典同步问题</span></h3><h4 id='641-有界缓冲问题-bounded-buffer-problem'><span>6.4.1 有界缓冲问题 Bounded-Buffer Problem</span></h4><ul><li><p><span>又称生产者-消费者问题 (Producer-Consumer Problem)</span></p></li><li><p><span>组成</span></p><ol start='' ><li><p><span>Bounded Buffer</span></p><ul><li><span>Shared object</span></li><li><span>Limited size</span></li><li><span>Queue</span></li></ul></li><li><p><span>Producer Process</span></p><ul><li><span>Produce a unit of data and writes that piece of data to the tail of the buffer at one time</span></li></ul></li><li><p><span>Consumer Process</span></p><ul><li><span>Remove a unit of data from the head of the buffer at one time</span></li></ul></li></ol></li><li><p><span>要求</span></p><ol start='' ><li><p><span>Producer</span></p><ul><li><span>当 producer 向 buffer 里放入数据,但是 buffer 已经满的时候,他需要 wait</span></li><li><span>放入数据后,通知 consumer (wake up)</span></li></ul></li><li><p><span>Consumer</span></p><ul><li><span>当 consumer 要消费数据,但是 buffer 是空的,他需要 wait</span></li><li><span>消费数据之后,通知 producer (wake up)</span></li></ul></li></ol></li><li><p><span>例子</span></p><p><img src="D:\TyporaPictures\OS\68.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>Semaphore 实现</span></p><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="c" style="break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="c"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 43px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 39px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>33</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -39px; width: 39px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">semaphore</span> <span class="cm-variable">mutex</span><span class="cm-operator">=</span><span class="cm-number">1</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">semaphore</span> <span class="cm-variable">avail</span><span class="cm-operator">=</span><span class="cm-variable">N</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">semaphore</span> <span class="cm-variable">fill</span><span class="cm-operator">=</span><span class="cm-number">0</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">void</span> <span class="cm-def">producer</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">int</span> <span class="cm-variable">item</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> </span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">while</span> (<span class="cm-atom">true</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">item</span><span class="cm-operator">=</span><span class="cm-variable">produce_item</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> </span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">wait</span>(<span class="cm-operator">&</span><span class="cm-variable">avail</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">wait</span>(<span class="cm-operator">&</span><span class="cm-variable">mutex</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> </span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">insert_item</span>(<span class="cm-variable">item</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> </span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">post</span>(<span class="cm-operator">&</span><span class="cm-variable">mutex</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-tab" role="presentation" cm-text=" "> </span><span class="cm-variable">post</span>(<span class="cm-operator">&</span><span class="cm-variable">fill</span>); </span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">18</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">20</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">21</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable-3">void</span> <span class="cm-def">consumer</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">22</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">int</span> <span class="cm-variable">item</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">23</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> </span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">24</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">while</span> (<span class="cm-atom">true</span>){</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">25</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">wait</span>(<span class="cm-operator">&</span><span class="cm-variable">fill</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">26</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">wait</span>(<span class="cm-operator">&</span><span class="cm-variable">mutex</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">27</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> </span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">28</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">item</span><span class="cm-operator">=</span><span class="cm-variable">remove_item</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">29</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> </span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">30</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">post</span>(<span class="cm-operator">&</span><span class="cm-variable">mutex</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">31</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">post</span>(<span class="cm-operator">&</span><span class="cm-variable">avail</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">32</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">33</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 726px;"></div><div class="CodeMirror-gutters" style="height: 726px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 38px;"></div></div></div></div></pre></li></ul><h4 id='642-读者-作者问题-reader-writer-problem'><span>6.4.2 读者-作者问题 Reader-Writer Problem</span></h4><ul><li><p><span>要求</span></p><ul><li><span>任何数量的 reader 都可以同时 read</span></li><li><span>同时只能有一个 writer 写</span></li><li><span>如果有 writer 在写,那么所有 reader 都不能读</span></li></ul></li></ul><h4 id='643-哲学家就餐问题-dining-philosophers-problem'><span>6.4.3 哲学家就餐问题 Dining-Philosophers Problem</span></h4><ul><li><p><span>问题描述</span></p><ul><li><span>有 5 个哲学家,5 根筷子,1 盘面条</span></li></ul><p><img src="D:\TyporaPictures\OS\69.png" referrerpolicy="no-referrer"></p><ul><li><p><span>每个哲学家有两个可能的动作</span></p><ul><li><span>Think</span></li><li><span>Eat</span></li></ul></li><li><p><span>如果一个哲学家要吃面条,他必须同时获得左右两根筷子</span></p></li><li><p><span>拿起来的筷子不会被别人抢</span></p></li></ul></li><li><p><span>要求</span></p><p><span>设计一个 Protocol,保证所有哲学家</span></p><ul><li><span>不会饿死</span></li><li><span>不会死锁</span></li></ul></li><li><p><span>解决方案设计</span></p><ul><li><span>如果一个哲学家想吃面条,那么他先问左右</span></li><li><span>如果左右都不在吃,那么他拿两根筷子吃</span></li><li><span>如果左右有人在吃,他就饿着等着,直到别人吃完了通知他</span></li><li><span>吃完之后,他放下筷子并且通知左右他吃完了</span></li></ul><p><img src="D:\TyporaPictures\OS\70.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\71.PNG" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\72.PNG" referrerpolicy="no-referrer"></p><ul><li><span>Finish Eating</span></li></ul><p><img src="D:\TyporaPictures\OS\73.PNG" referrerpolicy="no-referrer"></p></li></ul><hr /><h2 id='第七章-死锁-deadlock'><span>第七章 死锁 Deadlock</span></h2><h3 id='71-死锁的概念'><span>7.1 死锁的概念</span></h3><ul><li><p><span>在正常操作模式下,进程只能按如下顺序使用资源:</span></p><ol start='' ><li><p><span>申请</span></p><p><span>进程请求资源。如果进程不能立即被允许,那么它应该等待,直到获取该资源</span></p></li><li><p><span>使用</span></p><p><span>进程对资源进行操作</span></p></li><li><p><span>释放</span></p><p><span>进程释放资源</span></p></li></ol></li><li><p><span>死锁 Deadlock</span></p><p><span>Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process.</span></p><p><img src="D:\TyporaPictures\OS\74.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>饥饿 Starvation</span></p><p><span>Indefinite Blocking</span></p><p><span>A condition in which a process is indefinitely delayed because other processes are always given preference.</span></p><p><span>Starvation is the problem that occurs when high priority processes keep executing and low priority processes get blocked for indefinite time.</span></p><p><mark><span>Deadlock 一定会造成 starvation</span></mark></p></li></ul><hr /><h3 id='72-死锁的特征'><span>7.2 死锁的特征</span></h3><h4 id='721-死锁的必要条件'><span>7.2.1 死锁的必要条件</span></h4><ol start='' ><li><p><span>互斥 Mutual Exclusion</span></p><p><span>Only one thread at a time can use a resource.</span></p></li><li><p><span>占有并等待 Hold and Wait</span></p><p><span>一个进程应占有至少一个资源并等待另一个资源,而该资源为其他进程所占有</span></p></li><li><p><span>非抢占 No Preemption</span></p><p><span>资源不能被抢占,即资源只能被进程在完成任务后自愿释放</span></p></li><li><p><span>循环等待 Circular Wait</span></p><p><span>有一组等待进程 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="19.905ex" height="2.712ex" viewBox="0 -839.8 8570.3 1167.7" role="img" focusable="false" style="vertical-align: -0.761ex;"><defs><path stroke-width="0" id="E85-MJMAIN-7B" d="M434 -231Q434 -244 428 -250H410Q281 -250 230 -184Q225 -177 222 -172T217 -161T213 -148T211 -133T210 -111T209 -84T209 -47T209 0Q209 21 209 53Q208 142 204 153Q203 154 203 155Q189 191 153 211T82 231Q71 231 68 234T65 250T68 266T82 269Q116 269 152 289T203 345Q208 356 208 377T209 529V579Q209 634 215 656T244 698Q270 724 324 740Q361 748 377 749Q379 749 390 749T408 750H428Q434 744 434 732Q434 719 431 716Q429 713 415 713Q362 710 332 689T296 647Q291 634 291 499V417Q291 370 288 353T271 314Q240 271 184 255L170 250L184 245Q202 239 220 230T262 196T290 137Q291 131 291 1Q291 -134 296 -147Q306 -174 339 -192T415 -213Q429 -213 431 -216Q434 -219 434 -231Z"></path><path stroke-width="0" id="E85-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E85-MJMAIN-30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path><path stroke-width="0" id="E85-MJMAIN-2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path><path stroke-width="0" id="E85-MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path stroke-width="0" id="E85-MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path stroke-width="0" id="E85-MJMAIN-2026" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60ZM525 60Q525 84 542 102T585 120Q609 120 627 104T646 61Q646 36 629 18T586 0T543 17T525 60ZM972 60Q972 84 989 102T1032 120Q1056 120 1074 104T1093 61Q1093 36 1076 18T1033 0T990 17T972 60Z"></path><path stroke-width="0" id="E85-MJMATHI-6E" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E85-MJMAIN-7D" d="M65 731Q65 745 68 747T88 750Q171 750 216 725T279 670Q288 649 289 635T291 501Q292 362 293 357Q306 312 345 291T417 269Q428 269 431 266T434 250T431 234T417 231Q380 231 345 210T298 157Q293 143 292 121T291 -28V-79Q291 -134 285 -156T256 -198Q202 -250 89 -250Q71 -250 68 -247T65 -230Q65 -224 65 -223T66 -218T69 -214T77 -213Q91 -213 108 -210T146 -200T183 -177T207 -139Q208 -134 209 3L210 139Q223 196 280 230Q315 247 330 250Q305 257 280 270Q225 304 212 352L210 362L209 498Q208 635 207 640Q195 680 154 696T77 713Q68 713 67 716T65 731Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E85-MJMAIN-7B" x="0" y="0"></use><g transform="translate(500,0)"><use xlink:href="#E85-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E85-MJMAIN-30" x="907" y="-213"></use></g><use xlink:href="#E85-MJMAIN-2C" x="1595" y="0"></use><g transform="translate(2040,0)"><use xlink:href="#E85-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E85-MJMAIN-31" x="907" y="-213"></use></g><use xlink:href="#E85-MJMAIN-2C" x="3135" y="0"></use><g transform="translate(3580,0)"><use xlink:href="#E85-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E85-MJMAIN-32" x="907" y="-213"></use></g><use xlink:href="#E85-MJMAIN-2C" x="4675" y="0"></use><use xlink:href="#E85-MJMAIN-2026" x="5120" y="0"></use><use xlink:href="#E85-MJMAIN-2C" x="6459" y="0"></use><g transform="translate(6903,0)"><use xlink:href="#E85-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E85-MJMATHI-6E" x="907" y="-213"></use></g><use xlink:href="#E85-MJMAIN-7D" x="8070" y="0"></use></g></svg></span><script type="math/tex">\{P_0,P_1, P_2,\dots,P_n\}</script></p><ul><li><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.545ex" height="2.28ex" viewBox="0 -746.7 1095.6 981.5" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E109-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E109-MJMAIN-30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E109-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E109-MJMAIN-30" x="907" y="-213"></use></g></svg></span><script type="math/tex">P_0</script><span> 等待的资源被 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.545ex" height="2.28ex" viewBox="0 -746.7 1095.6 981.5" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E88-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E88-MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E88-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E88-MJMAIN-31" x="907" y="-213"></use></g></svg></span><script type="math/tex">P_1</script><span> 占有</span></li><li><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.545ex" height="2.28ex" viewBox="0 -746.7 1095.6 981.5" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E88-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E88-MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E88-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E88-MJMAIN-31" x="907" y="-213"></use></g></svg></span><script type="math/tex">P_1</script><span> 等待的资源被 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.545ex" height="2.28ex" viewBox="0 -746.7 1095.6 981.5" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E89-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E89-MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E89-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E89-MJMAIN-32" x="907" y="-213"></use></g></svg></span><script type="math/tex">P_2</script><span> 占有</span></li><li><span>...</span></li><li><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.709ex" height="2.28ex" viewBox="0 -746.7 1166.3 981.5" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E90-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E90-MJMATHI-6E" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E90-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E90-MJMATHI-6E" x="907" y="-213"></use></g></svg></span><script type="math/tex">P_n</script><span> 等待的资源被 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.545ex" height="2.28ex" viewBox="0 -746.7 1095.6 981.5" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E109-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E109-MJMAIN-30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E109-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E109-MJMAIN-30" x="907" y="-213"></use></g></svg></span><script type="math/tex">P_0</script><span> 占有</span></li></ul></li></ol><p><span>注意是必要条件,即使这些条件都满足也不一定死锁,还需要运气比较背</span></p><h4 id='722-资源分配图-resource-allocation-graph'><span>7.2.2 资源分配图 Resource-Allocation Graph</span></h4><p><img src="D:\TyporaPictures\OS\75.PNG" referrerpolicy="no-referrer"></p><ul><li><p><span>圆表示进程</span></p></li><li><p><span>矩形表示资源</span></p></li><li><p><span>矩形内的点表示资源实例</span></p></li><li><p><span>申请边 Request Edge</span></p><p><span>进程指向资源的边</span></p></li><li><p><span>分配边 Assignment Edge</span></p><p><span>资源指向进程的边</span></p></li></ul><p> </p><ul><li><p><span>如果分配图没有环,那么系统一定没有死锁;如果有环,那么可能存在死锁</span></p></li><li><p><span>死锁的例子</span></p><p><img src="D:\TyporaPictures\OS\76.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>有环没死锁的例子</span></p><p><span>让 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="6.122ex" height="2.388ex" viewBox="0 -746.7 2635.8 1028" role="img" focusable="false" style="vertical-align: -0.653ex;"><defs><path stroke-width="0" id="E92-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E92-MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path stroke-width="0" id="E92-MJMAIN-2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path><path stroke-width="0" id="E92-MJMAIN-34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E92-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E92-MJMAIN-32" x="907" y="-213"></use><use xlink:href="#E92-MJMAIN-2C" x="1095" y="0"></use><g transform="translate(1540,0)"><use xlink:href="#E92-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E92-MJMAIN-34" x="907" y="-213"></use></g></g></svg></span><script type="math/tex">P_2, P_4</script><span> 先执行完</span></p><p><img src="D:\TyporaPictures\OS\77.PNG" referrerpolicy="no-referrer"></p></li></ul><hr /><h3 id='73-死锁的处理方法'><span>7.3 死锁的处理方法</span></h3><ul><li><p><span>通过协议来预防或避免死锁,确保系统不会进入死锁状态</span></p></li><li><p><span>允许系统进入死锁状态,然后检测并恢复</span></p></li><li><p><span>忽视,认为死锁不可能在系统内发生</span></p><p><span>这种方案被 Linux, Windows 等大多数 OS 采用</span></p><p><span>就算出现了死锁,OS 也不管</span></p></li></ul><hr /><h3 id='74-死锁检测-deadlock-detection'><span>7.4 死锁检测 Deadlock Detection</span></h3><h4 id='741-死锁检测算法'><span>7.4.1 死锁检测算法</span></h4><p><span>[xxx] 表示数组</span></p><ul><li><span>[</span><span>FreeResources</span><span>]</span><span>: current free resources each type</span></li><li><span>[</span><span>Request</span><sub><span>X</span></sub><span>]: current requests from process X</span></li><li><span>[</span><span>Alloc</span><sub><span>X</span></sub><span>]: current resources held by process X</span></li></ul><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang=""><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang=""><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 43px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 39px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>13</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -39px; width: 39px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">[Avail] = [FreeResources]</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">Add all nodes to UNFINISHED</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">do {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> done = true</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> Foreach node in UNFINISHED {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> if ([Request_node] <= [Avail]) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> remove node from UNFINISHED</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> [Avail] = [Avail] + [Alloc_node]</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> done = false</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">} until(done)</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 286px;"></div><div class="CodeMirror-gutters" style="height: 286px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 38px;"></div></div></div></div></pre><h4 id='742-死锁恢复'><span>7.4.2 死锁恢复</span></h4><p><span>当检测到死锁后: </span></p><ul><li><p><span>进程终止</span></p><p><span>Terminate thread, force it to give up resources</span></p></li><li><p><span>资源抢占</span></p><p><span>Preempt resources without killing off process</span></p></li><li><p><span>回滚</span></p><p><span>Roll back actions of deadlocked threads</span></p></li><li><p><span>Many operating systems use other options</span></p></li></ul><hr /><h3 id='75-死锁预防-deadlock-prevention'><span>7.5 死锁预防 Deadlock Prevention</span></h3><p><span>核心:打破四个必要条件</span></p><ol start='' ><li><p><span>互斥</span></p><ul><li><span>大家都用只读文件</span></li><li><span>给足够多的资源</span></li></ul></li><li><p><span>持有且等待</span></p><ul><li><span>每个进程在执行前申请并获得所有资源</span></li><li><span>进程仅在没有资源时才申请资源</span></li></ul></li><li><p><span>无抢占</span></p><ul><li><p><span>如果一个进程持有资源并申请一个不能被立即分配的资源,那么它现在分配的资源都可以被抢占</span></p><p><span>相当于把它现有的资源都释放了</span></p></li></ul></li><li><p><span>循环等待</span></p><ul><li><span>给所有进程一个指定的顺序来申请资源</span></li></ul></li></ol><hr /><h3 id='76-死锁避免-deadlock-avoidance'><span>7.6 死锁避免 Deadlock Avoidance</span></h3><h4 id='761-安全状态'><span>7.6.1 安全状态</span></h4><ul><li><span>如果系统能按一定顺序为每个进程分配资源(不超过其最大需求),可以避免死锁,那么系统的状态就是安全的 (safe)</span></li><li><span>只有存在一个安全序列 (safe sequence),系统才处于安全状态</span></li><li><span>如果没有这样的序列存在,那么系统状态就是非安全 (unsafe)</span></li><li><span>非安全状态只是可能会导致死锁,不是一定</span></li></ul><h4 id='762-资源分配图算法'><span>7.6.2 资源分配图算法</span></h4><p><img src="D:\TyporaPictures\OS\78.PNG" referrerpolicy="no-referrer"></p><ul><li><p><span>需求边 Claim Edge</span></p><p><span>进程指向资源,虚线</span></p><p><span>进程 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.29ex" height="2.28ex" viewBox="0 -746.7 986 981.5" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E93-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E93-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E93-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E93-MJMATHI-69" x="907" y="-213"></use></g></svg></span><script type="math/tex">P_i</script><span> 可能在将来申请某个资源 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.672ex" height="2.604ex" viewBox="0 -746.7 1150.3 1121.1" role="img" focusable="false" style="vertical-align: -0.87ex;"><defs><path stroke-width="0" id="E94-MJMATHI-52" d="M230 637Q203 637 198 638T193 649Q193 676 204 682Q206 683 378 683Q550 682 564 680Q620 672 658 652T712 606T733 563T739 529Q739 484 710 445T643 385T576 351T538 338L545 333Q612 295 612 223Q612 212 607 162T602 80V71Q602 53 603 43T614 25T640 16Q668 16 686 38T712 85Q717 99 720 102T735 105Q755 105 755 93Q755 75 731 36Q693 -21 641 -21H632Q571 -21 531 4T487 82Q487 109 502 166T517 239Q517 290 474 313Q459 320 449 321T378 323H309L277 193Q244 61 244 59Q244 55 245 54T252 50T269 48T302 46H333Q339 38 339 37T336 19Q332 6 326 0H311Q275 2 180 2Q146 2 117 2T71 2T50 1Q33 1 33 10Q33 12 36 24Q41 43 46 45Q50 46 61 46H67Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628Q287 635 230 637ZM630 554Q630 586 609 608T523 636Q521 636 500 636T462 637H440Q393 637 386 627Q385 624 352 494T319 361Q319 360 388 360Q466 361 492 367Q556 377 592 426Q608 449 619 486T630 554Z"></path><path stroke-width="0" id="E94-MJMATHI-6A" d="M297 596Q297 627 318 644T361 661Q378 661 389 651T403 623Q403 595 384 576T340 557Q322 557 310 567T297 596ZM288 376Q288 405 262 405Q240 405 220 393T185 362T161 325T144 293L137 279Q135 278 121 278H107Q101 284 101 286T105 299Q126 348 164 391T252 441Q253 441 260 441T272 442Q296 441 316 432Q341 418 354 401T367 348V332L318 133Q267 -67 264 -75Q246 -125 194 -164T75 -204Q25 -204 7 -183T-12 -137Q-12 -110 7 -91T53 -71Q70 -71 82 -81T95 -112Q95 -148 63 -167Q69 -168 77 -168Q111 -168 139 -140T182 -74L193 -32Q204 11 219 72T251 197T278 308T289 365Q289 372 288 376Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E94-MJMATHI-52" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E94-MJMATHI-6A" x="1073" y="-213"></use></g></svg></span><script type="math/tex">R_j</script></p></li><li><p><span>只有在将申请边变成分配边 (反向实线箭头) 并且不会导致资源分配图形成环时,才能允许申请</span></p></li><li><p><span>时间复杂度</span></p><p><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="6.026ex" height="2.82ex" viewBox="0 -886.4 2594.6 1214.2" role="img" focusable="false" style="vertical-align: -0.761ex;"><defs><path stroke-width="0" id="E95-MJMATHI-4F" d="M740 435Q740 320 676 213T511 42T304 -22Q207 -22 138 35T51 201Q50 209 50 244Q50 346 98 438T227 601Q351 704 476 704Q514 704 524 703Q621 689 680 617T740 435ZM637 476Q637 565 591 615T476 665Q396 665 322 605Q242 542 200 428T157 216Q157 126 200 73T314 19Q404 19 485 98T608 313Q637 408 637 476Z"></path><path stroke-width="0" id="E95-MJMAIN-28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path><path stroke-width="0" id="E95-MJMATHI-6E" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E95-MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path stroke-width="0" id="E95-MJMAIN-29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E95-MJMATHI-4F" x="0" y="0"></use><use xlink:href="#E95-MJMAIN-28" x="763" y="0"></use><g transform="translate(1152,0)"><use xlink:href="#E95-MJMATHI-6E" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E95-MJMAIN-32" x="848" y="513"></use></g><use xlink:href="#E95-MJMAIN-29" x="2205" y="0"></use></g></svg></span><script type="math/tex">O(n^2)</script><span>, </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.394ex" height="1.415ex" viewBox="0 -514 600 609.1" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E124-MJMATHI-6E" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E124-MJMATHI-6E" x="0" y="0"></use></g></svg></span><script type="math/tex">n</script><span> 为进程数量</span></p></li></ul><h4 id='763-银行家算法-bankers-algorithm'><span>7.6.3 银行家算法 Banker's Algorithm</span></h4><p><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.394ex" height="1.415ex" viewBox="0 -514 600 609.1" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E124-MJMATHI-6E" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E124-MJMATHI-6E" x="0" y="0"></use></g></svg></span><script type="math/tex">n</script><span> 个进程, </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.039ex" height="1.415ex" viewBox="0 -514 878 609.1" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E98-MJMATHI-6D" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E98-MJMATHI-6D" x="0" y="0"></use></g></svg></span><script type="math/tex">m</script><span> 种资源</span></p><ul><li><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="9.59ex" height="2.063ex" viewBox="0 -793.3 4129 888.4" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E99-MJMATHI-41" d="M208 74Q208 50 254 46Q272 46 272 35Q272 34 270 22Q267 8 264 4T251 0Q249 0 239 0T205 1T141 2Q70 2 50 0H42Q35 7 35 11Q37 38 48 46H62Q132 49 164 96Q170 102 345 401T523 704Q530 716 547 716H555H572Q578 707 578 706L606 383Q634 60 636 57Q641 46 701 46Q726 46 726 36Q726 34 723 22Q720 7 718 4T704 0Q701 0 690 0T651 1T578 2Q484 2 455 0H443Q437 6 437 9T439 27Q443 40 445 43L449 46H469Q523 49 533 63L521 213H283L249 155Q208 86 208 74ZM516 260Q516 271 504 416T490 562L463 519Q447 492 400 412L310 260L413 259Q516 259 516 260Z"></path><path stroke-width="0" id="E99-MJMATHI-76" d="M173 380Q173 405 154 405Q130 405 104 376T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Q21 294 29 316T53 368T97 419T160 441Q202 441 225 417T249 361Q249 344 246 335Q246 329 231 291T200 202T182 113Q182 86 187 69Q200 26 250 26Q287 26 319 60T369 139T398 222T409 277Q409 300 401 317T383 343T365 361T357 383Q357 405 376 424T417 443Q436 443 451 425T467 367Q467 340 455 284T418 159T347 40T241 -11Q177 -11 139 22Q102 54 102 117Q102 148 110 181T151 298Q173 362 173 380Z"></path><path stroke-width="0" id="E99-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path><path stroke-width="0" id="E99-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E99-MJMATHI-6C" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path><path stroke-width="0" id="E99-MJMATHI-62" d="M73 647Q73 657 77 670T89 683Q90 683 161 688T234 694Q246 694 246 685T212 542Q204 508 195 472T180 418L176 399Q176 396 182 402Q231 442 283 442Q345 442 383 396T422 280Q422 169 343 79T173 -11Q123 -11 82 27T40 150V159Q40 180 48 217T97 414Q147 611 147 623T109 637Q104 637 101 637H96Q86 637 83 637T76 640T73 647ZM336 325V331Q336 405 275 405Q258 405 240 397T207 376T181 352T163 330L157 322L136 236Q114 150 114 114Q114 66 138 42Q154 26 178 26Q211 26 245 58Q270 81 285 114T318 219Q336 291 336 325Z"></path><path stroke-width="0" id="E99-MJMATHI-65" d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E99-MJMATHI-41" x="0" y="0"></use><use xlink:href="#E99-MJMATHI-76" x="750" y="0"></use><use xlink:href="#E99-MJMATHI-61" x="1235" y="0"></use><use xlink:href="#E99-MJMATHI-69" x="1764" y="0"></use><use xlink:href="#E99-MJMATHI-6C" x="2109" y="0"></use><use xlink:href="#E99-MJMATHI-61" x="2407" y="0"></use><use xlink:href="#E99-MJMATHI-62" x="2936" y="0"></use><use xlink:href="#E99-MJMATHI-6C" x="3365" y="0"></use><use xlink:href="#E99-MJMATHI-65" x="3663" y="0"></use></g></svg></span><script type="math/tex">Available</script><span>: 行向量,表示每种资源的可用实例数量</span></li><li><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="4.998ex" height="1.955ex" viewBox="0 -746.7 2152 841.8" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E100-MJMATHI-4D" d="M289 629Q289 635 232 637Q208 637 201 638T194 648Q194 649 196 659Q197 662 198 666T199 671T201 676T203 679T207 681T212 683T220 683T232 684Q238 684 262 684T307 683Q386 683 398 683T414 678Q415 674 451 396L487 117L510 154Q534 190 574 254T662 394Q837 673 839 675Q840 676 842 678T846 681L852 683H948Q965 683 988 683T1017 684Q1051 684 1051 673Q1051 668 1048 656T1045 643Q1041 637 1008 637Q968 636 957 634T939 623Q936 618 867 340T797 59Q797 55 798 54T805 50T822 48T855 46H886Q892 37 892 35Q892 19 885 5Q880 0 869 0Q864 0 828 1T736 2Q675 2 644 2T609 1Q592 1 592 11Q592 13 594 25Q598 41 602 43T625 46Q652 46 685 49Q699 52 704 61Q706 65 742 207T813 490T848 631L654 322Q458 10 453 5Q451 4 449 3Q444 0 433 0Q418 0 415 7Q413 11 374 317L335 624L267 354Q200 88 200 79Q206 46 272 46H282Q288 41 289 37T286 19Q282 3 278 1Q274 0 267 0Q265 0 255 0T221 1T157 2Q127 2 95 1T58 0Q43 0 39 2T35 11Q35 13 38 25T43 40Q45 46 65 46Q135 46 154 86Q158 92 223 354T289 629Z"></path><path stroke-width="0" id="E100-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path><path stroke-width="0" id="E100-MJMATHI-78" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E100-MJMATHI-4D" x="0" y="0"></use><use xlink:href="#E100-MJMATHI-61" x="1051" y="0"></use><use xlink:href="#E100-MJMATHI-78" x="1580" y="0"></use></g></svg></span><script type="math/tex">Max</script><span>: </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="6.272ex" height="1.523ex" viewBox="0 -560.6 2700.4 655.6" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E103-MJMATHI-6E" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E103-MJMAIN-D7" d="M630 29Q630 9 609 9Q604 9 587 25T493 118L389 222L284 117Q178 13 175 11Q171 9 168 9Q160 9 154 15T147 29Q147 36 161 51T255 146L359 250L255 354Q174 435 161 449T147 471Q147 480 153 485T168 490Q173 490 175 489Q178 487 284 383L389 278L493 382Q570 459 587 475T609 491Q630 491 630 471Q630 464 620 453T522 355L418 250L522 145Q606 61 618 48T630 29Z"></path><path stroke-width="0" id="E103-MJMATHI-6D" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E103-MJMATHI-6E" x="0" y="0"></use><use xlink:href="#E103-MJMAIN-D7" x="822" y="0"></use><use xlink:href="#E103-MJMATHI-6D" x="1822" y="0"></use></g></svg></span><script type="math/tex">n\times m</script><span> 矩阵,每个进程的最大需求</span></li><li><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="10.647ex" height="2.063ex" viewBox="0 -793.3 4584 888.4" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E102-MJMATHI-41" d="M208 74Q208 50 254 46Q272 46 272 35Q272 34 270 22Q267 8 264 4T251 0Q249 0 239 0T205 1T141 2Q70 2 50 0H42Q35 7 35 11Q37 38 48 46H62Q132 49 164 96Q170 102 345 401T523 704Q530 716 547 716H555H572Q578 707 578 706L606 383Q634 60 636 57Q641 46 701 46Q726 46 726 36Q726 34 723 22Q720 7 718 4T704 0Q701 0 690 0T651 1T578 2Q484 2 455 0H443Q437 6 437 9T439 27Q443 40 445 43L449 46H469Q523 49 533 63L521 213H283L249 155Q208 86 208 74ZM516 260Q516 271 504 416T490 562L463 519Q447 492 400 412L310 260L413 259Q516 259 516 260Z"></path><path stroke-width="0" id="E102-MJMATHI-6C" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path><path stroke-width="0" id="E102-MJMATHI-6F" d="M201 -11Q126 -11 80 38T34 156Q34 221 64 279T146 380Q222 441 301 441Q333 441 341 440Q354 437 367 433T402 417T438 387T464 338T476 268Q476 161 390 75T201 -11ZM121 120Q121 70 147 48T206 26Q250 26 289 58T351 142Q360 163 374 216T388 308Q388 352 370 375Q346 405 306 405Q243 405 195 347Q158 303 140 230T121 120Z"></path><path stroke-width="0" id="E102-MJMATHI-63" d="M34 159Q34 268 120 355T306 442Q362 442 394 418T427 355Q427 326 408 306T360 285Q341 285 330 295T319 325T330 359T352 380T366 386H367Q367 388 361 392T340 400T306 404Q276 404 249 390Q228 381 206 359Q162 315 142 235T121 119Q121 73 147 50Q169 26 205 26H209Q321 26 394 111Q403 121 406 121Q410 121 419 112T429 98T420 83T391 55T346 25T282 0T202 -11Q127 -11 81 37T34 159Z"></path><path stroke-width="0" id="E102-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path><path stroke-width="0" id="E102-MJMATHI-74" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path><path stroke-width="0" id="E102-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E102-MJMATHI-6E" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E102-MJMATHI-41" x="0" y="0"></use><use xlink:href="#E102-MJMATHI-6C" x="750" y="0"></use><use xlink:href="#E102-MJMATHI-6C" x="1048" y="0"></use><use xlink:href="#E102-MJMATHI-6F" x="1346" y="0"></use><use xlink:href="#E102-MJMATHI-63" x="1831" y="0"></use><use xlink:href="#E102-MJMATHI-61" x="2264" y="0"></use><use xlink:href="#E102-MJMATHI-74" x="2793" y="0"></use><use xlink:href="#E102-MJMATHI-69" x="3154" y="0"></use><use xlink:href="#E102-MJMATHI-6F" x="3499" y="0"></use><use xlink:href="#E102-MJMATHI-6E" x="3984" y="0"></use></g></svg></span><script type="math/tex">Allocation</script><span>: </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="6.272ex" height="1.523ex" viewBox="0 -560.6 2700.4 655.6" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E103-MJMATHI-6E" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E103-MJMAIN-D7" d="M630 29Q630 9 609 9Q604 9 587 25T493 118L389 222L284 117Q178 13 175 11Q171 9 168 9Q160 9 154 15T147 29Q147 36 161 51T255 146L359 250L255 354Q174 435 161 449T147 471Q147 480 153 485T168 490Q173 490 175 489Q178 487 284 383L389 278L493 382Q570 459 587 475T609 491Q630 491 630 471Q630 464 620 453T522 355L418 250L522 145Q606 61 618 48T630 29Z"></path><path stroke-width="0" id="E103-MJMATHI-6D" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E103-MJMATHI-6E" x="0" y="0"></use><use xlink:href="#E103-MJMAIN-D7" x="822" y="0"></use><use xlink:href="#E103-MJMATHI-6D" x="1822" y="0"></use></g></svg></span><script type="math/tex">n\times m</script><span> 矩阵,每个进程已经分配的实例数量</span></li><li><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="27.023ex" height="2.171ex" viewBox="0 -793.3 11635 934.9" role="img" focusable="false" style="vertical-align: -0.329ex;"><defs><path stroke-width="0" id="E104-MJMATHI-4E" d="M234 637Q231 637 226 637Q201 637 196 638T191 649Q191 676 202 682Q204 683 299 683Q376 683 387 683T401 677Q612 181 616 168L670 381Q723 592 723 606Q723 633 659 637Q635 637 635 648Q635 650 637 660Q641 676 643 679T653 683Q656 683 684 682T767 680Q817 680 843 681T873 682Q888 682 888 672Q888 650 880 642Q878 637 858 637Q787 633 769 597L620 7Q618 0 599 0Q585 0 582 2Q579 5 453 305L326 604L261 344Q196 88 196 79Q201 46 268 46H278Q284 41 284 38T282 19Q278 6 272 0H259Q228 2 151 2Q123 2 100 2T63 2T46 1Q31 1 31 10Q31 14 34 26T39 40Q41 46 62 46Q130 49 150 85Q154 91 221 362L289 634Q287 635 234 637Z"></path><path stroke-width="0" id="E104-MJMATHI-65" d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z"></path><path stroke-width="0" id="E104-MJMATHI-64" d="M366 683Q367 683 438 688T511 694Q523 694 523 686Q523 679 450 384T375 83T374 68Q374 26 402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487H491Q506 153 506 145Q506 140 503 129Q490 79 473 48T445 8T417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157Q33 205 53 255T101 341Q148 398 195 420T280 442Q336 442 364 400Q369 394 369 396Q370 400 396 505T424 616Q424 629 417 632T378 637H357Q351 643 351 645T353 664Q358 683 366 683ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z"></path><path stroke-width="0" id="E104-MJMAIN-3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path><path stroke-width="0" id="E104-MJMATHI-4D" d="M289 629Q289 635 232 637Q208 637 201 638T194 648Q194 649 196 659Q197 662 198 666T199 671T201 676T203 679T207 681T212 683T220 683T232 684Q238 684 262 684T307 683Q386 683 398 683T414 678Q415 674 451 396L487 117L510 154Q534 190 574 254T662 394Q837 673 839 675Q840 676 842 678T846 681L852 683H948Q965 683 988 683T1017 684Q1051 684 1051 673Q1051 668 1048 656T1045 643Q1041 637 1008 637Q968 636 957 634T939 623Q936 618 867 340T797 59Q797 55 798 54T805 50T822 48T855 46H886Q892 37 892 35Q892 19 885 5Q880 0 869 0Q864 0 828 1T736 2Q675 2 644 2T609 1Q592 1 592 11Q592 13 594 25Q598 41 602 43T625 46Q652 46 685 49Q699 52 704 61Q706 65 742 207T813 490T848 631L654 322Q458 10 453 5Q451 4 449 3Q444 0 433 0Q418 0 415 7Q413 11 374 317L335 624L267 354Q200 88 200 79Q206 46 272 46H282Q288 41 289 37T286 19Q282 3 278 1Q274 0 267 0Q265 0 255 0T221 1T157 2Q127 2 95 1T58 0Q43 0 39 2T35 11Q35 13 38 25T43 40Q45 46 65 46Q135 46 154 86Q158 92 223 354T289 629Z"></path><path stroke-width="0" id="E104-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path><path stroke-width="0" id="E104-MJMATHI-78" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path><path stroke-width="0" id="E104-MJMAIN-2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path><path stroke-width="0" id="E104-MJMATHI-41" d="M208 74Q208 50 254 46Q272 46 272 35Q272 34 270 22Q267 8 264 4T251 0Q249 0 239 0T205 1T141 2Q70 2 50 0H42Q35 7 35 11Q37 38 48 46H62Q132 49 164 96Q170 102 345 401T523 704Q530 716 547 716H555H572Q578 707 578 706L606 383Q634 60 636 57Q641 46 701 46Q726 46 726 36Q726 34 723 22Q720 7 718 4T704 0Q701 0 690 0T651 1T578 2Q484 2 455 0H443Q437 6 437 9T439 27Q443 40 445 43L449 46H469Q523 49 533 63L521 213H283L249 155Q208 86 208 74ZM516 260Q516 271 504 416T490 562L463 519Q447 492 400 412L310 260L413 259Q516 259 516 260Z"></path><path stroke-width="0" id="E104-MJMATHI-6C" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path><path stroke-width="0" id="E104-MJMATHI-6F" d="M201 -11Q126 -11 80 38T34 156Q34 221 64 279T146 380Q222 441 301 441Q333 441 341 440Q354 437 367 433T402 417T438 387T464 338T476 268Q476 161 390 75T201 -11ZM121 120Q121 70 147 48T206 26Q250 26 289 58T351 142Q360 163 374 216T388 308Q388 352 370 375Q346 405 306 405Q243 405 195 347Q158 303 140 230T121 120Z"></path><path stroke-width="0" id="E104-MJMATHI-63" d="M34 159Q34 268 120 355T306 442Q362 442 394 418T427 355Q427 326 408 306T360 285Q341 285 330 295T319 325T330 359T352 380T366 386H367Q367 388 361 392T340 400T306 404Q276 404 249 390Q228 381 206 359Q162 315 142 235T121 119Q121 73 147 50Q169 26 205 26H209Q321 26 394 111Q403 121 406 121Q410 121 419 112T429 98T420 83T391 55T346 25T282 0T202 -11Q127 -11 81 37T34 159Z"></path><path stroke-width="0" id="E104-MJMATHI-74" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path><path stroke-width="0" id="E104-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E104-MJMATHI-6E" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E104-MJMATHI-4E" x="0" y="0"></use><use xlink:href="#E104-MJMATHI-65" x="888" y="0"></use><use xlink:href="#E104-MJMATHI-65" x="1354" y="0"></use><use xlink:href="#E104-MJMATHI-64" x="1820" y="0"></use><use xlink:href="#E104-MJMAIN-3D" x="2620" y="0"></use><use xlink:href="#E104-MJMATHI-4D" x="3676" y="0"></use><use xlink:href="#E104-MJMATHI-61" x="4727" y="0"></use><use xlink:href="#E104-MJMATHI-78" x="5256" y="0"></use><use xlink:href="#E104-MJMAIN-2212" x="6050" y="0"></use><use xlink:href="#E104-MJMATHI-41" x="7051" y="0"></use><use xlink:href="#E104-MJMATHI-6C" x="7801" y="0"></use><use xlink:href="#E104-MJMATHI-6C" x="8099" y="0"></use><use xlink:href="#E104-MJMATHI-6F" x="8397" y="0"></use><use xlink:href="#E104-MJMATHI-63" x="8882" y="0"></use><use xlink:href="#E104-MJMATHI-61" x="9315" y="0"></use><use xlink:href="#E104-MJMATHI-74" x="9844" y="0"></use><use xlink:href="#E104-MJMATHI-69" x="10205" y="0"></use><use xlink:href="#E104-MJMATHI-6F" x="10550" y="0"></use><use xlink:href="#E104-MJMATHI-6E" x="11035" y="0"></use></g></svg></span><script type="math/tex">Need = Max - Allocation</script><span>,还缺多少实例才能完事</span></li></ul><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang=""><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang=""><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 43px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 39px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>12</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -39px; width: 39px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">Add all nodes to UNFINISHED</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">do {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> done = true</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> Foreach node in UNFINISHED {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> if ([Max_node] - [Alloc_node] <= [Avail]) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> remove node from UNFINISHED</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> [Avail] = [Avail] + [Alloc_node]</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> done = false</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">} until(done)</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 264px;"></div><div class="CodeMirror-gutters" style="height: 264px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 38px;"></div></div></div></div></pre><ul><li><p><span>例: 可以按 0213 或 0231 的顺序执行完</span></p><p><img src="D:\TyporaPictures\OS\79.PNG" referrerpolicy="no-referrer"></p></li><li><p><span>现在有个新的 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="8.252ex" height="2.388ex" viewBox="0 -746.7 3553 1028" role="img" focusable="false" style="vertical-align: -0.653ex;"><defs><path stroke-width="0" id="E105-MJMATHI-52" d="M230 637Q203 637 198 638T193 649Q193 676 204 682Q206 683 378 683Q550 682 564 680Q620 672 658 652T712 606T733 563T739 529Q739 484 710 445T643 385T576 351T538 338L545 333Q612 295 612 223Q612 212 607 162T602 80V71Q602 53 603 43T614 25T640 16Q668 16 686 38T712 85Q717 99 720 102T735 105Q755 105 755 93Q755 75 731 36Q693 -21 641 -21H632Q571 -21 531 4T487 82Q487 109 502 166T517 239Q517 290 474 313Q459 320 449 321T378 323H309L277 193Q244 61 244 59Q244 55 245 54T252 50T269 48T302 46H333Q339 38 339 37T336 19Q332 6 326 0H311Q275 2 180 2Q146 2 117 2T71 2T50 1Q33 1 33 10Q33 12 36 24Q41 43 46 45Q50 46 61 46H67Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628Q287 635 230 637ZM630 554Q630 586 609 608T523 636Q521 636 500 636T462 637H440Q393 637 386 627Q385 624 352 494T319 361Q319 360 388 360Q466 361 492 367Q556 377 592 426Q608 449 619 486T630 554Z"></path><path stroke-width="0" id="E105-MJMATHI-65" d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z"></path><path stroke-width="0" id="E105-MJMATHI-71" d="M33 157Q33 258 109 349T280 441Q340 441 372 389Q373 390 377 395T388 406T404 418Q438 442 450 442Q454 442 457 439T460 434Q460 425 391 149Q320 -135 320 -139Q320 -147 365 -148H390Q396 -156 396 -157T393 -175Q389 -188 383 -194H370Q339 -192 262 -192Q234 -192 211 -192T174 -192T157 -193Q143 -193 143 -185Q143 -182 145 -170Q149 -154 152 -151T172 -148Q220 -148 230 -141Q238 -136 258 -53T279 32Q279 33 272 29Q224 -10 172 -10Q117 -10 75 30T33 157ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z"></path><path stroke-width="0" id="E105-MJMATHI-75" d="M21 287Q21 295 30 318T55 370T99 420T158 442Q204 442 227 417T250 358Q250 340 216 246T182 105Q182 62 196 45T238 27T291 44T328 78L339 95Q341 99 377 247Q407 367 413 387T427 416Q444 431 463 431Q480 431 488 421T496 402L420 84Q419 79 419 68Q419 43 426 35T447 26Q469 29 482 57T512 145Q514 153 532 153Q551 153 551 144Q550 139 549 130T540 98T523 55T498 17T462 -8Q454 -10 438 -10Q372 -10 347 46Q345 45 336 36T318 21T296 6T267 -6T233 -11Q189 -11 155 7Q103 38 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E105-MJMATHI-73" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path><path stroke-width="0" id="E105-MJMATHI-74" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E105-MJMATHI-52" x="0" y="0"></use><use xlink:href="#E105-MJMATHI-65" x="759" y="0"></use><use xlink:href="#E105-MJMATHI-71" x="1225" y="0"></use><use xlink:href="#E105-MJMATHI-75" x="1685" y="0"></use><use xlink:href="#E105-MJMATHI-65" x="2257" y="0"></use><use xlink:href="#E105-MJMATHI-73" x="2723" y="0"></use><use xlink:href="#E105-MJMATHI-74" x="3192" y="0"></use></g></svg></span><script type="math/tex">Request</script><span>,比如 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="12.676ex" height="2.712ex" viewBox="0 -839.8 5457.6 1167.7" role="img" focusable="false" style="vertical-align: -0.761ex;"><defs><path stroke-width="0" id="E106-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E106-MJMAIN-30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path><path stroke-width="0" id="E106-MJMAIN-28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path><path stroke-width="0" id="E106-MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path stroke-width="0" id="E106-MJMAIN-2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path><path stroke-width="0" id="E106-MJMAIN-33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z"></path><path stroke-width="0" id="E106-MJMAIN-29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E106-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E106-MJMAIN-30" x="907" y="-213"></use><use xlink:href="#E106-MJMAIN-28" x="1345" y="0"></use><use xlink:href="#E106-MJMAIN-31" x="1734" y="0"></use><use xlink:href="#E106-MJMAIN-2C" x="2234" y="0"></use><use xlink:href="#E106-MJMAIN-33" x="2679" y="0"></use><use xlink:href="#E106-MJMAIN-2C" x="3179" y="0"></use><use xlink:href="#E106-MJMAIN-31" x="3623" y="0"></use><use xlink:href="#E106-MJMAIN-2C" x="4123" y="0"></use><use xlink:href="#E106-MJMAIN-30" x="4568" y="0"></use><use xlink:href="#E106-MJMAIN-29" x="5068" y="0"></use></g></svg></span><script type="math/tex">P_0\ (1, 3, 1, 0)</script></p><ol start='' ><li><p><span>检查 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="21.993ex" height="2.496ex" viewBox="0 -793.3 9469.1 1074.6" role="img" focusable="false" style="vertical-align: -0.653ex;"><defs><path stroke-width="0" id="E107-MJMATHI-52" d="M230 637Q203 637 198 638T193 649Q193 676 204 682Q206 683 378 683Q550 682 564 680Q620 672 658 652T712 606T733 563T739 529Q739 484 710 445T643 385T576 351T538 338L545 333Q612 295 612 223Q612 212 607 162T602 80V71Q602 53 603 43T614 25T640 16Q668 16 686 38T712 85Q717 99 720 102T735 105Q755 105 755 93Q755 75 731 36Q693 -21 641 -21H632Q571 -21 531 4T487 82Q487 109 502 166T517 239Q517 290 474 313Q459 320 449 321T378 323H309L277 193Q244 61 244 59Q244 55 245 54T252 50T269 48T302 46H333Q339 38 339 37T336 19Q332 6 326 0H311Q275 2 180 2Q146 2 117 2T71 2T50 1Q33 1 33 10Q33 12 36 24Q41 43 46 45Q50 46 61 46H67Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628Q287 635 230 637ZM630 554Q630 586 609 608T523 636Q521 636 500 636T462 637H440Q393 637 386 627Q385 624 352 494T319 361Q319 360 388 360Q466 361 492 367Q556 377 592 426Q608 449 619 486T630 554Z"></path><path stroke-width="0" id="E107-MJMATHI-65" d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z"></path><path stroke-width="0" id="E107-MJMATHI-71" d="M33 157Q33 258 109 349T280 441Q340 441 372 389Q373 390 377 395T388 406T404 418Q438 442 450 442Q454 442 457 439T460 434Q460 425 391 149Q320 -135 320 -139Q320 -147 365 -148H390Q396 -156 396 -157T393 -175Q389 -188 383 -194H370Q339 -192 262 -192Q234 -192 211 -192T174 -192T157 -193Q143 -193 143 -185Q143 -182 145 -170Q149 -154 152 -151T172 -148Q220 -148 230 -141Q238 -136 258 -53T279 32Q279 33 272 29Q224 -10 172 -10Q117 -10 75 30T33 157ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z"></path><path stroke-width="0" id="E107-MJMATHI-75" d="M21 287Q21 295 30 318T55 370T99 420T158 442Q204 442 227 417T250 358Q250 340 216 246T182 105Q182 62 196 45T238 27T291 44T328 78L339 95Q341 99 377 247Q407 367 413 387T427 416Q444 431 463 431Q480 431 488 421T496 402L420 84Q419 79 419 68Q419 43 426 35T447 26Q469 29 482 57T512 145Q514 153 532 153Q551 153 551 144Q550 139 549 130T540 98T523 55T498 17T462 -8Q454 -10 438 -10Q372 -10 347 46Q345 45 336 36T318 21T296 6T267 -6T233 -11Q189 -11 155 7Q103 38 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E107-MJMATHI-73" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path><path stroke-width="0" id="E107-MJMATHI-74" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path><path stroke-width="0" id="E107-MJMAIN-30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path><path stroke-width="0" id="E107-MJMAIN-3C" d="M694 -11T694 -19T688 -33T678 -40Q671 -40 524 29T234 166L90 235Q83 240 83 250Q83 261 91 266Q664 540 678 540Q681 540 687 534T694 519T687 505Q686 504 417 376L151 250L417 124Q686 -4 687 -5Q694 -11 694 -19Z"></path><path stroke-width="0" id="E107-MJMATHI-41" d="M208 74Q208 50 254 46Q272 46 272 35Q272 34 270 22Q267 8 264 4T251 0Q249 0 239 0T205 1T141 2Q70 2 50 0H42Q35 7 35 11Q37 38 48 46H62Q132 49 164 96Q170 102 345 401T523 704Q530 716 547 716H555H572Q578 707 578 706L606 383Q634 60 636 57Q641 46 701 46Q726 46 726 36Q726 34 723 22Q720 7 718 4T704 0Q701 0 690 0T651 1T578 2Q484 2 455 0H443Q437 6 437 9T439 27Q443 40 445 43L449 46H469Q523 49 533 63L521 213H283L249 155Q208 86 208 74ZM516 260Q516 271 504 416T490 562L463 519Q447 492 400 412L310 260L413 259Q516 259 516 260Z"></path><path stroke-width="0" id="E107-MJMATHI-76" d="M173 380Q173 405 154 405Q130 405 104 376T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Q21 294 29 316T53 368T97 419T160 441Q202 441 225 417T249 361Q249 344 246 335Q246 329 231 291T200 202T182 113Q182 86 187 69Q200 26 250 26Q287 26 319 60T369 139T398 222T409 277Q409 300 401 317T383 343T365 361T357 383Q357 405 376 424T417 443Q436 443 451 425T467 367Q467 340 455 284T418 159T347 40T241 -11Q177 -11 139 22Q102 54 102 117Q102 148 110 181T151 298Q173 362 173 380Z"></path><path stroke-width="0" id="E107-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path><path stroke-width="0" id="E107-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E107-MJMATHI-6C" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path><path stroke-width="0" id="E107-MJMATHI-62" d="M73 647Q73 657 77 670T89 683Q90 683 161 688T234 694Q246 694 246 685T212 542Q204 508 195 472T180 418L176 399Q176 396 182 402Q231 442 283 442Q345 442 383 396T422 280Q422 169 343 79T173 -11Q123 -11 82 27T40 150V159Q40 180 48 217T97 414Q147 611 147 623T109 637Q104 637 101 637H96Q86 637 83 637T76 640T73 647ZM336 325V331Q336 405 275 405Q258 405 240 397T207 376T181 352T163 330L157 322L136 236Q114 150 114 114Q114 66 138 42Q154 26 178 26Q211 26 245 58Q270 81 285 114T318 219Q336 291 336 325Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E107-MJMATHI-52" x="0" y="0"></use><use xlink:href="#E107-MJMATHI-65" x="759" y="0"></use><use xlink:href="#E107-MJMATHI-71" x="1225" y="0"></use><use xlink:href="#E107-MJMATHI-75" x="1685" y="0"></use><use xlink:href="#E107-MJMATHI-65" x="2257" y="0"></use><use xlink:href="#E107-MJMATHI-73" x="2723" y="0"></use><g transform="translate(3192,0)"><use xlink:href="#E107-MJMATHI-74" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E107-MJMAIN-30" x="510" y="-213"></use></g><use xlink:href="#E107-MJMAIN-3C" x="4284" y="0"></use><use xlink:href="#E107-MJMATHI-41" x="5340" y="0"></use><use xlink:href="#E107-MJMATHI-76" x="6090" y="0"></use><use xlink:href="#E107-MJMATHI-61" x="6575" y="0"></use><use xlink:href="#E107-MJMATHI-69" x="7104" y="0"></use><use xlink:href="#E107-MJMATHI-6C" x="7449" y="0"></use><use xlink:href="#E107-MJMATHI-61" x="7747" y="0"></use><use xlink:href="#E107-MJMATHI-62" x="8276" y="0"></use><use xlink:href="#E107-MJMATHI-6C" x="8705" y="0"></use><use xlink:href="#E107-MJMATHI-65" x="9003" y="0"></use></g></svg></span><script type="math/tex">Request_0 < Available</script></p></li><li><p><span>检查 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="32.994ex" height="2.496ex" viewBox="0 -793.3 14205.7 1074.6" role="img" focusable="false" style="vertical-align: -0.653ex;"><defs><path stroke-width="0" id="E108-MJMATHI-41" d="M208 74Q208 50 254 46Q272 46 272 35Q272 34 270 22Q267 8 264 4T251 0Q249 0 239 0T205 1T141 2Q70 2 50 0H42Q35 7 35 11Q37 38 48 46H62Q132 49 164 96Q170 102 345 401T523 704Q530 716 547 716H555H572Q578 707 578 706L606 383Q634 60 636 57Q641 46 701 46Q726 46 726 36Q726 34 723 22Q720 7 718 4T704 0Q701 0 690 0T651 1T578 2Q484 2 455 0H443Q437 6 437 9T439 27Q443 40 445 43L449 46H469Q523 49 533 63L521 213H283L249 155Q208 86 208 74ZM516 260Q516 271 504 416T490 562L463 519Q447 492 400 412L310 260L413 259Q516 259 516 260Z"></path><path stroke-width="0" id="E108-MJMATHI-6C" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path><path stroke-width="0" id="E108-MJMATHI-6F" d="M201 -11Q126 -11 80 38T34 156Q34 221 64 279T146 380Q222 441 301 441Q333 441 341 440Q354 437 367 433T402 417T438 387T464 338T476 268Q476 161 390 75T201 -11ZM121 120Q121 70 147 48T206 26Q250 26 289 58T351 142Q360 163 374 216T388 308Q388 352 370 375Q346 405 306 405Q243 405 195 347Q158 303 140 230T121 120Z"></path><path stroke-width="0" id="E108-MJMATHI-63" d="M34 159Q34 268 120 355T306 442Q362 442 394 418T427 355Q427 326 408 306T360 285Q341 285 330 295T319 325T330 359T352 380T366 386H367Q367 388 361 392T340 400T306 404Q276 404 249 390Q228 381 206 359Q162 315 142 235T121 119Q121 73 147 50Q169 26 205 26H209Q321 26 394 111Q403 121 406 121Q410 121 419 112T429 98T420 83T391 55T346 25T282 0T202 -11Q127 -11 81 37T34 159Z"></path><path stroke-width="0" id="E108-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path><path stroke-width="0" id="E108-MJMATHI-74" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path><path stroke-width="0" id="E108-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E108-MJMATHI-6E" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E108-MJMAIN-30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path><path stroke-width="0" id="E108-MJMAIN-2B" d="M56 237T56 250T70 270H369V420L370 570Q380 583 389 583Q402 583 409 568V270H707Q722 262 722 250T707 230H409V-68Q401 -82 391 -82H389H387Q375 -82 369 -68V230H70Q56 237 56 250Z"></path><path stroke-width="0" id="E108-MJMATHI-52" d="M230 637Q203 637 198 638T193 649Q193 676 204 682Q206 683 378 683Q550 682 564 680Q620 672 658 652T712 606T733 563T739 529Q739 484 710 445T643 385T576 351T538 338L545 333Q612 295 612 223Q612 212 607 162T602 80V71Q602 53 603 43T614 25T640 16Q668 16 686 38T712 85Q717 99 720 102T735 105Q755 105 755 93Q755 75 731 36Q693 -21 641 -21H632Q571 -21 531 4T487 82Q487 109 502 166T517 239Q517 290 474 313Q459 320 449 321T378 323H309L277 193Q244 61 244 59Q244 55 245 54T252 50T269 48T302 46H333Q339 38 339 37T336 19Q332 6 326 0H311Q275 2 180 2Q146 2 117 2T71 2T50 1Q33 1 33 10Q33 12 36 24Q41 43 46 45Q50 46 61 46H67Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628Q287 635 230 637ZM630 554Q630 586 609 608T523 636Q521 636 500 636T462 637H440Q393 637 386 627Q385 624 352 494T319 361Q319 360 388 360Q466 361 492 367Q556 377 592 426Q608 449 619 486T630 554Z"></path><path stroke-width="0" id="E108-MJMATHI-65" d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z"></path><path stroke-width="0" id="E108-MJMATHI-71" d="M33 157Q33 258 109 349T280 441Q340 441 372 389Q373 390 377 395T388 406T404 418Q438 442 450 442Q454 442 457 439T460 434Q460 425 391 149Q320 -135 320 -139Q320 -147 365 -148H390Q396 -156 396 -157T393 -175Q389 -188 383 -194H370Q339 -192 262 -192Q234 -192 211 -192T174 -192T157 -193Q143 -193 143 -185Q143 -182 145 -170Q149 -154 152 -151T172 -148Q220 -148 230 -141Q238 -136 258 -53T279 32Q279 33 272 29Q224 -10 172 -10Q117 -10 75 30T33 157ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z"></path><path stroke-width="0" id="E108-MJMATHI-75" d="M21 287Q21 295 30 318T55 370T99 420T158 442Q204 442 227 417T250 358Q250 340 216 246T182 105Q182 62 196 45T238 27T291 44T328 78L339 95Q341 99 377 247Q407 367 413 387T427 416Q444 431 463 431Q480 431 488 421T496 402L420 84Q419 79 419 68Q419 43 426 35T447 26Q469 29 482 57T512 145Q514 153 532 153Q551 153 551 144Q550 139 549 130T540 98T523 55T498 17T462 -8Q454 -10 438 -10Q372 -10 347 46Q345 45 336 36T318 21T296 6T267 -6T233 -11Q189 -11 155 7Q103 38 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E108-MJMATHI-73" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path><path stroke-width="0" id="E108-MJMAIN-3C" d="M694 -11T694 -19T688 -33T678 -40Q671 -40 524 29T234 166L90 235Q83 240 83 250Q83 261 91 266Q664 540 678 540Q681 540 687 534T694 519T687 505Q686 504 417 376L151 250L417 124Q686 -4 687 -5Q694 -11 694 -19Z"></path><path stroke-width="0" id="E108-MJMATHI-4D" d="M289 629Q289 635 232 637Q208 637 201 638T194 648Q194 649 196 659Q197 662 198 666T199 671T201 676T203 679T207 681T212 683T220 683T232 684Q238 684 262 684T307 683Q386 683 398 683T414 678Q415 674 451 396L487 117L510 154Q534 190 574 254T662 394Q837 673 839 675Q840 676 842 678T846 681L852 683H948Q965 683 988 683T1017 684Q1051 684 1051 673Q1051 668 1048 656T1045 643Q1041 637 1008 637Q968 636 957 634T939 623Q936 618 867 340T797 59Q797 55 798 54T805 50T822 48T855 46H886Q892 37 892 35Q892 19 885 5Q880 0 869 0Q864 0 828 1T736 2Q675 2 644 2T609 1Q592 1 592 11Q592 13 594 25Q598 41 602 43T625 46Q652 46 685 49Q699 52 704 61Q706 65 742 207T813 490T848 631L654 322Q458 10 453 5Q451 4 449 3Q444 0 433 0Q418 0 415 7Q413 11 374 317L335 624L267 354Q200 88 200 79Q206 46 272 46H282Q288 41 289 37T286 19Q282 3 278 1Q274 0 267 0Q265 0 255 0T221 1T157 2Q127 2 95 1T58 0Q43 0 39 2T35 11Q35 13 38 25T43 40Q45 46 65 46Q135 46 154 86Q158 92 223 354T289 629Z"></path><path stroke-width="0" id="E108-MJMATHI-78" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E108-MJMATHI-41" x="0" y="0"></use><use xlink:href="#E108-MJMATHI-6C" x="750" y="0"></use><use xlink:href="#E108-MJMATHI-6C" x="1048" y="0"></use><use xlink:href="#E108-MJMATHI-6F" x="1346" y="0"></use><use xlink:href="#E108-MJMATHI-63" x="1831" y="0"></use><use xlink:href="#E108-MJMATHI-61" x="2264" y="0"></use><use xlink:href="#E108-MJMATHI-74" x="2793" y="0"></use><use xlink:href="#E108-MJMATHI-69" x="3154" y="0"></use><use xlink:href="#E108-MJMATHI-6F" x="3499" y="0"></use><g transform="translate(3984,0)"><use xlink:href="#E108-MJMATHI-6E" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E108-MJMAIN-30" x="848" y="-213"></use></g><use xlink:href="#E108-MJMAIN-2B" x="5259" y="0"></use><use xlink:href="#E108-MJMATHI-52" x="6259" y="0"></use><use xlink:href="#E108-MJMATHI-65" x="7018" y="0"></use><use xlink:href="#E108-MJMATHI-71" x="7484" y="0"></use><use xlink:href="#E108-MJMATHI-75" x="7944" y="0"></use><use xlink:href="#E108-MJMATHI-65" x="8516" y="0"></use><use xlink:href="#E108-MJMATHI-73" x="8982" y="0"></use><g transform="translate(9451,0)"><use xlink:href="#E108-MJMATHI-74" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E108-MJMAIN-30" x="510" y="-213"></use></g><use xlink:href="#E108-MJMAIN-3C" x="10544" y="0"></use><use xlink:href="#E108-MJMATHI-4D" x="11600" y="0"></use><use xlink:href="#E108-MJMATHI-61" x="12651" y="0"></use><g transform="translate(13180,0)"><use xlink:href="#E108-MJMATHI-78" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E108-MJMAIN-30" x="808" y="-213"></use></g></g></svg></span><script type="math/tex">Allocation_0 + Request_0 < Max_0</script></p></li><li><p><span>假设把资源分配给 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.545ex" height="2.28ex" viewBox="0 -746.7 1095.6 981.5" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E109-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E109-MJMAIN-30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E109-MJMATHI-50" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E109-MJMAIN-30" x="907" y="-213"></use></g></svg></span><script type="math/tex">P_0</script></p><ul><li><span>如果分配之后还是 safe(能找到安全序列),那就真的分配给它</span></li><li><span>如果分配之后 unsafe,拒绝请求</span></li></ul></li></ol></li></ul><hr /><h2 id='第八章-内存管理策略'><span>第八章 内存管理策略</span></h2><h3 id='81-背景'><span>8.1 背景</span></h3><p><span>一个内存,多个进程,怎么管理</span></p><h4 id='811-aspects-of-memory-multiplexing'><span>8.1.1 Aspects of Memory Multiplexing</span></h4><ul><li><p><span>Protection</span></p><p><span>Prevent access to private memory of other processes</span></p></li><li><p><span>Controlled Overlap</span></p><p><span>Sometimes we want to share memory across processes</span></p></li><li><p><span>Translation</span></p><p><span>Ability to translate accesses from one address space (virtual) to a different one (physical)</span></p></li></ul><h4 id='812-地址绑定-address-binding'><span>8.1.2 地址绑定 Address Binding</span></h4><p><span>源程序中的地址通常是用符号表示 (如变量 </span><code>count</code><span>)。编译器通常将这些符号地址绑定 (bind) 到可重定位的地址 (如“从本模块开始的第 14 字节”)。链接程序或加载程序再将这些可重定位的地址绑定到绝对地址 (如 74014)。每次绑定都是一个从一个地址空间到另一个地址空间的映射。</span></p><p><img src="D:\TyporaPictures\OS\80.png" referrerpolicy="no-referrer"></p><p><span>通常,指令和数据绑定到存储器地址可以在任何一步进行:</span></p><ul><li><p><span>编译时 Compile Time</span></p><p><span>如果在编译时就已经知道进程将在内存中的驻留地址,那么就可以生成绝对代码 (Absolute Code)</span></p><p><span>例: MS-DOS 的 .COM 格式程序</span></p></li><li><p><span>加载时 Load Time</span></p><p><span>如果在编译时并不知道进程将驻留在何处,那么编译器就应生成可重定位代码 (Relocatable Code)。对这种情况,最后绑定会延迟到加载时进行</span></p></li><li><p><span>执行时 Runtime time</span></p><p><span>如果进程在执行时可以从一个内存段移到另一个内存段,那么绑定应延迟到执行时才进行</span></p><p><span>大多数通用 OS 采用</span></p></li></ul><h4 id='813-逻辑地址空间与物理地址空间'><span>8.1.3 逻辑地址空间与物理地址空间</span></h4><ul><li><p><span>逻辑地址 Logical Address = 虚拟地址 Virtual Address</span></p><p><span>CPU 生成的地址</span></p></li><li><p><span>物理地址 Physical Address</span></p><p><span>真正的内存地址,加载到内存地址寄存器 (Memory-Address Register) 的地址</span></p></li><li><p><span>编译时和加载时的地址绑定会生成相同的逻辑地址和物理地址</span></p></li><li><p><span>执行时的绑定生成不同的逻辑地址和物理地址</span></p></li><li><p><span>内存管理单元 MMU</span></p><p><span>从虚拟地址到物理地址的运行时映射是由</span><strong><span>内存管理单元 (Memory Management Unit)</span></strong><span> 的硬件设备来完成 (包括查页表之类的都是 MMU 干的)</span></p><p><span>大多数 on-chip</span></p></li></ul><p><img src="D:\TyporaPictures\OS\85.png" referrerpolicy="no-referrer"></p><h4 id='814-动态加载-dynamic-loading'><span>8.1.4 动态加载 Dynamic Loading</span></h4><ul><li><span>一个进程的整个程序和数据如果都必须处于物理内存中,则进程的大小受物理内存大小的限制</span></li><li><span>为了获得更好的内存空间使用率,使用动态加载(Dynamic Loading),即一个程序只有在调用时才被加载</span></li></ul><h4 id='815-动态链接与共享库'><span>8.1.5 动态链接与共享库</span></h4><ul><li><p><span>动态链接的概念与动态加载相似。只是这里不是将加载延迟到运行时,而是将链接延迟到运行时。这一特点通常用于系统库,如语言子程序库。没有这一点,系统上的所有程序都需要一份语言库的副本,这一需求浪费了磁盘空间和内存空间。</span></p></li><li><p><span>存根 Stub</span></p><p><span>如果有动态链接,二进制镜像中每个库程序的应用都有一个存根(stub)。存根是一小段代码,用以指出如何定位适当的内存驻留的库程序,或如果该程序不在内存中应如何安装入库。不管怎样,存根会用子程序地址来代替自己,并开始执行子程序。因此,下次再执行该子程序代码时,就可以直接进行,而不会因动态链接产生任何开销。采用这种方案,使用语言库的所有进程只需要一个库代码副本就可以了。</span></p></li><li><p><span>举例来说,你在程序里调用了 STL 里的 Map,如果没有动态链接,就相当于你把 STL 里 Map 的源文件复制一份到了你的项目里。在动态链接下,不管多少程序调用,都只会调用那一份代码。</span></p></li><li><p><span>动态连接也可用于库更新。一个库可以被新的版本所替代,且使用该库的所有程序会自动使用新的版本。没有动态链接,所有这些程序必须重新链接以便访问。</span></p></li></ul><hr /><h3 id='82-交换-swap'><span>8.2 交换 Swap</span></h3><p><span>Refer to 进程调度 5.1.3 中期调度程序</span></p><p><span>进程需要在内存中以便执行。进程也可以暂时从内存中交换 (swap) 到备份存储 (backing store,一般是磁盘) 上,当需要再次执行时在调回到内存中。</span></p><ul><li><span>换入 Swap In</span></li><li><span>换出 Swap Out</span></li></ul><hr /><h3 id='83-连续内存分配-contiguous-memory-allocation'><span>8.3 连续内存分配 Contiguous Memory Allocation</span></h3><h4 id='831-uniprogramming'><span>8.3.1 Uniprogramming</span></h4><ul><li><span>同时只能有一个程序运行</span></li><li><span>Application always runs at same place in physical memory since only one application at a time</span></li><li><span>Application can access any physical address</span></li></ul><p><img src="D:\TyporaPictures\OS\81.png" referrerpolicy="no-referrer"></p><h4 id='832-内存保护-protection'><span>8.3.2 内存保护 Protection</span></h4><ul><li><span>重定位寄存器 Relocation Register</span></li><li><span>界限寄存器 Limit Register: 里面是虚拟地址的 bound</span></li></ul><p><img src="D:\TyporaPictures\OS\83.jpg" referrerpolicy="no-referrer"></p><ul><li><span>保护</span></li></ul><p><img src="D:\TyporaPictures\OS\82.png" referrerpolicy="no-referrer"></p><h4 id='833-多分区方法-multiple-partition-method'><span>8.3.3 多分区方法 Multiple-Partition Method</span></h4><ul><li><p><span>将内存分为多个分区,每个分区分给一个进程</span></p></li><li><p><span>固定分区 Fixed-size Partition</span></p><p><span>Each process has same memory size</span></p><p><img src="D:\TyporaPictures\OS\83.png" referrerpolicy="no-referrer"></p></li><li><p><span>可变分区 Variable Partition</span></p><p><img src="D:\TyporaPictures\OS\84.png" referrerpolicy="no-referrer"></p><ul><li><span>每一块可用的内存称为一个孔 (hole)</span></li><li><span>可用的内存块为分散在内存里不同大小的孔的集合</span></li></ul></li><li><p><span>动态存储分配问题 Dynamic Storage-Alloction Problem</span></p><ul><li><p><span>当新进程需要内存时,系统为该进程查找足够大的孔</span></p></li><li><p><span>如果孔太大,那么就分为两块</span></p><ul><li><span>分配给新进程</span></li><li><span>合并回孔集合</span></li></ul></li><li><p><span>进程终止时,释放内存,该内存合并回孔的集合</span></p></li><li><p><span>如果新孔与其他孔相邻,则合并成大孔</span></p></li><li><p><span>系统检查是否有等待内存空间的进程,以及新合并的孔能否满足等待进程等</span></p></li></ul></li><li><p><span>从可用孔中选择一个分配的常用方法</span></p><ul><li><p><span>首次适应 First-fit</span></p><p><span>分配首个足够大的孔</span></p></li><li><p><span>最优适应 Best-fit</span></p><p><span>分配最小的足够大的孔</span></p></li><li><p><span>最差适应 Worst-fit</span></p><p><span>分配最大的足够大的孔</span></p></li></ul></li></ul><h4 id='833-碎片-fragmentation'><span>8.3.3 碎片 Fragmentation</span></h4><p><img src="D:\TyporaPictures\OS\86.png" referrerpolicy="no-referrer"></p><ul><li><p><span>内碎片 Internal Fragmentation</span></p><p><span>分配给进程的内存比所需的大,多余的那一部分就是内碎片</span></p></li><li><p><span>外碎片 External Fragmentation</span></p><p><span>两个进程之间的空闲孔,而且这个孔太小,没法分配给别的进程</span></p></li><li><p><span>紧缩 Compaction</span></p><p><span>移动已分配的内存,使得所有外碎片合并成一大块</span></p><p><span>只有在运行时绑定才可以使用紧缩,因为要重写基地址寄存器和界限寄存器</span></p><ul><li><span>编译时:不可能,因为直接就绑定绝对地址</span></li><li><span>加载时:但是目前进程已经加载到内存里了,这时候也已经是不可变地址了</span></li></ul></li></ul><hr /><h3 id='84-分段-segmentation'><span>8.4 分段 Segmentation</span></h3><p><img src="D:\TyporaPictures\OS\87.png" referrerpolicy="no-referrer"></p><ul><li><p><span>逻辑地址空间由一组段构成,每个段有名称和长度</span></p></li><li><p><span>地址指定了段名称和段内偏移 (Offset)</span></p></li><li><p><span>逻辑地址由有序对组成 <段号,偏移></span></p></li><li><p><span>段表 Segment Table</span></p><p><span>在 CPU 里</span></p><p><span>每个进程都有一个</span></p><p><span>段表的每个条目包含:</span></p><ul><li><span>段基地址 Segment Base</span></li><li><span>段界限 Segment Limit</span></li><li><span>Valid bit</span></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\88.png" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\89.png" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\92.png" referrerpolicy="no-referrer"></p><ul><li><p><span>图上没有 Check Valid 的内容</span></p></li><li><p><span>例 1:</span></p><p><img src="D:\TyporaPictures\OS\90.png" referrerpolicy="no-referrer"></p></li><li><p><span>例 2:</span></p><p><img src="D:\TyporaPictures\OS\91.png" referrerpolicy="no-referrer"></p><p><span>不多说,全是计组学过的内容</span></p><p><span>注意第一条指令 load address 取的是虚拟地址 0x4050, 物理地址只有真正访问内存的时候才翻译过去</span></p></li><li><p><span>分段的特点</span></p><ol start='' ><li><p><span>Virtual address space has holes</span></p><ul><li><span>Segmentation is efficient for sparse address spaces</span></li><li><span>A correct program should never address gaps</span></li></ul></li><li><p><span>When it is OK to address outside valid range?</span></p><ul><li><span>This is how the stack and heap are allowed to grow</span></li><li><span>For instance, stack takes fault, system automatically increases size of stack</span></li></ul></li><li><p><span>Need protection mode in segment table</span></p><ul><li><span>For example, code segment would be read only</span></li><li><span>Data and stack would be read write (stores allowed)</span></li><li><span>Shared segment could be read only or read write</span></li></ul></li><li><p><span>Fragmentation</span></p></li></ol></li><li><p><span>What must be saved/restored on context switch?</span></p><ul><li><span>Segment table that stored in CPU</span></li><li><span>Might store all of processes memory onto disk when switched (8.2 swap)</span></li></ul></li></ul><hr /><h3 id='85-分页-paging'><span>8.5 分页 Paging</span></h3><ul><li><span>将物理内存分为固定大小的块,称为帧或页帧 (frame)</span></li><li><span>将逻辑内存分为同样大小的块,称为页或页面 (page)</span></li><li><span>逻辑地址空间完全独立于物理地址空间,例如一个进程有 64 位逻辑地址空间,而系统的物理内存可以小于 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="3.036ex" height="2.28ex" viewBox="0 -932.9 1307.1 981.5" role="img" focusable="false" style="vertical-align: -0.113ex;"><defs><path stroke-width="0" id="E110-MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path stroke-width="0" id="E110-MJMAIN-36" d="M42 313Q42 476 123 571T303 666Q372 666 402 630T432 550Q432 525 418 510T379 495Q356 495 341 509T326 548Q326 592 373 601Q351 623 311 626Q240 626 194 566Q147 500 147 364L148 360Q153 366 156 373Q197 433 263 433H267Q313 433 348 414Q372 400 396 374T435 317Q456 268 456 210V192Q456 169 451 149Q440 90 387 34T253 -22Q225 -22 199 -14T143 16T92 75T56 172T42 313ZM257 397Q227 397 205 380T171 335T154 278T148 216Q148 133 160 97T198 39Q222 21 251 21Q302 21 329 59Q342 77 347 104T352 209Q352 289 347 316T329 361Q302 397 257 397Z"></path><path stroke-width="0" id="E110-MJMAIN-34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E110-MJMAIN-32" x="0" y="0"></use><g transform="translate(500,392)"><use transform="scale(0.707)" xlink:href="#E110-MJMAIN-36"></use><use transform="scale(0.707)" xlink:href="#E110-MJMAIN-34" x="500" y="0"></use></g></g></svg></span><script type="math/tex">2^{64}</script><span> 字节</span></li></ul><h4 id='851-页表-page-table'><span>8.5.1 页表 Page Table</span></h4><ul><li><p><span>每个逻辑地址分为两部分</span></p><ul><li><span>页码 Page Number: 页表的索引</span></li><li><span>页偏移 Page Offset</span></li></ul></li><li><p><span>与分段对比</span></p><ul><li><span>分页是先决定一页多大才知道分几页,比如一页 4 KiB, 那么 offset 是 12 位,所以 page number 是 32-12=20 位</span></li><li><span>分段是先决定分几个段才知道一个段多大,比如分 4 个,那么 segment number 是 2 位,所以 offset 是 32-2=30 位</span></li></ul></li><li><p><span>页表条目</span></p><ul><li><span>物理内存基地址</span></li><li><span>Valid bit, read, write ...</span></li></ul></li><li><p><span>在内存里</span></p></li><li><p><span>每个进程一个</span></p></li></ul><p><img src="D:\TyporaPictures\OS\93.png" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\96.png" referrerpolicy="no-referrer"></p><ul><li><span>下图来自计组课件(逻辑地址空间和物理地址空间不必非得一样大)</span></li></ul><p><img src="D:\TyporaPictures\OS\94.png" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\95.png" referrerpolicy="no-referrer"></p><ul><li><p><span>分页可以消除外碎片</span></p></li><li><p><span>一页的大小需要 trade off</span></p><ul><li><span>太大,内碎片</span></li><li><span>太小,页表条目太多,导致页表占用空间太大,页表也是在内存里的</span></li></ul></li><li><p><span>What needs to be switched on a context switch?</span>
<span>Page table pointer and limit</span></p></li><li><p><span>Core Map</span></p><p><span>Do we need a reverse mapping (i.e. physical page -> virtual page)?</span></p><ul><li><span>Yes. Clock algorithm runs through page frames. If sharing, then multiple virtual pages per physical page</span></li><li><span>Can't push page out to disk without invalidating all PTEs</span></li></ul></li></ul><h4 id='852-共享页'><span>8.5.2 共享页</span></h4><p><img src="D:\TyporaPictures\OS\97.png" referrerpolicy="no-referrer"></p><h4 id='853-分层分页-multilevel-paging'><span>8.5.3 分层分页 Multilevel Paging</span></h4><ul><li><span>向前映射页表 Forward-Mapped Page Table</span></li></ul><p><img src="D:\TyporaPictures\OS\98.png" referrerpolicy="no-referrer"></p><ul><li><span>Page Table Base Register (PTBR): 存的是页表的基地址</span></li></ul><ol start='' ><li><span>Reference to PTE in level 1 page table = PTBR value + Level 1 offset present in virtual address.</span></li><li><span>Reference to PTE in level 2 page table = Base address (present in Level 1 PTE) + Level 2 offset (present in VA). </span></li><li><span>Reference to PTE in level 3 page table= Base address (present in Level 2 PTE) + Level 3 offset (present in VA). </span></li><li><span>Actual page frame address = PTE (present in level 3).</span></li></ol><ul><li><p><span>注意这里,比如 level 1 offset 10 位,那么二级页表最多可能有 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="10.778ex" height="2.388ex" viewBox="0 -932.9 4640.7 1028" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E111-MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path stroke-width="0" id="E111-MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path stroke-width="0" id="E111-MJMAIN-30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path><path stroke-width="0" id="E111-MJMAIN-3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path><path stroke-width="0" id="E111-MJMAIN-34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E111-MJMAIN-32" x="0" y="0"></use><g transform="translate(500,392)"><use transform="scale(0.707)" xlink:href="#E111-MJMAIN-31"></use><use transform="scale(0.707)" xlink:href="#E111-MJMAIN-30" x="500" y="0"></use></g><use xlink:href="#E111-MJMAIN-3D" x="1584" y="0"></use><g transform="translate(2640,0)"><use xlink:href="#E111-MJMAIN-31"></use><use xlink:href="#E111-MJMAIN-30" x="500" y="0"></use><use xlink:href="#E111-MJMAIN-32" x="1000" y="0"></use><use xlink:href="#E111-MJMAIN-34" x="1500" y="0"></use></g></g></svg></span><script type="math/tex">2^{10}=1024</script><span> 个</span></p><p><span>当然大部分情况都是 < 1024 的,这就是多级页表的作用,解决了之前单个页表里一大堆 null 没用还占地方的问题</span></p></li></ul><p><img src="D:\TyporaPictures\OS\99.png" referrerpolicy="no-referrer"></p><h4 id='854-分段分页'><span>8.5.4 分段+分页</span></h4><ul><li><p><span>Tree of tables</span></p><ul><li><span>Lowest level page table: memory still allocated with bitmap</span></li><li><span>Higher levels often segmented</span></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\100.png" referrerpolicy="no-referrer"></p><ul><li><p><span>What must be saved/restored on context switch?</span></p><ul><li><span>Contents of top level segment registers (for this example)</span></li><li><span>Pointer to top level table (page table)</span></li></ul></li><li><p><span>共享</span></p></li></ul><p><img src="D:\TyporaPictures\OS\101.png" referrerpolicy="no-referrer"></p><hr /><h2 id='第九章-虚拟内存管理'><span>第九章 虚拟内存管理</span></h2><h3 id='91-缓存-cache'><span>9.1 缓存 Cache</span></h3><ul><li><p><span>Cache</span></p><p><span>A repository for copies that can be accessed more quickly than the original</span></p></li><li><p><span>平均访问时间 Average Access time</span></p><div contenteditable="false" spellcheck="false" class="mathjax-block md-end-block md-math-block md-rawblock" id="mathjax-n1579" cid="n1579" mdtype="math_block"><div class="md-rawblock-container md-math-container" tabindex="-1"><div class="MathJax_SVG_Display" style="text-align: center;"><span class="MathJax_SVG" id="MathJax-Element-1-Frame" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="50.027ex" height="2.067ex" viewBox="0 -748.1 21539.3 890" role="img" focusable="false" style="vertical-align: -0.33ex; max-width: 100%;"><defs><path stroke-width="0" id="E64-MJMATHI-48" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 219 683Q260 681 355 681Q389 681 418 681T463 682T483 682Q499 682 499 672Q499 670 497 658Q492 641 487 638H485Q483 638 480 638T473 638T464 637T455 637Q416 636 405 634T387 623Q384 619 355 500Q348 474 340 442T328 395L324 380Q324 378 469 378H614L615 381Q615 384 646 504Q674 619 674 627T617 637Q594 637 587 639T580 648Q580 650 582 660Q586 677 588 679T604 682Q609 682 646 681T740 680Q802 680 835 681T871 682Q888 682 888 672Q888 645 876 638H874Q872 638 869 638T862 638T853 637T844 637Q805 636 794 634T776 623Q773 618 704 340T634 58Q634 51 638 51Q646 48 692 46H723Q729 38 729 37T726 19Q722 6 716 0H701Q664 2 567 2Q533 2 504 2T458 2T437 1Q420 1 420 10Q420 15 423 24Q428 43 433 45Q437 46 448 46H454Q481 46 514 49Q520 50 522 50T528 55T534 64T540 82T547 110T558 153Q565 181 569 198Q602 330 602 331T457 332H312L279 197Q245 63 245 58Q245 51 253 49T303 46H334Q340 38 340 37T337 19Q333 6 327 0H312Q275 2 178 2Q144 2 115 2T69 2T48 1Q31 1 31 10Q31 12 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path><path stroke-width="0" id="E64-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E64-MJMATHI-74" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path><path stroke-width="0" id="E64-MJMATHI-52" d="M230 637Q203 637 198 638T193 649Q193 676 204 682Q206 683 378 683Q550 682 564 680Q620 672 658 652T712 606T733 563T739 529Q739 484 710 445T643 385T576 351T538 338L545 333Q612 295 612 223Q612 212 607 162T602 80V71Q602 53 603 43T614 25T640 16Q668 16 686 38T712 85Q717 99 720 102T735 105Q755 105 755 93Q755 75 731 36Q693 -21 641 -21H632Q571 -21 531 4T487 82Q487 109 502 166T517 239Q517 290 474 313Q459 320 449 321T378 323H309L277 193Q244 61 244 59Q244 55 245 54T252 50T269 48T302 46H333Q339 38 339 37T336 19Q332 6 326 0H311Q275 2 180 2Q146 2 117 2T71 2T50 1Q33 1 33 10Q33 12 36 24Q41 43 46 45Q50 46 61 46H67Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628Q287 635 230 637ZM630 554Q630 586 609 608T523 636Q521 636 500 636T462 637H440Q393 637 386 627Q385 624 352 494T319 361Q319 360 388 360Q466 361 492 367Q556 377 592 426Q608 449 619 486T630 554Z"></path><path stroke-width="0" id="E64-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path><path stroke-width="0" id="E64-MJMATHI-65" d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z"></path><path stroke-width="0" id="E64-MJMAIN-D7" d="M630 29Q630 9 609 9Q604 9 587 25T493 118L389 222L284 117Q178 13 175 11Q171 9 168 9Q160 9 154 15T147 29Q147 36 161 51T255 146L359 250L255 354Q174 435 161 449T147 471Q147 480 153 485T168 490Q173 490 175 489Q178 487 284 383L389 278L493 382Q570 459 587 475T609 491Q630 491 630 471Q630 464 620 453T522 355L418 250L522 145Q606 61 618 48T630 29Z"></path><path stroke-width="0" id="E64-MJMATHI-54" d="M40 437Q21 437 21 445Q21 450 37 501T71 602L88 651Q93 669 101 677H569H659Q691 677 697 676T704 667Q704 661 687 553T668 444Q668 437 649 437Q640 437 637 437T631 442L629 445Q629 451 635 490T641 551Q641 586 628 604T573 629Q568 630 515 631Q469 631 457 630T439 622Q438 621 368 343T298 60Q298 48 386 46Q418 46 427 45T436 36Q436 31 433 22Q429 4 424 1L422 0Q419 0 415 0Q410 0 363 1T228 2Q99 2 64 0H49Q43 6 43 9T45 27Q49 40 55 46H83H94Q174 46 189 55Q190 56 191 56Q196 59 201 76T241 233Q258 301 269 344Q339 619 339 625Q339 630 310 630H279Q212 630 191 624Q146 614 121 583T67 467Q60 445 57 441T43 437H40Z"></path><path stroke-width="0" id="E64-MJMATHI-6D" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E64-MJMAIN-2B" d="M56 237T56 250T70 270H369V420L370 570Q380 583 389 583Q402 583 409 568V270H707Q722 262 722 250T707 230H409V-68Q401 -82 391 -82H389H387Q375 -82 369 -68V230H70Q56 237 56 250Z"></path><path stroke-width="0" id="E64-MJMATHI-4D" d="M289 629Q289 635 232 637Q208 637 201 638T194 648Q194 649 196 659Q197 662 198 666T199 671T201 676T203 679T207 681T212 683T220 683T232 684Q238 684 262 684T307 683Q386 683 398 683T414 678Q415 674 451 396L487 117L510 154Q534 190 574 254T662 394Q837 673 839 675Q840 676 842 678T846 681L852 683H948Q965 683 988 683T1017 684Q1051 684 1051 673Q1051 668 1048 656T1045 643Q1041 637 1008 637Q968 636 957 634T939 623Q936 618 867 340T797 59Q797 55 798 54T805 50T822 48T855 46H886Q892 37 892 35Q892 19 885 5Q880 0 869 0Q864 0 828 1T736 2Q675 2 644 2T609 1Q592 1 592 11Q592 13 594 25Q598 41 602 43T625 46Q652 46 685 49Q699 52 704 61Q706 65 742 207T813 490T848 631L654 322Q458 10 453 5Q451 4 449 3Q444 0 433 0Q418 0 415 7Q413 11 374 317L335 624L267 354Q200 88 200 79Q206 46 272 46H282Q288 41 289 37T286 19Q282 3 278 1Q274 0 267 0Q265 0 255 0T221 1T157 2Q127 2 95 1T58 0Q43 0 39 2T35 11Q35 13 38 25T43 40Q45 46 65 46Q135 46 154 86Q158 92 223 354T289 629Z"></path><path stroke-width="0" id="E64-MJMATHI-73" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E64-MJMATHI-48" x="0" y="0"></use><use xlink:href="#E64-MJMATHI-69" x="888" y="0"></use><use xlink:href="#E64-MJMATHI-74" x="1233" y="0"></use><use xlink:href="#E64-MJMATHI-52" x="1844" y="0"></use><use xlink:href="#E64-MJMATHI-61" x="2603" y="0"></use><use xlink:href="#E64-MJMATHI-74" x="3132" y="0"></use><use xlink:href="#E64-MJMATHI-65" x="3493" y="0"></use><use xlink:href="#E64-MJMAIN-D7" x="4181" y="0"></use><use xlink:href="#E64-MJMATHI-48" x="5181" y="0"></use><use xlink:href="#E64-MJMATHI-69" x="6069" y="0"></use><use xlink:href="#E64-MJMATHI-74" x="6414" y="0"></use><use xlink:href="#E64-MJMATHI-54" x="7025" y="0"></use><use xlink:href="#E64-MJMATHI-69" x="7729" y="0"></use><use xlink:href="#E64-MJMATHI-6D" x="8074" y="0"></use><use xlink:href="#E64-MJMATHI-65" x="8952" y="0"></use><use xlink:href="#E64-MJMAIN-2B" x="9640" y="0"></use><use xlink:href="#E64-MJMATHI-4D" x="10640" y="0"></use><use xlink:href="#E64-MJMATHI-69" x="11691" y="0"></use><use xlink:href="#E64-MJMATHI-73" x="12036" y="0"></use><use xlink:href="#E64-MJMATHI-73" x="12505" y="0"></use><use xlink:href="#E64-MJMATHI-52" x="13224" y="0"></use><use xlink:href="#E64-MJMATHI-61" x="13983" y="0"></use><use xlink:href="#E64-MJMATHI-74" x="14512" y="0"></use><use xlink:href="#E64-MJMATHI-65" x="14873" y="0"></use><use xlink:href="#E64-MJMAIN-D7" x="15562" y="0"></use><use xlink:href="#E64-MJMATHI-4D" x="16562" y="0"></use><use xlink:href="#E64-MJMATHI-69" x="17613" y="0"></use><use xlink:href="#E64-MJMATHI-73" x="17958" y="0"></use><use xlink:href="#E64-MJMATHI-73" x="18427" y="0"></use><use xlink:href="#E64-MJMATHI-54" x="19146" y="0"></use><use xlink:href="#E64-MJMATHI-69" x="19850" y="0"></use><use xlink:href="#E64-MJMATHI-6D" x="20195" y="0"></use><use xlink:href="#E64-MJMATHI-65" x="21073" y="0"></use></g></svg></span></div><script type="math/tex; mode=display" id="MathJax-Element-1">Hit\ Rate \times Hit\ Time + Miss\ Rate \times Miss\ Time</script></div></div><p><span>注意这里跟计组学的不一样,访问 cache 然后没找到的时间没算进去</span></p><p><span>计组: </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="37.993ex" height="2.063ex" viewBox="0 -746.7 16357.9 888.4" role="img" focusable="false" style="vertical-align: -0.329ex;"><defs><path stroke-width="0" id="E112-MJMATHI-48" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 219 683Q260 681 355 681Q389 681 418 681T463 682T483 682Q499 682 499 672Q499 670 497 658Q492 641 487 638H485Q483 638 480 638T473 638T464 637T455 637Q416 636 405 634T387 623Q384 619 355 500Q348 474 340 442T328 395L324 380Q324 378 469 378H614L615 381Q615 384 646 504Q674 619 674 627T617 637Q594 637 587 639T580 648Q580 650 582 660Q586 677 588 679T604 682Q609 682 646 681T740 680Q802 680 835 681T871 682Q888 682 888 672Q888 645 876 638H874Q872 638 869 638T862 638T853 637T844 637Q805 636 794 634T776 623Q773 618 704 340T634 58Q634 51 638 51Q646 48 692 46H723Q729 38 729 37T726 19Q722 6 716 0H701Q664 2 567 2Q533 2 504 2T458 2T437 1Q420 1 420 10Q420 15 423 24Q428 43 433 45Q437 46 448 46H454Q481 46 514 49Q520 50 522 50T528 55T534 64T540 82T547 110T558 153Q565 181 569 198Q602 330 602 331T457 332H312L279 197Q245 63 245 58Q245 51 253 49T303 46H334Q340 38 340 37T337 19Q333 6 327 0H312Q275 2 178 2Q144 2 115 2T69 2T48 1Q31 1 31 10Q31 12 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path><path stroke-width="0" id="E112-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E112-MJMATHI-74" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path><path stroke-width="0" id="E112-MJMATHI-54" d="M40 437Q21 437 21 445Q21 450 37 501T71 602L88 651Q93 669 101 677H569H659Q691 677 697 676T704 667Q704 661 687 553T668 444Q668 437 649 437Q640 437 637 437T631 442L629 445Q629 451 635 490T641 551Q641 586 628 604T573 629Q568 630 515 631Q469 631 457 630T439 622Q438 621 368 343T298 60Q298 48 386 46Q418 46 427 45T436 36Q436 31 433 22Q429 4 424 1L422 0Q419 0 415 0Q410 0 363 1T228 2Q99 2 64 0H49Q43 6 43 9T45 27Q49 40 55 46H83H94Q174 46 189 55Q190 56 191 56Q196 59 201 76T241 233Q258 301 269 344Q339 619 339 625Q339 630 310 630H279Q212 630 191 624Q146 614 121 583T67 467Q60 445 57 441T43 437H40Z"></path><path stroke-width="0" id="E112-MJMATHI-6D" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E112-MJMATHI-65" d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z"></path><path stroke-width="0" id="E112-MJMAIN-2B" d="M56 237T56 250T70 270H369V420L370 570Q380 583 389 583Q402 583 409 568V270H707Q722 262 722 250T707 230H409V-68Q401 -82 391 -82H389H387Q375 -82 369 -68V230H70Q56 237 56 250Z"></path><path stroke-width="0" id="E112-MJMATHI-4D" d="M289 629Q289 635 232 637Q208 637 201 638T194 648Q194 649 196 659Q197 662 198 666T199 671T201 676T203 679T207 681T212 683T220 683T232 684Q238 684 262 684T307 683Q386 683 398 683T414 678Q415 674 451 396L487 117L510 154Q534 190 574 254T662 394Q837 673 839 675Q840 676 842 678T846 681L852 683H948Q965 683 988 683T1017 684Q1051 684 1051 673Q1051 668 1048 656T1045 643Q1041 637 1008 637Q968 636 957 634T939 623Q936 618 867 340T797 59Q797 55 798 54T805 50T822 48T855 46H886Q892 37 892 35Q892 19 885 5Q880 0 869 0Q864 0 828 1T736 2Q675 2 644 2T609 1Q592 1 592 11Q592 13 594 25Q598 41 602 43T625 46Q652 46 685 49Q699 52 704 61Q706 65 742 207T813 490T848 631L654 322Q458 10 453 5Q451 4 449 3Q444 0 433 0Q418 0 415 7Q413 11 374 317L335 624L267 354Q200 88 200 79Q206 46 272 46H282Q288 41 289 37T286 19Q282 3 278 1Q274 0 267 0Q265 0 255 0T221 1T157 2Q127 2 95 1T58 0Q43 0 39 2T35 11Q35 13 38 25T43 40Q45 46 65 46Q135 46 154 86Q158 92 223 354T289 629Z"></path><path stroke-width="0" id="E112-MJMATHI-73" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path><path stroke-width="0" id="E112-MJMATHI-52" d="M230 637Q203 637 198 638T193 649Q193 676 204 682Q206 683 378 683Q550 682 564 680Q620 672 658 652T712 606T733 563T739 529Q739 484 710 445T643 385T576 351T538 338L545 333Q612 295 612 223Q612 212 607 162T602 80V71Q602 53 603 43T614 25T640 16Q668 16 686 38T712 85Q717 99 720 102T735 105Q755 105 755 93Q755 75 731 36Q693 -21 641 -21H632Q571 -21 531 4T487 82Q487 109 502 166T517 239Q517 290 474 313Q459 320 449 321T378 323H309L277 193Q244 61 244 59Q244 55 245 54T252 50T269 48T302 46H333Q339 38 339 37T336 19Q332 6 326 0H311Q275 2 180 2Q146 2 117 2T71 2T50 1Q33 1 33 10Q33 12 36 24Q41 43 46 45Q50 46 61 46H67Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628Q287 635 230 637ZM630 554Q630 586 609 608T523 636Q521 636 500 636T462 637H440Q393 637 386 627Q385 624 352 494T319 361Q319 360 388 360Q466 361 492 367Q556 377 592 426Q608 449 619 486T630 554Z"></path><path stroke-width="0" id="E112-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path><path stroke-width="0" id="E112-MJMAIN-D7" d="M630 29Q630 9 609 9Q604 9 587 25T493 118L389 222L284 117Q178 13 175 11Q171 9 168 9Q160 9 154 15T147 29Q147 36 161 51T255 146L359 250L255 354Q174 435 161 449T147 471Q147 480 153 485T168 490Q173 490 175 489Q178 487 284 383L389 278L493 382Q570 459 587 475T609 491Q630 491 630 471Q630 464 620 453T522 355L418 250L522 145Q606 61 618 48T630 29Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E112-MJMATHI-48" x="0" y="0"></use><use xlink:href="#E112-MJMATHI-69" x="888" y="0"></use><use xlink:href="#E112-MJMATHI-74" x="1233" y="0"></use><use xlink:href="#E112-MJMATHI-54" x="1844" y="0"></use><use xlink:href="#E112-MJMATHI-69" x="2548" y="0"></use><use xlink:href="#E112-MJMATHI-6D" x="2893" y="0"></use><use xlink:href="#E112-MJMATHI-65" x="3771" y="0"></use><use xlink:href="#E112-MJMAIN-2B" x="4459" y="0"></use><use xlink:href="#E112-MJMATHI-4D" x="5459" y="0"></use><use xlink:href="#E112-MJMATHI-69" x="6510" y="0"></use><use xlink:href="#E112-MJMATHI-73" x="6855" y="0"></use><use xlink:href="#E112-MJMATHI-73" x="7324" y="0"></use><use xlink:href="#E112-MJMATHI-52" x="8043" y="0"></use><use xlink:href="#E112-MJMATHI-61" x="8802" y="0"></use><use xlink:href="#E112-MJMATHI-74" x="9331" y="0"></use><use xlink:href="#E112-MJMATHI-65" x="9692" y="0"></use><use xlink:href="#E112-MJMAIN-D7" x="10380" y="0"></use><use xlink:href="#E112-MJMATHI-4D" x="11380" y="0"></use><use xlink:href="#E112-MJMATHI-69" x="12431" y="0"></use><use xlink:href="#E112-MJMATHI-73" x="12776" y="0"></use><use xlink:href="#E112-MJMATHI-73" x="13245" y="0"></use><use xlink:href="#E112-MJMATHI-54" x="13964" y="0"></use><use xlink:href="#E112-MJMATHI-69" x="14668" y="0"></use><use xlink:href="#E112-MJMATHI-6D" x="15013" y="0"></use><use xlink:href="#E112-MJMATHI-65" x="15891" y="0"></use></g></svg></span><script type="math/tex">Hit\ Time+Miss\ Rate \times Miss\ Time</script></p></li><li><p><span>时间局部性 Temporal Locality</span></p><p><span>If you used some data recently, you will likely use it again</span></p><p><span>Keep recently accessed data items closer to processor</span></p></li><li><p><span>空间局部性 Spatial Locality</span></p><p><span>If you used some data recently, you will likely access its neighbors</span></p><p><span>Move contiguous blocks to the upper levels</span></p></li><li><p><span>其他内容详见计组课件</span></p></li><li><p><span>Cache 的应用</span></p><ul><li><span>TLB</span></li><li><span>虚拟内存</span></li><li><span>文件系统</span></li><li><span>DNS</span></li><li><span>Web Proxy</span></li></ul></li></ul><hr /><h3 id='92-转换表缓冲区-transition-look-aside-buffer-tlb'><span>9.2 转换表缓冲区 Transition Look-aside Buffer (TLB)</span></h3><p><img src="D:\TyporaPictures\OS\102.png" referrerpolicy="no-referrer"></p><ul><li><p><span>Page Table Entry (PTE) 的 cache</span></p></li><li><p><span>在 CPU 里</span></p></li><li><p><span>TLB Miss 的处理 (以下来自计组课件 永远滴神)</span></p><ul><li><p><span>If page is in memory</span></p><ul><li><p><span>Load the PTE from memory and retry</span></p></li><li><p><span>Could be handled in hardware</span></p><ul><li><span>Can get complex for more complicated page table structures</span></li></ul></li><li><p><span>Or in software</span></p><ul><li><span>Raise a special exception, with optimized handler</span></li></ul></li></ul></li><li><p><span>If page is not in memory (page fault)</span></p><ul><li><span>OS handles fetching the page and updating the page table</span></li><li><span>Restart the faulting instruction</span></li></ul></li></ul></li><li><p><span>TLB 也可以多级 (L1, L2, ...)</span></p></li></ul><p><img src="D:\TyporaPictures\OS\103.png" referrerpolicy="no-referrer"></p><ul><li><p><span>TLB + Cache</span></p><ul><li><span>注意:cache(就是 CPU 里的 L1, L2, ... 那些)用的是物理地址</span></li></ul><p><img src="D:\TyporaPictures\OS\104.jpg" referrerpolicy="no-referrer"></p></li><li><p><span>Does software loaded TLB need use bit?</span></p><ul><li><span>Hardware sets use bit in TLB; when TLB entry is replaced, software copies use bit back to page table</span></li><li><span>Software manages TLB entries as FIFO list; everything not in TLB is Second Chance list, managed as strict LRU</span></li></ul></li></ul><hr /><h3 id='93-请求调页-demand-paging'><span>9.3 请求调页 Demand Paging</span></h3><h4 id='931-基本概念'><span>9.3.1 基本概念</span></h4><ul><li><p><span>定义</span></p><p><span>Keep all pages of the frames in the secondary memory (外存) until they are required.</span></p><p><span>A page is delivered into the memory on demand i.e., only when a reference is made to a location on that page.</span></p></li><li><p><span>为什么要请求调页</span></p><ul><li><span>一个程序运行需要的内存比实际内存大,但是这个程序不是同时需要申请这么多内存</span></li><li><span>程序所使用的虚拟地址也可能超出物理地址 (比如 64 位系统的虚拟地址空间相当大)</span></li></ul></li><li><p><span>内存相当于外存的 cache</span></p><ul><li><span>Block size: 1 page</span></li><li><span>Organization: fully associative</span></li><li><span>How to find a page: first TLB, then page table traversal</span></li><li><span>How to handle write: write-back, need dirty bit</span></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\104.png" referrerpolicy="no-referrer"></p><h4 id='932-请求调页的性能'><span>9.3.2 请求调页的性能</span></h4><ul><li><p><span>缺页错误 Page Fault</span></p><p><span>对标记为无效 (valid bit) 的页面访问</span></p></li><li><p><span>缺页错误的处理</span></p><ul><li><p><span>概括</span></p><ul><li><span>处理缺页错误中断</span></li><li><span>读入页面</span></li><li><span>重启进程</span></li></ul></li><li><p><span>具体</span></p><ol start='' ><li><p><span>陷入操作系统 (trap)</span></p></li><li><p><span>保存寄存器和进程状态</span></p></li><li><p><span>确定中断是否为缺页错误</span></p></li><li><p><span>检查页面是否合法,并确定页面的磁盘位置</span></p></li><li><p><span>从磁盘读入页面到空闲帧</span></p><ul><li><span>在该磁盘队列中等待 (IO 队列),直到读请求被处理</span></li><li><span>等待磁盘的寻道或延迟时间</span></li><li><span>开始传输磁盘页面到空闲帧</span></li></ul></li><li><p><span>在等待时,将 CPU 分配给其他用户</span></p></li><li><p><span>收到来自 IO 子系统的中断 (IO 完成)</span></p></li><li><p><span>保存其他用户的寄存器和进程状态</span></p></li><li><p><span>确认中断是来自上述磁盘的</span></p></li><li><p><span>修正页表和其他表,以表示所需页面现在已在内存中</span></p></li><li><p><span>等待 CPU 再次分配给本进程</span></p></li><li><p><span>恢复用户寄存器、进程状态和新页表,再重新执行中断的指令</span></p></li></ol></li><li><p><span>课件</span></p><ul><li><span>Choose an old page to replace</span></li><li><span>If old page modified (</span><code>D=1</code><span>), write contents back to disk</span></li><li><span>Change its PTE and any cached TLB to be invalid</span></li><li><span>Load new page into memory from disk</span></li><li><span>Update page table entry, invalidate TLB for new entry</span></li><li><span>Continue thread from original faulting location</span></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\187.png" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\188.png" referrerpolicy="no-referrer"></p></li><li><p><span>OS 如何拿到一个空闲帧</span></p><ul><li><p><span>Keeps a free list</span></p></li><li><p><span>Unix runs a "reaper" if memory gets too full</span></p><ul><li><span>Schedule dirty pages to be written back on disk</span></li><li><span>Zero (clean) pages which have not been accessed in a while</span></li></ul></li><li><p><span>As a last resort, evict a dirty page first</span></p></li></ul></li><li><p><span>有效访问时间 Effective Access Time</span></p><p><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.259ex" height="1.847ex" viewBox="-39 -514 542 795.3" role="img" focusable="false" style="vertical-align: -0.653ex; margin-left: -0.091ex;"><defs><path stroke-width="0" id="E113-MJMATHI-70" d="M23 287Q24 290 25 295T30 317T40 348T55 381T75 411T101 433T134 442Q209 442 230 378L240 387Q302 442 358 442Q423 442 460 395T497 281Q497 173 421 82T249 -10Q227 -10 210 -4Q199 1 187 11T168 28L161 36Q160 35 139 -51T118 -138Q118 -144 126 -145T163 -148H188Q194 -155 194 -157T191 -175Q188 -187 185 -190T172 -194Q170 -194 161 -194T127 -193T65 -192Q-5 -192 -24 -194H-32Q-39 -187 -39 -183Q-37 -156 -26 -148H-6Q28 -147 33 -136Q36 -130 94 103T155 350Q156 355 156 364Q156 405 131 405Q109 405 94 377T71 316T59 280Q57 278 43 278H29Q23 284 23 287ZM178 102Q200 26 252 26Q282 26 310 49T356 107Q374 141 392 215T411 325V331Q411 405 350 405Q339 405 328 402T306 393T286 380T269 365T254 350T243 336T235 326L232 322Q232 321 229 308T218 264T204 212Q178 106 178 102Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E113-MJMATHI-70" x="0" y="0"></use></g></svg></span><script type="math/tex">p</script><span>: 缺页错误率 Page Fault Rate</span></p><p><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="3.268ex" height="1.415ex" viewBox="0 -514 1407 609.1" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E114-MJMATHI-6D" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E114-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E114-MJMATHI-6D" x="0" y="0"></use><use xlink:href="#E114-MJMATHI-61" x="878" y="0"></use></g></svg></span><script type="math/tex">ma</script><span>: 内存访问时间</span></p><div contenteditable="false" spellcheck="false" class="mathjax-block md-end-block md-math-block md-rawblock" id="mathjax-n1768" cid="n1768" mdtype="math_block"><div class="md-rawblock-container md-math-container" tabindex="-1"><div class="MathJax_SVG_Display" style="text-align: center;"><span class="MathJax_SVG" id="MathJax-Element-2-Frame" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="36.08ex" height="2.5ex" viewBox="0 -794.7 15534.4 1076.5" role="img" focusable="false" style="vertical-align: -0.654ex; max-width: 100%;"><defs><path stroke-width="0" id="E65-MJMATHI-45" d="M492 213Q472 213 472 226Q472 230 477 250T482 285Q482 316 461 323T364 330H312Q311 328 277 192T243 52Q243 48 254 48T334 46Q428 46 458 48T518 61Q567 77 599 117T670 248Q680 270 683 272Q690 274 698 274Q718 274 718 261Q613 7 608 2Q605 0 322 0H133Q31 0 31 11Q31 13 34 25Q38 41 42 43T65 46Q92 46 125 49Q139 52 144 61Q146 66 215 342T285 622Q285 629 281 629Q273 632 228 634H197Q191 640 191 642T193 659Q197 676 203 680H757Q764 676 764 669Q764 664 751 557T737 447Q735 440 717 440H705Q698 445 698 453L701 476Q704 500 704 528Q704 558 697 578T678 609T643 625T596 632T532 634H485Q397 633 392 631Q388 629 386 622Q385 619 355 499T324 377Q347 376 372 376H398Q464 376 489 391T534 472Q538 488 540 490T557 493Q562 493 565 493T570 492T572 491T574 487T577 483L544 351Q511 218 508 216Q505 213 492 213Z"></path><path stroke-width="0" id="E65-MJMATHI-41" d="M208 74Q208 50 254 46Q272 46 272 35Q272 34 270 22Q267 8 264 4T251 0Q249 0 239 0T205 1T141 2Q70 2 50 0H42Q35 7 35 11Q37 38 48 46H62Q132 49 164 96Q170 102 345 401T523 704Q530 716 547 716H555H572Q578 707 578 706L606 383Q634 60 636 57Q641 46 701 46Q726 46 726 36Q726 34 723 22Q720 7 718 4T704 0Q701 0 690 0T651 1T578 2Q484 2 455 0H443Q437 6 437 9T439 27Q443 40 445 43L449 46H469Q523 49 533 63L521 213H283L249 155Q208 86 208 74ZM516 260Q516 271 504 416T490 562L463 519Q447 492 400 412L310 260L413 259Q516 259 516 260Z"></path><path stroke-width="0" id="E65-MJMATHI-54" d="M40 437Q21 437 21 445Q21 450 37 501T71 602L88 651Q93 669 101 677H569H659Q691 677 697 676T704 667Q704 661 687 553T668 444Q668 437 649 437Q640 437 637 437T631 442L629 445Q629 451 635 490T641 551Q641 586 628 604T573 629Q568 630 515 631Q469 631 457 630T439 622Q438 621 368 343T298 60Q298 48 386 46Q418 46 427 45T436 36Q436 31 433 22Q429 4 424 1L422 0Q419 0 415 0Q410 0 363 1T228 2Q99 2 64 0H49Q43 6 43 9T45 27Q49 40 55 46H83H94Q174 46 189 55Q190 56 191 56Q196 59 201 76T241 233Q258 301 269 344Q339 619 339 625Q339 630 310 630H279Q212 630 191 624Q146 614 121 583T67 467Q60 445 57 441T43 437H40Z"></path><path stroke-width="0" id="E65-MJMAIN-3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path><path stroke-width="0" id="E65-MJMATHI-6D" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E65-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path><path stroke-width="0" id="E65-MJMAIN-2B" d="M56 237T56 250T70 270H369V420L370 570Q380 583 389 583Q402 583 409 568V270H707Q722 262 722 250T707 230H409V-68Q401 -82 391 -82H389H387Q375 -82 369 -68V230H70Q56 237 56 250Z"></path><path stroke-width="0" id="E65-MJMATHI-70" d="M23 287Q24 290 25 295T30 317T40 348T55 381T75 411T101 433T134 442Q209 442 230 378L240 387Q302 442 358 442Q423 442 460 395T497 281Q497 173 421 82T249 -10Q227 -10 210 -4Q199 1 187 11T168 28L161 36Q160 35 139 -51T118 -138Q118 -144 126 -145T163 -148H188Q194 -155 194 -157T191 -175Q188 -187 185 -190T172 -194Q170 -194 161 -194T127 -193T65 -192Q-5 -192 -24 -194H-32Q-39 -187 -39 -183Q-37 -156 -26 -148H-6Q28 -147 33 -136Q36 -130 94 103T155 350Q156 355 156 364Q156 405 131 405Q109 405 94 377T71 316T59 280Q57 278 43 278H29Q23 284 23 287ZM178 102Q200 26 252 26Q282 26 310 49T356 107Q374 141 392 215T411 325V331Q411 405 350 405Q339 405 328 402T306 393T286 380T269 365T254 350T243 336T235 326L232 322Q232 321 229 308T218 264T204 212Q178 106 178 102Z"></path><path stroke-width="0" id="E65-MJMAIN-D7" d="M630 29Q630 9 609 9Q604 9 587 25T493 118L389 222L284 117Q178 13 175 11Q171 9 168 9Q160 9 154 15T147 29Q147 36 161 51T255 146L359 250L255 354Q174 435 161 449T147 471Q147 480 153 485T168 490Q173 490 175 489Q178 487 284 383L389 278L493 382Q570 459 587 475T609 491Q630 491 630 471Q630 464 620 453T522 355L418 250L522 145Q606 61 618 48T630 29Z"></path><path stroke-width="0" id="E65-MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z"></path><path stroke-width="0" id="E65-MJMATHI-67" d="M311 43Q296 30 267 15T206 0Q143 0 105 45T66 160Q66 265 143 353T314 442Q361 442 401 394L404 398Q406 401 409 404T418 412T431 419T447 422Q461 422 470 413T480 394Q480 379 423 152T363 -80Q345 -134 286 -169T151 -205Q10 -205 10 -137Q10 -111 28 -91T74 -71Q89 -71 102 -80T116 -111Q116 -121 114 -130T107 -144T99 -154T92 -162L90 -164H91Q101 -167 151 -167Q189 -167 211 -155Q234 -144 254 -122T282 -75Q288 -56 298 -13Q311 35 311 43ZM384 328L380 339Q377 350 375 354T369 368T359 382T346 393T328 402T306 405Q262 405 221 352Q191 313 171 233T151 117Q151 38 213 38Q269 38 323 108L331 118L384 328Z"></path><path stroke-width="0" id="E65-MJMATHI-65" d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z"></path><path stroke-width="0" id="E65-MJMATHI-46" d="M48 1Q31 1 31 11Q31 13 34 25Q38 41 42 43T65 46Q92 46 125 49Q139 52 144 61Q146 66 215 342T285 622Q285 629 281 629Q273 632 228 634H197Q191 640 191 642T193 659Q197 676 203 680H742Q749 676 749 669Q749 664 736 557T722 447Q720 440 702 440H690Q683 445 683 453Q683 454 686 477T689 530Q689 560 682 579T663 610T626 626T575 633T503 634H480Q398 633 393 631Q388 629 386 623Q385 622 352 492L320 363H375Q378 363 398 363T426 364T448 367T472 374T489 386Q502 398 511 419T524 457T529 475Q532 480 548 480H560Q567 475 567 470Q567 467 536 339T502 207Q500 200 482 200H470Q463 206 463 212Q463 215 468 234T473 274Q473 303 453 310T364 317H309L277 190Q245 66 245 60Q245 46 334 46H359Q365 40 365 39T363 19Q359 6 353 0H336Q295 2 185 2Q120 2 86 2T48 1Z"></path><path stroke-width="0" id="E65-MJMATHI-75" d="M21 287Q21 295 30 318T55 370T99 420T158 442Q204 442 227 417T250 358Q250 340 216 246T182 105Q182 62 196 45T238 27T291 44T328 78L339 95Q341 99 377 247Q407 367 413 387T427 416Q444 431 463 431Q480 431 488 421T496 402L420 84Q419 79 419 68Q419 43 426 35T447 26Q469 29 482 57T512 145Q514 153 532 153Q551 153 551 144Q550 139 549 130T540 98T523 55T498 17T462 -8Q454 -10 438 -10Q372 -10 347 46Q345 45 336 36T318 21T296 6T267 -6T233 -11Q189 -11 155 7Q103 38 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E65-MJMATHI-6C" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path><path stroke-width="0" id="E65-MJMATHI-74" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path><path stroke-width="0" id="E65-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E65-MJMATHI-45" x="0" y="0"></use><use xlink:href="#E65-MJMATHI-41" x="764" y="0"></use><use xlink:href="#E65-MJMATHI-54" x="1514" y="0"></use><use xlink:href="#E65-MJMAIN-3D" x="2495" y="0"></use><use xlink:href="#E65-MJMATHI-6D" x="3551" y="0"></use><use xlink:href="#E65-MJMATHI-61" x="4429" y="0"></use><use xlink:href="#E65-MJMAIN-2B" x="5180" y="0"></use><use xlink:href="#E65-MJMATHI-70" x="6181" y="0"></use><use xlink:href="#E65-MJMAIN-D7" x="6906" y="0"></use><use xlink:href="#E65-MJMATHI-50" x="7906" y="0"></use><use xlink:href="#E65-MJMATHI-61" x="8657" y="0"></use><use xlink:href="#E65-MJMATHI-67" x="9186" y="0"></use><use xlink:href="#E65-MJMATHI-65" x="9666" y="0"></use><use xlink:href="#E65-MJMATHI-46" x="10382" y="0"></use><use xlink:href="#E65-MJMATHI-61" x="11131" y="0"></use><use xlink:href="#E65-MJMATHI-75" x="11660" y="0"></use><use xlink:href="#E65-MJMATHI-6C" x="12232" y="0"></use><use xlink:href="#E65-MJMATHI-74" x="12530" y="0"></use><use xlink:href="#E65-MJMATHI-54" x="13141" y="0"></use><use xlink:href="#E65-MJMATHI-69" x="13845" y="0"></use><use xlink:href="#E65-MJMATHI-6D" x="14190" y="0"></use><use xlink:href="#E65-MJMATHI-65" x="15068" y="0"></use></g></svg></span></div><script type="math/tex; mode=display" id="MathJax-Element-2">EAT=ma+p\times Page\ Fault\ Time</script></div></div><p><span>注意这里课件和书上不一样,书上是 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="10.244ex" height="2.712ex" viewBox="0 -839.8 4410.4 1167.7" role="img" focusable="false" style="vertical-align: -0.761ex;"><defs><path stroke-width="0" id="E115-MJMAIN-28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path><path stroke-width="0" id="E115-MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path stroke-width="0" id="E115-MJMAIN-2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path><path stroke-width="0" id="E115-MJMATHI-70" d="M23 287Q24 290 25 295T30 317T40 348T55 381T75 411T101 433T134 442Q209 442 230 378L240 387Q302 442 358 442Q423 442 460 395T497 281Q497 173 421 82T249 -10Q227 -10 210 -4Q199 1 187 11T168 28L161 36Q160 35 139 -51T118 -138Q118 -144 126 -145T163 -148H188Q194 -155 194 -157T191 -175Q188 -187 185 -190T172 -194Q170 -194 161 -194T127 -193T65 -192Q-5 -192 -24 -194H-32Q-39 -187 -39 -183Q-37 -156 -26 -148H-6Q28 -147 33 -136Q36 -130 94 103T155 350Q156 355 156 364Q156 405 131 405Q109 405 94 377T71 316T59 280Q57 278 43 278H29Q23 284 23 287ZM178 102Q200 26 252 26Q282 26 310 49T356 107Q374 141 392 215T411 325V331Q411 405 350 405Q339 405 328 402T306 393T286 380T269 365T254 350T243 336T235 326L232 322Q232 321 229 308T218 264T204 212Q178 106 178 102Z"></path><path stroke-width="0" id="E115-MJMAIN-29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path><path stroke-width="0" id="E115-MJMATHI-6D" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E115-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E115-MJMAIN-28" x="0" y="0"></use><use xlink:href="#E115-MJMAIN-31" x="389" y="0"></use><use xlink:href="#E115-MJMAIN-2212" x="1111" y="0"></use><use xlink:href="#E115-MJMATHI-70" x="2111" y="0"></use><use xlink:href="#E115-MJMAIN-29" x="2614" y="0"></use><use xlink:href="#E115-MJMATHI-6D" x="3003" y="0"></use><use xlink:href="#E115-MJMATHI-61" x="3881" y="0"></use></g></svg></span><script type="math/tex">(1-p)ma</script><span>,一个破公式就不能统一一下??</span></p></li></ul><hr /><h3 id='94-页面置换-page-replacement'><span>9.4 页面置换 Page Replacement</span></h3><h4 id='941-cache-miss-的分类'><span>9.4.1 Cache Miss 的分类</span></h4><ul><li><p><span>Compulsory Miss (Cold Start Miss)</span></p><ul><li><p><span>Pages that have never been paged into memory before</span></p></li><li><p><span>How might we remove these misses?</span></p><ul><li><span>Prefetching: loading them into memory before needed</span></li><li><span>Need to predict future somehow!</span></li></ul></li></ul></li><li><p><span>Capacity Miss</span></p><ul><li><p><span>Not enough memory. Must somehow increase available memory size</span></p><ul><li><span>Increase amount of DRAM</span></li><li><span>If multiple processes in memory: adjust percentage of memory allocated to each one</span></li></ul></li></ul></li><li><p><span>Conflict Miss</span></p><ul><li><span>Happen in direct mapped cache and set-associative cache</span></li><li><span>刚把他踢走,接着又要访问他</span></li><li><span>Technically, conflict misses don't exist in virtual memory, since it is a "fully associative" cache</span></li></ul></li><li><p><span>Policy Miss</span></p><ul><li><span>Caused when pages were in memory, but kicked out prematurely because of the replacement policy</span></li></ul></li></ul><h4 id='942-fifo-页面置换'><span>9.4.2 FIFO 页面置换</span></h4><ul><li><p><span>Throw out oldest page. Be fair let every page live in memory for sameamount of time.</span></p></li><li><p><span>Bad: may throw out heavily used pages instead of infrequently used</span></p></li><li><p><span>理论实现:队列</span></p><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="c++" style="break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="c++"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 43px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 39px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>26</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -39px; width: 39px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">class</span> <span class="cm-def">FIFOCache</span> : <span class="cm-keyword">public</span> <span class="cm-variable">Cache</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">private</span>:</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">list</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span> <span class="cm-variable">lst</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">public</span>:</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">FIFOCache</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">size</span>) : <span class="cm-variable">Cache</span>(<span class="cm-variable">size</span>) {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">bool</span> <span class="cm-variable">full</span>() <span class="cm-keyword">override</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">lst</span>.<span class="cm-variable">size</span>()<span class="cm-operator">==</span><span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">size</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">bool</span> <span class="cm-variable">contains</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">x</span>) <span class="cm-keyword">override</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">find</span>(<span class="cm-variable">lst</span>.<span class="cm-variable">begin</span>(), <span class="cm-variable">lst</span>.<span class="cm-variable">end</span>(), <span class="cm-variable">x</span>)<span class="cm-operator">!=</span><span class="cm-variable">lst</span>.<span class="cm-variable">end</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">void</span> <span class="cm-variable">insert</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">x</span>) <span class="cm-keyword">override</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> (<span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">contains</span>(<span class="cm-variable">x</span>)) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">18</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">hitCount</span><span class="cm-operator">++</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">20</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">else</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">21</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> (<span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">full</span>())</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">22</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">lst</span>.<span class="cm-variable">pop_front</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">23</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">lst</span>.<span class="cm-variable">push_back</span>(<span class="cm-variable">x</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">24</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">25</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">26</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 572px;"></div><div class="CodeMirror-gutters" style="height: 572px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 38px;"></div></div></div></div></pre></li><li><p><span>例: 3 个 frame,4 个 page,访问顺序: </span><code>A B C A B D A D B C B</code></p><p><img src="D:\TyporaPictures\OS\105.png" referrerpolicy="no-referrer"></p><p><span>Page Fault: 7</span></p></li><li><p><span>Bélády 异常 (Bélády's Anomaly)</span></p><p><span>对于 FIFO 策略,Mem size 增大,page fault 有可能反而变多</span></p><p><img src="D:\TyporaPictures\OS\108.png" referrerpolicy="no-referrer"></p></li></ul><h4 id='943-最优页面置换-min'><span>9.4.3 最优页面置换 MIN</span></h4><ul><li><p><span>置换最长时间不会使用的页面 (farthest-in-the-future)</span></p></li><li><p><span>Offline 算法,需要提前知道访问序列</span></p></li><li><p><span>理论天花板,实际不可能</span></p></li><li><p><span>理论实现: 好多实现方式</span></p><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="c++" style="break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="c++"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 43px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 39px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>57</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -39px; width: 39px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">class</span> <span class="cm-def">MINCache</span> : <span class="cm-keyword">public</span> <span class="cm-variable">Cache</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">private</span>:</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">set</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span> <span class="cm-variable">s</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">priority_queue</span><span class="cm-operator"><</span><span class="cm-variable">pair</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span>, <span class="cm-variable-3">int</span><span class="cm-operator">>></span> <span class="cm-variable">pq</span>; <span class="cm-comment">// <index, pagenumber></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span> <span class="cm-variable">pages</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span> <span class="cm-variable">nxt</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">public</span>:</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">MINCache</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">size</span>) : <span class="cm-variable">Cache</span>(<span class="cm-variable">size</span>) {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">bool</span> <span class="cm-variable">full</span>() <span class="cm-keyword">override</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">s</span>.<span class="cm-variable">size</span>()<span class="cm-operator">==</span><span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">size</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">bool</span> <span class="cm-variable">contains</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">x</span>) <span class="cm-keyword">override</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">s</span>.<span class="cm-variable">find</span>(<span class="cm-variable">x</span>)<span class="cm-operator">!=</span><span class="cm-variable">s</span>.<span class="cm-variable">end</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">18</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">20</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">void</span> <span class="cm-variable">init</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">n</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">21</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">int</span> <span class="cm-variable">pageNum</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">22</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">for</span> (<span class="cm-variable-3">int</span> <span class="cm-variable">i</span><span class="cm-operator">=</span><span class="cm-number">0</span>;<span class="cm-variable">i</span><span class="cm-operator"><</span><span class="cm-variable">n</span>;<span class="cm-variable">i</span><span class="cm-operator">++</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">23</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">cin</span><span class="cm-operator">>></span><span class="cm-variable">pageNum</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">24</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">pages</span>.<span class="cm-variable">push_back</span>(<span class="cm-variable">pageNum</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">25</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">nxt</span>.<span class="cm-variable">push_back</span>(<span class="cm-variable">INT_MAX</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">26</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">27</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">28</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">map</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span>, <span class="cm-variable-3">int</span><span class="cm-operator">></span> <span class="cm-variable">temp</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">29</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">for</span> (<span class="cm-variable-3">int</span> <span class="cm-variable">i</span><span class="cm-operator">=</span><span class="cm-variable">n</span><span class="cm-operator">-</span><span class="cm-number">1</span>;<span class="cm-variable">i</span><span class="cm-operator">>=</span><span class="cm-number">0</span>;<span class="cm-variable">i</span><span class="cm-operator">--</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">30</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">map</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span>, <span class="cm-variable-3">int</span><span class="cm-operator">></span>::<span class="cm-variable">iterator</span> <span class="cm-variable">p</span><span class="cm-operator">=</span><span class="cm-variable">temp</span>.<span class="cm-variable">find</span>(<span class="cm-variable">pages</span>[<span class="cm-variable">i</span>]);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">31</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> (<span class="cm-variable">p</span><span class="cm-operator">!=</span><span class="cm-variable">temp</span>.<span class="cm-variable">end</span>())</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">32</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">nxt</span>[<span class="cm-variable">i</span>]<span class="cm-operator">=</span><span class="cm-variable">p</span><span class="cm-operator">-></span><span class="cm-variable">second</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">33</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">temp</span>[<span class="cm-variable">pages</span>[<span class="cm-variable">i</span>]]<span class="cm-operator">=</span><span class="cm-variable">i</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">34</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">35</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">36</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">37</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">void</span> <span class="cm-variable">insert</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">i</span>) <span class="cm-keyword">override</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">38</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> (<span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">contains</span>(<span class="cm-variable">pages</span>[<span class="cm-variable">i</span>]))</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">39</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">hitCount</span><span class="cm-operator">++</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">40</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">else</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">41</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> (<span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">full</span>()) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">42</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">auto</span> <span class="cm-variable">t</span><span class="cm-operator">=</span><span class="cm-variable">pq</span>.<span class="cm-variable">top</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">43</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">pq</span>.<span class="cm-variable">pop</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">44</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">s</span>.<span class="cm-variable">erase</span>(<span class="cm-variable">t</span>.<span class="cm-variable">second</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">45</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">46</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">47</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">s</span>.<span class="cm-variable">insert</span>(<span class="cm-variable">pages</span>[<span class="cm-variable">i</span>]);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">48</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">49</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">50</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">pq</span>.<span class="cm-variable">push</span>(<span class="cm-variable">make_pair</span>(<span class="cm-variable">nxt</span>[<span class="cm-variable">i</span>], <span class="cm-variable">pages</span>[<span class="cm-variable">i</span>]));</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">51</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">52</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">53</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">void</span> <span class="cm-variable">start</span>() {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">54</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">for</span> (<span class="cm-variable-3">int</span> <span class="cm-variable">i</span><span class="cm-operator">=</span><span class="cm-number">0</span>;<span class="cm-variable">i</span><span class="cm-operator"><</span><span class="cm-variable">pages</span>.<span class="cm-variable">size</span>();<span class="cm-variable">i</span><span class="cm-operator">++</span>)</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">55</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">insert</span>(<span class="cm-variable">i</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">56</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">57</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 1254px;"></div><div class="CodeMirror-gutters" style="height: 1254px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 38px;"></div></div></div></div></pre></li><li><p><span>例:</span><code>A B C A B D A D B C B</code></p><p><img src="D:\TyporaPictures\OS\106.png" referrerpolicy="no-referrer"></p><p><span>Page Fault: 5</span></p></li></ul><h4 id='944-lru-页面置换'><span>9.4.4 LRU 页面置换</span></h4><ul><li><p><span>最近最少使用 Least Recently Used</span></p></li><li><p><span>Replace page that has not been used for the longest time</span></p></li><li><p><span>理论实现:双向链表</span></p><ul><li><span>如果 hit,把他删除然后插回 tail,O(1) 的操作</span></li><li><span>没 hit,pop head 然后插到 tail</span></li><li><span>就是查询是否 hit 的时候需要遍历链表,不过可以开一个 set 专门用来查询 (空间换时间)</span></li><li><span>实际上这个对于硬件来说太复杂了,无法实现</span></li></ul><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="c++" style="break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="c++"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 43px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 39px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>34</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -39px; width: 39px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">class</span> <span class="cm-def">LRUCache</span> : <span class="cm-keyword">public</span> <span class="cm-variable">Cache</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">private</span>:</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">list</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span> <span class="cm-variable">lst</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">public</span>:</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">LRUCache</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">size</span>) : <span class="cm-variable">Cache</span>(<span class="cm-variable">size</span>) {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">bool</span> <span class="cm-variable">full</span>() <span class="cm-keyword">override</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">lst</span>.<span class="cm-variable">size</span>()<span class="cm-operator">==</span><span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">size</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">list</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span>::<span class="cm-variable">iterator</span> <span class="cm-variable">findElement</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">x</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">find</span>(<span class="cm-variable">lst</span>.<span class="cm-variable">begin</span>(), <span class="cm-variable">lst</span>.<span class="cm-variable">end</span>(), <span class="cm-variable">x</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">bool</span> <span class="cm-variable">contains</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">x</span>) <span class="cm-keyword">override</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">findElement</span>(<span class="cm-variable">x</span>)<span class="cm-operator">!=</span><span class="cm-variable">lst</span>.<span class="cm-variable">end</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">18</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">20</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">void</span> <span class="cm-variable">insert</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">x</span>) <span class="cm-keyword">override</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">21</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">list</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span><span class="cm-operator">></span>::<span class="cm-variable">iterator</span> <span class="cm-variable">it</span><span class="cm-operator">=</span><span class="cm-variable">findElement</span>(<span class="cm-variable">x</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">22</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">23</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> (<span class="cm-variable">it</span><span class="cm-operator">!=</span><span class="cm-variable">lst</span>.<span class="cm-variable">end</span>()) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">24</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">hitCount</span><span class="cm-operator">++</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">25</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">lst</span>.<span class="cm-variable">erase</span>(<span class="cm-variable">it</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">26</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">lst</span>.<span class="cm-variable">push_back</span>(<span class="cm-variable">x</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">27</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">28</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">else</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">29</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> (<span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">full</span>())</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">30</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">lst</span>.<span class="cm-variable">pop_front</span>();</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">31</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">lst</span>.<span class="cm-variable">push_back</span>(<span class="cm-variable">x</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">32</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">33</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">34</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 748px;"></div><div class="CodeMirror-gutters" style="height: 748px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 38px;"></div></div></div></div></pre></li><li><p><span>例: </span><code>A B C D A B C D A B C D</code></p><p><img src="D:\TyporaPictures\OS\107.png" referrerpolicy="no-referrer"></p><p><span>Page Fault: 全是</span></p></li></ul><h4 id='945-近似-lru-页面置换'><span>9.4.5 近似 LRU 页面置换</span></h4><h5 id='9451-时钟算法-clock-algorithm'><span>9.4.5.1 时钟算法 Clock Algorithm</span></h5><ul><li><p><span>Arrange physical pages in circle with single clock hand</span></p></li><li><p><span>Hardware "use" bit per physical page</span></p><ul><li><span>Hardware sets use bit on each reference</span></li><li><span>If use bit is not set, means not referenced in a long time</span></li></ul></li><li><p><span>On page fault</span></p><ul><li><p><span>Advance clock hand</span></p></li><li><p><span>Check use bit</span></p><ul><li><span>1: used recently; clear (set used bit to 0) and leave alone (go next)</span></li><li><span>0: selected candidate for replacement</span></li></ul></li></ul></li><li><p><span>最差情况:转了一圈,1 全变 0,又回到最开始 (FIFO)</span></p></li><li><p><span>hit 的时候时针是不转的</span></p></li><li><p><span>稳定时,时针永远指向刚被替换的下一个位置</span></p></li><li><p><span>理论实现</span></p><pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" lang="c++" style="break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="c++"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 9px; left: 43px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 39px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>44</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -39px; width: 39px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">class</span> <span class="cm-def">ClockCache</span> : <span class="cm-keyword">public</span> <span class="cm-variable">Cache</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">private</span>:</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">vector</span><span class="cm-operator"><</span><span class="cm-variable">pair</span><span class="cm-operator"><</span><span class="cm-variable-3">int</span>, <span class="cm-variable-3">int</span><span class="cm-operator">>></span> <span class="cm-variable">vec</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">int</span> <span class="cm-variable">ptr</span><span class="cm-operator">=</span><span class="cm-number">0</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">public</span>:</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">ClockCache</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">size</span>) : <span class="cm-variable">Cache</span>(<span class="cm-variable">size</span>) {}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">bool</span> <span class="cm-variable">full</span>() <span class="cm-keyword">override</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">vec</span>.<span class="cm-variable">size</span>()<span class="cm-operator">==</span><span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">size</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">int</span> <span class="cm-variable">findElement</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">x</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">for</span> (<span class="cm-variable-3">int</span> <span class="cm-variable">i</span><span class="cm-operator">=</span><span class="cm-number">0</span>;<span class="cm-variable">i</span><span class="cm-operator"><</span><span class="cm-variable">vec</span>.<span class="cm-variable">size</span>();<span class="cm-variable">i</span><span class="cm-operator">++</span>)</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> (<span class="cm-variable">vec</span>[<span class="cm-variable">i</span>].<span class="cm-variable">first</span><span class="cm-operator">==</span><span class="cm-variable">x</span>) </span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">i</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-operator">-</span><span class="cm-number">1</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">18</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">20</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">bool</span> <span class="cm-variable">contains</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">x</span>) <span class="cm-keyword">override</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">21</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">findElement</span>(<span class="cm-variable">x</span>)<span class="cm-operator">>=</span><span class="cm-number">0</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">22</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">23</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">24</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">void</span> <span class="cm-variable">insert</span>(<span class="cm-variable-3">int</span> <span class="cm-variable">x</span>) <span class="cm-keyword">override</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">25</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable-3">int</span> <span class="cm-variable">index</span><span class="cm-operator">=</span><span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">findElement</span>(<span class="cm-variable">x</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">26</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">27</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> (<span class="cm-variable">index</span><span class="cm-operator">>=</span><span class="cm-number">0</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">28</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">vec</span>[<span class="cm-variable">index</span>].<span class="cm-variable">second</span><span class="cm-operator">=</span><span class="cm-number">1</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">29</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">hitCount</span><span class="cm-operator">++</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">30</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">31</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">else</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">32</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> (<span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">full</span>()) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">33</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">while</span> (<span class="cm-variable">vec</span>[<span class="cm-variable">ptr</span>].<span class="cm-variable">second</span><span class="cm-operator">!=</span><span class="cm-number">0</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">34</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">vec</span>[<span class="cm-variable">ptr</span>].<span class="cm-variable">second</span><span class="cm-operator">=</span><span class="cm-number">0</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">35</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">ptr</span><span class="cm-operator">=</span>(<span class="cm-variable">ptr</span><span class="cm-operator">+</span><span class="cm-number">1</span>)<span class="cm-operator">%</span><span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">size</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">36</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">37</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">vec</span>[<span class="cm-variable">ptr</span>]<span class="cm-operator">=</span><span class="cm-variable">make_pair</span>(<span class="cm-variable">x</span>, <span class="cm-number">1</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">38</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">39</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-keyword">else</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">40</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">vec</span>.<span class="cm-variable">push_back</span>(<span class="cm-variable">make_pair</span>(<span class="cm-variable">x</span>, <span class="cm-number">1</span>));</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">41</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> <span class="cm-variable">ptr</span><span class="cm-operator">=</span>(<span class="cm-variable">ptr</span><span class="cm-operator">+</span><span class="cm-number">1</span>)<span class="cm-operator">%</span><span class="cm-keyword">this</span><span class="cm-operator">-></span><span class="cm-variable">size</span>;</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">42</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 30px;">43</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> }</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -39px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 30px;">44</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">};</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 968px;"></div><div class="CodeMirror-gutters" style="height: 968px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 38px;"></div></div></div></div></pre></li><li><p><span>N</span><sup><span>th</span></sup><span> chance algorithm: Used bit 变成一个 counter,从 N 开始倒计</span></p><ul><li><span>N=1K 比较合适</span></li><li><span>N 越小越快</span></li><li><span>Clean pages, use N=1</span></li><li><span>Dirty pages, use N=2 (and write back to disk when N=1)</span></li></ul></li></ul><h5 id='9452-second-chance-list-algorithm'><span>9.4.5.2 Second Chance List Algorithm</span></h5><p><img src="D:\TyporaPictures\OS\109.png" referrerpolicy="no-referrer"></p><ul><li><p><span>Split memory in two list</span></p><ul><li><span>Active List</span></li><li><span>Second Chance List</span></li></ul></li><li><p><span>Access pages in Active list at full speed</span></p></li><li><p><span>Page Fault</span></p><ul><li><span>Always move overflow page from end of Active list to front of Second chance list (SC) and mark invalid</span></li><li><span>Desired Page On SC List: move to front of Active list, mark RW</span></li><li><span>Not on SC list: page in to front of Active list, mark RW; page out LRU victim at end of SC list</span></li></ul></li></ul><hr /><h3 id='95-帧分配-frame-allocation'><span>9.5 帧分配 Frame Allocation</span></h3><h4 id='951-全局分配与局部分配'><span>9.5.1 全局分配与局部分配</span></h4><ul><li><p><span>全局置换 Global Replacement</span></p><p><span>process selects replacement frame from set of all frames; one process can take a frame from another</span></p></li><li><p><span>局部置换 Local Replacement</span></p><p><span>each process selects from only its own set of allocated frames</span></p></li></ul><h4 id='952-分配算法'><span>9.5.2 分配算法</span></h4><ul><li><p><span>Equal Allocation (Fixed Scheme)</span></p><ul><li><span>Every process gets same amount of memory</span></li><li><span>Example: 100 frames, 5 processes -> each process gets 20 frames</span></li></ul></li><li><p><span>Proportional Allocation (Fixed Scheme)</span></p><p><span>Allocate according to the size of process</span></p><p><span>设 </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="4.34ex" height="1.739ex" viewBox="0 -514 1868.7 748.7" role="img" focusable="false" style="vertical-align: -0.545ex;"><defs><path stroke-width="0" id="E116-MJMATHI-73" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path><path stroke-width="0" id="E116-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E116-MJMAIN-3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E116-MJMATHI-73" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E116-MJMATHI-69" x="663" y="-213"></use><use xlink:href="#E116-MJMAIN-3D" x="1090" y="0"></use></g></svg></span><script type="math/tex">s_i=</script><span> size of process </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.058ex" height="1.847ex" viewBox="-39 -514 886 795.3" role="img" focusable="false" style="vertical-align: -0.653ex; margin-left: -0.091ex;"><defs><path stroke-width="0" id="E122-MJMATHI-70" d="M23 287Q24 290 25 295T30 317T40 348T55 381T75 411T101 433T134 442Q209 442 230 378L240 387Q302 442 358 442Q423 442 460 395T497 281Q497 173 421 82T249 -10Q227 -10 210 -4Q199 1 187 11T168 28L161 36Q160 35 139 -51T118 -138Q118 -144 126 -145T163 -148H188Q194 -155 194 -157T191 -175Q188 -187 185 -190T172 -194Q170 -194 161 -194T127 -193T65 -192Q-5 -192 -24 -194H-32Q-39 -187 -39 -183Q-37 -156 -26 -148H-6Q28 -147 33 -136Q36 -130 94 103T155 350Q156 355 156 364Q156 405 131 405Q109 405 94 377T71 316T59 280Q57 278 43 278H29Q23 284 23 287ZM178 102Q200 26 252 26Q282 26 310 49T356 107Q374 141 392 215T411 325V331Q411 405 350 405Q339 405 328 402T306 393T286 380T269 365T254 350T243 336T235 326L232 322Q232 321 229 308T218 264T204 212Q178 106 178 102Z"></path><path stroke-width="0" id="E122-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E122-MJMATHI-70" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E122-MJMATHI-69" x="711" y="-213"></use></g></svg></span><script type="math/tex">p_i</script></p><p><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="9.323ex" height="2.712ex" viewBox="0 -839.8 4014.2 1167.7" role="img" focusable="false" style="vertical-align: -0.761ex;"><defs><path stroke-width="0" id="E118-MJMATHI-53" d="M308 24Q367 24 416 76T466 197Q466 260 414 284Q308 311 278 321T236 341Q176 383 176 462Q176 523 208 573T273 648Q302 673 343 688T407 704H418H425Q521 704 564 640Q565 640 577 653T603 682T623 704Q624 704 627 704T632 705Q645 705 645 698T617 577T585 459T569 456Q549 456 549 465Q549 471 550 475Q550 478 551 494T553 520Q553 554 544 579T526 616T501 641Q465 662 419 662Q362 662 313 616T263 510Q263 480 278 458T319 427Q323 425 389 408T456 390Q490 379 522 342T554 242Q554 216 546 186Q541 164 528 137T492 78T426 18T332 -20Q320 -22 298 -22Q199 -22 144 33L134 44L106 13Q83 -14 78 -18T65 -22Q52 -22 52 -14Q52 -11 110 221Q112 227 130 227H143Q149 221 149 216Q149 214 148 207T144 186T142 153Q144 114 160 87T203 47T255 29T308 24Z"></path><path stroke-width="0" id="E118-MJMAIN-3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path><path stroke-width="0" id="E118-MJSZ1-2211" d="M61 748Q64 750 489 750H913L954 640Q965 609 976 579T993 533T999 516H979L959 517Q936 579 886 621T777 682Q724 700 655 705T436 710H319Q183 710 183 709Q186 706 348 484T511 259Q517 250 513 244L490 216Q466 188 420 134T330 27L149 -187Q149 -188 362 -188Q388 -188 436 -188T506 -189Q679 -189 778 -162T936 -43Q946 -27 959 6H999L913 -249L489 -250Q65 -250 62 -248Q56 -246 56 -239Q56 -234 118 -161Q186 -81 245 -11L428 206Q428 207 242 462L57 717L56 728Q56 744 61 748Z"></path><path stroke-width="0" id="E118-MJMATHI-73" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path><path stroke-width="0" id="E118-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E118-MJMATHI-53" x="0" y="0"></use><use xlink:href="#E118-MJMAIN-3D" x="922" y="0"></use><use xlink:href="#E118-MJSZ1-2211" x="1978" y="0"></use><g transform="translate(3201,0)"><use xlink:href="#E118-MJMATHI-73" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E118-MJMATHI-69" x="663" y="-213"></use></g></g></svg></span><script type="math/tex">S=\sum s_i</script></p><p><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="4.491ex" height="1.415ex" viewBox="0 -514 1933.8 609.1" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E119-MJMATHI-6D" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E119-MJMAIN-3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E119-MJMATHI-6D" x="0" y="0"></use><use xlink:href="#E119-MJMAIN-3D" x="1155" y="0"></use></g></svg></span><script type="math/tex">m=</script><span> total # of frames</span></p><p><span>Allocation for </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.058ex" height="1.847ex" viewBox="-39 -514 886 795.3" role="img" focusable="false" style="vertical-align: -0.653ex; margin-left: -0.091ex;"><defs><path stroke-width="0" id="E122-MJMATHI-70" d="M23 287Q24 290 25 295T30 317T40 348T55 381T75 411T101 433T134 442Q209 442 230 378L240 387Q302 442 358 442Q423 442 460 395T497 281Q497 173 421 82T249 -10Q227 -10 210 -4Q199 1 187 11T168 28L161 36Q160 35 139 -51T118 -138Q118 -144 126 -145T163 -148H188Q194 -155 194 -157T191 -175Q188 -187 185 -190T172 -194Q170 -194 161 -194T127 -193T65 -192Q-5 -192 -24 -194H-32Q-39 -187 -39 -183Q-37 -156 -26 -148H-6Q28 -147 33 -136Q36 -130 94 103T155 350Q156 355 156 364Q156 405 131 405Q109 405 94 377T71 316T59 280Q57 278 43 278H29Q23 284 23 287ZM178 102Q200 26 252 26Q282 26 310 49T356 107Q374 141 392 215T411 325V331Q411 405 350 405Q339 405 328 402T306 393T286 380T269 365T254 350T243 336T235 326L232 322Q232 321 229 308T218 264T204 212Q178 106 178 102Z"></path><path stroke-width="0" id="E122-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E122-MJMATHI-70" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E122-MJMATHI-69" x="711" y="-213"></use></g></svg></span><script type="math/tex">p_i</script><span> is </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="11.013ex" height="3.144ex" viewBox="0 -886.4 4741.8 1353.8" role="img" focusable="false" style="vertical-align: -1.086ex;"><defs><path stroke-width="0" id="E121-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path><path stroke-width="0" id="E121-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E121-MJMAIN-3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path><path stroke-width="0" id="E121-MJMATHI-73" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path><path stroke-width="0" id="E121-MJMATHI-53" d="M308 24Q367 24 416 76T466 197Q466 260 414 284Q308 311 278 321T236 341Q176 383 176 462Q176 523 208 573T273 648Q302 673 343 688T407 704H418H425Q521 704 564 640Q565 640 577 653T603 682T623 704Q624 704 627 704T632 705Q645 705 645 698T617 577T585 459T569 456Q549 456 549 465Q549 471 550 475Q550 478 551 494T553 520Q553 554 544 579T526 616T501 641Q465 662 419 662Q362 662 313 616T263 510Q263 480 278 458T319 427Q323 425 389 408T456 390Q490 379 522 342T554 242Q554 216 546 186Q541 164 528 137T492 78T426 18T332 -20Q320 -22 298 -22Q199 -22 144 33L134 44L106 13Q83 -14 78 -18T65 -22Q52 -22 52 -14Q52 -11 110 221Q112 227 130 227H143Q149 221 149 216Q149 214 148 207T144 186T142 153Q144 114 160 87T203 47T255 29T308 24Z"></path><path stroke-width="0" id="E121-MJMAIN-22C5" d="M78 250Q78 274 95 292T138 310Q162 310 180 294T199 251Q199 226 182 208T139 190T96 207T78 250Z"></path><path stroke-width="0" id="E121-MJMATHI-6D" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E121-MJMATHI-61" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E121-MJMATHI-69" x="748" y="-213"></use><use xlink:href="#E121-MJMAIN-3D" x="1150" y="0"></use><g transform="translate(1928,0)"><g transform="translate(397,0)"><rect stroke="none" width="694" height="60" x="0" y="220"></rect><g transform="translate(60,508)"><use transform="scale(0.707)" xlink:href="#E121-MJMATHI-73" x="0" y="0"></use><use transform="scale(0.5)" xlink:href="#E121-MJMATHI-69" x="663" y="-213"></use></g><use transform="scale(0.707)" xlink:href="#E121-MJMATHI-53" x="168" y="-560"></use></g></g><use xlink:href="#E121-MJMAIN-22C5" x="3363" y="0"></use><use xlink:href="#E121-MJMATHI-6D" x="3863" y="0"></use></g></svg></span><script type="math/tex">a_i=\frac{s_i}{S}\cdot m</script></p></li><li><p><span>Priority Allocation</span></p><ul><li><p><span>Proportional scheme using priorities rather than size</span></p><ul><li><span>Same type of computation as previous scheme</span></li></ul></li><li><p><span>Possible behavior: If process </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.058ex" height="1.847ex" viewBox="-39 -514 886 795.3" role="img" focusable="false" style="vertical-align: -0.653ex; margin-left: -0.091ex;"><defs><path stroke-width="0" id="E122-MJMATHI-70" d="M23 287Q24 290 25 295T30 317T40 348T55 381T75 411T101 433T134 442Q209 442 230 378L240 387Q302 442 358 442Q423 442 460 395T497 281Q497 173 421 82T249 -10Q227 -10 210 -4Q199 1 187 11T168 28L161 36Q160 35 139 -51T118 -138Q118 -144 126 -145T163 -148H188Q194 -155 194 -157T191 -175Q188 -187 185 -190T172 -194Q170 -194 161 -194T127 -193T65 -192Q-5 -192 -24 -194H-32Q-39 -187 -39 -183Q-37 -156 -26 -148H-6Q28 -147 33 -136Q36 -130 94 103T155 350Q156 355 156 364Q156 405 131 405Q109 405 94 377T71 316T59 280Q57 278 43 278H29Q23 284 23 287ZM178 102Q200 26 252 26Q282 26 310 49T356 107Q374 141 392 215T411 325V331Q411 405 350 405Q339 405 328 402T306 393T286 380T269 365T254 350T243 336T235 326L232 322Q232 321 229 308T218 264T204 212Q178 106 178 102Z"></path><path stroke-width="0" id="E122-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E122-MJMATHI-70" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E122-MJMATHI-69" x="711" y="-213"></use></g></svg></span><script type="math/tex">p_i</script><span> generates a page fault, select for replacement a frame from a process with lower priority number</span></p></li></ul></li></ul><hr /><h2 id='第十--十一章--文件系统-file-system'><span>第十 & 十一章 文件系统 File System</span></h2><h3 id='101-文件系统概念'><span>10.1 文件系统概念</span></h3><ul><li><p><span>文件系统 File System</span></p><p><span>Layer of OS that transforms block interface of disks (or other block devices) into Files, Directories, etc.</span></p></li><li><p><span>文件系统的功能</span></p><ul><li><span>Naming : Interface to find files by name, not by blocks</span></li><li><span>Disk Management: Collecting disk blocks into files</span></li><li><span>Protection: Layers to keep data secure</span></li><li><span>Reliability/Durability: Keeping of files durable despite crashes, media failures, attacks, etc.</span></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\127.png" referrerpolicy="no-referrer"></p><hr /><h3 id='102-文件和目录-files-and-directories'><span>10.2 文件和目录 Files and Directories</span></h3><h4 id='1021-目录的组成'><span>10.2.1 目录的组成</span></h4><ul><li><p><span>多级继承结构 Hierarchical Structure</span></p></li><li><p><span>Each directory entry is a collection of</span></p><ul><li><span>Files</span></li><li><span>Directories: A link to another entries</span></li></ul></li><li><p><span>Each has a name and attributes</span></p></li><li><p><span>Links (hard links) make it a DAG, not just a tree</span></p></li></ul><p><img src="D:\TyporaPictures\OS\128.png" referrerpolicy="no-referrer"></p><h4 id='1022-文件'><span>10.2.2 文件</span></h4><ul><li><p><span>文件 File</span></p><p><span>操作系统对存储设备的物理属性加以抽象,从而定义逻辑存储单位</span></p><p><span>Named permanent storage</span></p></li><li><p><span>文件的组成</span></p><ul><li><p><span>Data</span></p><p><span>Blocks on disk somewhere</span></p></li><li><p><span>Metadata (Attributes)</span></p><ul><li><p><span>Owner, size, last opened, …</span></p></li><li><p><span>Access rights</span></p><ul><li><span>R, W, X</span></li><li><span>Owner, Group, Other (in Unix systems)</span></li><li><span>Access control list in Windows system</span></li></ul></li></ul></li></ul></li></ul><hr /><h3 id='103-磁盘管理策略'><span>10.3 磁盘管理策略</span></h3><ul><li><p><span>Basic entities on a disk</span></p><ul><li><p><span>File</span></p><p><span>User visible group of blocks arranged sequentially in logical space</span></p></li><li><p><span>Directory</span></p><p><span>User visible index mapping names to files</span></p></li></ul></li><li><p><span>Access disk as linear array of sectors</span></p><ul><li><span>Identify sectors as vectors [cylinder, surface, sector], sort in cylinder major order: </span><strong><span>not used anymore</span></strong></li><li><span>Logical Block Addressing (LBA): Every sector has integer address from zero up to max number of sectors</span></li><li><span>Controller translates from address to physical position</span></li></ul></li><li><p><span>Need way to track free disk blocks</span></p><ul><li><span>Link free blocks together: too slow today</span></li><li><span>Use bitmap to represent free space on disk</span></li></ul></li><li><p><span>Need way to structure files: File header</span></p><ul><li><span>Track which blocks belong at which offsets within the logical file structure</span></li><li><span>Optimize placement of files' disk blocks to match access and usage patterns</span></li></ul></li></ul><hr /><h3 id='104-目录分配'><span>10.4 目录分配</span></h3><h4 id='1041-连续分配-contiguous-allocation'><span>10.4.1 连续分配 Contiguous Allocation</span></h4><ul><li><p><span>连续分配方法要求每个文件在磁盘上占有一组连续的块。磁盘地址为磁盘定义了一个线性排序。有了这个排序,假设只有一个作业正在访问磁盘,在块 b 之后访问块 b+1 时通常不需要移动磁头。当需要磁头移动(从一个柱面的最后扇区到下一个柱面的第一个扇区时),只需要移动一个磁道</span></p></li><li><p><span>Locate Files</span></p><p><span>Start address and size -> easy to random access</span></p><p><img src="D:\TyporaPictures\OS\129.png" referrerpolicy="no-referrer"></p></li><li><p><span>Delete Files</span></p><p><img src="D:\TyporaPictures\OS\130.png" referrerpolicy="no-referrer"></p></li><li><p><span>缺点:外碎片 External Fragmentation</span></p><p><img src="D:\TyporaPictures\OS\131.png" referrerpolicy="no-referrer"></p></li><li><p><span>缺点:无法应对文件增长 File Growth Problem</span></p><p><img src="D:\TyporaPictures\OS\132.png" referrerpolicy="no-referrer"></p></li><li><p><span>连续分配的应用</span></p><ul><li><span>ISO 9660</span></li><li><span>CD-ROM</span></li></ul></li></ul><h4 id='1042-链接分配-linked-allocation'><span>10.4.2 链接分配 Linked Allocation</span></h4><ol start='' ><li><p><span>Chop the storage device into equal sized blocks</span></p><p><img src="D:\TyporaPictures\OS\133.png" referrerpolicy="no-referrer"></p></li><li><p><span>Fill the empty space in a block by block manner</span></p><p><img src="D:\TyporaPictures\OS\134.png" referrerpolicy="no-referrer"></p></li><li><p><span>Leave 4 bytes from each block as the pointer</span></p><p><img src="D:\TyporaPictures\OS\135.png" referrerpolicy="no-referrer"></p></li><li><p><span>Keep the file size in the root directory table</span></p><p><img src="D:\TyporaPictures\OS\136.png" referrerpolicy="no-referrer"></p></li></ol><ul><li><p><span>缺点:内碎片 Internal Fragmentation</span></p><p><span>The last block of a file may not be fully filled</span></p></li><li><p><span>缺点:随机访问性能差</span></p><p><span>What if I want to access the 2019-th block of ubuntu.iso?</span></p><p><span>You have to access blocks 1~2018 of ubuntu.iso until the 2019-th block.</span></p></li><li><p><span>优点</span></p><ul><li><span>没有外碎片</span></li><li><span>解决了文件增长的问题</span></li></ul></li></ul><h4 id='1043-索引分配-index-allocation'><span>10.4.3 索引分配 Index Allocation</span></h4><ul><li><p><span>索引分配通过将所有指针放在一起,即索引块 (Index Block),实现了高效的随机访问</span></p></li><li><p><span>索引块的组织方式</span></p><ul><li><p><span>链接方案</span></p><p><span>一个索引块通常为一个磁盘块,本身可以读写,通过链表的形式存放大文件</span></p></li><li><p><span>多级索引</span></p><p><span>通过第一级索引块指向一组第二级索引块,它又指向文件块</span></p></li><li><p><span>组合方案</span></p><p><span>iNode</span></p></li></ul></li></ul><hr /><h3 id='105-文件分配表-fat'><span>10.5 文件分配表 FAT</span></h3><h4 id='1051-fat-的原理'><span>10.5.1 FAT 的原理</span></h4><ul><li><p><span>文件分配表 File Allocation Table</span></p><p><span>Centralize all the block links as FAT</span></p><p><img src="D:\TyporaPictures\OS\137.png" referrerpolicy="no-referrer"></p></li><li><p><span>FAT 相当于一个 next 数组</span></p><p><img src="D:\TyporaPictures\OS\138.png" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\139.png" referrerpolicy="no-referrer"></p></li></ul><h4 id='1052-fat-文件系统的大小'><span>10.5.2 FAT 文件系统的大小</span></h4><p><img src="D:\TyporaPictures\OS\140.png" referrerpolicy="no-referrer"></p><ul><li><p><span>在 DOS 里 block 被称为 cluster</span></p></li><li><p><span>假设 block size = 32 KB,那么对于 FAT32 来说,它支持的文件总大小为</span></p><p><span>32 * 2</span><sup><span>10</span></sup><span> * 2</span><sup><span>28</span></sup><span> = 2</span><sup><span>43</span></sup><span> = 8 TB</span></p></li><li><p><span>然而,微软为了催人用 NTFS,手动把 FAT 文件系统大小的上限设为了 32 GB</span></p><p><span>如果你有一个 64 GB 的 U 盘,在 FAT 文件系统下你最多只能往里存 32 GB</span></p></li><li><p><span>但是你可以不用微软的工具格式化,用别的手段把它格式化成 FAT 就没有这个限制了</span></p></li></ul><h4 id='1053-fat-文件系统结构'><span>10.5.3 FAT 文件系统结构</span></h4><p><img src="D:\TyporaPictures\OS\141.png" referrerpolicy="no-referrer"></p><h4 id='1054-fat-文件遍历'><span>10.5.4 FAT 文件遍历</span></h4><p><span>例: </span><code>dir c:\windows</code></p><p><img src="D:\TyporaPictures\OS\142.png" referrerpolicy="no-referrer"></p><ul><li><span>为什么是 cluster 2</span></li></ul><p><img src="D:\TyporaPictures\OS\143.png" referrerpolicy="no-referrer"></p><h4 id='1055-fat-directory-entry'><span>10.5.5 FAT Directory Entry</span></h4><ul><li><span>A 32 byte directory entry in a directory file</span></li><li><span>A directory entry is describing a file (or a sub directory) under a particular directory</span></li></ul><p><img src="D:\TyporaPictures\OS\144.png" referrerpolicy="no-referrer"></p><ul><li><p><span>文件名被规范成了 8+3 的格式</span></p></li><li><p><span>FAT32 的最大文件大小</span></p><p><span>4G - 1 bytes</span></p></li></ul><p><img src="D:\TyporaPictures\OS\145.png" referrerpolicy="no-referrer"></p><ul><li><span>长文件名 Long File Name (LFN) Directory Entry</span></li></ul><p><img src="D:\TyporaPictures\OS\146.png" referrerpolicy="no-referrer"></p><ul><li><span>Normal directory entry vs LFN directory entry</span></li></ul><p><img src="D:\TyporaPictures\OS\147.png" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\148.png" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\149.png" referrerpolicy="no-referrer"></p><ul><li><p><span>Directory entry is important</span></p><ul><li><p><span>It stores the start cluster number.</span></p></li><li><p><span>It stores the file size</span></p><p><span>Without the file size, how can you know when you should stop reading a cluster?</span></p></li><li><p><span>It stores all file attributes</span></p></li></ul></li></ul><h4 id='1056-fat-读文件'><span>10.5.6 FAT 读文件</span></h4><p><span>例: 顺序读取 </span><code>C:\windows\gamedata.dat</code></p><p><img src="D:\TyporaPictures\OS\150.png" referrerpolicy="no-referrer"></p><p> </p><p><img src="D:\TyporaPictures\OS\151.png" referrerpolicy="no-referrer"></p><h4 id='1057-fat-写文件'><span>10.5.7 FAT 写文件</span></h4><p><span>例: 向 </span><code>C:\windows\gamedata.dat</code><span> 写入数据</span></p><p><img src="D:\TyporaPictures\OS\152.png" referrerpolicy="no-referrer"></p><p> </p><p><img src="D:\TyporaPictures\OS\153.png" referrerpolicy="no-referrer"></p><p> </p><p><img src="D:\TyporaPictures\OS\154.png" referrerpolicy="no-referrer"></p><p> </p><p><img src="D:\TyporaPictures\OS\155.png" referrerpolicy="no-referrer"></p><h4 id='1058-fat-删除文件'><span>10.5.8 FAT 删除文件</span></h4><p><span>例: 删除 </span><code>C:\windows\gamedata.dat</code></p><p><img src="D:\TyporaPictures\OS\156.png" referrerpolicy="no-referrer"></p><p> </p><p><img src="D:\TyporaPictures\OS\157.png" referrerpolicy="no-referrer"></p><ul><li><p><span>实际上数据还在硬盘中,直到 de-allocated 的 cluster 被再次使用(覆盖)</span></p></li><li><p><span>所以会产生安全问题,如果删除的数据还没被覆盖,就能通过其他手段获取</span></p></li><li><p><span>Secure Disk Erase</span></p><p><span>一种简单的办法是把释放出的 cluster 直接全写成 0</span></p></li><li><p><span>数据恢复</span></p><p><span>既然还在硬盘里,就可以在没被覆盖之前恢复</span></p><p><span>首先要抓紧拔电源,然后拿下硬盘</span></p><p><img src="D:\TyporaPictures\OS\158.png" referrerpolicy="no-referrer"></p></li></ul><h4 id='1059-总结'><span>10.5.9 总结</span></h4><ul><li><p><span>Space efficient:</span></p><ul><li><span>4 bytes overhead (FAT entry) per data cluster.</span></li></ul></li><li><p><span>Delete</span></p><ul><li><p><span>Lazy delete efficient</span></p></li><li><p><span>Insecure</span></p><p><span>designed for single user 20+ years ago</span></p></li></ul></li><li><p><span>Deployment: (FAT32 and FAT12)</span></p><p><span>It is everywhere: CF cards, SD cards, USB drives</span></p></li><li><p><span>Search</span></p><ul><li><p><span>Block addresses of a file may scatter discontinuously</span></p></li><li><p><span>To locate the 888-th block of a file?</span></p><p><span>Start from the first FAT entry and follow 888 pointers</span></p></li></ul></li><li><p><span>The most commonly used file system in the world</span></p></li></ul><hr /><h3 id='106-inode'><span>10.6 iNode</span></h3><h4 id='1061-inode-的原理'><span>10.6.1 iNode 的原理</span></h4><ul><li><span>All pointers of a file are located together</span></li><li><span>One directory/file has one iNode</span></li></ul><p><img src="D:\TyporaPictures\OS\159.png" referrerpolicy="no-referrer"></p><ul><li><p><span>iNode Table</span></p><p><span>An array of iNodes</span></p></li><li><p><span>Pointers are unbalanced tree based</span></p></li></ul><p><img src="D:\TyporaPictures\OS\160.png" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\161.png" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\162.jpg" referrerpolicy="no-referrer"></p><h4 id='1062-inode-的结构'><span>10.6.2 iNode 的结构</span></h4><ul><li><p><span>iNode Metadata</span></p><p><img src="D:\TyporaPictures\OS\163.jpg" referrerpolicy="no-referrer"></p></li><li><p><span>12 direct pointers</span></p><p><img src="D:\TyporaPictures\OS\164.png" referrerpolicy="no-referrer"></p></li><li><p><span>Indirect Pointers</span></p><p><span>一个 pointer 4 个 bytes (实际上就是个 32 bit 的地址)</span></p><p><img src="D:\TyporaPictures\OS\165.png" referrerpolicy="no-referrer"></p></li></ul><p><img src="D:\TyporaPictures\OS\167.png" referrerpolicy="no-referrer"></p><h4 id='1063-inode-文件大小'><span>10.6.3 iNode 文件大小</span></h4><p><img src="D:\TyporaPictures\OS\166.png" referrerpolicy="no-referrer"></p><hr /><h3 id='107-可扩展文件系统-ext'><span>10.7 可扩展文件系统 Ext</span></h3><ul><li><span>Extended File System</span></li><li><span>The latest default FS for Linux distribution is the Fourth Extended File System, Ext4 for short.</span></li><li><span>基于 iNode</span></li></ul><h4 id='1071-ext-文件系统的大小'><span>10.7.1 Ext 文件系统的大小</span></h4><ul><li><p><span>For Ext2 & Ext3:</span></p><ul><li><span>Block size: 1,024, 2,048, or 4,096 bytes.</span></li><li><span>Block address size: 4 bytes => # of block addresses = 2</span><sup><span>32</span></sup></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\168.png" referrerpolicy="no-referrer"></p><h4 id='1072-ext-文件系统结构'><span>10.7.2 Ext 文件系统结构</span></h4><ul><li><span>The file system is divided into block groups and every block group has the same structure</span></li></ul><p><img src="D:\TyporaPictures\OS\169.png" referrerpolicy="no-referrer"></p><ul><li><span>Block Group 的结构</span></li></ul><p><img src="D:\TyporaPictures\OS\170.png" referrerpolicy="no-referrer"></p><ul><li><p><span>Block Bitmap & iNode Bitmap</span></p><ul><li><p><span>Block bitmap tells which block is allocated</span></p></li><li><p><span>iNode Bitmap</span></p><p><span>A bit string that represents if an iNode (index node) is allocated or not</span></p><p><span>Implies that the number of files in the file system is </span><strong><span>fixed</span></strong></p></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\171.png" referrerpolicy="no-referrer"></p><ul><li><p><span>Block Group 的优点</span></p><ul><li><p><span>Performance: spatial locality</span></p><p><span>Group iNodes and data blocks of related files together</span></p></li><li><p><span>Reliability</span></p><p><span>Superblock and GDT are replicated in each block group</span></p><p><span>可以互相校验</span></p></li></ul></li><li><p><span>磁盘管理</span></p><p><span>Disk divided into block groups</span></p><ul><li><span>Each group has two block sized bitmaps (free blocks/ inodes)</span></li><li><span>Block sizes settable at format time: 1K, 2K, 4K, 8K…</span></li><li><span>Provides locality</span></li></ul><p><span>例: 在 </span><code>/dir1/</code><span> 目录下创建 </span><code>file1.dat</code></p><p><img src="D:\TyporaPictures\OS\172.png" referrerpolicy="no-referrer"></p></li></ul><h4 id='1073-ext-的-inode-结构'><span>10.7.3 Ext 的 iNode 结构</span></h4><p><img src="D:\TyporaPictures\OS\173.png" referrerpolicy="no-referrer"></p><h4 id='1074-ext-删除文件'><span>10.7.4 Ext 删除文件</span></h4><p><img src="D:\TyporaPictures\OS\174.png" referrerpolicy="no-referrer"></p><p><img src="D:\TyporaPictures\OS\175.png" referrerpolicy="no-referrer"></p><h4 id='1075-硬链接-hard-link'><span>10.7.5 硬链接 Hard Link</span></h4><ul><li><p><span>A hard link is a directory entry pointing to the iNode of an existing file</span></p><p><span>That file can accessed through two different pathnames</span></p><p><span>例: </span><code>ln /home/csbtang/dir1/12.jpg /tmp/mylink</code></p><p><img src="D:\TyporaPictures\OS\176.png" referrerpolicy="no-referrer"></p></li><li><p><span>例: </span><code>/</code><span> 下有 20 个目录,</span><code>/</code><span> 有多少硬链接</span></p><ul><li><p><span>20 sub directories: they have link </span><code>..</code></p></li><li><p><span>Root directory: </span><code>.</code><span> and </span><code>..</code><span> pointing to itself</span></p><p><span>根目录比较特殊:</span><code>..</code><span> 指向自己,因为它没有上级目录</span></p></li><li><p><span>20 + 2 = 22 hard links</span></p></li></ul><p><img src="D:\TyporaPictures\OS\177.png" referrerpolicy="no-referrer"></p></li><li><p><span>删除硬链接</span></p><p><img src="D:\TyporaPictures\OS\178.png" referrerpolicy="no-referrer"></p><p><span>就是把链接的 iNode 的 link count --,当 = 0 的时候就会被 deallocate</span></p></li></ul><h4 id='1076-符号链接-软链接-symbolic-soft-link'><span>10.7.6 符号链接 (软链接) Symbolic (Soft) Link</span></h4><ul><li><p><span>A symbolic link creates a new iNode</span></p><p><span>例: </span><code>ln -s /home/csbtang /dir1/12.jpg /tmp/mylink</code></p><p><img src="D:\TyporaPictures\OS\179.png" referrerpolicy="no-referrer"></p></li><li><p><span>Symbolic link is pointing to a new iNode whose target's </span><strong><span>pathname</span></strong><span> are stored using the space originally designed for 12 direct block and the 3 indirect block pointers if the pathname is shorter than 60 characters</span></p><p><span>Use back a normal iNode + one direct data block to hold the long pathname otherwise</span></p><p><img src="D:\TyporaPictures\OS\180.png" referrerpolicy="no-referrer"></p><p><span>软链接的 iNode 把本来存 pointer 的位置用来存链接到的文件的路径,如果路径大于 60 bytes 就再用一个 direct block 存</span></p></li><li><p><span>硬链接和软链接比较</span></p><ul><li><p><span>Hard link</span></p><ul><li><span>Sets another directory entry to contain the file number for the file</span></li><li><span>Creates another name (path) for the file</span></li><li><span>Each is "first class"</span></li></ul></li><li><p><span>Soft link or Symbolic Link</span></p><ul><li><span>Directory entry contains the path and name of the file</span></li><li><span>Map one name to another name</span></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\181.png" referrerpolicy="no-referrer"></p></li></ul><hr /><h3 id='108-ntfs'><span>10.8 NTFS</span></h3><ul><li><p><span>New Technology File System (NTFS)</span></p><p><span>Default on Microsoft Windows systems</span></p></li><li><p><span>Variable length extents</span></p></li><li><p><span>Everything (almost) is a sequence of </span><code><attribute: value></code><span> pairs</span></p></li><li><p><span>Mix direct and indirect freely</span></p></li><li><p><span>Directories organized in B-tree structure by default</span></p></li></ul><h4 id='1081-ntfs-文件系统结构'><span>10.8.1 NTFS 文件系统结构</span></h4><ul><li><p><span>Master File Table</span></p><ul><li><span>Database with flexible 1KB entries for metadata/data</span></li><li><span>Variable sized attribute records (data or metadata)</span></li><li><span>Extend with variable depth tree (non resident)</span></li></ul></li><li><p><span>Extents</span></p><p><span>Variable length contiguous regions</span></p><ul><li><span>Block pointers cover runs of blocks</span></li><li><span>Similar approach in Linux (ext4)</span></li><li><span>File create can provide hint as to size of file</span></li></ul></li><li><p><span>Journaling for reliability 日志记录</span></p></li></ul><p><img src="D:\TyporaPictures\OS\182.gif" referrerpolicy="no-referrer"></p><h4 id='1082-ntfs-文件存储'><span>10.8.2 NTFS 文件存储</span></h4><ul><li><p><span>小文件</span></p><p><img src="D:\TyporaPictures\OS\182.png" referrerpolicy="no-referrer"></p><p><span>直接把数据存在 MFT Entry 里,不需要 data block</span></p></li><li><p><span>中文件</span></p><p><img src="D:\TyporaPictures\OS\183.png" referrerpolicy="no-referrer"></p></li><li><p><span>大文件</span></p><p><img src="D:\TyporaPictures\OS\184.png" referrerpolicy="no-referrer"></p></li></ul><hr /><h3 id='109-内存映射文件-memory-mapped-file'><span>10.9 内存映射文件 Memory Mapped File</span></h3><ul><li><p><span>定义</span></p><p><span>A memory-mapped file contains the contents of a file in virtual memory. This mapping between a file and memory space enables an application, including multiple processes, to modify the file by reading and writing directly to the memory</span></p></li><li><p><span>Traditional I/O involves explicit transfers between buffers in process address space to/from regions of a file</span></p><p><span>This involves multiple copies into caches in memory, plus system calls</span></p></li><li><p><span>Map the file directly into an empty region of our address space</span></p><ul><li><span>Implicitly "page it in" when we read it</span></li><li><span>Write it and "eventually" page it out</span></li></ul></li><li><p><span>Executable files are treated this way when we </span><code>exec</code><span> the process</span></p></li></ul><p><img src="D:\TyporaPictures\OS\185.png" referrerpolicy="no-referrer"></p><hr /><h3 id='1010-文件系统总结'><span>10.10 文件系统总结</span></h3><ul><li><p><span>File System</span></p><ul><li><span>Transforms blocks into Files and Directories</span></li><li><span>Optimize for size, access and usage patterns</span></li><li><span>Maximize sequential access, allow efficient random access</span></li></ul></li><li><p><span>File defined by header, called iNode</span></p></li><li><p><span>Naming: translating from user visible names to actual sys resources</span></p><ul><li><span>Directories used for naming for local file systems</span></li><li><span>Linked or tree structure stored in files</span></li></ul></li><li><p><span>Multilevel Indexed Scheme</span></p><ul><li><span>iNode contains file info, direct pointers to blocks, indirect blocks, doubly indirect, etc..</span></li><li><span>NTFS: variable extents not fixed blocks, tiny files data is in header</span></li></ul></li><li><p><span>File Allocation Table (FAT) Scheme</span></p><ul><li><span>Linked list approach</span></li><li><span>Very widely used: Cameras, USB drives, SD cards</span></li><li><span>Simple to implement, but poor performance and no security</span></li></ul></li><li><p><span>4.2 BSD Multilevel index files</span></p><ul><li><span>iNode contains pointers to actual blocks, indirect blocks, double indirect blocks, etc.</span></li><li><span>Optimizations for sequential access: start new files in open ranges of free blocks, rotational optimization</span></li></ul></li><li><p><span>File layout driven by freespace management</span></p><ul><li><span>Integrate freespace , iNode table, file blocks and dirs into block group</span></li></ul></li><li><p><span>Deep interactions between memory management, file system, sharing</span></p><ul><li><code>mmap()</code><span>: map file or anonymous segment to memory</span></li></ul></li></ul><hr /><h2 id='第十二章-大容量存储结构'><span>第十二章 大容量存储结构</span></h2><h3 id='121-大容量存储结构概述'><span>12.1 大容量存储结构概述</span></h3><h4 id='1211-磁盘-magnetic-disk-hard-disk'><span>12.1.1 磁盘 Magnetic Disk (Hard Disk)</span></h4><p><img src="D:\TyporaPictures\OS\120.png" referrerpolicy="no-referrer"></p><ul><li><p><span>盘片 Platter</span></p><ul><li><p><span>磁道 Track</span></p><p><span>每个 track 都有一个编号,一般 0 号指最外侧的 track</span></p><ul><li><p><span>扇区 Sector</span></p><p><span>Sector 是磁盘存数据的最小 unit</span></p><p><span>通常一个 sector 的 size 比一个 block 要小</span></p></li></ul></li></ul></li><li><p><span>柱面 Cylinder</span></p></li><li><p><span>磁臂 Disk Arm</span></p></li></ul><p><img src="D:\TyporaPictures\OS\119.png" referrerpolicy="no-referrer"></p><h4 id='1212-磁盘性能'><span>12.1.2 磁盘性能</span></h4><ul><li><p><span>每分钟转数 Rotation Per Minute (RPM)</span></p></li><li><p><span>传输速率 Transfer Rate</span></p><p><span>驱动器和计算机之间的数据流的速率</span></p></li><li><p><span>定位时间 (Positioning Time) 或随机访问时间 (Random Access Time)</span></p><ul><li><p><span>寻道时间 Seek Time</span></p><p><span>移动磁臂到柱面所需时间</span></p></li><li><p><span>旋转延迟 Rotational Latency</span></p><p><span>旋转磁臂到所要扇区所需的时间</span></p></li></ul></li><li><p><span>Data R/W Time</span></p><p><span>Positioning Time + Transfer Time (transfer a block of bits (sector) under r/w head)</span></p><p><span>= Seek Time + Rotational Latency + Transfer Time</span></p></li><li><p><span>Disk Latency</span></p><p><span>Queueing Time + Controller time + Seek Time + Rotation Lantency + Transfer Time</span></p></li><li><p><span>例:</span></p><ul><li><p><span>Assumptions</span></p><ul><li><span>Ignoring queuing and controller times for now</span></li><li><span>Avg seek time of 5 ms</span></li><li><span>7200 RPM: Time for rotation: 60000 (ms/min) / 7200 (rev/min)</span>
<span>~= 8 ms (转一圈 8 ms)</span></li><li><span>平均 Rotational Latency 为 0.5 * 8 = 4 ms</span></li><li><span>Transfer rate of 4 MB/s, sector size of 1 Kbyte</span>
<span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="59.516ex" height="2.928ex" viewBox="0 -932.9 25624.7 1260.8" role="img" focusable="false" style="vertical-align: -0.761ex;"><defs><path stroke-width="0" id="E123-MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path stroke-width="0" id="E123-MJMAIN-30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path><path stroke-width="0" id="E123-MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path stroke-width="0" id="E123-MJMAIN-34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z"></path><path stroke-width="0" id="E123-MJMATHI-62" d="M73 647Q73 657 77 670T89 683Q90 683 161 688T234 694Q246 694 246 685T212 542Q204 508 195 472T180 418L176 399Q176 396 182 402Q231 442 283 442Q345 442 383 396T422 280Q422 169 343 79T173 -11Q123 -11 82 27T40 150V159Q40 180 48 217T97 414Q147 611 147 623T109 637Q104 637 101 637H96Q86 637 83 637T76 640T73 647ZM336 325V331Q336 405 275 405Q258 405 240 397T207 376T181 352T163 330L157 322L136 236Q114 150 114 114Q114 66 138 42Q154 26 178 26Q211 26 245 58Q270 81 285 114T318 219Q336 291 336 325Z"></path><path stroke-width="0" id="E123-MJMATHI-79" d="M21 287Q21 301 36 335T84 406T158 442Q199 442 224 419T250 355Q248 336 247 334Q247 331 231 288T198 191T182 105Q182 62 196 45T238 27Q261 27 281 38T312 61T339 94Q339 95 344 114T358 173T377 247Q415 397 419 404Q432 431 462 431Q475 431 483 424T494 412T496 403Q496 390 447 193T391 -23Q363 -106 294 -155T156 -205Q111 -205 77 -183T43 -117Q43 -95 50 -80T69 -58T89 -48T106 -45Q150 -45 150 -87Q150 -107 138 -122T115 -142T102 -147L99 -148Q101 -153 118 -160T152 -167H160Q177 -167 186 -165Q219 -156 247 -127T290 -65T313 -9T321 21L315 17Q309 13 296 6T270 -6Q250 -11 231 -11Q185 -11 150 11T104 82Q103 89 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E123-MJMATHI-74" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path><path stroke-width="0" id="E123-MJMATHI-65" d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z"></path><path stroke-width="0" id="E123-MJMATHI-73" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path><path stroke-width="0" id="E123-MJMAIN-F7" d="M318 466Q318 500 339 518T386 537Q418 537 438 517T458 466Q458 438 440 417T388 396Q355 396 337 417T318 466ZM56 237T56 250T70 270H706Q721 262 721 250T706 230H70Q56 237 56 250ZM318 34Q318 68 339 86T386 105Q418 105 438 85T458 34Q458 6 440 -15T388 -36Q355 -36 337 -15T318 34Z"></path><path stroke-width="0" id="E123-MJMAIN-D7" d="M630 29Q630 9 609 9Q604 9 587 25T493 118L389 222L284 117Q178 13 175 11Q171 9 168 9Q160 9 154 15T147 29Q147 36 161 51T255 146L359 250L255 354Q174 435 161 449T147 471Q147 480 153 485T168 490Q173 490 175 489Q178 487 284 383L389 278L493 382Q570 459 587 475T609 491Q630 491 630 471Q630 464 620 453T522 355L418 250L522 145Q606 61 618 48T630 29Z"></path><path stroke-width="0" id="E123-MJMAIN-36" d="M42 313Q42 476 123 571T303 666Q372 666 402 630T432 550Q432 525 418 510T379 495Q356 495 341 509T326 548Q326 592 373 601Q351 623 311 626Q240 626 194 566Q147 500 147 364L148 360Q153 366 156 373Q197 433 263 433H267Q313 433 348 414Q372 400 396 374T435 317Q456 268 456 210V192Q456 169 451 149Q440 90 387 34T253 -22Q225 -22 199 -14T143 16T92 75T56 172T42 313ZM257 397Q227 397 205 380T171 335T154 278T148 216Q148 133 160 97T198 39Q222 21 251 21Q302 21 329 59Q342 77 347 104T352 209Q352 289 347 316T329 361Q302 397 257 397Z"></path><path stroke-width="0" id="E123-MJMAIN-28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path><path stroke-width="0" id="E123-MJMAIN-2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path><path stroke-width="0" id="E123-MJMATHI-63" d="M34 159Q34 268 120 355T306 442Q362 442 394 418T427 355Q427 326 408 306T360 285Q341 285 330 295T319 325T330 359T352 380T366 386H367Q367 388 361 392T340 400T306 404Q276 404 249 390Q228 381 206 359Q162 315 142 235T121 119Q121 73 147 50Q169 26 205 26H209Q321 26 394 111Q403 121 406 121Q410 121 419 112T429 98T420 83T391 55T346 25T282 0T202 -11Q127 -11 81 37T34 159Z"></path><path stroke-width="0" id="E123-MJMAIN-29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path><path stroke-width="0" id="E123-MJMAIN-3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path><path stroke-width="0" id="E123-MJMAIN-35" d="M164 157Q164 133 148 117T109 101H102Q148 22 224 22Q294 22 326 82Q345 115 345 210Q345 313 318 349Q292 382 260 382H254Q176 382 136 314Q132 307 129 306T114 304Q97 304 95 310Q93 314 93 485V614Q93 664 98 664Q100 666 102 666Q103 666 123 658T178 642T253 634Q324 634 389 662Q397 666 402 666Q410 666 410 648V635Q328 538 205 538Q174 538 149 544L139 546V374Q158 388 169 396T205 412T256 420Q337 420 393 355T449 201Q449 109 385 44T229 -22Q148 -22 99 32T50 154Q50 178 61 192T84 210T107 214Q132 214 148 197T164 157Z"></path><path stroke-width="0" id="E123-MJMAIN-2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path><path stroke-width="0" id="E123-MJMAIN-2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z"></path><path stroke-width="0" id="E123-MJMATHI-6D" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E123-MJMAIN-31"></use><use xlink:href="#E123-MJMAIN-30" x="500" y="0"></use><use xlink:href="#E123-MJMAIN-32" x="1000" y="0"></use><use xlink:href="#E123-MJMAIN-34" x="1500" y="0"></use><use xlink:href="#E123-MJMATHI-62" x="2000" y="0"></use><use xlink:href="#E123-MJMATHI-79" x="2429" y="0"></use><use xlink:href="#E123-MJMATHI-74" x="2926" y="0"></use><use xlink:href="#E123-MJMATHI-65" x="3287" y="0"></use><use xlink:href="#E123-MJMATHI-73" x="3753" y="0"></use><use xlink:href="#E123-MJMAIN-F7" x="4444" y="0"></use><use xlink:href="#E123-MJMAIN-34" x="5444" y="0"></use><use xlink:href="#E123-MJMAIN-D7" x="6166" y="0"></use><g transform="translate(7166,0)"><use xlink:href="#E123-MJMAIN-31"></use><use xlink:href="#E123-MJMAIN-30" x="500" y="0"></use><use transform="scale(0.707)" xlink:href="#E123-MJMAIN-36" x="1414" y="555"></use></g><use xlink:href="#E123-MJMAIN-28" x="8620" y="0"></use><use xlink:href="#E123-MJMATHI-62" x="9009" y="0"></use><use xlink:href="#E123-MJMATHI-79" x="9438" y="0"></use><use xlink:href="#E123-MJMATHI-74" x="9935" y="0"></use><use xlink:href="#E123-MJMATHI-65" x="10296" y="0"></use><use xlink:href="#E123-MJMATHI-73" x="10762" y="0"></use><use xlink:href="#E123-MJMAIN-2F" x="11231" y="0"></use><use xlink:href="#E123-MJMATHI-73" x="11731" y="0"></use><use xlink:href="#E123-MJMATHI-65" x="12200" y="0"></use><use xlink:href="#E123-MJMATHI-63" x="12666" y="0"></use><use xlink:href="#E123-MJMAIN-29" x="13099" y="0"></use><use xlink:href="#E123-MJMAIN-3D" x="13766" y="0"></use><g transform="translate(14821,0)"><use xlink:href="#E123-MJMAIN-32"></use><use xlink:href="#E123-MJMAIN-35" x="500" y="0"></use><use xlink:href="#E123-MJMAIN-36" x="1000" y="0"></use></g><use xlink:href="#E123-MJMAIN-D7" x="16544" y="0"></use><g transform="translate(17544,0)"><use xlink:href="#E123-MJMAIN-31"></use><use xlink:href="#E123-MJMAIN-30" x="500" y="0"></use><g transform="translate(1000,392)"><use transform="scale(0.707)" xlink:href="#E123-MJMAIN-2212" x="0" y="0"></use><use transform="scale(0.707)" xlink:href="#E123-MJMAIN-36" x="778" y="0"></use></g></g><use xlink:href="#E123-MJMATHI-73" x="19548" y="0"></use><use xlink:href="#E123-MJMATHI-65" x="20017" y="0"></use><use xlink:href="#E123-MJMATHI-63" x="20483" y="0"></use><use xlink:href="#E123-MJMAIN-3D" x="21443" y="0"></use><g transform="translate(22499,0)"><use xlink:href="#E123-MJMAIN-30"></use><use xlink:href="#E123-MJMAIN-2E" x="500" y="0"></use><use xlink:href="#E123-MJMAIN-32" x="778" y="0"></use><use xlink:href="#E123-MJMAIN-36" x="1278" y="0"></use></g><use xlink:href="#E123-MJMATHI-6D" x="24277" y="0"></use><use xlink:href="#E123-MJMATHI-73" x="25155" y="0"></use></g></svg></span><script type="math/tex">1024 bytes \div 4 × 10^6 (bytes/sec) = 256 × 10^{-6} sec ~= 0.26 ms</script></li></ul></li><li><p><span>Read sector from random place on disk</span></p><p><span>Seek Time + Rotational Latency + Transfer Time = 5 + 4 + 0.26 = 9.26 ms</span></p><p><span>Approx 10ms to fetch/put data: 100 KByte /sec</span></p></li><li><p><span>Read sector from random place in same cylinder</span></p><p><span>Rotational Latency + Transfer Time = 4 + 0.26 = 4.26 ms</span></p><p><span>Approx 5ms to fetch/put data: 200 KByte /sec</span></p></li><li><p><span>Read next sector on same track</span></p><p><span>Transfer Time = 0.26 ms</span></p><p><span>4 MByte /sec</span></p></li></ul></li><li><p><span>Typical Numbers for Magnetic Disk</span></p></li></ul><p><img src="D:\TyporaPictures\OS\121.png" referrerpolicy="no-referrer"></p><h4 id='1212-固态磁盘-solid-state-disk-ssd'><span>12.1.2 固态磁盘 Solid State Disk (SSD)</span></h4><p><img src="D:\TyporaPictures\OS\122.jpg" referrerpolicy="no-referrer"></p><hr /><h3 id='122-磁盘调度-disk-scheduling'><span>12.2 磁盘调度 Disk Scheduling</span></h3><ul><li><p><span>Given a sequence of access pages in the HDD</span></p><ul><li><span>98, 183, 37, 122, 14, 124, 65, 67</span></li><li><span>Head point (now): 53</span></li><li><span>Pages: 0 ~ 199</span></li></ul><p><span>How to minimize seek time?</span></p></li></ul><h4 id='1221-fcfs-调度'><span>12.2.1 FCFS 调度</span></h4><p><span>First come, first service</span></p><p><img src="D:\TyporaPictures\OS\122.png" referrerpolicy="no-referrer"></p><p><span>Total head movement distance = 640</span></p><h4 id='1222-sstf-调度'><span>12.2.2 SSTF 调度</span></h4><p><span>最短寻道时间优先 Shortest-Seek-Time-First</span></p><p><img src="D:\TyporaPictures\OS\123.png" referrerpolicy="no-referrer"></p><p><span>Total distance = 236</span></p><ul><li><span>可能会出现饥饿 (Starvation)</span></li></ul><h4 id='1223-scan-调度'><span>12.2.3 SCAN 调度</span></h4><ul><li><span>扫描算法 or 电梯算法 (elevator algorithm)</span></li><li><span>磁臂从磁盘的一头开始,向另一头移动,在移过每个柱面时处理请求;当到达另一端时,磁头移动方向反转并继续处理</span></li></ul><p><img src="D:\TyporaPictures\OS\124.png" referrerpolicy="no-referrer"></p><p><span>Total distance = 236</span></p><h4 id='1224-c-scan-调度'><span>12.2.4 C-SCAN 调度</span></h4><ul><li><span>循环扫描 (Circular SCAN)</span></li><li><span>磁头到另一端时,马上回到开头,不处理返回时的请求</span></li></ul><p><img src="D:\TyporaPictures\OS\125.png" referrerpolicy="no-referrer"></p><p><span>Total distance = 382</span></p><p><span>但是它的 avg wait time 比较小</span></p><h4 id='1225-look-与-c-look-调度'><span>12.2.5 LOOK 与 C-LOOK 调度</span></h4><ul><li><span>聪明点的 SCAN,不走到头了,走到最远的请求</span></li></ul><p><span>C-LOOK:</span></p><p><img src="D:\TyporaPictures\OS\126.png" referrerpolicy="no-referrer"></p><p><span>Total distance = 350</span></p><h4 id='1226-调度算法的选择'><span>12.2.6 调度算法的选择</span></h4><ul><li><span>SSTF is common and has a natural appeal</span></li><li><span>SCAN and C-SCAN perform better for systems that place a heavy load on the disk (less starvation)</span></li><li><span>Either SSTF or LOOK is a reasonable choice for the default algorithm</span></li><li><span>Performance depends on the number and types of requests</span></li><li><span>The disk scheduling algorithm should be written as a separate module of the operating system, allowing it to be replaced with a different algorithm if necessary</span></li></ul><hr /><h2 id='第十三章-io-系统'><span>第十三章 I/O 系统</span></h2><p><img src="D:\TyporaPictures\OS\110.png" referrerpolicy="no-referrer"></p><h3 id='131-io-硬件'><span>13.1 I/O 硬件</span></h3><p><img src="D:\TyporaPictures\OS\112.jpg" referrerpolicy="no-referrer"></p><ul><li><p><span>端口 Port</span></p><p><span>设备与计算机的通信连接点</span></p><ul><li><p><span>数据输入寄存器 Data-in Register</span></p><p><span>被主机读出以获取数据</span></p></li><li><p><span>数据输出寄存器 Data-out Register</span></p><p><span>被主机写入以发送数据</span></p></li><li><p><span>状态寄存器 Status Register</span></p><p><span>包含一些主机可以读取的位,表示一些状态,如当前命令是否已完成</span></p></li><li><p><span>控制寄存器 Control Register</span></p><p><span>可由主机写入,以便启动命令或更改设备模式</span></p></li></ul></li><li><p><span>总线 Bus</span></p><p><span>一组线路和通过线路传输信息的严格定义的一个协议</span></p><ul><li><p><span>PCI 总线</span></p></li><li><p><span>扩展总线 Expansion Bus</span></p></li><li><p><span>SCSI 总线</span></p><ul><li><span>小型计算机连接接口 Small Computer System Interface (SCSI)</span></li></ul></li></ul></li><li><p><span>控制器 Controller</span></p><p><span>可以操作端口、总线或者设备的一组电子器件</span></p><ul><li><p><span>磁盘控制器 Disk Controller</span></p><ul><li><span>串行高级技术连接 Serial Advanced Technology Attachment (SATA)</span></li></ul></li><li><p><span>SCSI 控制器</span></p><p><span>主机适配器 Host Adapter 或单独的电路板</span></p></li></ul></li></ul><p><span>以下为课件内容,我实在不知道该放在哪一节了</span></p><p><span>Operational Parameters for I/O</span></p><ul><li><p><span>Data granularity: Byte vs. Block</span></p><ul><li><span>Some devices provide single byte at a time (e.g., keyboard)</span></li><li><span>Others provide whole blocks (e.g., disks, networks, etc.)</span></li></ul></li><li><p><span>Access pattern: Sequential vs. Random</span></p><ul><li><p><span>Some devices must be accessed sequentially (e.g., tape)</span></p></li><li><p><span>Others can be accessed “randomly” (e.g., disk, cd, etc.)</span></p><ul><li><span>Fixed overhead to start transfers</span></li></ul></li><li><p><span>Some devices require continual monitoring</span></p></li><li><p><span>Others generate interrupts when they need service</span></p></li></ul></li><li><p><span>Transfer Mechanism: Programmed IO and DMA</span></p></li></ul><hr /><h3 id='132-cpu-访问-io-设备'><span>13.2 CPU 访问 I/O 设备</span></h3><ul><li><p><span>CPU 与 Contorller 交互</span></p><p><span>控制器有一个或多个寄存器,用于数据和控制信号</span></p><p><span>处理器通过读写这些寄存器的位模式来与控制器通信</span></p><ul><li><p><span>I/O Instuction</span></p><p><span>通过特殊 IO 指令针对 IO 端口地址传输一个字节或字</span></p><p><span>IO 指令触发总线线路,选择适当设备,并将位移入或移出设备寄存器</span></p></li><li><p><span>内存映射 (Memory Mapped) I/O</span></p><p><span>设备控制寄存器被映射到处理器的地址空间</span></p><p><span>处理器执行 IO 请求是通过标准数据传输指令读写映射到内存的设备控制器</span></p></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\113.png" referrerpolicy="no-referrer"></p><ul><li><p><span>例: Memory Mapped Display Controller</span></p><ul><li><p><span>Hardware maps control registers and display memory into physical address space</span></p><ul><li><span>Addresses set by HW jumpers or at boot time</span></li></ul></li><li><p><span>Simply writing to display memory (also called the "frame buffer") changes image on screen</span></p><ul><li><span>Addr: 0x8000F000 — 0x8000FFFF</span></li></ul></li><li><p><span>Writing graphics description to cmd queue</span></p><ul><li><span>Say enter a set of triangles describing some scene</span></li><li><span>Addr: 0x80010000 — 0x8001FFFF</span></li></ul></li><li><p><span>Writing to the command register may cause onboard graphics hardware to do something</span></p><ul><li><span>Say render the above scene</span></li><li><span>Addr: 0x0007F004</span></li></ul></li><li><p><span>Can protect with address translation</span></p></li></ul><p><img src="D:\TyporaPictures\OS\114.png" referrerpolicy="no-referrer"></p></li></ul><hr /><h3 id='133-控制器与-io-设备的数据传输'><span>13.3 控制器与 I/O 设备的数据传输</span></h3><ul><li><p><span>程序控制 (Programmed) I/O</span></p><ul><li><span>Each byte transferred via processor in/out or load/store</span></li><li><span>Pro: Simple hardware, easy to program</span></li><li><span>Con: Consumes processor cycles proportional to data size</span></li></ul></li><li><p><span>直接内存访问 Direct Memory Access (DMA)</span></p><ul><li><span>Give controller access to memory bus</span></li><li><span>Ask it to transfer data blocks to/from memory directly</span></li></ul></li></ul><p><img src="D:\TyporaPictures\OS\115.jpg" referrerpolicy="no-referrer"></p><hr /><h3 id='134-io-设备与-cpu-通信'><span>13.4 I/O 设备与 CPU 通信</span></h3><h4 id='1341-轮询-polling'><span>13.4.1 轮询 Polling</span></h4><ul><li><p><span>OS periodically checks a device specific status register</span></p><ul><li><span>I/O device puts completion information in status register</span></li></ul></li><li><p><span>Pro: low overhead</span></p></li><li><p><span>Con: may waste many cycles on polling if infrequent or unpredictable I/O operations</span></p></li></ul><p><span>主机与控制器之间的握手协调:</span></p><ol start='' ><li><span>主机重复读取忙位 (busy bit, 在 status register 里),直到该位清零</span></li><li><span>主机设置命令寄存器的写位,并写出一个字节到数据输入寄存器</span></li><li><span>主机设置命令就绪位</span></li><li><span>当控制器注意到命令就绪位已设置,则设置忙位</span></li><li><span>控制器读取命令寄存器,并看到写命令。它从数据输出寄存器中读取一个字节,并向设备执行 I/O 操作</span></li><li><span>控制器清除命令就绪位,清除状态寄存器的故障位表示设备 I/O 成功,清除忙位表示完成</span></li></ol><p><span>在步骤 1 中,主机一直处于忙等待 (busy waiting) 或轮询 (polling)。在该循环中,一直读取状态寄存器,直到忙位被清除</span></p><h4 id='1342-io-中断-io-interrupt'><span>13.4.2 I/O 中断 I/O Interrupt</span></h4><ul><li><span>Device generates an interrupt whenever it needs service</span></li><li><span>Pro: handles unpredictable events well</span></li><li><span>Con: interrupts relatively high overhead</span></li></ul><p><span>CPU 硬件有一条中断请求线 (Interrupt-Request Line, IRL)。CPU 在执行完每条指令后,都会检测 IRL。当 CPU 检测到控制器已在 IRL 上发出了一个信号时,CPU执行状态保存并且跳到内存固定位置的中断处理程序 (Interrupt Handler Routine)。中断处理程序确定中断原因,执行必要处理,执行状态恢复,并且执行返回中断指令以便 CPU 回到中断前的执行状态。</span></p><p><span>总结:设备控制器通过 IRL 发送信号从而引起 (raise) 中断,CPU捕获 (catch) 中断并且分派 (dispatch) 到中断处理程序,中断处理程序通过处理设备来清除 (clear) 中断。</span></p><p><img src="D:\TyporaPictures\OS\116.jpg" referrerpolicy="no-referrer"></p><ul><li><p><span>中断请求线 IRL: 两条</span></p><ul><li><p><span>非屏蔽中断 (Nonmaskable Interrupt)</span></p><p><span>保留用于诸如不可恢复的内存错误等事件</span></p></li><li><p><span>可屏蔽中断 (Maskable Interrupt)</span></p><p><span>在执行不得中断的关键指令序列之前,它可以由 CPU 关闭。可屏蔽中断可由设备控制器用来请求服务</span></p></li></ul></li><li><p><span>中断向量 Interrupt Vector</span></p><p><span>是一个地址,根据这个地址+偏移量来选择特定的中断处理程序</span></p></li><li><p><span>中断优先级 Interrupt Priority Level</span></p></li><li><p><span>Top-half/bottom-half interrupt architecture</span></p><p><a href='https://stackoverflow.com/questions/45095735/top-halves-and-bottom-halves-concept-clarification' target='_blank' class='url'>https://stackoverflow.com/questions/45095735/top-halves-and-bottom-halves-concept-clarification</a></p><p><a href='https://www.tutorialsdaddy.com/uncategorized/top-half-vs-bottom-half/' target='_blank' class='url'>https://www.tutorialsdaddy.com/uncategorized/top-half-vs-bottom-half/</a></p><ul><li><p><span>上半部 Top Half Handler (硬中断)</span></p><p><span>快速处理中断,它在中断禁止模式下运行,主要处理跟硬件紧密相关的或时间敏感的工作</span></p></li><li><p><span>下半部 Bottom Half Handler (软中断)</span></p><p><span>延迟处理上半部未完成的工作,通常以内核线程的方式运行</span></p></li></ul></li><li><p><span>设备驱动 Device Driver</span></p><p><a href='http://www.differencebetween.net/technology/difference-between-device-driver-and-device-controller' target='_blank' class='url'>http://www.differencebetween.net/technology/difference-between-device-driver-and-device-controller</a></p><p><span>Device driver is a specialized software program running as part of the operating system that interacts with a device attached to a computer. It is just a code inside the OS that allows to be empowered with the specific commands needed to operate the associated device. </span></p><ul><li><span>Supports a standard, internal interface</span></li><li><span>Same kernel I/O system can interact easily with different device drivers</span></li><li><span>Special device specific configuration supported with the </span><code>ioctl</code><span> system call</span></li></ul></li></ul><h3 id='135-io-请求生命周期'><span>13.5 I/O 请求生命周期</span></h3><p><img src="D:\TyporaPictures\OS\117.jpg" referrerpolicy="no-referrer"></p><hr /><h3 id='136-io-性能'><span>13.6 I/O 性能</span></h3><ul><li><p><span>Response Time or Latency</span></p><p><span>Time to perform an operation(s)</span></p><p><span>Response Time = Queue + I/O device service time</span></p></li><li><p><span>Bandwidth or Throughput</span></p><p><span>Rate at which operations are performed (op/s)</span></p><ul><li><p><span>Files: MB/s, Networks: Mb/s, Arithmetic: GFLOP/s</span></p></li><li><p><span>Effective BW per op = transfer size / response time</span></p><p><span>EffBW(n) = n / (S + n/B) = B / (1 + SB/n)</span></p></li></ul></li><li><p><span>Start up or "Overhead"</span></p><p><span>Time to initiate an operation</span></p><ul><li><p><span>Syscall overhead</span></p></li><li><p><span>Operating system processing</span></p></li><li><p><span>Controller Overhead</span></p></li><li><p><span>Device Startup</span></p><ul><li><span>Mechanical latency for a disk</span></li><li><span>Media Access + Speed of light + Routing for network</span></li></ul></li><li><p><span>Queuing</span></p></li></ul></li><li><p><span>Most I/O operations are roughly linear in </span><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.394ex" height="1.415ex" viewBox="0 -514 600 609.1" role="img" focusable="false" style="vertical-align: -0.221ex;"><defs><path stroke-width="0" id="E124-MJMATHI-6E" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E124-MJMATHI-6E" x="0" y="0"></use></g></svg></span><script type="math/tex">n</script><span> bytes</span></p><p><span class="MathJax_SVG" tabindex="-1" style="font-size: 100%; display: inline-block;"><svg xmlns:xlink="http://www.w3.org/1999/xlink" width="48.86ex" height="2.712ex" viewBox="0 -839.8 21037 1167.7" role="img" focusable="false" style="vertical-align: -0.761ex;"><defs><path stroke-width="0" id="E125-MJMATHI-4C" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path><path stroke-width="0" id="E125-MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z"></path><path stroke-width="0" id="E125-MJMATHI-74" d="M26 385Q19 392 19 395Q19 399 22 411T27 425Q29 430 36 430T87 431H140L159 511Q162 522 166 540T173 566T179 586T187 603T197 615T211 624T229 626Q247 625 254 615T261 596Q261 589 252 549T232 470L222 433Q222 431 272 431H323Q330 424 330 420Q330 398 317 385H210L174 240Q135 80 135 68Q135 26 162 26Q197 26 230 60T283 144Q285 150 288 151T303 153H307Q322 153 322 145Q322 142 319 133Q314 117 301 95T267 48T216 6T155 -11Q125 -11 98 4T59 56Q57 64 57 83V101L92 241Q127 382 128 383Q128 385 77 385H26Z"></path><path stroke-width="0" id="E125-MJMATHI-65" d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z"></path><path stroke-width="0" id="E125-MJMATHI-6E" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E125-MJMATHI-63" d="M34 159Q34 268 120 355T306 442Q362 442 394 418T427 355Q427 326 408 306T360 285Q341 285 330 295T319 325T330 359T352 380T366 386H367Q367 388 361 392T340 400T306 404Q276 404 249 390Q228 381 206 359Q162 315 142 235T121 119Q121 73 147 50Q169 26 205 26H209Q321 26 394 111Q403 121 406 121Q410 121 419 112T429 98T420 83T391 55T346 25T282 0T202 -11Q127 -11 81 37T34 159Z"></path><path stroke-width="0" id="E125-MJMATHI-79" d="M21 287Q21 301 36 335T84 406T158 442Q199 442 224 419T250 355Q248 336 247 334Q247 331 231 288T198 191T182 105Q182 62 196 45T238 27Q261 27 281 38T312 61T339 94Q339 95 344 114T358 173T377 247Q415 397 419 404Q432 431 462 431Q475 431 483 424T494 412T496 403Q496 390 447 193T391 -23Q363 -106 294 -155T156 -205Q111 -205 77 -183T43 -117Q43 -95 50 -80T69 -58T89 -48T106 -45Q150 -45 150 -87Q150 -107 138 -122T115 -142T102 -147L99 -148Q101 -153 118 -160T152 -167H160Q177 -167 186 -165Q219 -156 247 -127T290 -65T313 -9T321 21L315 17Q309 13 296 6T270 -6Q250 -11 231 -11Q185 -11 150 11T104 82Q103 89 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E125-MJMAIN-28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path><path stroke-width="0" id="E125-MJMAIN-29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path><path stroke-width="0" id="E125-MJMAIN-3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z"></path><path stroke-width="0" id="E125-MJMATHI-4F" d="M740 435Q740 320 676 213T511 42T304 -22Q207 -22 138 35T51 201Q50 209 50 244Q50 346 98 438T227 601Q351 704 476 704Q514 704 524 703Q621 689 680 617T740 435ZM637 476Q637 565 591 615T476 665Q396 665 322 605Q242 542 200 428T157 216Q157 126 200 73T314 19Q404 19 485 98T608 313Q637 408 637 476Z"></path><path stroke-width="0" id="E125-MJMATHI-76" d="M173 380Q173 405 154 405Q130 405 104 376T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Q21 294 29 316T53 368T97 419T160 441Q202 441 225 417T249 361Q249 344 246 335Q246 329 231 291T200 202T182 113Q182 86 187 69Q200 26 250 26Q287 26 319 60T369 139T398 222T409 277Q409 300 401 317T383 343T365 361T357 383Q357 405 376 424T417 443Q436 443 451 425T467 367Q467 340 455 284T418 159T347 40T241 -11Q177 -11 139 22Q102 54 102 117Q102 148 110 181T151 298Q173 362 173 380Z"></path><path stroke-width="0" id="E125-MJMATHI-72" d="M21 287Q22 290 23 295T28 317T38 348T53 381T73 411T99 433T132 442Q161 442 183 430T214 408T225 388Q227 382 228 382T236 389Q284 441 347 441H350Q398 441 422 400Q430 381 430 363Q430 333 417 315T391 292T366 288Q346 288 334 299T322 328Q322 376 378 392Q356 405 342 405Q286 405 239 331Q229 315 224 298T190 165Q156 25 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 114 189T154 366Q154 405 128 405Q107 405 92 377T68 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path><path stroke-width="0" id="E125-MJMATHI-68" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z"></path><path stroke-width="0" id="E125-MJMATHI-64" d="M366 683Q367 683 438 688T511 694Q523 694 523 686Q523 679 450 384T375 83T374 68Q374 26 402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487H491Q506 153 506 145Q506 140 503 129Q490 79 473 48T445 8T417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157Q33 205 53 255T101 341Q148 398 195 420T280 442Q336 442 364 400Q369 394 369 396Q370 400 396 505T424 616Q424 629 417 632T378 637H357Q351 643 351 645T353 664Q358 683 366 683ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z"></path><path stroke-width="0" id="E125-MJMAIN-2B" d="M56 237T56 250T70 270H369V420L370 570Q380 583 389 583Q402 583 409 568V270H707Q722 262 722 250T707 230H409V-68Q401 -82 391 -82H389H387Q375 -82 369 -68V230H70Q56 237 56 250Z"></path><path stroke-width="0" id="E125-MJMAIN-2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path><path stroke-width="0" id="E125-MJMATHI-54" d="M40 437Q21 437 21 445Q21 450 37 501T71 602L88 651Q93 669 101 677H569H659Q691 677 697 676T704 667Q704 661 687 553T668 444Q668 437 649 437Q640 437 637 437T631 442L629 445Q629 451 635 490T641 551Q641 586 628 604T573 629Q568 630 515 631Q469 631 457 630T439 622Q438 621 368 343T298 60Q298 48 386 46Q418 46 427 45T436 36Q436 31 433 22Q429 4 424 1L422 0Q419 0 415 0Q410 0 363 1T228 2Q99 2 64 0H49Q43 6 43 9T45 27Q49 40 55 46H83H94Q174 46 189 55Q190 56 191 56Q196 59 201 76T241 233Q258 301 269 344Q339 619 339 625Q339 630 310 630H279Q212 630 191 624Q146 614 121 583T67 467Q60 445 57 441T43 437H40Z"></path><path stroke-width="0" id="E125-MJMATHI-73" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path><path stroke-width="0" id="E125-MJMATHI-66" d="M118 -162Q120 -162 124 -164T135 -167T147 -168Q160 -168 171 -155T187 -126Q197 -99 221 27T267 267T289 382V385H242Q195 385 192 387Q188 390 188 397L195 425Q197 430 203 430T250 431Q298 431 298 432Q298 434 307 482T319 540Q356 705 465 705Q502 703 526 683T550 630Q550 594 529 578T487 561Q443 561 443 603Q443 622 454 636T478 657L487 662Q471 668 457 668Q445 668 434 658T419 630Q412 601 403 552T387 469T380 433Q380 431 435 431Q480 431 487 430T498 424Q499 420 496 407T491 391Q489 386 482 386T428 385H372L349 263Q301 15 282 -47Q255 -132 212 -173Q175 -205 139 -205Q107 -205 81 -186T55 -132Q55 -95 76 -78T118 -61Q162 -61 162 -103Q162 -122 151 -136T127 -157L118 -162Z"></path><path stroke-width="0" id="E125-MJMATHI-43" d="M50 252Q50 367 117 473T286 641T490 704Q580 704 633 653Q642 643 648 636T656 626L657 623Q660 623 684 649Q691 655 699 663T715 679T725 690L740 705H746Q760 705 760 698Q760 694 728 561Q692 422 692 421Q690 416 687 415T669 413H653Q647 419 647 422Q647 423 648 429T650 449T651 481Q651 552 619 605T510 659Q484 659 454 652T382 628T299 572T226 479Q194 422 175 346T156 222Q156 108 232 58Q280 24 350 24Q441 24 512 92T606 240Q610 253 612 255T628 257Q648 257 648 248Q648 243 647 239Q618 132 523 55T319 -22Q206 -22 128 53T50 252Z"></path><path stroke-width="0" id="E125-MJMATHI-70" d="M23 287Q24 290 25 295T30 317T40 348T55 381T75 411T101 433T134 442Q209 442 230 378L240 387Q302 442 358 442Q423 442 460 395T497 281Q497 173 421 82T249 -10Q227 -10 210 -4Q199 1 187 11T168 28L161 36Q160 35 139 -51T118 -138Q118 -144 126 -145T163 -148H188Q194 -155 194 -157T191 -175Q188 -187 185 -190T172 -194Q170 -194 161 -194T127 -193T65 -192Q-5 -192 -24 -194H-32Q-39 -187 -39 -183Q-37 -156 -26 -148H-6Q28 -147 33 -136Q36 -130 94 103T155 350Q156 355 156 364Q156 405 131 405Q109 405 94 377T71 316T59 280Q57 278 43 278H29Q23 284 23 287ZM178 102Q200 26 252 26Q282 26 310 49T356 107Q374 141 392 215T411 325V331Q411 405 350 405Q339 405 328 402T306 393T286 380T269 365T254 350T243 336T235 326L232 322Q232 321 229 308T218 264T204 212Q178 106 178 102Z"></path><path stroke-width="0" id="E125-MJMATHI-69" d="M184 600Q184 624 203 642T247 661Q265 661 277 649T290 619Q290 596 270 577T226 557Q211 557 198 567T184 600ZM21 287Q21 295 30 318T54 369T98 420T158 442Q197 442 223 419T250 357Q250 340 236 301T196 196T154 83Q149 61 149 51Q149 26 166 26Q175 26 185 29T208 43T235 78T260 137Q263 149 265 151T282 153Q302 153 302 143Q302 135 293 112T268 61T223 11T161 -11Q129 -11 102 10T74 74Q74 91 79 106T122 220Q160 321 166 341T173 380Q173 404 156 404H154Q124 404 99 371T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Z"></path></defs><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><use xlink:href="#E125-MJMATHI-4C" x="0" y="0"></use><use xlink:href="#E125-MJMATHI-61" x="681" y="0"></use><use xlink:href="#E125-MJMATHI-74" x="1210" y="0"></use><use xlink:href="#E125-MJMATHI-65" x="1571" y="0"></use><use xlink:href="#E125-MJMATHI-6E" x="2037" y="0"></use><use xlink:href="#E125-MJMATHI-63" x="2637" y="0"></use><use xlink:href="#E125-MJMATHI-79" x="3070" y="0"></use><use xlink:href="#E125-MJMAIN-28" x="3567" y="0"></use><use xlink:href="#E125-MJMATHI-6E" x="3956" y="0"></use><use xlink:href="#E125-MJMAIN-29" x="4556" y="0"></use><use xlink:href="#E125-MJMAIN-3D" x="5222" y="0"></use><use xlink:href="#E125-MJMATHI-4F" x="6278" y="0"></use><use xlink:href="#E125-MJMATHI-76" x="7041" y="0"></use><use xlink:href="#E125-MJMATHI-65" x="7526" y="0"></use><use xlink:href="#E125-MJMATHI-72" x="7992" y="0"></use><use xlink:href="#E125-MJMATHI-68" x="8443" y="0"></use><use xlink:href="#E125-MJMATHI-65" x="9019" y="0"></use><use xlink:href="#E125-MJMATHI-61" x="9485" y="0"></use><use xlink:href="#E125-MJMATHI-64" x="10014" y="0"></use><use xlink:href="#E125-MJMAIN-2B" x="10759" y="0"></use><use xlink:href="#E125-MJMATHI-6E" x="11760" y="0"></use><use xlink:href="#E125-MJMAIN-2F" x="12360" y="0"></use><use xlink:href="#E125-MJMATHI-54" x="12860" y="0"></use><use xlink:href="#E125-MJMATHI-72" x="13564" y="0"></use><use xlink:href="#E125-MJMATHI-61" x="14015" y="0"></use><use xlink:href="#E125-MJMATHI-6E" x="14544" y="0"></use><use xlink:href="#E125-MJMATHI-73" x="15144" y="0"></use><use xlink:href="#E125-MJMATHI-66" x="15613" y="0"></use><use xlink:href="#E125-MJMATHI-65" x="16163" y="0"></use><use xlink:href="#E125-MJMATHI-72" x="16629" y="0"></use><use xlink:href="#E125-MJMATHI-43" x="17080" y="0"></use><use xlink:href="#E125-MJMATHI-61" x="17840" y="0"></use><use xlink:href="#E125-MJMATHI-70" x="18369" y="0"></use><use xlink:href="#E125-MJMATHI-61" x="18872" y="0"></use><use xlink:href="#E125-MJMATHI-63" x="19401" y="0"></use><use xlink:href="#E125-MJMATHI-69" x="19834" y="0"></use><use xlink:href="#E125-MJMATHI-74" x="20179" y="0"></use><use xlink:href="#E125-MJMATHI-79" x="20540" y="0"></use></g></svg></span><script type="math/tex">Latency(n) = Overhead + n/ TransferCapacity</script></p></li><li><p><span>例: Fast Network</span></p><p><img src="D:\TyporaPictures\OS\118.png" referrerpolicy="no-referrer"></p></li><li><p><span>影响最大带宽的因素</span></p><ul><li><p><span>Bus speed</span></p><ul><li><span>PCI X: 1064 MB/s = 133 MHz x 64 bit (per lane)</span></li><li><span>ULTRA WIDE SCSI: 40 MB/s</span></li><li><span>Serial Attached SCSI & Serial ATA & IEEE 1394 ( firewire ): 1.6 Gb/s full duplex (200 MB/s)</span></li><li><span>USB 3.0: 5 Gb/s</span></li><li><span>Thunderbolt 3: 40 Gb/s</span></li></ul></li><li><p><span>Device transfer bandwidth</span></p><ul><li><span>Rotational speed of disk</span></li><li><span>Write / Read rate of NAND flash</span></li><li><span>Signaling rate of network link</span></li></ul></li></ul></li></ul><hr /><h2 id='end'><span>END</span></h2><p><img src="D:\TyporaPictures\OS\186.png" referrerpolicy="no-referrer"></p></div></div>
</body>
</html>