-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1265 lines (831 loc) · 370 KB
/
index.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge" >
<link rel="dns-prefetch" href="https://mowang111.github.io">
<title>crz's blog</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta property="og:type" content="website">
<meta property="og:title" content="crz's blog">
<meta property="og:url" content="https://mowang111.github.io/index.html">
<meta property="og:site_name" content="crz's blog">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="crz">
<meta name="twitter:card" content="summary">
<link rel="alternative" href="/atom.xml" title="crz's blog" type="application/atom+xml">
<link rel="icon" href="https://cdn.acwing.com/media/user/profile/photo/55242_lg_99e1edfd6a.jpeg">
<link rel="stylesheet" type="text/css" href="/./main.0cf68a.css">
<style type="text/css">
#container.show {
background: linear-gradient(200deg,#a0cfe4,#e8c37e);
}
</style>
<meta name="generator" content="Hexo 5.4.2"></head>
<body>
<div id="container" q-class="show:isCtnShow">
<canvas id="anm-canvas" class="anm-canvas"></canvas>
<div class="left-col" q-class="show:isShow">
<div class="overlay" style="background: #4d4d4d"></div>
<div class="intrude-less">
<header id="header" class="inner">
<a href="/" class="profilepic">
<img src="https://cdn.acwing.com/media/user/profile/photo/55242_lg_99e1edfd6a.jpeg" class="js-avatar">
</a>
<hgroup>
<h1 class="header-author"><a href="/"></a></h1>
</hgroup>
<nav class="header-menu">
<ul>
<li><a href="/">主页</a></li>
</ul>
</nav>
<nav class="header-smart-menu">
<a q-on="click: openSlider(e, 'innerArchive')" href="javascript:void(0)">所有文章</a>
<a q-on="click: openSlider(e, 'friends')" href="javascript:void(0)">友链</a>
<a q-on="click: openSlider(e, 'aboutme')" href="javascript:void(0)">关于我</a>
</nav>
<nav class="header-nav">
<div class="social">
<a class="github" target="_blank" href="https://github.com/mowang111" title="github"><i class="icon-github"></i></a>
<a class="mail" target="_blank" href="/rzchen@zju.edu.cn" title="mail"><i class="icon-mail"></i></a>
</div>
</nav>
</header>
</div>
</div>
<div class="mid-col" q-class="show:isShow,hide:isShow|isFalse">
<nav id="mobile-nav">
<div class="overlay js-overlay" style="background: #4d4d4d"></div>
<div class="btnctn js-mobile-btnctn">
<div class="slider-trigger list" q-on="click: openSlider(e)"><i class="icon icon-sort"></i></div>
</div>
<div class="intrude-less">
<header id="header" class="inner">
<div class="profilepic">
<img src="https://cdn.acwing.com/media/user/profile/photo/55242_lg_99e1edfd6a.jpeg" class="js-avatar">
</div>
<hgroup>
<h1 class="header-author js-header-author"></h1>
</hgroup>
<nav class="header-nav">
<div class="social">
<a class="github" target="_blank" href="https://github.com/mowang111" title="github"><i class="icon-github"></i></a>
<a class="mail" target="_blank" href="/rzchen@zju.edu.cn" title="mail"><i class="icon-mail"></i></a>
</div>
</nav>
<nav class="header-menu js-header-menu">
<ul style="width: 50%">
<li style="width: 100%"><a href="/">主页</a></li>
</ul>
</nav>
</header>
</div>
<div class="mobile-mask" style="display:none" q-show="isShow"></div>
</nav>
<div id="wrapper" class="body-wrap">
<div class="menu-l">
<div class="canvas-wrap">
<canvas data-colors="#eaeaea" data-sectionHeight="100" data-contentId="js-content" id="myCanvas1" class="anm-canvas"></canvas>
</div>
<div id="js-content" class="content-ll">
<article id="post-哈工大操作系统实验/6.地址映射与共享" class="article article-type-post article-index" itemscope itemprop="blogPost">
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/6.%E5%9C%B0%E5%9D%80%E6%98%A0%E5%B0%84%E4%B8%8E%E5%85%B1%E4%BA%AB/">6.地址映射与共享</a>
</h1>
<a href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/6.%E5%9C%B0%E5%9D%80%E6%98%A0%E5%B0%84%E4%B8%8E%E5%85%B1%E4%BA%AB/" class="archive-article-date">
<time datetime="2024-07-22T06:41:31.581Z" itemprop="datePublished"><i class="icon-calendar icon"></i>2024-07-22</time>
</a>
</header>
<div class="article-entry" itemprop="articleBody">
<p>本次实验的基本内容是:</p>
<ul>
<li><input checked="" disabled="" type="checkbox"> 用Bochs调试工具跟踪Linux 0.11的地址翻译(地址映射)过程,了解IA-32和Linux 0.11的内存管理机制;</li>
<li><input checked="" disabled="" type="checkbox"> 在Ubuntu上编写多进程的生产者—消费者程序,用共享内存做缓冲区;</li>
<li><input checked="" disabled="" type="checkbox"> 在信号量实验的基础上,为Linux 0.11增加共享内存功能,并将生产者—消费者程序移植到Linux 0.11。</li>
</ul>
<p>![[Linux 0.11地址翻译调试.png]]</p>
<p>producer.c</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdlib.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><sys/shm.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><sys/stat.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><fcntl.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><semaphore.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">const</span> <span class="type">int</span> MAX_BUFFER = <span class="number">10</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> N = <span class="number">500</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*semaphore*/</span></span><br><span class="line"><span class="type">sem_t</span> *empty, *full, *mutex;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*shared memery*/</span></span><br><span class="line"><span class="type">int</span> shmid;</span><br><span class="line"><span class="type">key_t</span> key = <span class="number">1234</span>;</span><br><span class="line"><span class="type">int</span> *shm;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">producer</span><span class="params">()</span>{</span><br><span class="line"> <span class="type">int</span> i;</span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < N; i++){</span><br><span class="line"> sem_wait(empty);</span><br><span class="line"> sem_wait(mutex);</span><br><span class="line"> shm[i] = i;</span><br><span class="line"> sem_post(mutex);</span><br><span class="line"> sem_post(full);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>{</span><br><span class="line"> <span class="comment">/*create shared memory*/</span></span><br><span class="line"> shmid = shmget(key, <span class="keyword">sizeof</span>(<span class="type">int</span>)*N, <span class="number">0666</span>);</span><br><span class="line"> <span class="keyword">if</span>(shmid < <span class="number">0</span>){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"shmget error!\n"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> shm = shmat(shmid, <span class="literal">NULL</span>, <span class="number">0</span>);</span><br><span class="line"> <span class="keyword">if</span>(shm == (<span class="type">int</span> *)<span class="number">-1</span>){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"shm error!\n"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/*initialize semaphore*/</span></span><br><span class="line"> <span class="comment">/*empty = sem_open("myempty", O_CREAT, 0666, MAX_BUFFER);</span></span><br><span class="line"><span class="comment"> full = sem_open("myfull", O_CREAT, 0666, 0);</span></span><br><span class="line"><span class="comment"> mutex = sem_open("mymutex", O_CREAT, 0666, 1);*/</span></span><br><span class="line"> empty = sem_open(<span class="string">"myempty"</span>, MAX_BUFFER);</span><br><span class="line"> full = sem_open(<span class="string">"myfull"</span>, <span class="number">0</span>);</span><br><span class="line"> mutex = sem_open(<span class="string">"mymutex"</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"> producer();</span><br><span class="line"></span><br><span class="line"> sem_unlink(<span class="string">"myempty"</span>);</span><br><span class="line"> sem_unlink(<span class="string">"myfull"</span>);</span><br><span class="line"> sem_unlink(<span class="string">"mymutex"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>consumer.c</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdlib.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><sys/shm.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><sys/stat.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><fcntl.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><semaphore.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">const</span> <span class="type">int</span> MAX_BUFFER = <span class="number">10</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> N = <span class="number">500</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*semaphore*/</span></span><br><span class="line"><span class="type">sem_t</span> *empty, *full, *mutex;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*shared memery*/</span></span><br><span class="line"><span class="type">int</span> shmid;</span><br><span class="line"><span class="type">key_t</span> key = <span class="number">1234</span>;</span><br><span class="line"><span class="type">int</span> *shm;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">consumer</span><span class="params">()</span>{</span><br><span class="line"> <span class="type">int</span> i;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"entering consumer\n"</span>);</span><br><span class="line"> fflush(<span class="built_in">stdout</span>);</span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < N; i++){</span><br><span class="line"> sem_wait(full);</span><br><span class="line"> sem_wait(mutex);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, shm[i]);</span><br><span class="line"> fflush(<span class="built_in">stdout</span>);</span><br><span class="line"> sem_post(mutex);</span><br><span class="line"> sem_post(empty);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>{</span><br><span class="line"> <span class="comment">/*create shared memory*/</span></span><br><span class="line"> shmid = shmget(key, <span class="keyword">sizeof</span>(<span class="type">int</span>)*N, <span class="number">0666</span>);</span><br><span class="line"> <span class="keyword">if</span>(shmid < <span class="number">0</span>){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"shmget error!\n"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> shm = shmat(shmid, <span class="literal">NULL</span>, <span class="number">0</span>);</span><br><span class="line"> <span class="keyword">if</span>(shm == (<span class="type">int</span> *)<span class="number">-1</span>){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"shm error!\n"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/*initialize semaphore*/</span></span><br><span class="line"> <span class="comment">/*empty = sem_open("myempty", O_CREAT, 0666, MAX_BUFFER);</span></span><br><span class="line"><span class="comment"> full = sem_open("myfull", O_CREAT, 0666, 0);</span></span><br><span class="line"><span class="comment"> mutex = sem_open("mymutex", O_CREAT, 0666, 1);*/</span></span><br><span class="line"> empty = sem_open(<span class="string">"myempty"</span>, MAX_BUFFER);</span><br><span class="line"> full = sem_open(<span class="string">"myfull"</span>, <span class="number">0</span>);</span><br><span class="line"> mutex = sem_open(<span class="string">"mymutex"</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"> consumer();</span><br><span class="line"></span><br><span class="line"> sem_unlink(<span class="string">"myempty"</span>);</span><br><span class="line"> sem_unlink(<span class="string">"myfull"</span>);</span><br><span class="line"> sem_unlink(<span class="string">"mymutex"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="增加共享内存"><a href="#增加共享内存" class="headerlink" title="增加共享内存"></a>增加共享内存</h2><ol>
<li>增加系统调用<code>shm_get</code>和<code>shm_at</code></li>
<li>找到一段空闲的物理页面</li>
<li>将这段物理页面映射到进程的线性空间中<br>shm.h<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> __LIBRARY__</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><unistd.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> _LINUX_SHM_H</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> _LINUX_SHM_H</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><sys/types.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><linux/sched.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><linux/mm.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">int</span> <span class="type">key_t</span>;</span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">shmid_ds</span> <span class="title">shmid_ds</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAX_SHM_NUM 32</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*shmid map physic address */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">shmid_ds</span> {</span></span><br><span class="line"> <span class="type">key_t</span> key;</span><br><span class="line"> <span class="type">int</span> shmid;</span><br><span class="line"> <span class="type">int</span> size;</span><br><span class="line"> <span class="type">unsigned</span> <span class="type">long</span> addr;</span><br><span class="line"> <span class="type">int</span> is_used;</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">shmid_ds shm_table[MAX_SHM_NUM];</span><br><span class="line"></span><br><span class="line">_syscall3(<span class="type">int</span>, shmget, <span class="type">key_t</span>, key, <span class="type">size_t</span>, size, <span class="type">int</span>, shmflg);</span><br><span class="line">_syscall3(<span class="type">void</span> *, shmat, <span class="type">int</span>, shmid, <span class="type">unsigned</span> <span class="type">long</span>, addr, <span class="type">int</span>, shmflg);</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure></li>
</ol>
<p>shm.c</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><sys/shm.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">sys_shmget</span><span class="params">(<span class="type">key_t</span> key, <span class="type">size_t</span> size, <span class="type">int</span> shmflg)</span>{</span><br><span class="line"> <span class="type">int</span> i;</span><br><span class="line"> <span class="comment">/*judge if size > a page*/</span></span><br><span class="line"> <span class="keyword">if</span>(size > PAGE_SIZE){</span><br><span class="line"> <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/*find if the key is already in the shm_table*/</span></span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < MAX_SHM_NUM; i++){</span><br><span class="line"> <span class="keyword">if</span>(shm_table[i].key == key){</span><br><span class="line"> add_memmap(shm_table[i].addr);</span><br><span class="line"> <span class="keyword">return</span> shm_table[i].shmid;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/*find a free shmid*/</span></span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < MAX_SHM_NUM; i++){</span><br><span class="line"> <span class="keyword">if</span>(!shm_table[i].is_used){</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> shm_table[i].key = key;</span><br><span class="line"> shm_table[i].shmid = i;</span><br><span class="line"> shm_table[i].size = size;</span><br><span class="line"> shm_table[i].addr = (<span class="type">int</span> *)get_free_page();</span><br><span class="line"> shm_table[i].is_used = <span class="number">1</span>;</span><br><span class="line"> printk(<span class="string">"shm key: %d, phyaddr: %ld\n"</span>, key, shm_table[i].addr);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> i;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> * <span class="title function_">sys_shmat</span><span class="params">(<span class="type">int</span> shmid, <span class="type">unsigned</span> <span class="type">long</span> addr, <span class="type">int</span> shmflg)</span>{</span><br><span class="line"> <span class="type">unsigned</span> <span class="type">long</span> tmp;</span><br><span class="line"> <span class="keyword">if</span>(shmid > MAX_SHM_NUM || !shm_table[shmid].is_used){</span><br><span class="line"> <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(!addr){</span><br><span class="line"> <span class="comment">/*find a page virtual page from data segment*/</span></span><br><span class="line"> tmp = get_base(current->ldt[<span class="number">1</span>]) + current->brk;</span><br><span class="line"> put_page(shm_table[shmid].addr, tmp);</span><br><span class="line"> printk(<span class="string">"phyadr: %lu has been maped to virsual adr: %lu\n"</span>, shm_table[shmid].addr, tmp);</span><br><span class="line"> <span class="keyword">return</span> current->brk;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>另外memory.c中需要增加一个小函数,用来修改<code>mem_map</code></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">add_memmap</span><span class="params">(<span class="type">unsigned</span> <span class="type">long</span> addr)</span>{</span><br><span class="line"> mem_map[(addr-LOW_MEM) >> <span class="number">12</span>]++;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>实验结果:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">...</span><br><span class="line">499</span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<i class="icon-price-tags icon"></i>
<ul class="article-tag-list">
<li class="article-tag-list-item">
<a href="javascript:void(0)" class="js-tag article-tag-list-link color5">操作系统</a>
</li>
</ul>
</div>
<p class="article-more-link">
<a class="article-more-a" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/6.%E5%9C%B0%E5%9D%80%E6%98%A0%E5%B0%84%E4%B8%8E%E5%85%B1%E4%BA%AB/">展开全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<aside class="wrap-side-operation">
<div class="mod-side-operation">
<div class="jump-container" id="js-jump-container" style="display:none;">
<a href="javascript:void(0)" class="mod-side-operation__jump-to-top">
<i class="icon-font icon-back"></i>
</a>
<div id="js-jump-plan-container" class="jump-plan-container" style="top: -11px;">
<i class="icon-font icon-plane jump-plane"></i>
</div>
</div>
</div>
</aside>
<article id="post-哈工大操作系统实验/3.进程运行轨迹的跟踪与统计" class="article article-type-post article-index" itemscope itemprop="blogPost">
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/3.%E8%BF%9B%E7%A8%8B%E8%BF%90%E8%A1%8C%E8%BD%A8%E8%BF%B9%E7%9A%84%E8%B7%9F%E8%B8%AA%E4%B8%8E%E7%BB%9F%E8%AE%A1/">3.进程运行轨迹的跟踪与统计</a>
</h1>
<a href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/3.%E8%BF%9B%E7%A8%8B%E8%BF%90%E8%A1%8C%E8%BD%A8%E8%BF%B9%E7%9A%84%E8%B7%9F%E8%B8%AA%E4%B8%8E%E7%BB%9F%E8%AE%A1/" class="archive-article-date">
<time datetime="2024-07-22T06:41:31.564Z" itemprop="datePublished"><i class="icon-calendar icon"></i>2024-07-22</time>
</a>
</header>
<div class="article-entry" itemprop="articleBody">
<p>就绪与运行之间的状态转移(记住是两者之间的相互状态转移)是通过 <code>schedule()</code>函数完成的;运行到阻塞的转移依靠的是 <code>sleep-on()</code>和 <code>interruptible _sleep_on()</code>,进程通过系统调用 <code>sys-pause()</code>和 <code>sys-waitpid()</code> 可以主动进入阻塞态;阻塞到就绪的转移依靠的是 <code>wake_up()</code>。所以只要在这些函数的适当位置插人适当的处理语句就能完成进程运行轨迹的全面跟踪了。</p>
<h1 id="自定义打印日志的函数"><a href="#自定义打印日志的函数" class="headerlink" title="自定义打印日志的函数"></a>自定义打印日志的函数</h1><blockquote>
<p>可变参数使用和原理:[[可变参数]]</p>
</blockquote>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">char</span> logbuf[<span class="number">1024</span>];</span><br><span class="line"><span class="type">int</span> <span class="title function_">fprintk</span><span class="params">(<span class="type">int</span> fd, <span class="type">const</span> <span class="type">char</span> *fmt, ...)</span></span><br><span class="line">{</span><br><span class="line"> va_list args;</span><br><span class="line"> <span class="type">int</span> count;</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">file</span> * <span class="title">file</span>;</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">m_inode</span> * <span class="title">inode</span>;</span></span><br><span class="line"> va_start(args, fmt);</span><br><span class="line"> count = <span class="built_in">vsprintf</span>(logbuf, fmt, args);</span><br><span class="line"> va_end(args);</span><br><span class="line"> <span class="keyword">if</span> (fd == <span class="number">3</span>) {</span><br><span class="line"> <span class="comment">// get file by 0 process</span></span><br><span class="line"> <span class="keyword">if</span> (!(file=task[<span class="number">0</span>]->filp[fd])) <span class="comment">/* 从进程0的文件描述符表中得到文件句柄 */</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> inode = file->f_inode;</span><br><span class="line"> __asm__(<span class="string">"push %%fs\n\t"</span></span><br><span class="line"> <span class="string">"push %%ds\n\t"</span></span><br><span class="line"> <span class="string">"pop %%fs\n\t"</span></span><br><span class="line"> <span class="string">"pushl %0\n\t"</span></span><br><span class="line"> <span class="string">"pushl $logbuf\n\t"</span></span><br><span class="line"> <span class="string">"pushl %1\n\t"</span></span><br><span class="line"> <span class="string">"pushl %2\n\t"</span></span><br><span class="line"> <span class="string">"call file_write\n\t"</span></span><br><span class="line"> <span class="string">"addl $12,%%esp\n\t"</span></span><br><span class="line"> <span class="string">"popl %0\n\t"</span></span><br><span class="line"> <span class="string">"pop %%fs\n\t"</span></span><br><span class="line"> ::<span class="string">"r"</span>(count), <span class="string">"r"</span>(file), <span class="string">"r"</span>(inode):<span class="string">"ax"</span>,<span class="string">"cx"</span>,<span class="string">"dx"</span>);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> count;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h1 id="适当位置添加打印语句"><a href="#适当位置添加打印语句" class="headerlink" title="适当位置添加打印语句"></a>适当位置添加打印语句</h1><h2 id="sched-c"><a href="#sched-c" class="headerlink" title="sched.c"></a>sched.c</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">schedule</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> i,next,c;</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">task_struct</span> ** <span class="title">p</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* check alarm, wake up any interruptible tasks that have got a signal */</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">/* 省略部分代码 */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* this is the scheduler proper: */</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">while</span> (<span class="number">1</span>) {</span><br><span class="line"> <span class="comment">/* 省略部分代码 */</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">/*如果被调度的进程和当前运行的进程不是同一个,说明发生了切换</span></span><br><span class="line"><span class="comment"> 如果当前进程不是阻塞或者退出的话,则是进入就绪态</span></span><br><span class="line"><span class="comment"> next一定是进入运行态*/</span></span><br><span class="line"> <span class="keyword">if</span>(task[next]->pid != current->pid){</span><br><span class="line"> <span class="keyword">if</span>(current->state != TASK_INTERRUPTIBLE && current->state != TASK_UNINTERRUPTIBLE && current->state != TASK_ZOMBIE){</span><br><span class="line"> fprintk(<span class="number">3</span>, <span class="string">"%ld\t%c\t%ld\n"</span>, current->pid, <span class="string">'J'</span>, jiffies);</span><br><span class="line"> }</span><br><span class="line"> fprintk(<span class="number">3</span>, <span class="string">"%ld\t%c\t%ld\n"</span>, task[next]->pid, <span class="string">'R'</span>, jiffies);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> switch_to(next);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">sys_pause</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">/*主要是0号进程会反复调用此函数,防止重复输出*/</span></span><br><span class="line"> <span class="keyword">if</span>(current->state != TASK_INTERRUPTIBLE)</span><br><span class="line"> fprintk(<span class="number">3</span>, <span class="string">"%ld\t%c\t%ld\n"</span>, current->pid, <span class="string">'W'</span>, jiffies);</span><br><span class="line"> current->state = TASK_INTERRUPTIBLE;</span><br><span class="line"> schedule();</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">sleep_on</span><span class="params">(<span class="keyword">struct</span> task_struct **p)</span></span><br><span class="line">{</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">task_struct</span> *<span class="title">tmp</span>;</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (!p)</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> <span class="keyword">if</span> (current == &(init_task.task))</span><br><span class="line"> panic(<span class="string">"task[0] trying to sleep"</span>);</span><br><span class="line"> tmp = *p;</span><br><span class="line"> *p = current;</span><br><span class="line"> current->state = TASK_UNINTERRUPTIBLE;</span><br><span class="line"> <span class="comment">/*进程阻塞*/</span></span><br><span class="line"> fprintk(<span class="number">3</span>, <span class="string">"%ld\t%c\t%ld\n"</span>, current->pid, <span class="string">'W'</span>, jiffies);</span><br><span class="line"> schedule();</span><br><span class="line"> <span class="keyword">if</span> (tmp){</span><br><span class="line"> tmp->state=<span class="number">0</span>;</span><br><span class="line"> <span class="comment">/*进程被唤醒后运行时,将链上的上一个进程切换到就绪态*/</span></span><br><span class="line"> fprintk(<span class="number">3</span>, <span class="string">"%ld\t%c\t%ld\n"</span>, current->pid, <span class="string">'J'</span>, jiffies);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">interruptible_sleep_on</span><span class="params">(<span class="keyword">struct</span> task_struct **p)</span></span><br><span class="line">{</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">task_struct</span> *<span class="title">tmp</span>;</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (!p)</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> <span class="keyword">if</span> (current == &(init_task.task))</span><br><span class="line"> panic(<span class="string">"task[0] trying to sleep"</span>);</span><br><span class="line"> tmp=*p;</span><br><span class="line"> *p=current;</span><br><span class="line">repeat: current->state = TASK_INTERRUPTIBLE;</span><br><span class="line"> <span class="comment">/*进程阻塞*/</span></span><br><span class="line"> fprintk(<span class="number">3</span>, <span class="string">"%ld\t%c\t%ld\n"</span>, current->pid, <span class="string">'W'</span>, jiffies);</span><br><span class="line"> schedule();</span><br><span class="line"> <span class="keyword">if</span> (*p && *p != current) {</span><br><span class="line"> (**p).state=<span class="number">0</span>;</span><br><span class="line"> fprintk(<span class="number">3</span>, <span class="string">"%ld\t%c\t%ld\n"</span>, (**p).pid, <span class="string">'J'</span>, jiffies);</span><br><span class="line"> <span class="keyword">goto</span> repeat;</span><br><span class="line"> }</span><br><span class="line"> *p=<span class="literal">NULL</span>;</span><br><span class="line"> <span class="keyword">if</span> (tmp){</span><br><span class="line"> tmp->state=<span class="number">0</span>;</span><br><span class="line"> fprintk(<span class="number">3</span>, <span class="string">"%ld\t%c\t%ld\n"</span>, current->pid, <span class="string">'J'</span>, jiffies);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">wake_up</span><span class="params">(<span class="keyword">struct</span> task_struct **p)</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">if</span> (p && *p) {</span><br><span class="line"> (**p).state=<span class="number">0</span>;</span><br><span class="line"> <span class="comment">/*进程就绪*/</span></span><br><span class="line"> fprintk(<span class="number">3</span>, <span class="string">"%ld\t%c\t%ld\n"</span>, current->pid, <span class="string">'J'</span>, jiffies);</span><br><span class="line"> *p=<span class="literal">NULL</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="exit-c"><a href="#exit-c" class="headerlink" title="exit.c"></a>exit.c</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">do_exit</span><span class="params">(<span class="type">long</span> code)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">/* 省略部分代码 */</span></span><br><span class="line"> current->state = TASK_ZOMBIE;</span><br><span class="line"> current->exit_code = code;</span><br><span class="line"> <span class="comment">// before father wakes up, children exits</span></span><br><span class="line"> <span class="comment">/*进程退出*/</span></span><br><span class="line"> fprintk(<span class="number">3</span>, <span class="string">"%ld\t%c\t%ld\n"</span>, current->pid, <span class="string">'E'</span>, jiffies);</span><br><span class="line"> tell_father(current->father);</span><br><span class="line"> schedule();</span><br><span class="line"> <span class="keyword">return</span> (<span class="number">-1</span>); <span class="comment">/* just to suppress warnings */</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="fock-c"><a href="#fock-c" class="headerlink" title="fock.c"></a>fock.c</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">copy_process</span><span class="params">(<span class="type">int</span> nr,<span class="type">long</span> ebp,<span class="type">long</span> edi,<span class="type">long</span> esi,<span class="type">long</span> gs,<span class="type">long</span> none,</span></span><br><span class="line"><span class="params"> <span class="type">long</span> ebx,<span class="type">long</span> ecx,<span class="type">long</span> edx,</span></span><br><span class="line"><span class="params"> <span class="type">long</span> fs,<span class="type">long</span> es,<span class="type">long</span> ds,</span></span><br><span class="line"><span class="params"> <span class="type">long</span> eip,<span class="type">long</span> cs,<span class="type">long</span> eflags,<span class="type">long</span> esp,<span class="type">long</span> ss)</span></span><br><span class="line">{</span><br><span class="line"> <span class="comment">/* 省略部分代码 */</span></span><br><span class="line"> p->state = TASK_RUNNING; <span class="comment">/* do this last, just in case */</span></span><br><span class="line"> <span class="comment">/*进程被建立,然后进入就绪态*/</span></span><br><span class="line"> fprintk(<span class="number">3</span>, <span class="string">"%ld\t%c\t%ld\n"</span>, p->pid, <span class="string">'N'</span>, jiffies);</span><br><span class="line"> fprintk(<span class="number">3</span>, <span class="string">"%ld\t%c\t%ld\n"</span>, p->pid, <span class="string">'J'</span>, jiffies);</span><br><span class="line"> <span class="keyword">return</span> last_pid;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h1 id="编写测试程序process-c"><a href="#编写测试程序process-c" class="headerlink" title="编写测试程序process.c"></a>编写测试程序process.c</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><unistd.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><time.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><sys/times.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><sys/wait.h></span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> HZ 100</span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">cpuio_bound</span><span class="params">(<span class="type">int</span> last, <span class="type">int</span> cpu_time, <span class="type">int</span> io_time)</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">children</span><span class="params">()</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">children</span><span class="params">()</span>{</span><br><span class="line"> cpuio_bound(<span class="number">10</span>, <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> * argv[])</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> sta;</span><br><span class="line"> <span class="type">pid_t</span> wpid;</span><br><span class="line"> <span class="type">int</span> cnt = <span class="number">2</span>;</span><br><span class="line"> <span class="type">pid_t</span> pid;</span><br><span class="line"> <span class="type">int</span> i;</span><br><span class="line"></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"father: %d\n"</span>, getpid());</span><br><span class="line"> fflush(<span class="built_in">stdout</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < cnt; i++){</span><br><span class="line"> <span class="keyword">if</span>(pid = fork()){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"children%d: %d\n"</span>, i, pid);</span><br><span class="line"> fflush(<span class="built_in">stdout</span>);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> children();</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/* wait for all children */</span></span><br><span class="line"> <span class="keyword">while</span>((wpid = wait(&sta)) > <span class="number">0</span>){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"children%d exit\n"</span>, wpid);</span><br><span class="line"> fflush(<span class="built_in">stdout</span>);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 此函数按照参数占用CPU和I/O时间</span></span><br><span class="line"><span class="comment"> * last: 函数实际占用CPU和I/O的总时间,不含在就绪队列中的时间,>=0是必须的</span></span><br><span class="line"><span class="comment"> * cpu_time: 一次连续占用CPU的时间,>=0是必须的</span></span><br><span class="line"><span class="comment"> * io_time: 一次I/O消耗的时间,>=0是必须的</span></span><br><span class="line"><span class="comment"> * 如果last > cpu_time + io_time,则往复多次占用CPU和I/O</span></span><br><span class="line"><span class="comment"> * 所有时间的单位为秒</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">cpuio_bound</span><span class="params">(<span class="type">int</span> last, <span class="type">int</span> cpu_time, <span class="type">int</span> io_time)</span></span><br><span class="line">{</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">tms</span> <span class="title">start_time</span>, <span class="title">current_time</span>;</span></span><br><span class="line"> <span class="type">clock_t</span> utime, stime;</span><br><span class="line"> <span class="type">int</span> sleep_time;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">while</span> (last > <span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="comment">/* CPU Burst */</span></span><br><span class="line"> times(&start_time);</span><br><span class="line"> <span class="comment">/* 其实只有t.tms_utime才是真正的CPU时间。但我们是在模拟一个</span></span><br><span class="line"><span class="comment"> * 只在用户状态运行的CPU大户,就像“for(;;);”。所以把t.tms_stime</span></span><br><span class="line"><span class="comment"> * 加上很合理。*/</span></span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> {</span><br><span class="line"> times(&current_time);</span><br><span class="line"> utime = current_time.tms_utime - start_time.tms_utime;</span><br><span class="line"> stime = current_time.tms_stime - start_time.tms_stime;</span><br><span class="line"> } <span class="keyword">while</span> ( ( (utime + stime) / HZ ) < cpu_time );</span><br><span class="line"> last -= cpu_time;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (last <= <span class="number">0</span> )</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/* IO Burst */</span></span><br><span class="line"> <span class="comment">/* 用sleep(1)模拟1秒钟的I/O操作 */</span></span><br><span class="line"> sleep_time=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span> (sleep_time < io_time)</span><br><span class="line"> {</span><br><span class="line"> sleep(<span class="number">1</span>);</span><br><span class="line"> sleep_time++;</span><br><span class="line"> }</span><br><span class="line"> last -= sleep_time;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h1 id="实验结果"><a href="#实验结果" class="headerlink" title="实验结果"></a>实验结果</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> N <span class="number">48</span> <span class="comment">//记录是从0号进程进入用户模式后开始的,所以0号进程的建立没有记录</span></span><br><span class="line"> <span class="comment">//0号进程建立的1号进程(init)</span></span><br><span class="line"><span class="number">1</span> J <span class="number">48</span></span><br><span class="line"><span class="number">0</span> J <span class="number">48</span> <span class="comment">//0号进程被调度进入就绪态,1号进程运行</span></span><br><span class="line"><span class="number">1</span> R <span class="number">48</span></span><br><span class="line"><span class="number">2</span> N <span class="number">49</span> <span class="comment">//1号进程建立2号进程,打开/etc/rc</span></span><br><span class="line"><span class="number">2</span> J <span class="number">49</span></span><br><span class="line"><span class="number">2</span> R <span class="number">49</span></span><br><span class="line"><span class="number">3</span> N <span class="number">64</span> <span class="comment">//2号进程建立3号进程</span></span><br><span class="line"><span class="number">3</span> J <span class="number">64</span></span><br><span class="line"><span class="number">2</span> J <span class="number">64</span></span><br><span class="line"><span class="number">3</span> R <span class="number">64</span></span><br><span class="line"><span class="number">3</span> W <span class="number">68</span></span><br><span class="line"><span class="number">2</span> R <span class="number">68</span></span><br><span class="line"><span class="number">2</span> E <span class="number">73</span> <span class="comment">//2号进程不等3号进程退出,先行离开</span></span><br><span class="line"><span class="number">1</span> J <span class="number">73</span></span><br><span class="line"><span class="number">1</span> R <span class="number">73</span></span><br><span class="line"><span class="number">4</span> N <span class="number">74</span> <span class="comment">//1号进程建立4号进程,即shell进程</span></span><br><span class="line"><span class="number">4</span> J <span class="number">74</span></span><br><span class="line"><span class="number">4</span> R <span class="number">74</span></span><br><span class="line"><span class="number">5</span> N <span class="number">106</span> <span class="comment">//shell进程建立的5号进程,不知道干啥用</span></span><br><span class="line"><span class="number">5</span> J <span class="number">106</span></span><br><span class="line"><span class="number">4</span> W <span class="number">107</span></span><br><span class="line"><span class="number">5</span> R <span class="number">107</span></span><br><span class="line"><span class="number">5</span> J <span class="number">109</span></span><br><span class="line"><span class="number">5</span> E <span class="number">109</span> <span class="comment">//5号进程很快退出</span></span><br><span class="line"><span class="number">4</span> R <span class="number">109</span> <span class="comment">//下面0号和4号进程交替运行</span></span><br><span class="line"><span class="number">4</span> W <span class="number">115</span></span><br><span class="line"><span class="number">0</span> R <span class="number">115</span></span><br><span class="line"><span class="number">0</span> W <span class="number">115</span></span><br><span class="line"><span class="number">0</span> J <span class="number">496</span></span><br><span class="line"><span class="number">4</span> R <span class="number">496</span></span><br><span class="line"><span class="number">4</span> W <span class="number">496</span></span><br><span class="line"><span class="number">0</span> R <span class="number">496</span></span><br><span class="line"><span class="number">0</span> J <span class="number">502</span></span><br><span class="line"><span class="number">4</span> R <span class="number">502</span></span><br><span class="line"><span class="number">4</span> W <span class="number">502</span></span><br><span class="line"><span class="number">0</span> R <span class="number">503</span></span><br><span class="line"><span class="number">0</span> J <span class="number">562</span></span><br><span class="line"><span class="number">4</span> R <span class="number">562</span></span><br><span class="line"><span class="number">4</span> W <span class="number">562</span></span><br><span class="line"><span class="number">0</span> R <span class="number">562</span></span><br><span class="line"><span class="number">0</span> J <span class="number">562</span></span><br><span class="line"><span class="number">4</span> R <span class="number">562</span></span><br><span class="line"><span class="number">4</span> W <span class="number">562</span></span><br><span class="line"><span class="number">0</span> R <span class="number">563</span></span><br><span class="line"><span class="number">0</span> J <span class="number">652</span></span><br><span class="line"><span class="number">4</span> R <span class="number">652</span></span><br><span class="line"><span class="number">4</span> W <span class="number">653</span></span><br><span class="line"><span class="number">0</span> R <span class="number">653</span></span><br><span class="line"><span class="number">0</span> J <span class="number">682</span></span><br><span class="line"><span class="number">4</span> R <span class="number">682</span></span><br><span class="line"><span class="number">4</span> W <span class="number">682</span></span><br><span class="line"><span class="number">0</span> R <span class="number">683</span></span><br><span class="line"><span class="number">0</span> J <span class="number">712</span></span><br><span class="line"><span class="number">4</span> R <span class="number">712</span></span><br><span class="line"><span class="number">4</span> W <span class="number">718</span></span><br><span class="line"><span class="number">0</span> R <span class="number">718</span></span><br><span class="line"><span class="number">0</span> J <span class="number">952</span></span><br><span class="line"><span class="number">4</span> R <span class="number">952</span></span><br><span class="line"><span class="number">6</span> N <span class="number">953</span> <span class="comment">//shell建立6号进程,即process的主进程</span></span><br><span class="line"><span class="number">6</span> J <span class="number">953</span></span><br><span class="line"><span class="number">6</span> R <span class="number">954</span></span><br><span class="line"><span class="number">7</span> N <span class="number">956</span> <span class="comment">//process建立第一个子进程</span></span><br><span class="line"><span class="number">7</span> J <span class="number">957</span></span><br><span class="line"><span class="number">8</span> N <span class="number">957</span> <span class="comment">//process建立第二个子进程</span></span><br><span class="line"><span class="number">8</span> J <span class="number">957</span> <span class="comment">//下面是两个子进程交替运行</span></span><br><span class="line"><span class="number">8</span> R <span class="number">958</span></span><br><span class="line"><span class="number">8</span> J <span class="number">973</span></span><br><span class="line"><span class="number">7</span> R <span class="number">973</span></span><br><span class="line"><span class="number">7</span> J <span class="number">988</span></span><br><span class="line"><span class="number">8</span> R <span class="number">988</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1003</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1003</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1018</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1018</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1033</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1033</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1048</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1048</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1063</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1063</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1078</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1078</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1093</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1093</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1108</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1108</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1123</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1123</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1138</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1138</span></span><br><span class="line"><span class="number">8</span> W <span class="number">1148</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1148</span></span><br><span class="line"><span class="number">7</span> W <span class="number">1158</span></span><br><span class="line"><span class="number">0</span> R <span class="number">1158</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1249</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1249</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1269</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1269</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1269</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1286</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1286</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1301</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1301</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1316</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1316</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1331</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1331</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1346</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1346</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1361</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1361</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1376</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1376</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1391</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1391</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1406</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1406</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1421</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1421</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1436</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1436</span></span><br><span class="line"><span class="number">8</span> W <span class="number">1441</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1441</span></span><br><span class="line"><span class="number">7</span> W <span class="number">1449</span></span><br><span class="line"><span class="number">0</span> R <span class="number">1449</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1542</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1542</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1552</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1552</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1552</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1559</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1559</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1574</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1574</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1589</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1589</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1604</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1604</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1619</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1619</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1634</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1634</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1649</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1649</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1664</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1664</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1679</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1679</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1694</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1694</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1709</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1709</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1724</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1724</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1739</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1739</span></span><br><span class="line"><span class="number">8</span> W <span class="number">1739</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1739</span></span><br><span class="line"><span class="number">7</span> W <span class="number">1742</span></span><br><span class="line"><span class="number">0</span> R <span class="number">1742</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1840</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1840</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1855</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1855</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1855</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1867</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1867</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1882</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1882</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1897</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1897</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1912</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1912</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1927</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1927</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1942</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1942</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1957</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1957</span></span><br><span class="line"><span class="number">8</span> J <span class="number">1972</span></span><br><span class="line"><span class="number">7</span> R <span class="number">1972</span></span><br><span class="line"><span class="number">7</span> J <span class="number">1987</span></span><br><span class="line"><span class="number">8</span> R <span class="number">1987</span></span><br><span class="line"><span class="number">8</span> J <span class="number">2002</span></span><br><span class="line"><span class="number">7</span> R <span class="number">2002</span></span><br><span class="line"><span class="number">7</span> J <span class="number">2017</span></span><br><span class="line"><span class="number">8</span> R <span class="number">2017</span></span><br><span class="line"><span class="number">8</span> W <span class="number">2027</span></span><br><span class="line"><span class="number">7</span> R <span class="number">2027</span></span><br><span class="line"><span class="number">7</span> W <span class="number">2040</span></span><br><span class="line"><span class="number">0</span> R <span class="number">2040</span></span><br><span class="line"><span class="number">8</span> J <span class="number">2128</span></span><br><span class="line"><span class="number">8</span> R <span class="number">2128</span></span><br><span class="line"><span class="number">7</span> J <span class="number">2148</span></span><br><span class="line"><span class="number">8</span> J <span class="number">2148</span></span><br><span class="line"><span class="number">7</span> R <span class="number">2148</span></span><br><span class="line"><span class="number">7</span> J <span class="number">2164</span></span><br><span class="line"><span class="number">8</span> R <span class="number">2164</span></span><br><span class="line"><span class="number">8</span> J <span class="number">2179</span></span><br><span class="line"><span class="number">7</span> R <span class="number">2179</span></span><br><span class="line"><span class="number">7</span> J <span class="number">2194</span></span><br><span class="line"><span class="number">8</span> R <span class="number">2194</span></span><br><span class="line"><span class="number">8</span> J <span class="number">2209</span></span><br><span class="line"><span class="number">7</span> R <span class="number">2209</span></span><br><span class="line"><span class="number">7</span> J <span class="number">2224</span></span><br><span class="line"><span class="number">8</span> R <span class="number">2224</span></span><br><span class="line"><span class="number">8</span> J <span class="number">2239</span></span><br><span class="line"><span class="number">7</span> R <span class="number">2239</span></span><br><span class="line"><span class="number">7</span> J <span class="number">2254</span></span><br><span class="line"><span class="number">8</span> R <span class="number">2254</span></span><br><span class="line"><span class="number">8</span> J <span class="number">2269</span></span><br><span class="line"><span class="number">7</span> R <span class="number">2269</span></span><br><span class="line"><span class="number">7</span> J <span class="number">2284</span></span><br><span class="line"><span class="number">8</span> R <span class="number">2284</span></span><br><span class="line"><span class="number">8</span> J <span class="number">2299</span></span><br><span class="line"><span class="number">7</span> R <span class="number">2299</span></span><br><span class="line"><span class="number">7</span> J <span class="number">2314</span></span><br><span class="line"><span class="number">8</span> R <span class="number">2314</span></span><br><span class="line"><span class="number">8</span> W <span class="number">2319</span></span><br><span class="line"><span class="number">7</span> R <span class="number">2319</span></span><br><span class="line"><span class="number">7</span> W <span class="number">2328</span></span><br><span class="line"><span class="number">0</span> R <span class="number">2328</span></span><br><span class="line"><span class="number">8</span> J <span class="number">2420</span></span><br><span class="line"><span class="number">8</span> R <span class="number">2420</span></span><br><span class="line"><span class="number">8</span> E <span class="number">2420</span> <span class="comment">//子进程退出</span></span><br><span class="line"><span class="number">6</span> J <span class="number">2420</span></span><br><span class="line"><span class="number">6</span> R <span class="number">2420</span></span><br><span class="line"><span class="number">0</span> R <span class="number">2420</span></span><br><span class="line"><span class="number">7</span> J <span class="number">2429</span></span><br><span class="line"><span class="number">7</span> R <span class="number">2429</span></span><br><span class="line"><span class="number">7</span> E <span class="number">2429</span> <span class="comment">//子进程退出</span></span><br><span class="line"><span class="number">6</span> J <span class="number">2429</span></span><br><span class="line"><span class="number">6</span> R <span class="number">2429</span></span><br><span class="line"><span class="number">6</span> E <span class="number">2430</span> <span class="comment">//process主进程退出</span></span><br><span class="line"><span class="number">4</span> J <span class="number">2430</span></span><br><span class="line"><span class="number">4</span> R <span class="number">2430</span></span><br><span class="line"><span class="number">9</span> N <span class="number">2431</span></span><br><span class="line"><span class="number">9</span> J <span class="number">2431</span></span><br><span class="line"><span class="number">4</span> W <span class="number">2431</span></span><br><span class="line"><span class="number">9</span> R <span class="number">2431</span></span><br><span class="line"><span class="number">9</span> J <span class="number">2433</span></span><br><span class="line"><span class="number">9</span> E <span class="number">2434</span></span><br><span class="line"><span class="number">4</span> R <span class="number">2434</span></span><br><span class="line"><span class="number">4</span> W <span class="number">2434</span></span><br><span class="line"><span class="number">0</span> R <span class="number">2434</span></span><br><span class="line"><span class="number">0</span> J <span class="number">2602</span></span><br><span class="line"><span class="number">4</span> R <span class="number">2602</span></span><br><span class="line"><span class="number">4</span> W <span class="number">2603</span></span><br><span class="line"><span class="number">0</span> R <span class="number">2603</span></span><br><span class="line"><span class="number">0</span> J <span class="number">2632</span></span><br><span class="line"><span class="number">4</span> R <span class="number">2632</span></span><br><span class="line"><span class="number">4</span> W <span class="number">2632</span></span><br><span class="line"><span class="number">0</span> R <span class="number">2633</span></span><br><span class="line"><span class="number">0</span> J <span class="number">2662</span></span><br><span class="line"><span class="number">4</span> R <span class="number">2662</span></span><br><span class="line"><span class="number">4</span> W <span class="number">2662</span></span><br><span class="line"><span class="number">0</span> R <span class="number">2663</span></span><br><span class="line"><span class="number">0</span> J <span class="number">2692</span></span><br><span class="line"><span class="number">4</span> R <span class="number">2692</span></span><br><span class="line"><span class="number">4</span> W <span class="number">2692</span></span><br><span class="line"><span class="number">0</span> R <span class="number">2692</span></span><br><span class="line"><span class="number">0</span> J <span class="number">2729</span></span><br><span class="line"><span class="number">4</span> R <span class="number">2729</span></span><br><span class="line"><span class="number">4</span> W <span class="number">2729</span></span><br><span class="line"><span class="number">0</span> R <span class="number">2729</span></span><br><span class="line"><span class="number">0</span> J <span class="number">2752</span></span><br><span class="line"><span class="number">4</span> R <span class="number">2752</span></span><br><span class="line"><span class="number">4</span> W <span class="number">2752</span></span><br><span class="line"><span class="number">0</span> R <span class="number">2753</span></span><br><span class="line"><span class="number">0</span> J <span class="number">2754</span></span><br><span class="line"><span class="number">4</span> R <span class="number">2754</span></span><br><span class="line"><span class="number">4</span> W <span class="number">2754</span></span><br><span class="line"><span class="number">0</span> R <span class="number">2754</span></span><br><span class="line"><span class="number">0</span> J <span class="number">2782</span></span><br><span class="line"><span class="number">4</span> R <span class="number">2782</span></span><br><span class="line"><span class="number">4</span> W <span class="number">2782</span></span><br><span class="line"><span class="number">0</span> R <span class="number">2783</span></span><br><span class="line"><span class="number">0</span> J <span class="number">2812</span></span><br><span class="line"><span class="number">4</span> R <span class="number">2812</span></span><br><span class="line"><span class="number">10</span> N <span class="number">2814</span></span><br><span class="line"><span class="number">10</span> J <span class="number">2814</span></span><br><span class="line"><span class="number">10</span> R <span class="number">2815</span></span><br></pre></td></tr></table></figure>
<p>![[statlog.png]]</p>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<i class="icon-price-tags icon"></i>
<ul class="article-tag-list">
<li class="article-tag-list-item">
<a href="javascript:void(0)" class="js-tag article-tag-list-link color5">操作系统</a>
</li>
</ul>
</div>
<p class="article-more-link">
<a class="article-more-a" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/3.%E8%BF%9B%E7%A8%8B%E8%BF%90%E8%A1%8C%E8%BD%A8%E8%BF%B9%E7%9A%84%E8%B7%9F%E8%B8%AA%E4%B8%8E%E7%BB%9F%E8%AE%A1/">展开全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<aside class="wrap-side-operation">
<div class="mod-side-operation">
<div class="jump-container" id="js-jump-container" style="display:none;">
<a href="javascript:void(0)" class="mod-side-operation__jump-to-top">
<i class="icon-font icon-back"></i>
</a>
<div id="js-jump-plan-container" class="jump-plan-container" style="top: -11px;">
<i class="icon-font icon-plane jump-plane"></i>
</div>
</div>
</div>
</aside>
<article id="post-哈工大操作系统实验/2.系统调用" class="article article-type-post article-index" itemscope itemprop="blogPost">
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/2.%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8/">2.系统调用</a>
</h1>
<a href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/2.%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8/" class="archive-article-date">
<time datetime="2024-07-22T06:41:31.500Z" itemprop="datePublished"><i class="icon-calendar icon"></i>2024-07-22</time>
</a>
</header>
<div class="article-entry" itemprop="articleBody">
<ol>
<li><p><code>/include/unistd.h</code>中增加两个宏定义</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> __NR_iam 72</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> __NR_whoami 73</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意不仅要修改内核的unistd.h,还有对应修改用户空间的头文件</p>
</blockquote>
</li>
<li><p><code>/include/linux/sys.h</code>中增加</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">extern</span> <span class="type">int</span> <span class="title function_">sys_iam</span><span class="params">()</span>;</span><br><span class="line"><span class="keyword">extern</span> <span class="type">int</span> <span class="title function_">sys_whoami</span><span class="params">()</span>;</span><br><span class="line">fn_ptr sys_call_table[] = {...</span><br><span class="line">..., sys_iam, sys_whoami};</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>/kernel/system_call.s</code>中修改</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nr_system_calls = <span class="number">74</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><code>/kernel/</code>文件夹中添加<code>who.c</code></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> __LIBRARY__</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><unistd.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><errno.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><asm/segment.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">const</span> <span class="type">int</span> NAME_MAXSIZE = <span class="number">23</span></span><br><span class="line"></span><br><span class="line"><span class="type">char</span>* global_name = <span class="literal">NULL</span>;</span><br><span class="line"><span class="type">char</span>* tmp_name = <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">sys_iam</span><span class="params">(<span class="type">const</span> <span class="type">char</span>* name)</span>{</span><br><span class="line"> <span class="type">int</span> length = <span class="number">0</span>;</span><br><span class="line"> <span class="type">char</span> c;</span><br><span class="line"> <span class="keyword">if</span>(global_name==<span class="literal">NULL</span>){</span><br><span class="line"> global_name = <span class="built_in">malloc</span>(NAME_MAXSIZE);</span><br><span class="line"> tmp_name = <span class="built_in">malloc</span>(NAME_MAXSIZE);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">while</span>(name&&(c=get_fs_byte(name++))){</span><br><span class="line"> tmp_name[length++] = c;</span><br><span class="line"> <span class="keyword">if</span>(length > NAME_MAXSIZE){</span><br><span class="line"> errno = EINVAL;</span><br><span class="line"> <span class="keyword">return</span> -errno;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < length; i++)</span><br><span class="line"> global_name[i] = tmp_name[i];</span><br><span class="line"> global_name[length] = <span class="string">'\0'</span>;</span><br><span class="line"> <span class="keyword">return</span> length;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">sys_whoami</span><span class="params">(<span class="type">char</span>* name, <span class="type">unsigned</span> <span class="type">int</span> size)</span>{</span><br><span class="line"> <span class="type">int</span> length = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(length <= size){</span><br><span class="line"> put_fs_byte(global_name[length], name++);</span><br><span class="line"> <span class="keyword">if</span> (global_name[length] == <span class="string">'\0'</span>)</span><br><span class="line"> <span class="keyword">return</span> length;</span><br><span class="line"> length++;</span><br><span class="line"> }</span><br><span class="line"> errno = EINVAL;</span><br><span class="line"> <span class="keyword">return</span> -errno;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="测试代码"><a href="#测试代码" class="headerlink" title="测试代码"></a>测试代码</h2><p><code>iam.c</code></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> __LIBRARY__</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><unistd.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line">_syscall1(<span class="type">int</span>, iam, <span class="type">const</span> <span class="type">char</span>*, name);</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span>* argv[])</span> {</span><br><span class="line"> <span class="type">const</span> <span class="type">char</span>* name = argv[<span class="number">1</span>];</span><br><span class="line"> <span class="type">int</span> result = iam(name);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><code>whoami.c</code></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> __LIBRARY__</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><unistd.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line">_syscall2(<span class="type">int</span>, whoami, <span class="type">char</span>*, name, <span class="type">unsigned</span> <span class="type">int</span>, size);</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> NAME_MAXSIZE = <span class="number">23</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span> {</span><br><span class="line"> <span class="type">char</span> name[NAME_MAXSIZE+<span class="number">1</span>];</span><br><span class="line"> <span class="type">int</span> result = whoami(name, NAME_MAXSIZE);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, name);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li>
</ol>
<p>![[系统调用实验结果1.png]]<br>![[系统调用实验结果2.png]]</p>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<i class="icon-price-tags icon"></i>
<ul class="article-tag-list">
<li class="article-tag-list-item">
<a href="javascript:void(0)" class="js-tag article-tag-list-link color5">操作系统</a>
</li>
</ul>
</div>
<p class="article-more-link">
<a class="article-more-a" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/2.%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8/">展开全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<aside class="wrap-side-operation">
<div class="mod-side-operation">
<div class="jump-container" id="js-jump-container" style="display:none;">
<a href="javascript:void(0)" class="mod-side-operation__jump-to-top">
<i class="icon-font icon-back"></i>
</a>
<div id="js-jump-plan-container" class="jump-plan-container" style="top: -11px;">
<i class="icon-font icon-plane jump-plane"></i>
</div>
</div>
</div>
</aside>
<article id="post-哈工大操作系统实验/1.操作系统引导" class="article article-type-post article-index" itemscope itemprop="blogPost">
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/1.%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%BC%95%E5%AF%BC/">1.操作系统引导</a>
</h1>
<a href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/1.%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%BC%95%E5%AF%BC/" class="archive-article-date">
<time datetime="2024-07-22T06:41:31.490Z" itemprop="datePublished"><i class="icon-calendar icon"></i>2024-07-22</time>
</a>
</header>
<div class="article-entry" itemprop="articleBody">
<p>此次实验的基本内容是:</p>
<ol>
<li>阅读《Linux内核完全注释》的第6章,对计算机和Linux 0.11的引导过程进行初步的了解;</li>
<li>按照下面的要求改写0.11的引导程序bootsect.s</li>
<li>有兴趣同学可以做做进入保护模式前的设置程序setup.s。</li>
</ol>
<p>改写bootsect.s主要完成如下功能:</p>
<ul>
<li><input checked="" disabled="" type="checkbox"> <ol>
<li>bootsect.s能在屏幕上打印一段提示信息“XXX is booting…”,其中XXX是你给自己的操作系统起的名字,例如LZJos、Sunix等(可以上论坛上秀秀谁的OS名字最帅,也可以显示一个特色logo,以表示自己操作系统的与众不同。)</li>
</ol>
</li>
</ul>
<p>改写setup.s主要完成如下功能:</p>
<ul>
<li><input checked="" disabled="" type="checkbox"> <ol>
<li>bootsect.s能完成setup.s的载入,并跳转到setup.s开始地址执行。而setup.s向屏幕输出一行”Now we are in SETUP”。</li>
</ol>
</li>
<li><input checked="" disabled="" type="checkbox"> <ol start="2">
<li>setup.s能获取至少一个基本的硬件参数(如内存参数、显卡参数、硬盘参数等),将其存放在内存的特定地址,并输出到屏幕上。</li>
</ol>
</li>
<li><input checked="" disabled="" type="checkbox"> <ol start="3">
<li>setup.s不再加载Linux内核,保持上述信息显示在屏幕上即可。</li>
</ol>
</li>
</ul>
<h1 id="任务一-打印提示信息"><a href="#任务一-打印提示信息" class="headerlink" title="任务一 打印提示信息"></a>任务一 打印提示信息</h1><p>主要修改/boot/bootsect.s中的代码:</p>
<figure class="highlight as"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">! Print some inane message</span><br><span class="line"></span><br><span class="line"> mov ah,#<span class="number">0x03</span> ! read cursor pos</span><br><span class="line"> xor bh,bh</span><br><span class="line"> int <span class="number">0x10</span></span><br><span class="line"></span><br><span class="line"> mov cx,#<span class="number">24</span></span><br><span class="line"> mov bx,#<span class="number">0x0007</span> ! page <span class="number">0</span>, attribute <span class="number">7</span> (normal)</span><br><span class="line"> mov bp,#msg1</span><br><span class="line"> mov ax,#<span class="number">0x1301</span> ! write string, move cursor</span><br><span class="line"> int <span class="number">0x10</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">msg1:</span><br><span class="line"> ! 插入回车换行符</span><br><span class="line"> .byte <span class="number">13</span>,<span class="number">10</span></span><br><span class="line"> ! 定义消息内容</span><br><span class="line"> .ascii <span class="string">"Loading system ..."</span></span><br><span class="line"> ! 再次插入回车换行符</span><br><span class="line"> .byte <span class="number">13</span>,<span class="number">10</span>,<span class="number">13</span>,<span class="number">10</span></span><br></pre></td></tr></table></figure>
<p>上述信息共24个字符,下面将他修改为“crzos is booting …”,加上换行空格,共26个字符。</p>
<figure class="highlight as"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">! Print some inane message</span><br><span class="line"></span><br><span class="line"> mov ah,#<span class="number">0x03</span> ! read cursor pos</span><br><span class="line"> xor bh,bh</span><br><span class="line"> int <span class="number">0x10</span></span><br><span class="line"></span><br><span class="line"> mov cx,#<span class="number">26</span></span><br><span class="line"> mov bx,#<span class="number">0x0007</span> ! page <span class="number">0</span>, attribute <span class="number">7</span> (normal)</span><br><span class="line"> mov bp,#msg1</span><br><span class="line"> mov ax,#<span class="number">0x1301</span> ! write string, move cursor</span><br><span class="line"> int <span class="number">0x10</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">msg1:</span><br><span class="line"> ! 插入回车换行符</span><br><span class="line"> .byte <span class="number">13</span>,<span class="number">10</span></span><br><span class="line"> ! 定义消息内容</span><br><span class="line"> .ascii <span class="string">"crzos is booting ..."</span></span><br><span class="line"> ! 再次插入回车换行符</span><br><span class="line"> .byte <span class="number">13</span>,<span class="number">10</span>,<span class="number">13</span>,<span class="number">10</span></span><br></pre></td></tr></table></figure>
<p>![[修改启动提示信息.png]]</p>
<h1 id="任务二-输出setup"><a href="#任务二-输出setup" class="headerlink" title="任务二 输出setup"></a>任务二 输出setup</h1><p>在setup.s中添加代码:</p>
<figure class="highlight as"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">! now, we are <span class="keyword">in</span> setup.s</span><br><span class="line">! print some message</span><br><span class="line"></span><br><span class="line"> mov si,#setup_msg</span><br><span class="line"> call print_string</span><br><span class="line"></span><br><span class="line">! print string</span><br><span class="line">! ds:si stores the starting address of the string</span><br><span class="line">! ds <span class="keyword">is</span> <span class="keyword">set</span> to cs within the <span class="keyword">function</span></span><br><span class="line"><span class="title function_">print_string</span>:</span><br><span class="line">! <span class="title function_">cs</span> = <span class="title function_">ds</span> = <span class="title function_">es</span></span><br><span class="line"> <span class="title function_">push</span> <span class="title function_">ds</span></span><br><span class="line"> <span class="title function_">push</span> <span class="title function_">es</span></span><br><span class="line"> <span class="title function_">pusha</span></span><br><span class="line"> <span class="title function_">mov</span> <span class="title function_">ax</span>,<span class="title function_">cs</span></span><br><span class="line"> <span class="title function_">mov</span> <span class="title function_">ds</span>,<span class="title function_">ax</span></span><br><span class="line"> <span class="title function_">mov</span> <span class="title function_">es</span>,<span class="title function_">ax</span></span><br><span class="line"><span class="title function_">next_char</span>:</span><br><span class="line"> lodsb</span><br><span class="line"> <span class="title function_">cmp</span> <span class="title function_">al</span>, #0</span><br><span class="line"> <span class="title function_">je</span> <span class="title function_">print_string_end</span></span><br><span class="line"> <span class="title function_">mov</span> <span class="title function_">ah</span>, #0<span class="title function_">x0e</span></span><br><span class="line"> <span class="title function_">int</span> 0<span class="title function_">x10</span></span><br><span class="line"> <span class="title function_">inc</span> <span class="title function_">bp</span></span><br><span class="line"> <span class="title function_">jmp</span> <span class="title function_">next_char</span></span><br><span class="line"><span class="title function_">print_string_end</span>:</span><br><span class="line"> popa</span><br><span class="line"> <span class="title function_">pop</span> <span class="title function_">es</span></span><br><span class="line"> <span class="title function_">pop</span> <span class="title function_">ds</span></span><br><span class="line"> <span class="title function_">ret</span></span><br><span class="line"></span><br><span class="line"><span class="title function_">setup_msg</span>:</span><br><span class="line"> .<span class="title function_">byte</span> 13,10</span><br><span class="line"> .<span class="title function_">ascii</span> "<span class="title function_">now</span> <span class="title function_">we</span> <span class="title function_">are</span> <span class="title function_">in</span> <span class="title function_">setup</span>"</span><br><span class="line"> .<span class="title function_">byte</span> 13,10,0</span><br></pre></td></tr></table></figure>
<blockquote>
<p>尤其注意添加cs = ds = es代码<br>主要是es 要 = cs,否则不会正常显示<br><code>int 0x10</code> 的某些功能号(在这里是 <code>0x1301</code>)会使用 <code>ES:BP</code> 来指向要显示的字符串。如果 <code>ES</code> 不正确,BIOS 将会查找错误的内存位置来获取字符串数据,导致无法正常显示信息。</p>
</blockquote>
<h1 id="任务三-输出参数"><a href="#任务三-输出参数" class="headerlink" title="任务三 输出参数"></a>任务三 输出参数</h1><p>![[setup读取并保留的参数.png]]</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">! print information</span><br><span class="line"> mov cx, #4</span><br><span class="line"> mov di, #0</span><br><span class="line">print_info_loop:</span><br><span class="line"> mov ax, cs</span><br><span class="line"> mov ds, ax</span><br><span class="line"> mov bx, #prompt</span><br><span class="line"> mov si, (bx+di)</span><br><span class="line"> call print_string</span><br><span class="line"></span><br><span class="line"> xchg bx, bx</span><br><span class="line"> mov bx, #info_addr</span><br><span class="line"> mov bx, (bx+di)</span><br><span class="line"> mov ax, #INITSEG</span><br><span class="line"> mov ds, ax</span><br><span class="line"> mov ax, (bx)</span><br><span class="line"> call print_hex</span><br><span class="line"> call print_nl</span><br><span class="line"></span><br><span class="line"> add di, #2</span><br><span class="line"> loop print_info_loop</span><br><span class="line"></span><br><span class="line">; l: jmp l</span><br><span class="line"></span><br><span class="line">! print hex number in ax</span><br><span class="line">print_hex:</span><br><span class="line"> pusha</span><br><span class="line"> mov dx, ax</span><br><span class="line"> mov ah, #0x0e</span><br><span class="line"> mov cx, #4</span><br><span class="line">print_hex_loop:</span><br><span class="line"> rol dx, 4</span><br><span class="line"> mov al, dl</span><br><span class="line"> and al, #0x0f</span><br><span class="line"> cmp al, #0x0a</span><br><span class="line"> jb L</span><br><span class="line"> add al, #0x07</span><br><span class="line">L:</span><br><span class="line"> add al, #0x30</span><br><span class="line"> int 0x10</span><br><span class="line"> loop print_hex_loop</span><br><span class="line"> popa</span><br><span class="line"> ret</span><br><span class="line"></span><br><span class="line">! print \n</span><br><span class="line">print_nl:</span><br><span class="line"> pusha</span><br><span class="line"> mov ax, #0x0e0d</span><br><span class="line"> int 0x10</span><br><span class="line"> mov al, #0x0a</span><br><span class="line"> int 0x10</span><br><span class="line"> popa</span><br><span class="line"> ret</span><br><span class="line"> </span><br><span class="line">mem:</span><br><span class="line"> .byte 13,10</span><br><span class="line"> .ascii "extended memory size: "</span><br><span class="line"> .byte 0</span><br><span class="line">cyl:</span><br><span class="line"> .byte 13,10</span><br><span class="line"> .ascii "cylinders: "</span><br><span class="line"> .byte 0</span><br><span class="line">head:</span><br><span class="line"> .byte 13,10</span><br><span class="line"> .ascii "heads: "</span><br><span class="line"> .byte 0</span><br><span class="line">sect:</span><br><span class="line"> .byte 13,10</span><br><span class="line"> .ascii "sectors: "</span><br><span class="line"> .byte 0</span><br><span class="line">prompt:</span><br><span class="line"> .word mem, cyl, head, sect</span><br><span class="line">info_addr:</span><br><span class="line"> .word 0x02, 0x80, 0x82, 0x8e</span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<i class="icon-price-tags icon"></i>
<ul class="article-tag-list">
<li class="article-tag-list-item">
<a href="javascript:void(0)" class="js-tag article-tag-list-link color5">操作系统</a>
</li>
</ul>
</div>
<p class="article-more-link">
<a class="article-more-a" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/1.%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%BC%95%E5%AF%BC/">展开全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<aside class="wrap-side-operation">
<div class="mod-side-operation">
<div class="jump-container" id="js-jump-container" style="display:none;">
<a href="javascript:void(0)" class="mod-side-operation__jump-to-top">
<i class="icon-font icon-back"></i>
</a>
<div id="js-jump-plan-container" class="jump-plan-container" style="top: -11px;">
<i class="icon-font icon-plane jump-plane"></i>
</div>
</div>
</div>
</aside>
<article id="post-哈工大操作系统实验/4.基于内核栈完成进程切换" class="article article-type-post article-index" itemscope itemprop="blogPost">
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/4.%E5%9F%BA%E4%BA%8E%E5%86%85%E6%A0%B8%E6%A0%88%E5%AE%8C%E6%88%90%E8%BF%9B%E7%A8%8B%E5%88%87%E6%8D%A2/">4.基于内核栈完成进程切换</a>
</h1>
<a href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/4.%E5%9F%BA%E4%BA%8E%E5%86%85%E6%A0%B8%E6%A0%88%E5%AE%8C%E6%88%90%E8%BF%9B%E7%A8%8B%E5%88%87%E6%8D%A2/" class="archive-article-date">
<time datetime="2024-07-22T06:41:31.480Z" itemprop="datePublished"><i class="icon-calendar icon"></i>2024-07-22</time>
</a>
</header>
<div class="article-entry" itemprop="articleBody">
<h1 id="实验目标"><a href="#实验目标" class="headerlink" title="实验目标"></a>实验目标</h1><p>本次实验主要将linux0.11中采用的TSS切换部分(switch_to实现)去掉,改写成一段基于堆栈切换的代码程序。<br>![[基于tss的进程切换过程.jpg]]</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">#define _TSS(n) ((((unsigned long) n)<<4)+(FIRST_TSS_ENTRY<<3))</span><br><span class="line">/*</span><br><span class="line"> * switch_to(n) should switch tasks to task nr n, first</span><br><span class="line"> * checking that n isn't the current task, in which case it does nothing.</span><br><span class="line"> * This also clears the TS-flag if the task we switched to has used</span><br><span class="line"> * tha math co-processor latest.</span><br><span class="line"> */</span><br><span class="line">#define switch_to(n) {\</span><br><span class="line">struct {long a,b;} __tmp; \</span><br><span class="line">__asm__("cmpl %%ecx,current\n\t" \</span><br><span class="line"> "je 1f\n\t" \</span><br><span class="line"> "movw %%dx,%1\n\t" \</span><br><span class="line"> "xchgl %%ecx,current\n\t" \</span><br><span class="line"> "ljmp *%0\n\t" \</span><br><span class="line"> "cmpl %%ecx,last_task_used_math\n\t" \</span><br><span class="line"> "jne 1f\n\t" \</span><br><span class="line"> "clts\n" \</span><br><span class="line"> "1:" \</span><br><span class="line"> ::"m" (*&__tmp.a),"m" (*&__tmp.b), \</span><br><span class="line"> "d" (_TSS(n)),"c" ((long) task[n])); \</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<blockquote>
<p>switch_to函数解释</p>
<ol>
<li>是否是要切换到当前进程,不是则继续</li>
<li>将即将切换的tss段描述符(2个字节)放到_tmp.b(long占四个字节,高2字节不用)中</li>
<li>注意ljmp指令执行时,当指定参数对应的段描述符是tss段时,就会执行cpu切换工作,而不是长跳转,具体来说,CPU 会进行以下操作:<ol>
<li><strong>保存当前任务状态</strong>:当前任务的状态会被保存到当前任务状态段(TSS)中。这包括所有的寄存器状态、堆栈指针等。</li>
<li><strong>加载新任务状态</strong>:CPU 会从目标任务的 TSS 中加载新的任务状态。这包括新的寄存器状态、堆栈指针等。</li>
<li><strong>切换任务</strong>:CPU 会切换到新的任务,执行新的任务的代码。</li>
</ol>
</li>
<li>宏<code>_TSS(n)</code>对应进程n的TSS在GDT表中的位置,又因为每个进程对应一个TSS和一个LDT,每个描述符的长度都是8个字节,所以要乘以16</li>
</ol>
</blockquote>
<h1 id="实验步骤"><a href="#实验步骤" class="headerlink" title="实验步骤"></a>实验步骤</h1><p>![[基于内核栈完成进程切换.png]]<br>上图是基于内核栈完成进程切换的基本步骤,主要在switch_to中体现:</p>
<h2 id="重写switch-to-(system-call-s中)"><a href="#重写switch-to-(system-call-s中)" class="headerlink" title="重写switch_to (system_call.s中)"></a>重写switch_to (system_call.s中)</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">schedule</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">{</span><br><span class="line"> <span class="type">int</span> i,next,c;</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">task_struct</span> ** <span class="title">p</span>;</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">task_struct</span> * <span class="title">pnext</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* check alarm, wake up any interruptible tasks that have got a signal */</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span>(p = &LAST_TASK ; p > &FIRST_TASK ; --p)</span><br><span class="line"> <span class="keyword">if</span> (*p) {</span><br><span class="line"> <span class="keyword">if</span> ((*p)->alarm && (*p)->alarm < jiffies) {</span><br><span class="line"> (*p)->signal |= (<span class="number">1</span><<(SIGALRM<span class="number">-1</span>));</span><br><span class="line"> (*p)->alarm = <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (((*p)->signal & ~(_BLOCKABLE & (*p)->blocked)) &&</span><br><span class="line"> (*p)->state==TASK_INTERRUPTIBLE)</span><br><span class="line"> (*p)->state=TASK_RUNNING;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="comment">/* this is the scheduler proper: */</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">while</span> (<span class="number">1</span>) {</span><br><span class="line"> c = <span class="number">-1</span>;</span><br><span class="line"> next = <span class="number">0</span>;</span><br><span class="line"> <span class="comment">/*notice:这里一定要加,否则当没有进程调度时,pnext是空值*/</span></span><br><span class="line"> pnext = task[next];</span><br><span class="line"> </span><br><span class="line"> i = NR_TASKS;</span><br><span class="line"> p = &task[NR_TASKS];</span><br><span class="line"> <span class="keyword">while</span> (--i) {</span><br><span class="line"> <span class="keyword">if</span> (!*--p)</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">if</span> ((*p)->state == TASK_RUNNING && (*p)->counter > c)</span><br><span class="line"> c = (*p)->counter, next = i, pnext = *p;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (c) <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">for</span>(p = &LAST_TASK ; p > &FIRST_TASK ; --p)</span><br><span class="line"> <span class="keyword">if</span> (*p)</span><br><span class="line"> (*p)->counter = ((*p)->counter >> <span class="number">1</span>) +</span><br><span class="line"> (*p)->priority;</span><br><span class="line"> }</span><br><span class="line"> switch_to(pnext, _LDT(next));</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">switch_to:</span><br><span class="line"> pushl %ebp</span><br><span class="line"> movl %esp,%ebp</span><br><span class="line"> pushl %ecx</span><br><span class="line"> pushl %ebx</span><br><span class="line"> pushl %eax</span><br><span class="line"> movl <span class="number">8</span>(%ebp),%ebx</span><br><span class="line"> cmpl %ebx,current</span><br><span class="line"> je <span class="number">1f</span></span><br><span class="line"> ! 切换PCB</span><br><span class="line"> movl %ebx,%eax</span><br><span class="line"> xchgl %eax,current</span><br><span class="line"> ! 小插曲:利用<span class="number">0</span>号进程的tss段,将其ESP0指向当前进程内核栈</span><br><span class="line"> movl tss,%ecx</span><br><span class="line"> addl $<span class="number">4096</span>,%ebx</span><br><span class="line"> movl %ebx,ESP0(%ecx)</span><br><span class="line"> ! 切换内核栈,这里注意task_struct中需要新增kernel_stack这个域</span><br><span class="line"> movl %esp,kernel_stack(%eax)</span><br><span class="line"> movl <span class="number">8</span>(%ebp),%ebx</span><br><span class="line"> movl <span class="title function_">kernel_stack</span><span class="params">(%ebx)</span>,%esp</span><br><span class="line"> ! 切换LDT</span><br><span class="line"> movl 12<span class="params">(%ebp)</span>,%ecx</span><br><span class="line"> lldt %cx</span><br><span class="line"> ! 小插曲:由于影子寄存器的存在,需要重新载入fs寄存器</span><br><span class="line"> movl $0x17,%ecx</span><br><span class="line"> mov %cx,%fs</span><br><span class="line"> ! 处理协处理器,暂不分析</span><br><span class="line"> cmpl %eax,last_task_used_math</span><br><span class="line"> jne 1f</span><br><span class="line"> clts</span><br><span class="line">1: popl %eax</span><br><span class="line"> popl %ebx</span><br><span class="line"> popl %ecx</span><br><span class="line"> popl %ebp</span><br><span class="line"> ret</span><br></pre></td></tr></table></figure>
<blockquote>
<p>在 Linux 0.11 中,每个进程的内核栈和 PCB(进程控制块)都被分配在同一页(4KB)内存中。PCB 位于页的低地址部分,内核栈位于页的高地址部分。内核栈从页的高地址向低地址增长,而 PCB 位于低地址部分。</p>
</blockquote>
<p>在<code>task_struct</code>结构体中增加<code>kernelstack</code>域:</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">task_struct</span> {</span></span><br><span class="line"><span class="comment">/* these are hardcoded - don't touch */</span></span><br><span class="line"> <span class="type">long</span> state; <span class="comment">/* -1 unrunnable, 0 runnable, >0 stopped */</span></span><br><span class="line"> <span class="type">long</span> counter;</span><br><span class="line"> <span class="type">long</span> priority;</span><br><span class="line"> <span class="type">long</span> kernelstack;</span><br><span class="line"> ...</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><ol>
<li>增加<code>kernelstack</code>域后,需要对应修改<code>system_call.s</code>中的偏移值:<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">state = <span class="number">0</span> <span class="meta"># these are offsets into the task_struct.</span></span><br><span class="line">counter = <span class="number">4</span></span><br><span class="line">priority = <span class="number">8</span></span><br><span class="line">kernel_stack = <span class="number">12</span></span><br><span class="line">signal = <span class="number">16</span></span><br><span class="line">sigaction = <span class="number">20</span> # MUST be <span class="number">16</span> (=len of sigaction)</span><br><span class="line">blocked = (<span class="number">32</span>*<span class="number">16</span>) + <span class="number">20</span></span><br></pre></td></tr></table></figure>
之前没有修改此处,造成了进程<code>signal</code>的取值错误,本来应该是0,结果取成了乱七八糟的值,结果判断进程退出,系统不停重启,这个是在调试<code>ret_from_sys_call</code>的过程中发现<code>current->signal</code>和赋值后的<code>ebx</code>不相同,<code>ebx</code>应该为0,却不为0:<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">movl <span class="title function_">signal</span><span class="params">(%eax)</span>,%ebx</span><br></pre></td></tr></table></figure></li>
<li><code>schedule()</code>函数中的<code>pnext</code>一定要记得赋值:<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pnext = task[next];</span><br></pre></td></tr></table></figure>
当没有进程调度时,pnext是空值,之前一直出现<code>page_fault</code>的问题,一直没有调试出来,结果参考其他人代码才发现,现象如下:<br>![[系统宕机.gif]]</li>
</ol>
<h2 id="修改现在的fork"><a href="#修改现在的fork" class="headerlink" title="修改现在的fork()"></a>修改现在的fork()</h2><p>需要将新建进程的用户栈、用户程序地址和其内核栈关联起来,因为使用tss切换时没有做这样的关联。<br>fork()要求父子进程共享用户代码、用户数据和用户堆栈。<br>![[fork中父子进程的用户栈内核栈之间的关系.jpg]]</p>
<p>这里对应切回用户栈和用户代码的操作,<code>switch_to</code>最后的<code>ret</code>会跳到从内核栈中弹出的地址处执行,对应<code>first_return_from_kernel</code>。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">krnstack = (<span class="type">long</span>*)(PAGE_SIZE + (<span class="type">long</span>) p);</span><br><span class="line"></span><br><span class="line"><span class="comment">/*下面这部分和first_return_from_kernel中的iret对应</span></span><br><span class="line"><span class="comment">也使得子进程与父进程共享用户代码、用户数据和用户堆栈*/</span></span><br><span class="line">*(--krnstack) = ss & <span class="number">0xffff</span>; <span class="comment">/*Segment selector for user stacks*/</span></span><br><span class="line">*(--krnstack) = esp; <span class="comment">/*offset of user stacks*/</span></span><br><span class="line">*(--krnstack) = eflags;</span><br><span class="line">*(--krnstack) = cs & <span class="number">0xffff</span>;</span><br><span class="line">*(--krnstack) = eip;</span><br><span class="line"><span class="comment">/*下面这部分和first_return_from_kernel中的pop寄存器对应*/</span></span><br><span class="line">*(--krnstack) = ds & <span class="number">0xffff</span>;</span><br><span class="line">*(--krnstack) = es & <span class="number">0xffff</span>;</span><br><span class="line">*(--krnstack) = fs & <span class="number">0xffff</span>;</span><br><span class="line">*(--krnstack) = gs & <span class="number">0xffff</span>;</span><br><span class="line">*(--krnstack) = esi;</span><br><span class="line">*(--krnstack) = edi;</span><br><span class="line">*(--krnstack) = edx;</span><br><span class="line"><span class="comment">/*first_return_from_kernel地址*/</span></span><br><span class="line">*(--krnstack) = (<span class="type">long</span>) first_return_from_kernel;</span><br><span class="line"><span class="comment">/*下面这部分对应switch_to函数中的1标号处的pop寄存器</span></span><br><span class="line"><span class="comment">因为运行到1标号时,已经切换到新的进程,如果新建进程的内核栈没有与之对应的寄存器,就会出错*/</span></span><br><span class="line">*(--krnstack) = ebp;</span><br><span class="line">*(--krnstack) = ecx;</span><br><span class="line">*(--krnstack) = ebx;</span><br><span class="line">*(--krnstack) = <span class="number">0</span>; <span class="comment">/* eax */</span></span><br><span class="line">p->kernelstack = krnstack;</span><br></pre></td></tr></table></figure>
<p><code>first_return_from_kernel</code>就担负起跳回用户态的重任,就是设置相应的寄存器,与fork()初始化操作对应。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">first_return_from_kernel:</span><br><span class="line"> popl %edx</span><br><span class="line"> popl %edi</span><br><span class="line"> popl %esi</span><br><span class="line"> pop %gs</span><br><span class="line"> pop %fs</span><br><span class="line"> pop %es</span><br><span class="line"> pop %ds</span><br><span class="line"> iret</span><br></pre></td></tr></table></figure>
<blockquote>
<p>IRET 指令在从特权级返回时会处理更多的寄存器,具体情况如下:<br>• 当从低特权级(如用户模式,CPL=3)返回到高特权级(如内核模式,CPL=0)时,需要恢复更多的寄存器状态,这包括 SS 和 ESP 寄存器。</p>
<ol>
<li>POP EIP(指令指针)</li>
<li>POP CS(代码段寄存器)</li>
<li>POP EFLAGS(标志寄存器)</li>
<li>POP ESP(栈指针)</li>
<li>POP SS(栈段寄存器)<br>• 而在相同特权级之间的中断处理,则只需要恢复 EIP、CS 和 EFLAGS 寄存器。</li>
<li>POP EIP</li>
<li>POP CS</li>
<li>POP EFLAGS</li>
</ol>
</blockquote>
<h1 id="实验结果"><a href="#实验结果" class="headerlink" title="实验结果"></a>实验结果</h1><p>正常运行<br>![[基于内核栈切换线程实验结果.png]]</p>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<i class="icon-price-tags icon"></i>
<ul class="article-tag-list">
<li class="article-tag-list-item">
<a href="javascript:void(0)" class="js-tag article-tag-list-link color5">操作系统</a>
</li>
</ul>
</div>
<p class="article-more-link">
<a class="article-more-a" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/4.%E5%9F%BA%E4%BA%8E%E5%86%85%E6%A0%B8%E6%A0%88%E5%AE%8C%E6%88%90%E8%BF%9B%E7%A8%8B%E5%88%87%E6%8D%A2/">展开全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<aside class="wrap-side-operation">
<div class="mod-side-operation">
<div class="jump-container" id="js-jump-container" style="display:none;">
<a href="javascript:void(0)" class="mod-side-operation__jump-to-top">
<i class="icon-font icon-back"></i>
</a>
<div id="js-jump-plan-container" class="jump-plan-container" style="top: -11px;">
<i class="icon-font icon-plane jump-plane"></i>
</div>
</div>
</div>
</aside>
<article id="post-哈工大操作系统实验/7.终端设备字符显示的控制" class="article article-type-post article-index" itemscope itemprop="blogPost">
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/7.%E7%BB%88%E7%AB%AF%E8%AE%BE%E5%A4%87%E5%AD%97%E7%AC%A6%E6%98%BE%E7%A4%BA%E7%9A%84%E6%8E%A7%E5%88%B6/">7.终端设备字符显示的控制</a>
</h1>
<a href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/7.%E7%BB%88%E7%AB%AF%E8%AE%BE%E5%A4%87%E5%AD%97%E7%AC%A6%E6%98%BE%E7%A4%BA%E7%9A%84%E6%8E%A7%E5%88%B6/" class="archive-article-date">
<time datetime="2024-07-22T06:41:31.464Z" itemprop="datePublished"><i class="icon-calendar icon"></i>2024-07-22</time>
</a>
</header>
<div class="article-entry" itemprop="articleBody">
<p>keyboard.S中,添加一个<code>f12state</code>byte来存储f12的状态</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">.globl f12state</span><br><span class="line">f12state: .byte <span class="number">0</span></span><br><span class="line"></span><br><span class="line">func:</span><br><span class="line"> ...</span><br><span class="line"> ...</span><br><span class="line"> cmpb $<span class="number">11</span>, %al</span><br><span class="line"> ja end_func</span><br><span class="line"> notb f12state <span class="comment">// 这一句是添加的</span></span><br><span class="line"> ...</span><br></pre></td></tr></table></figure>
<p>tty_io中在<code>tty_write</code>中添加改变字母为<code>*</code>的代码</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">extern</span> <span class="type">char</span> f12state;</span><br><span class="line">...</span><br><span class="line"><span class="keyword">while</span> (nr><span class="number">0</span>) {</span><br><span class="line"> sleep_if_full(&tty->write_q);</span><br><span class="line"> <span class="keyword">if</span> (current->signal)</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">while</span> (nr><span class="number">0</span> && !FULL(tty->write_q)) {</span><br><span class="line"> c=get_fs_byte(b);</span><br><span class="line"> <span class="keyword">if</span> (O_POST(tty)) {</span><br><span class="line"> <span class="keyword">if</span> (c==<span class="string">'\r'</span> && O_CRNL(tty))</span><br><span class="line"> c=<span class="string">'\n'</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span> (c==<span class="string">'\n'</span> && O_NLRET(tty))</span><br><span class="line"> c=<span class="string">'\r'</span>;</span><br><span class="line"> <span class="keyword">if</span> (c==<span class="string">'\n'</span> && !cr_flag && O_NLCR(tty)) {</span><br><span class="line"> cr_flag = <span class="number">1</span>;</span><br><span class="line"> PUTCH(<span class="number">13</span>,tty->write_q);</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (O_LCUC(tty))</span><br><span class="line"> c=<span class="built_in">toupper</span>(c);</span><br><span class="line"> }</span><br><span class="line"> b++; nr--;</span><br><span class="line"> cr_flag = <span class="number">0</span>;</span><br><span class="line"> <span class="comment">//以下是添加的代码</span></span><br><span class="line"> <span class="keyword">if</span>(f12state){</span><br><span class="line"> <span class="keyword">if</span>(c >= <span class="string">'a'</span> && c <= <span class="string">'z'</span> || c >= <span class="string">'A'</span> && c <= <span class="string">'Z'</span>){</span><br><span class="line"> c = <span class="string">'*'</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> PUTCH(c,tty->write_q);</span><br><span class="line"> }</span><br><span class="line"> tty->write(tty);</span><br><span class="line"> <span class="keyword">if</span> (nr><span class="number">0</span>)</span><br><span class="line"> schedule();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> (b-buf);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>按下f12实验结果<br>![[操作系统实验七结果1.png]]<br>重新按下f12实验结果<br>![[操作系统实验七实验结果2.png]]</p>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<i class="icon-price-tags icon"></i>
<ul class="article-tag-list">
<li class="article-tag-list-item">
<a href="javascript:void(0)" class="js-tag article-tag-list-link color5">操作系统</a>
</li>
</ul>
</div>
<p class="article-more-link">
<a class="article-more-a" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/7.%E7%BB%88%E7%AB%AF%E8%AE%BE%E5%A4%87%E5%AD%97%E7%AC%A6%E6%98%BE%E7%A4%BA%E7%9A%84%E6%8E%A7%E5%88%B6/">展开全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<aside class="wrap-side-operation">
<div class="mod-side-operation">
<div class="jump-container" id="js-jump-container" style="display:none;">
<a href="javascript:void(0)" class="mod-side-operation__jump-to-top">
<i class="icon-font icon-back"></i>
</a>
<div id="js-jump-plan-container" class="jump-plan-container" style="top: -11px;">
<i class="icon-font icon-plane jump-plane"></i>
</div>
</div>
</div>
</aside>
<article id="post-哈工大操作系统实验/8.Proc文件系统的实现" class="article article-type-post article-index" itemscope itemprop="blogPost">
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/8.Proc%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%AE%9E%E7%8E%B0/">8.Proc文件系统的实现</a>
</h1>
<a href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/8.Proc%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%AE%9E%E7%8E%B0/" class="archive-article-date">
<time datetime="2024-07-22T06:41:31.453Z" itemprop="datePublished"><i class="icon-calendar icon"></i>2024-07-22</time>
</a>
</header>
<div class="article-entry" itemprop="articleBody">
<p>proc.c</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><linux/proc.h></span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">psinfo</span><span class="params">(<span class="type">char</span>* buf, <span class="type">char</span>* tmp)</span>{</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">task_struct</span> **<span class="title">p</span>;</span></span><br><span class="line"> <span class="type">int</span> n = <span class="number">0</span>;</span><br><span class="line"> n = <span class="built_in">sprintf</span>(tmp,<span class="string">"pid\tstate\tfather\tcounter\tstart_time\n"</span>);</span><br><span class="line"> <span class="keyword">for</span>(p = &FIRST_TASK; p <= &LAST_TASK; p++){</span><br><span class="line"> <span class="keyword">if</span>(*p){</span><br><span class="line"> n += <span class="built_in">sprintf</span>((tmp+n),<span class="string">"%ld\t%ld\t%ld\t%ld\t%ld\n"</span>,(*p)->pid,(*p)->state,(*p)->father,(*p)->counter,(*p)->start_time);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> n;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">hdinfo</span><span class="params">(<span class="type">char</span>* buf, <span class="type">char</span>* tmp)</span>{</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">super_block</span> *<span class="title">sb</span>;</span></span><br><span class="line"> sb = get_super(ROOT_DEV);</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">buffer_head</span> *<span class="title">bh</span>;</span></span><br><span class="line"> <span class="type">int</span> n = <span class="number">0</span>;</span><br><span class="line"> <span class="type">unsigned</span> <span class="type">short</span> total_blocks = sb->s_nzones;</span><br><span class="line"> <span class="type">unsigned</span> <span class="type">short</span> total_inodes = sb->s_ninodes;</span><br><span class="line"> <span class="type">unsigned</span> <span class="type">short</span> s_zmap_blocks = sb->s_zmap_blocks;</span><br><span class="line"> <span class="type">unsigned</span> <span class="type">short</span> free_blocks = <span class="number">0</span>;</span><br><span class="line"> <span class="type">unsigned</span> <span class="type">short</span> used_blocks = <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < s_zmap_blocks; i++){</span><br><span class="line"> bh = sb->s_zmap[i];</span><br><span class="line"> <span class="type">char</span>* p = bh->b_data;</span><br><span class="line"> <span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(j = <span class="number">0</span>; j < <span class="number">1024</span>; j++){</span><br><span class="line"> <span class="keyword">if</span>(*(p+j) == <span class="number">0</span>){</span><br><span class="line"> free_blocks++;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> used_blocks++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> n += <span class="built_in">sprintf</span>(tmp,<span class="string">"total_blocks: %d\n"</span>,total_blocks);</span><br><span class="line"> n += <span class="built_in">sprintf</span>(tmp+n,<span class="string">"total_inodes: %d\n"</span>,total_inodes);</span><br><span class="line"> n += <span class="built_in">sprintf</span>(tmp+n,<span class="string">"free_blocks: %d\n"</span>,free_blocks);</span><br><span class="line"> n += <span class="built_in">sprintf</span>(tmp+n,<span class="string">"used_blocks: %d\n"</span>,used_blocks);</span><br><span class="line"> <span class="keyword">return</span> n;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">proc_handler</span><span class="params">(<span class="type">int</span> dev,<span class="type">char</span>* buf,<span class="type">int</span> count,<span class="type">off_t</span>* f_pos)</span>{</span><br><span class="line"> <span class="type">char</span>* tmp;</span><br><span class="line"> <span class="type">char</span>* f_p;</span><br><span class="line"> <span class="type">int</span> n = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span>(count > PAGE_SIZE) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> </span><br><span class="line"> tmp = <span class="built_in">malloc</span>(PAGE_SIZE);</span><br><span class="line"> f_p = tmp + *f_pos;</span><br><span class="line"> <span class="keyword">if</span>(dev == <span class="number">0</span>){</span><br><span class="line"> n = psinfo(buf,tmp);</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>(dev == <span class="number">1</span>){</span><br><span class="line"> n = hdinfo(buf,tmp);</span><br><span class="line"> }</span><br><span class="line"> n -= *f_pos;</span><br><span class="line"> n = count = n > count? count : n;</span><br><span class="line"> <span class="keyword">while</span>(n--){</span><br><span class="line"> put_fs_byte(*(f_p++),buf++);</span><br><span class="line"> (*f_pos)++;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">free</span>(tmp);</span><br><span class="line"> <span class="keyword">return</span> count;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>proc.h</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">ifndef</span> PROC_H</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROC_H</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><linux/kernel.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><linux/mm.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><linux/sched.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><sys/types.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><asm/segment.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><linux/fs.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">proc_handler</span><span class="params">(<span class="type">int</span> dev, <span class="type">char</span>* buf, <span class="type">int</span> count, <span class="type">off_t</span>* f_pos)</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
<h1 id="实验结果"><a href="#实验结果" class="headerlink" title="实验结果"></a>实验结果</h1><p>![[操作系统实验8结果.png]]</p>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<i class="icon-price-tags icon"></i>
<ul class="article-tag-list">
<li class="article-tag-list-item">
<a href="javascript:void(0)" class="js-tag article-tag-list-link color5">操作系统</a>
</li>
</ul>
</div>
<p class="article-more-link">
<a class="article-more-a" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/8.Proc%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%AE%9E%E7%8E%B0/">展开全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<aside class="wrap-side-operation">
<div class="mod-side-operation">
<div class="jump-container" id="js-jump-container" style="display:none;">
<a href="javascript:void(0)" class="mod-side-operation__jump-to-top">
<i class="icon-font icon-back"></i>
</a>
<div id="js-jump-plan-container" class="jump-plan-container" style="top: -11px;">
<i class="icon-font icon-plane jump-plane"></i>
</div>
</div>
</div>
</aside>
<article id="post-哈工大操作系统实验/5.信号量的实现与应用" class="article article-type-post article-index" itemscope itemprop="blogPost">
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/5.%E4%BF%A1%E5%8F%B7%E9%87%8F%E7%9A%84%E5%AE%9E%E7%8E%B0%E4%B8%8E%E5%BA%94%E7%94%A8/">5.信号量的实现与应用</a>
</h1>
<a href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/5.%E4%BF%A1%E5%8F%B7%E9%87%8F%E7%9A%84%E5%AE%9E%E7%8E%B0%E4%B8%8E%E5%BA%94%E7%94%A8/" class="archive-article-date">
<time datetime="2024-07-22T06:41:31.428Z" itemprop="datePublished"><i class="icon-calendar icon"></i>2024-07-22</time>
</a>
</header>
<div class="article-entry" itemprop="articleBody">
<h1 id="pc-c"><a href="#pc-c" class="headerlink" title="pc.c"></a>pc.c</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><semaphore.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><unistd.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdlib.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><fcntl.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><sys/wait.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> N 5</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> M 500</span></span><br><span class="line"></span><br><span class="line">FILE *fp_write, *fp_read;</span><br><span class="line"><span class="type">sem_t</span> *empty, *full, *mutex;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">Producer</span><span class="params">()</span>{</span><br><span class="line"> <span class="type">int</span> i;</span><br><span class="line"> fflush(<span class="built_in">stdout</span>); </span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < M; i++){</span><br><span class="line"> sem_wait(empty);</span><br><span class="line"> sem_wait(mutex);</span><br><span class="line"> fwrite(&i, <span class="keyword">sizeof</span>(<span class="type">int</span>), <span class="number">1</span>, fp_write);</span><br><span class="line"> fflush(fp_write);</span><br><span class="line"> sem_post(mutex);</span><br><span class="line"> sem_post(full);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">Consumer</span><span class="params">()</span>{</span><br><span class="line"> <span class="type">int</span> num;</span><br><span class="line"> fflush(<span class="built_in">stdout</span>);</span><br><span class="line"> <span class="keyword">while</span>(<span class="number">1</span>){</span><br><span class="line"> sem_wait(full);</span><br><span class="line"> sem_wait(mutex);</span><br><span class="line"> fread(&num, <span class="keyword">sizeof</span>(<span class="type">int</span>), <span class="number">1</span>, fp_read);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d: %d\n"</span>, getpid(), num);</span><br><span class="line"> fflush(<span class="built_in">stdout</span>);</span><br><span class="line"> sem_post(mutex);</span><br><span class="line"> sem_post(empty);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>{</span><br><span class="line"> <span class="type">int</span> i;</span><br><span class="line"> fp_write = fopen(<span class="string">"buffer.txt"</span>, <span class="string">"w+"</span>);</span><br><span class="line"> fp_read = fopen(<span class="string">"buffer.txt"</span>, <span class="string">"r"</span>);</span><br><span class="line"> setvbuf(fp_read, <span class="literal">NULL</span>, _IONBF, <span class="number">0</span>);</span><br><span class="line"> <span class="keyword">if</span>(fp_write == <span class="literal">NULL</span> || fp_read == <span class="literal">NULL</span>){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"File open error!\n"</span>);</span><br><span class="line"> <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> empty = sem_open(<span class="string">"/myempty"</span>, <span class="number">10</span>);</span><br><span class="line"> <span class="keyword">if</span>(!empty){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"sem_open empty error!\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> full = sem_open(<span class="string">"/myfull"</span>, <span class="number">0</span>);</span><br><span class="line"> <span class="keyword">if</span>(!full){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"sem_open full error!\n"</span>);</span><br><span class="line"> }</span><br><span class="line"> mutex = sem_open(<span class="string">"/mymutex"</span>, <span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span>(!mutex){</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"sem_open mutex error!\n"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < N; i++){</span><br><span class="line"> <span class="keyword">if</span>(fork() == <span class="number">0</span>){</span><br><span class="line"> Consumer();</span><br><span class="line"> <span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> Producer();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < N; i++){</span><br><span class="line"> wait(<span class="literal">NULL</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> fclose(fp_write);</span><br><span class="line"> fclose(fp_read);</span><br><span class="line"> sem_unlink(<span class="string">"/myempty"</span>);</span><br><span class="line"> sem_unlink(<span class="string">"/myfull"</span>);</span><br><span class="line"> sem_unlink(<span class="string">"/mymutex"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意:<br> 1.多进程编程中,注意printf后使用fflush刷新输出缓冲区,否则当fork出子进程后,子进程会拷贝父进程的缓冲区,于是子进程也会重复输出,导致输出混乱<br>2. 另外注意使用setvbuf将read设置成无缓冲,否则当其他进程修改buffer后,缓冲区没有同步修改,会造成读入错误。</p>
</blockquote>
<h1 id="系统调用实现"><a href="#系统调用实现" class="headerlink" title="系统调用实现"></a>系统调用实现</h1><p>增加四个系统调用<code>sys_sem_open</code>, <code>sys_sem_wait</code>, <code>sys_sem_post</code>, <code>sys_sem_unlink</code>,具体增加的步骤同实验2系统调用,这里只说明函数实现方式:<br>semaphore.h</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> __LIBRARY__</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><unistd.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> _LINUX_SEM_H</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> _LINUX_SEM_H</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><linux/sched.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAX_SEM_NUM 10</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SEM_NAME_LEN 32</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">sem_t</span> <span class="title">sem_t</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">sem_t</span> {</span></span><br><span class="line"> <span class="type">char</span> * name;</span><br><span class="line"> <span class="type">unsigned</span> <span class="type">int</span> sem_value;</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">task_struct</span> * <span class="title">sem_wait</span>;</span></span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">sem_t</span> <span class="title">sem_array</span>[<span class="title">MAX_SEM_NUM</span>];</span></span><br><span class="line"><span class="type">unsigned</span> <span class="type">int</span> sem_bitmap = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">_syscall2(<span class="type">sem_t</span>*, sem_open, <span class="type">const</span> <span class="type">char</span>*, name, <span class="type">unsigned</span> <span class="type">int</span>, value);</span><br><span class="line">_syscall1(<span class="type">int</span>, sem_wait, <span class="type">sem_t</span>*, sem);</span><br><span class="line">_syscall1(<span class="type">int</span>, sem_post, <span class="type">sem_t</span>*, sem);</span><br><span class="line">_syscall1(<span class="type">int</span>, sem_unlink, <span class="type">const</span> <span class="type">char</span>*, name);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
<p>semaphore.c</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><semaphore.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><asm/system.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><asm/segment.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><linux/kernel.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="type">sem_t</span>* <span class="title function_">sys_sem_open</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *name, <span class="type">unsigned</span> <span class="type">int</span> value)</span> {</span><br><span class="line"> <span class="comment">// assign num for sem</span></span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span>, min_i = MAX_SEM_NUM;</span><br><span class="line"> <span class="type">int</span> name_len = <span class="number">0</span>;</span><br><span class="line"> <span class="type">char</span> c;</span><br><span class="line"> <span class="type">char</span> tmp_name[SEM_NAME_LEN];</span><br><span class="line"></span><br><span class="line"> <span class="keyword">while</span>(c = get_fs_byte(name++)){</span><br><span class="line"> tmp_name[name_len] = c;</span><br><span class="line"> name_len++;</span><br><span class="line"> }</span><br><span class="line"> tmp_name[name_len] = <span class="string">'\0'</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < MAX_SEM_NUM; i++){</span><br><span class="line"> <span class="keyword">if</span>(sem_bitmap & (<span class="number">1</span> << i)){</span><br><span class="line"> <span class="keyword">if</span>(!<span class="built_in">strcmp</span>(tmp_name, sem_array[i].name)){</span><br><span class="line"> sem_array[i].sem_cnt++;</span><br><span class="line"> printk(<span class="string">"sem_open sem name: %s has already exist, addr: %x\n"</span>, tmp_name, &sem_array[i]);</span><br><span class="line"> <span class="keyword">return</span> &sem_array[i];</span><br><span class="line"> } </span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> min_i = i < min_i ? i : min_i;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span>(min_i == MAX_SEM_NUM){</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">sem_t</span>* <span class="title">sem</span> =</span> &sem_array[min_i];</span><br><span class="line"> sem->name = <span class="built_in">malloc</span>(SEM_NAME_LEN);</span><br><span class="line"> <span class="built_in">strcpy</span>(sem->name, tmp_name);</span><br><span class="line"> sem->sem_value = value;</span><br><span class="line"> sem->sem_wait = <span class="literal">NULL</span>;</span><br><span class="line"> sem->sem_cnt++;</span><br><span class="line"> sem_bitmap |= (<span class="number">1</span> << min_i);</span><br><span class="line"> printk(<span class="string">"sem_open sem name: %s, addr: %x\n"</span>, sem->name, sem);</span><br><span class="line"> <span class="keyword">return</span> sem;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">sys_sem_wait</span><span class="params">(<span class="keyword">struct</span> <span class="type">sem_t</span>* sem)</span> {</span><br><span class="line"> <span class="keyword">while</span>(sem->sem_value <= <span class="number">0</span>){</span><br><span class="line"> sleep_on(&sem->sem_wait);</span><br><span class="line"> }</span><br><span class="line"> cli();</span><br><span class="line"> sem->sem_value--;</span><br><span class="line"> sti();</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">sys_sem_post</span><span class="params">(<span class="keyword">struct</span> <span class="type">sem_t</span>* sem)</span> {</span><br><span class="line"> cli();</span><br><span class="line"> sem->sem_value++;</span><br><span class="line"> sti();</span><br><span class="line"> wake_up(&sem->sem_wait);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">sys_sem_unlink</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *name)</span> {</span><br><span class="line"> <span class="type">char</span> kname[SEM_NAME_LEN];</span><br><span class="line"> <span class="type">int</span> name_len = <span class="number">0</span>;</span><br><span class="line"> <span class="type">char</span> c;</span><br><span class="line"> <span class="keyword">while</span>(c = get_fs_byte(name++)){</span><br><span class="line"> kname[name_len] = c;</span><br><span class="line"> name_len++;</span><br><span class="line"> }</span><br><span class="line"> kname[name_len] = <span class="string">'\0'</span>;</span><br><span class="line"> printk(<span class="string">"sem_unlink name: %s\n"</span>, kname);</span><br><span class="line"> <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(i = <span class="number">0</span>; i < MAX_SEM_NUM; i++){</span><br><span class="line"> <span class="keyword">if</span>(!<span class="built_in">strcmp</span>(sem_array[i].name, kname) && (sem_bitmap & (<span class="number">1</span> << i))){</span><br><span class="line"> cli();</span><br><span class="line"> sem_array[i].sem_cnt--;</span><br><span class="line"> sti();</span><br><span class="line"> <span class="keyword">if</span>(!sem_array[i].sem_cnt){</span><br><span class="line"> sem_bitmap &= ~(<span class="number">1</span> << i);</span><br><span class="line"> printk(<span class="string">"free: %s, sem_cnt: %d\n"</span>,sem_array[i].name, sem_array[i].sem_cnt);</span><br><span class="line"> <span class="built_in">free</span>(sem_array[i].name);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意当sys_sem_open传入相同的name时,需要搜索一下,不能直接创建,做实验6的时候才发现这个bug</p>
</blockquote>
<h1 id="实验结果"><a href="#实验结果" class="headerlink" title="实验结果"></a>实验结果</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br><span class="line">457</span><br><span class="line">458</span><br><span class="line">459</span><br><span class="line">460</span><br><span class="line">461</span><br><span class="line">462</span><br><span class="line">463</span><br><span class="line">464</span><br><span class="line">465</span><br><span class="line">466</span><br><span class="line">467</span><br><span class="line">468</span><br><span class="line">469</span><br><span class="line">470</span><br><span class="line">471</span><br><span class="line">472</span><br><span class="line">473</span><br><span class="line">474</span><br><span class="line">475</span><br><span class="line">476</span><br><span class="line">477</span><br><span class="line">478</span><br><span class="line">479</span><br><span class="line">480</span><br><span class="line">481</span><br><span class="line">482</span><br><span class="line">483</span><br><span class="line">484</span><br><span class="line">485</span><br><span class="line">486</span><br><span class="line">487</span><br><span class="line">488</span><br><span class="line">489</span><br><span class="line">490</span><br><span class="line">491</span><br><span class="line">492</span><br><span class="line">493</span><br><span class="line">494</span><br><span class="line">495</span><br><span class="line">496</span><br><span class="line">497</span><br><span class="line">498</span><br><span class="line">499</span><br><span class="line">500</span><br></pre></td><td class="code"><pre><span class="line">17: 0</span><br><span class="line">17: 1</span><br><span class="line">17: 2</span><br><span class="line">17: 3</span><br><span class="line">17: 4</span><br><span class="line">17: 5</span><br><span class="line">17: 6</span><br><span class="line">17: 7</span><br><span class="line">17: 8</span><br><span class="line">17: 9</span><br><span class="line">13: 10</span><br><span class="line">13: 11</span><br><span class="line">13: 12</span><br><span class="line">13: 13</span><br><span class="line">13: 14</span><br><span class="line">13: 15</span><br><span class="line">13: 16</span><br><span class="line">13: 17</span><br><span class="line">13: 18</span><br><span class="line">13: 19</span><br><span class="line">17: 20</span><br><span class="line">17: 21</span><br><span class="line">17: 22</span><br><span class="line">17: 23</span><br><span class="line">17: 24</span><br><span class="line">17: 25</span><br><span class="line">17: 26</span><br><span class="line">17: 27</span><br><span class="line">17: 28</span><br><span class="line">17: 29</span><br><span class="line">13: 30</span><br><span class="line">13: 31</span><br><span class="line">13: 32</span><br><span class="line">13: 33</span><br><span class="line">13: 34</span><br><span class="line">13: 35</span><br><span class="line">13: 36</span><br><span class="line">13: 37</span><br><span class="line">13: 38</span><br><span class="line">13: 39</span><br><span class="line">17: 40</span><br><span class="line">17: 41</span><br><span class="line">17: 42</span><br><span class="line">17: 43</span><br><span class="line">17: 44</span><br><span class="line">17: 45</span><br><span class="line">17: 46</span><br><span class="line">17: 47</span><br><span class="line">17: 48</span><br><span class="line">17: 49</span><br><span class="line">13: 50</span><br><span class="line">13: 51</span><br><span class="line">13: 52</span><br><span class="line">13: 53</span><br><span class="line">13: 54</span><br><span class="line">13: 55</span><br><span class="line">13: 56</span><br><span class="line">13: 57</span><br><span class="line">13: 58</span><br><span class="line">13: 59</span><br><span class="line">17: 60</span><br><span class="line">17: 61</span><br><span class="line">17: 62</span><br><span class="line">17: 63</span><br><span class="line">17: 64</span><br><span class="line">17: 65</span><br><span class="line">17: 66</span><br><span class="line">17: 67</span><br><span class="line">17: 68</span><br><span class="line">17: 69</span><br><span class="line">13: 70</span><br><span class="line">13: 71</span><br><span class="line">13: 72</span><br><span class="line">13: 73</span><br><span class="line">13: 74</span><br><span class="line">13: 75</span><br><span class="line">13: 76</span><br><span class="line">13: 77</span><br><span class="line">13: 78</span><br><span class="line">13: 79</span><br><span class="line">17: 80</span><br><span class="line">17: 81</span><br><span class="line">17: 82</span><br><span class="line">17: 83</span><br><span class="line">17: 84</span><br><span class="line">17: 85</span><br><span class="line">17: 86</span><br><span class="line">17: 87</span><br><span class="line">17: 88</span><br><span class="line">17: 89</span><br><span class="line">13: 90</span><br><span class="line">13: 91</span><br><span class="line">13: 92</span><br><span class="line">13: 93</span><br><span class="line">13: 94</span><br><span class="line">13: 95</span><br><span class="line">13: 96</span><br><span class="line">13: 97</span><br><span class="line">13: 98</span><br><span class="line">13: 99</span><br><span class="line">17: 100</span><br><span class="line">17: 101</span><br><span class="line">17: 102</span><br><span class="line">17: 103</span><br><span class="line">17: 104</span><br><span class="line">17: 105</span><br><span class="line">17: 106</span><br><span class="line">17: 107</span><br><span class="line">17: 108</span><br><span class="line">17: 109</span><br><span class="line">13: 110</span><br><span class="line">13: 111</span><br><span class="line">13: 112</span><br><span class="line">13: 113</span><br><span class="line">13: 114</span><br><span class="line">13: 115</span><br><span class="line">13: 116</span><br><span class="line">13: 117</span><br><span class="line">13: 118</span><br><span class="line">13: 119</span><br><span class="line">17: 120</span><br><span class="line">17: 121</span><br><span class="line">17: 122</span><br><span class="line">17: 123</span><br><span class="line">17: 124</span><br><span class="line">17: 125</span><br><span class="line">17: 126</span><br><span class="line">17: 127</span><br><span class="line">17: 128</span><br><span class="line">17: 129</span><br><span class="line">13: 130</span><br><span class="line">13: 131</span><br><span class="line">13: 132</span><br><span class="line">13: 133</span><br><span class="line">13: 134</span><br><span class="line">13: 135</span><br><span class="line">13: 136</span><br><span class="line">13: 137</span><br><span class="line">13: 138</span><br><span class="line">13: 139</span><br><span class="line">16: 140</span><br><span class="line">16: 141</span><br><span class="line">16: 142</span><br><span class="line">16: 143</span><br><span class="line">16: 144</span><br><span class="line">16: 145</span><br><span class="line">16: 146</span><br><span class="line">16: 147</span><br><span class="line">16: 148</span><br><span class="line">16: 149</span><br><span class="line">14: 150</span><br><span class="line">14: 151</span><br><span class="line">14: 152</span><br><span class="line">14: 153</span><br><span class="line">14: 154</span><br><span class="line">14: 155</span><br><span class="line">14: 156</span><br><span class="line">14: 157</span><br><span class="line">14: 158</span><br><span class="line">14: 159</span><br><span class="line">16: 160</span><br><span class="line">16: 161</span><br><span class="line">16: 162</span><br><span class="line">16: 163</span><br><span class="line">16: 164</span><br><span class="line">16: 165</span><br><span class="line">16: 166</span><br><span class="line">16: 167</span><br><span class="line">16: 168</span><br><span class="line">16: 169</span><br><span class="line">14: 170</span><br><span class="line">14: 171</span><br><span class="line">14: 172</span><br><span class="line">14: 173</span><br><span class="line">14: 174</span><br><span class="line">14: 175</span><br><span class="line">14: 176</span><br><span class="line">14: 177</span><br><span class="line">14: 178</span><br><span class="line">14: 179</span><br><span class="line">16: 180</span><br><span class="line">16: 181</span><br><span class="line">16: 182</span><br><span class="line">16: 183</span><br><span class="line">16: 184</span><br><span class="line">16: 185</span><br><span class="line">16: 186</span><br><span class="line">16: 187</span><br><span class="line">16: 188</span><br><span class="line">16: 189</span><br><span class="line">14: 190</span><br><span class="line">14: 191</span><br><span class="line">14: 192</span><br><span class="line">14: 193</span><br><span class="line">14: 194</span><br><span class="line">14: 195</span><br><span class="line">14: 196</span><br><span class="line">14: 197</span><br><span class="line">14: 198</span><br><span class="line">14: 199</span><br><span class="line">16: 200</span><br><span class="line">16: 201</span><br><span class="line">16: 202</span><br><span class="line">16: 203</span><br><span class="line">16: 204</span><br><span class="line">16: 205</span><br><span class="line">16: 206</span><br><span class="line">16: 207</span><br><span class="line">16: 208</span><br><span class="line">16: 209</span><br><span class="line">14: 210</span><br><span class="line">14: 211</span><br><span class="line">14: 212</span><br><span class="line">14: 213</span><br><span class="line">14: 214</span><br><span class="line">14: 215</span><br><span class="line">14: 216</span><br><span class="line">14: 217</span><br><span class="line">14: 218</span><br><span class="line">14: 219</span><br><span class="line">13: 220</span><br><span class="line">13: 221</span><br><span class="line">13: 222</span><br><span class="line">13: 223</span><br><span class="line">13: 224</span><br><span class="line">13: 225</span><br><span class="line">13: 226</span><br><span class="line">13: 227</span><br><span class="line">13: 228</span><br><span class="line">13: 229</span><br><span class="line">14: 230</span><br><span class="line">14: 231</span><br><span class="line">14: 232</span><br><span class="line">14: 233</span><br><span class="line">14: 234</span><br><span class="line">14: 235</span><br><span class="line">14: 236</span><br><span class="line">14: 237</span><br><span class="line">14: 238</span><br><span class="line">14: 239</span><br><span class="line">13: 240</span><br><span class="line">13: 241</span><br><span class="line">13: 242</span><br><span class="line">13: 243</span><br><span class="line">13: 244</span><br><span class="line">13: 245</span><br><span class="line">13: 246</span><br><span class="line">13: 247</span><br><span class="line">13: 248</span><br><span class="line">13: 249</span><br><span class="line">14: 250</span><br><span class="line">14: 251</span><br><span class="line">14: 252</span><br><span class="line">14: 253</span><br><span class="line">14: 254</span><br><span class="line">14: 255</span><br><span class="line">14: 256</span><br><span class="line">14: 257</span><br><span class="line">14: 258</span><br><span class="line">14: 259</span><br><span class="line">13: 260</span><br><span class="line">13: 261</span><br><span class="line">13: 262</span><br><span class="line">13: 263</span><br><span class="line">13: 264</span><br><span class="line">13: 265</span><br><span class="line">13: 266</span><br><span class="line">13: 267</span><br><span class="line">13: 268</span><br><span class="line">13: 269</span><br><span class="line">14: 270</span><br><span class="line">14: 271</span><br><span class="line">14: 272</span><br><span class="line">14: 273</span><br><span class="line">14: 274</span><br><span class="line">14: 275</span><br><span class="line">14: 276</span><br><span class="line">14: 277</span><br><span class="line">14: 278</span><br><span class="line">14: 279</span><br><span class="line">13: 280</span><br><span class="line">13: 281</span><br><span class="line">13: 282</span><br><span class="line">13: 283</span><br><span class="line">13: 284</span><br><span class="line">13: 285</span><br><span class="line">13: 286</span><br><span class="line">13: 287</span><br><span class="line">13: 288</span><br><span class="line">13: 289</span><br><span class="line">14: 290</span><br><span class="line">14: 291</span><br><span class="line">14: 292</span><br><span class="line">14: 293</span><br><span class="line">14: 294</span><br><span class="line">14: 295</span><br><span class="line">14: 296</span><br><span class="line">14: 297</span><br><span class="line">14: 298</span><br><span class="line">14: 299</span><br><span class="line">16: 300</span><br><span class="line">16: 301</span><br><span class="line">16: 302</span><br><span class="line">16: 303</span><br><span class="line">16: 304</span><br><span class="line">16: 305</span><br><span class="line">16: 306</span><br><span class="line">16: 307</span><br><span class="line">17: 308</span><br><span class="line">13: 309</span><br><span class="line">13: 310</span><br><span class="line">13: 311</span><br><span class="line">13: 312</span><br><span class="line">13: 313</span><br><span class="line">13: 314</span><br><span class="line">13: 315</span><br><span class="line">13: 316</span><br><span class="line">13: 317</span><br><span class="line">13: 318</span><br><span class="line">13: 319</span><br><span class="line">16: 320</span><br><span class="line">16: 321</span><br><span class="line">16: 322</span><br><span class="line">16: 323</span><br><span class="line">16: 324</span><br><span class="line">16: 325</span><br><span class="line">16: 326</span><br><span class="line">16: 327</span><br><span class="line">16: 328</span><br><span class="line">16: 329</span><br><span class="line">13: 330</span><br><span class="line">13: 331</span><br><span class="line">13: 332</span><br><span class="line">13: 333</span><br><span class="line">13: 334</span><br><span class="line">13: 335</span><br><span class="line">13: 336</span><br><span class="line">13: 337</span><br><span class="line">13: 338</span><br><span class="line">13: 339</span><br><span class="line">16: 340</span><br><span class="line">16: 341</span><br><span class="line">16: 342</span><br><span class="line">16: 343</span><br><span class="line">16: 344</span><br><span class="line">16: 345</span><br><span class="line">16: 346</span><br><span class="line">16: 347</span><br><span class="line">16: 348</span><br><span class="line">16: 349</span><br><span class="line">13: 350</span><br><span class="line">13: 351</span><br><span class="line">13: 352</span><br><span class="line">13: 353</span><br><span class="line">13: 354</span><br><span class="line">13: 355</span><br><span class="line">13: 356</span><br><span class="line">13: 357</span><br><span class="line">13: 358</span><br><span class="line">13: 359</span><br><span class="line">16: 360</span><br><span class="line">16: 361</span><br><span class="line">16: 362</span><br><span class="line">16: 363</span><br><span class="line">16: 364</span><br><span class="line">16: 365</span><br><span class="line">16: 366</span><br><span class="line">16: 367</span><br><span class="line">16: 368</span><br><span class="line">16: 369</span><br><span class="line">13: 370</span><br><span class="line">13: 371</span><br><span class="line">13: 372</span><br><span class="line">13: 373</span><br><span class="line">13: 374</span><br><span class="line">13: 375</span><br><span class="line">13: 376</span><br><span class="line">13: 377</span><br><span class="line">13: 378</span><br><span class="line">13: 379</span><br><span class="line">16: 380</span><br><span class="line">16: 381</span><br><span class="line">16: 382</span><br><span class="line">16: 383</span><br><span class="line">16: 384</span><br><span class="line">16: 385</span><br><span class="line">16: 386</span><br><span class="line">16: 387</span><br><span class="line">16: 388</span><br><span class="line">16: 389</span><br><span class="line">13: 390</span><br><span class="line">13: 391</span><br><span class="line">13: 392</span><br><span class="line">13: 393</span><br><span class="line">13: 394</span><br><span class="line">13: 395</span><br><span class="line">13: 396</span><br><span class="line">13: 397</span><br><span class="line">13: 398</span><br><span class="line">13: 399</span><br><span class="line">16: 400</span><br><span class="line">16: 401</span><br><span class="line">16: 402</span><br><span class="line">16: 403</span><br><span class="line">16: 404</span><br><span class="line">16: 405</span><br><span class="line">16: 406</span><br><span class="line">16: 407</span><br><span class="line">16: 408</span><br><span class="line">16: 409</span><br><span class="line">13: 410</span><br><span class="line">13: 411</span><br><span class="line">13: 412</span><br><span class="line">13: 413</span><br><span class="line">13: 414</span><br><span class="line">13: 415</span><br><span class="line">13: 416</span><br><span class="line">13: 417</span><br><span class="line">13: 418</span><br><span class="line">13: 419</span><br><span class="line">16: 420</span><br><span class="line">16: 421</span><br><span class="line">16: 422</span><br><span class="line">16: 423</span><br><span class="line">16: 424</span><br><span class="line">16: 425</span><br><span class="line">16: 426</span><br><span class="line">16: 427</span><br><span class="line">16: 428</span><br><span class="line">16: 429</span><br><span class="line">13: 430</span><br><span class="line">13: 431</span><br><span class="line">13: 432</span><br><span class="line">13: 433</span><br><span class="line">13: 434</span><br><span class="line">13: 435</span><br><span class="line">13: 436</span><br><span class="line">13: 437</span><br><span class="line">13: 438</span><br><span class="line">13: 439</span><br><span class="line">16: 440</span><br><span class="line">16: 441</span><br><span class="line">16: 442</span><br><span class="line">16: 443</span><br><span class="line">16: 444</span><br><span class="line">16: 445</span><br><span class="line">16: 446</span><br><span class="line">16: 447</span><br><span class="line">16: 448</span><br><span class="line">16: 449</span><br><span class="line">13: 450</span><br><span class="line">13: 451</span><br><span class="line">13: 452</span><br><span class="line">13: 453</span><br><span class="line">13: 454</span><br><span class="line">13: 455</span><br><span class="line">13: 456</span><br><span class="line">13: 457</span><br><span class="line">13: 458</span><br><span class="line">13: 459</span><br><span class="line">16: 460</span><br><span class="line">16: 461</span><br><span class="line">16: 462</span><br><span class="line">16: 463</span><br><span class="line">16: 464</span><br><span class="line">16: 465</span><br><span class="line">16: 466</span><br><span class="line">16: 467</span><br><span class="line">16: 468</span><br><span class="line">16: 469</span><br><span class="line">13: 470</span><br><span class="line">13: 471</span><br><span class="line">13: 472</span><br><span class="line">13: 473</span><br><span class="line">13: 474</span><br><span class="line">13: 475</span><br><span class="line">13: 476</span><br><span class="line">13: 477</span><br><span class="line">13: 478</span><br><span class="line">13: 479</span><br><span class="line">16: 480</span><br><span class="line">16: 481</span><br><span class="line">16: 482</span><br><span class="line">16: 483</span><br><span class="line">16: 484</span><br><span class="line">16: 485</span><br><span class="line">16: 486</span><br><span class="line">16: 487</span><br><span class="line">16: 488</span><br><span class="line">16: 489</span><br><span class="line">13: 490</span><br><span class="line">13: 491</span><br><span class="line">13: 492</span><br><span class="line">13: 493</span><br><span class="line">13: 494</span><br><span class="line">13: 495</span><br><span class="line">13: 496</span><br><span class="line">13: 497</span><br><span class="line">13: 498</span><br><span class="line">13: 499</span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<i class="icon-price-tags icon"></i>
<ul class="article-tag-list">
<li class="article-tag-list-item">
<a href="javascript:void(0)" class="js-tag article-tag-list-link color5">操作系统</a>
</li>
</ul>
</div>
<p class="article-more-link">
<a class="article-more-a" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/5.%E4%BF%A1%E5%8F%B7%E9%87%8F%E7%9A%84%E5%AE%9E%E7%8E%B0%E4%B8%8E%E5%BA%94%E7%94%A8/">展开全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<aside class="wrap-side-operation">
<div class="mod-side-operation">
<div class="jump-container" id="js-jump-container" style="display:none;">
<a href="javascript:void(0)" class="mod-side-operation__jump-to-top">
<i class="icon-font icon-back"></i>
</a>
<div id="js-jump-plan-container" class="jump-plan-container" style="top: -11px;">
<i class="icon-font icon-plane jump-plane"></i>
</div>
</div>
</div>
</aside>
<article id="post-哈工大操作系统实验/1.操作系统引导实验报告" class="article article-type-post article-index" itemscope itemprop="blogPost">
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/1.%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%BC%95%E5%AF%BC%E5%AE%9E%E9%AA%8C%E6%8A%A5%E5%91%8A/">1.操作系统引导实验报告</a>
</h1>
<a href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/1.%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%BC%95%E5%AF%BC%E5%AE%9E%E9%AA%8C%E6%8A%A5%E5%91%8A/" class="archive-article-date">
<time datetime="2024-07-22T06:30:22.696Z" itemprop="datePublished"><i class="icon-calendar icon"></i>2024-07-22</time>
</a>
</header>
<div class="article-entry" itemprop="articleBody">
<p>此次实验的基本内容是:</p>
<ol>
<li>阅读《Linux内核完全注释》的第6章,对计算机和Linux 0.11的引导过程进行初步的了解;</li>
<li>按照下面的要求改写0.11的引导程序bootsect.s</li>
<li>有兴趣同学可以做做进入保护模式前的设置程序setup.s。</li>
</ol>
<p>改写bootsect.s主要完成如下功能:</p>
<ul>
<li><input checked="" disabled="" type="checkbox"> <ol>
<li>bootsect.s能在屏幕上打印一段提示信息“XXX is booting…”,其中XXX是你给自己的操作系统起的名字,例如LZJos、Sunix等(可以上论坛上秀秀谁的OS名字最帅,也可以显示一个特色logo,以表示自己操作系统的与众不同。)</li>
</ol>
</li>
</ul>
<p>改写setup.s主要完成如下功能:</p>
<ul>
<li><input checked="" disabled="" type="checkbox"> <ol>
<li>bootsect.s能完成setup.s的载入,并跳转到setup.s开始地址执行。而setup.s向屏幕输出一行”Now we are in SETUP”。</li>
</ol>
</li>
<li><input checked="" disabled="" type="checkbox"> <ol start="2">
<li>setup.s能获取至少一个基本的硬件参数(如内存参数、显卡参数、硬盘参数等),将其存放在内存的特定地址,并输出到屏幕上。</li>
</ol>
</li>
<li><input checked="" disabled="" type="checkbox"> <ol start="3">
<li>setup.s不再加载Linux内核,保持上述信息显示在屏幕上即可。</li>
</ol>
</li>
</ul>
<h1 id="任务一-打印提示信息"><a href="#任务一-打印提示信息" class="headerlink" title="任务一 打印提示信息"></a>任务一 打印提示信息</h1><p>主要修改/boot/bootsect.s中的代码:</p>
<figure class="highlight as"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">! Print some inane message</span><br><span class="line"></span><br><span class="line"> mov ah,#<span class="number">0x03</span> ! read cursor pos</span><br><span class="line"> xor bh,bh</span><br><span class="line"> int <span class="number">0x10</span></span><br><span class="line"></span><br><span class="line"> mov cx,#<span class="number">24</span></span><br><span class="line"> mov bx,#<span class="number">0x0007</span> ! page <span class="number">0</span>, attribute <span class="number">7</span> (normal)</span><br><span class="line"> mov bp,#msg1</span><br><span class="line"> mov ax,#<span class="number">0x1301</span> ! write string, move cursor</span><br><span class="line"> int <span class="number">0x10</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">msg1:</span><br><span class="line"> ! 插入回车换行符</span><br><span class="line"> .byte <span class="number">13</span>,<span class="number">10</span></span><br><span class="line"> ! 定义消息内容</span><br><span class="line"> .ascii <span class="string">"Loading system ..."</span></span><br><span class="line"> ! 再次插入回车换行符</span><br><span class="line"> .byte <span class="number">13</span>,<span class="number">10</span>,<span class="number">13</span>,<span class="number">10</span></span><br></pre></td></tr></table></figure>
<p>上述信息共24个字符,下面将他修改为“crzos is booting …”,加上换行空格,共26个字符。</p>
<figure class="highlight as"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">! Print some inane message</span><br><span class="line"></span><br><span class="line"> mov ah,#<span class="number">0x03</span> ! read cursor pos</span><br><span class="line"> xor bh,bh</span><br><span class="line"> int <span class="number">0x10</span></span><br><span class="line"></span><br><span class="line"> mov cx,#<span class="number">26</span></span><br><span class="line"> mov bx,#<span class="number">0x0007</span> ! page <span class="number">0</span>, attribute <span class="number">7</span> (normal)</span><br><span class="line"> mov bp,#msg1</span><br><span class="line"> mov ax,#<span class="number">0x1301</span> ! write string, move cursor</span><br><span class="line"> int <span class="number">0x10</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">msg1:</span><br><span class="line"> ! 插入回车换行符</span><br><span class="line"> .byte <span class="number">13</span>,<span class="number">10</span></span><br><span class="line"> ! 定义消息内容</span><br><span class="line"> .ascii <span class="string">"crzos is booting ..."</span></span><br><span class="line"> ! 再次插入回车换行符</span><br><span class="line"> .byte <span class="number">13</span>,<span class="number">10</span>,<span class="number">13</span>,<span class="number">10</span></span><br></pre></td></tr></table></figure>
<p>![[修改启动提示信息.png]]</p>
<h1 id="任务二-输出setup"><a href="#任务二-输出setup" class="headerlink" title="任务二 输出setup"></a>任务二 输出setup</h1><p>在setup.s中添加代码:</p>
<figure class="highlight as"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">! now, we are <span class="keyword">in</span> setup.s</span><br><span class="line">! print some message</span><br><span class="line"></span><br><span class="line"> mov si,#setup_msg</span><br><span class="line"> call print_string</span><br><span class="line"></span><br><span class="line">! print string</span><br><span class="line">! ds:si stores the starting address of the string</span><br><span class="line">! ds <span class="keyword">is</span> <span class="keyword">set</span> to cs within the <span class="keyword">function</span></span><br><span class="line"><span class="title function_">print_string</span>:</span><br><span class="line">! <span class="title function_">cs</span> = <span class="title function_">ds</span> = <span class="title function_">es</span></span><br><span class="line"> <span class="title function_">push</span> <span class="title function_">ds</span></span><br><span class="line"> <span class="title function_">push</span> <span class="title function_">es</span></span><br><span class="line"> <span class="title function_">pusha</span></span><br><span class="line"> <span class="title function_">mov</span> <span class="title function_">ax</span>,<span class="title function_">cs</span></span><br><span class="line"> <span class="title function_">mov</span> <span class="title function_">ds</span>,<span class="title function_">ax</span></span><br><span class="line"> <span class="title function_">mov</span> <span class="title function_">es</span>,<span class="title function_">ax</span></span><br><span class="line"><span class="title function_">next_char</span>:</span><br><span class="line"> lodsb</span><br><span class="line"> <span class="title function_">cmp</span> <span class="title function_">al</span>, #0</span><br><span class="line"> <span class="title function_">je</span> <span class="title function_">print_string_end</span></span><br><span class="line"> <span class="title function_">mov</span> <span class="title function_">ah</span>, #0<span class="title function_">x0e</span></span><br><span class="line"> <span class="title function_">int</span> 0<span class="title function_">x10</span></span><br><span class="line"> <span class="title function_">inc</span> <span class="title function_">bp</span></span><br><span class="line"> <span class="title function_">jmp</span> <span class="title function_">next_char</span></span><br><span class="line"><span class="title function_">print_string_end</span>:</span><br><span class="line"> popa</span><br><span class="line"> <span class="title function_">pop</span> <span class="title function_">es</span></span><br><span class="line"> <span class="title function_">pop</span> <span class="title function_">ds</span></span><br><span class="line"> <span class="title function_">ret</span></span><br><span class="line"></span><br><span class="line"><span class="title function_">setup_msg</span>:</span><br><span class="line"> .<span class="title function_">byte</span> 13,10</span><br><span class="line"> .<span class="title function_">ascii</span> "<span class="title function_">now</span> <span class="title function_">we</span> <span class="title function_">are</span> <span class="title function_">in</span> <span class="title function_">setup</span>"</span><br><span class="line"> .<span class="title function_">byte</span> 13,10,0</span><br></pre></td></tr></table></figure>
<blockquote>
<p>尤其注意添加cs = ds = es代码<br>主要是es 要 = cs,否则不会正常显示<br><code>int 0x10</code> 的某些功能号(在这里是 <code>0x1301</code>)会使用 <code>ES:BP</code> 来指向要显示的字符串。如果 <code>ES</code> 不正确,BIOS 将会查找错误的内存位置来获取字符串数据,导致无法正常显示信息。</p>
</blockquote>
<h1 id="任务三-输出参数"><a href="#任务三-输出参数" class="headerlink" title="任务三 输出参数"></a>任务三 输出参数</h1><p>![[setup读取并保留的参数.png]]</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">! print information</span><br><span class="line"> mov cx, #4</span><br><span class="line"> mov di, #0</span><br><span class="line">print_info_loop:</span><br><span class="line"> mov ax, cs</span><br><span class="line"> mov ds, ax</span><br><span class="line"> mov bx, #prompt</span><br><span class="line"> mov si, (bx+di)</span><br><span class="line"> call print_string</span><br><span class="line"></span><br><span class="line"> xchg bx, bx</span><br><span class="line"> mov bx, #info_addr</span><br><span class="line"> mov bx, (bx+di)</span><br><span class="line"> mov ax, #INITSEG</span><br><span class="line"> mov ds, ax</span><br><span class="line"> mov ax, (bx)</span><br><span class="line"> call print_hex</span><br><span class="line"> call print_nl</span><br><span class="line"></span><br><span class="line"> add di, #2</span><br><span class="line"> loop print_info_loop</span><br><span class="line"></span><br><span class="line">; l: jmp l</span><br><span class="line"></span><br><span class="line">! print hex number in ax</span><br><span class="line">print_hex:</span><br><span class="line"> pusha</span><br><span class="line"> mov dx, ax</span><br><span class="line"> mov ah, #0x0e</span><br><span class="line"> mov cx, #4</span><br><span class="line">print_hex_loop:</span><br><span class="line"> rol dx, 4</span><br><span class="line"> mov al, dl</span><br><span class="line"> and al, #0x0f</span><br><span class="line"> cmp al, #0x0a</span><br><span class="line"> jb L</span><br><span class="line"> add al, #0x07</span><br><span class="line">L:</span><br><span class="line"> add al, #0x30</span><br><span class="line"> int 0x10</span><br><span class="line"> loop print_hex_loop</span><br><span class="line"> popa</span><br><span class="line"> ret</span><br><span class="line"></span><br><span class="line">! print \n</span><br><span class="line">print_nl:</span><br><span class="line"> pusha</span><br><span class="line"> mov ax, #0x0e0d</span><br><span class="line"> int 0x10</span><br><span class="line"> mov al, #0x0a</span><br><span class="line"> int 0x10</span><br><span class="line"> popa</span><br><span class="line"> ret</span><br><span class="line"> </span><br><span class="line">mem:</span><br><span class="line"> .byte 13,10</span><br><span class="line"> .ascii "extended memory size: "</span><br><span class="line"> .byte 0</span><br><span class="line">cyl:</span><br><span class="line"> .byte 13,10</span><br><span class="line"> .ascii "cylinders: "</span><br><span class="line"> .byte 0</span><br><span class="line">head:</span><br><span class="line"> .byte 13,10</span><br><span class="line"> .ascii "heads: "</span><br><span class="line"> .byte 0</span><br><span class="line">sect:</span><br><span class="line"> .byte 13,10</span><br><span class="line"> .ascii "sectors: "</span><br><span class="line"> .byte 0</span><br><span class="line">prompt:</span><br><span class="line"> .word mem, cyl, head, sect</span><br><span class="line">info_addr:</span><br><span class="line"> .word 0x02, 0x80, 0x82, 0x8e</span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<i class="icon-price-tags icon"></i>
<ul class="article-tag-list">
<li class="article-tag-list-item">
<a href="javascript:void(0)" class="js-tag article-tag-list-link color5">操作系统</a>
</li>
</ul>
</div>
<p class="article-more-link">
<a class="article-more-a" href="/2024/07/22/%E5%93%88%E5%B7%A5%E5%A4%A7%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%AE%9E%E9%AA%8C/1.%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%BC%95%E5%AF%BC%E5%AE%9E%E9%AA%8C%E6%8A%A5%E5%91%8A/">展开全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>