操作系統(tǒng)課程設計報告-linux頁面換入換出分析、進程管理器_第1頁
已閱讀1頁,還剩15頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、<p>  計算機與通信工程學院</p><p><b>  操作系統(tǒng)課程設計</b></p><p>  設計題目 Linux頁面換入換出分析、 進程管理器</p><p><b>  課程設計任務書</b></p><p>  設計題目:Linux頁面換入換出分析、進程管理器

2、</p><p><b>  一、設計實驗條件</b></p><p>  Ubuntu Kylin 16.04, Visual Studio 2015</p><p><b>  二、設計任務及要求</b></p><p>  必做題目:Linux頁面換入換出分析。</p><

3、p>  要求:分析Linux內核對頁面換入換出地實現。說明主要函數的功能及相互間地調用關系。畫出換入換出流程圖,并用文字加以說明。</p><p>  選作題目:進程管理器</p><p>  設計任務:在Linux或Windows系統(tǒng)環(huán)境下,實現一個系統(tǒng)進程管理器,能夠顯示當前系統(tǒng)的活動進程信息(進程名、用戶、優(yōu)先級、內存使用等),并能結束或創(chuàng)建特定進程??蓞⒖糤indows下“任

4、務管理器”功能。</p><p><b>  三、設計報告的內容</b></p><p><b>  設計題目與設計任務</b></p><p>  Linux頁面換入換出分析</p><p>  要求:分析Linux內核對頁面換入換出地實現。說明主要函數的功能及相互間地調用關系。畫出換入換出流程圖

5、,并用文字加以說明。</p><p><b>  進程管理器</b></p><p>  設計任務:在Linux或Windows系統(tǒng)環(huán)境下,實現一個系統(tǒng)進程管理器,能夠顯示當前系統(tǒng)的活動進程信息(進程名、用戶、優(yōu)先級、內存使用等),并能結束或創(chuàng)建特定進程??蓞⒖糤indows下“任務管理器”功能。</p><p><b>  前言&l

6、t;/b></p><p>  Linux頁面換入換出分析</p><p>  目的與意義:深入理解Linux內核中換入換出的操作,對操作系統(tǒng)虛擬存儲器操作有進一步認識。</p><p><b>  進程管理器</b></p><p>  目的與意義:深入理解操作系統(tǒng)進程管理的原理</p><p

7、><b>  設計主體</b></p><p>  Linux頁面換入換出分析</p><p><b>  換出的流程圖:</b></p><p><b>  圖中的相關代碼:</b></p><p>  ==================== mm/vmscan.c

8、1146 1153 ====================</p><p>  1146 static int __init kswapd_init(void)</p><p><b>  1147 {</b></p><p>  1148 printk("Starting kswapd v1.8\n");</

9、p><p>  1149 swap_setup();</p><p>  1150 kernel_thread(kswapd, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL);</p><p>  1151 kernel_thread(kreclaimd, NULL, CLONE_FS | CLONE_FILE

10、S | CLONE_SIGNAL);</p><p>  1152 return 0;</p><p><b>  1153 }</b></p><p>  ==================== mm/swap.c 293 305 ====================</p><p>  [kswapd_i

11、nit()>swap_setup()]</p><p><b>  293 /*</b></p><p>  294 * Perform any setup for the swap system</p><p><b>  295 */</b></p><p>  296 voi

12、d __init swap_setup(void)</p><p><b>  297 {</b></p><p>  298 /* Use a smaller cluster for memory <16MB or <32MB */</p><p>  299 if (num_physpages < ((1

13、6 * 1024 * 1024) >> PAGE_SHIFT))</p><p>  300 page_cluster = 2;</p><p>  301 else if (num_physpages < ((32 * 1024 * 1024) >> PAGE_SHIFT))</p><p>  302

14、 page_cluster = 3;</p><p>  303 else</p><p>  304 page_cluster = 4;</p><p><b>  305 }</b></p><p>  ==================== mm/vmscan.c 1146 11

15、53 ====================</p><p>  1146 static int __init kswapd_init(void)</p><p><b>  1147 {</b></p><p>  1148 printk("Starting kswapd v1.8\n");</p>

16、<p>  1149 swap_setup();</p><p>  1150 kernel_thread(kswapd, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL);</p><p>  1151 kernel_thread(kreclaimd, NULL, CLONE_FS | CLONE_FILES | CLO

17、NE_SIGNAL);</p><p>  1152 return 0;</p><p><b>  1153 }</b></p><p>  ==================== mm/swap.c 293 305 ====================</p><p>  [kswapd_init()&g

18、t;swap_setup()]</p><p><b>  293 /*</b></p><p>  294 * Perform any setup for the swap system</p><p><b>  295 */</b></p><p>  296 void __ini

19、t swap_setup(void)</p><p><b>  297 {</b></p><p>  298 /* Use a smaller cluster for memory <16MB or <32MB */</p><p>  299 if (num_physpages < ((16 * 102

20、4 * 1024) >> PAGE_SHIFT))</p><p>  300 page_cluster = 2;</p><p>  301 else if (num_physpages < ((32 * 1024 * 1024) >> PAGE_SHIFT))</p><p>  302 pa

21、ge_cluster = 3;</p><p>  303 else</p><p>  304 page_cluster = 4;</p><p><b>  305 }</b></p><p>  ==================== mm/vmscan.c 805 822 ======

22、==============</p><p>  [kswapd()>inactive_shortage()]</p><p><b>  805 /*</b></p><p>  806 * How many inactive pages are we short?</p><p><b>  8

23、07 */</b></p><p>  808 int inactive_shortage(void)</p><p><b>  809 {</b></p><p>  810 int shortage = 0;</p><p><b>  811</b></p&

24、gt;<p>  812 shortage += freepages.high;</p><p>  813 shortage += inactive_target;</p><p>  814 shortage -= nr_free_pages();</p><p>  815 shortage -= nr_inact

25、ive_clean_pages();</p><p>  816 shortage -= nr_inactive_dirty_pages;</p><p><b>  817</b></p><p>  818 if (shortage > 0)</p><p>  819 retu

26、rn shortage;</p><p><b>  820</b></p><p>  821 return 0;</p><p><b>  822 }</b></p><p>  ==================== mm/vmscan.c 907 941 ===========

27、=========</p><p>  [kswapd()>do_try_to_free_pages()]</p><p>  907 static int do_try_to_free_pages(unsigned int gfp_mask, int user)</p><p><b>  908 {</b></p>

28、<p>  909 int ret = 0;</p><p><b>  910</b></p><p>  911 /*</p><p>  912 * If we‘re low on free pages, move pages from the</p><p>  913

29、* inactive_dirty list to the inactive_clean list.</p><p><b>  914 *</b></p><p>  915 * Usually bdflush will have pre-cleaned the pages</p><p>  916 * before

30、we get around to moving them to the other</p><p>  917 * list, so this is a relatively cheap operation.</p><p>  918 */</p><p>  919 if (free_shortage() || nr_inactive_d

31、irty_pages > nr_free_pages() +</p><p>  920 nr_inactive_clean_pages())</p><p>  921 ret += page_launder(gfp_mask, user);</p><p><b>  922</b></p

32、><p>  923 /*</p><p>  924 * If needed, we move pages from the active list</p><p>  925 * to the inactive list. We also "eat" pages from</p><p>  926

33、 * the inode and dentry cache whenever we do this.</p><p>  927 */</p><p>  928 if (free_shortage() || inactive_shortage()) {</p><p>  929 shrink_dcache_memory(6

34、, gfp_mask);</p><p>  930 shrink_icache_memory(6, gfp_mask);</p><p>  931 ret += refill_inactive(gfp_mask, user);</p><p>  932 } else {</p><p>  933

35、 /*</p><p>  934 * Reclaim unused slab cache memory.</p><p>  935 */</p><p>  936 kmem_cache_reap(gfp_mask);</p><p>  937 ret =

36、1;</p><p><b>  938 }</b></p><p><b>  939</b></p><p>  940 return ret;</p><p><b>  941 }</b></p><p>  =========

37、=========== mm/vmscan.c 465 670 ====================</p><p>  [kswapd()>do_try_to_free_pages()>page_launder()]</p><p><b>  465 /**</b></p><p>  466 * page_laund

38、er - clean dirty inactive pages, move to inactive_clean list</p><p>  467 * @gfp_mask: what operations we are allowed to do</p><p>  468 * @sync: should we wait synchronously for the cleanin

39、g of pages</p><p><b>  469 *</b></p><p>  470 * When this function is called, we are most likely low on free +</p><p>  471 * inactive_clean pages. Since we want t

40、o refill those pages as</p><p>  472 * soon as possible, we‘ll make two loops over the inactive list,</p><p>  473 * one to move the already cleaned pages to the inactive_clean lists</p&g

41、t;<p>  474 * and one to (often asynchronously) clean the dirty inactive pages.</p><p><b>  475 *</b></p><p>  476 * In situations where kswapd cannot keep up, user proc

42、esses will</p><p>  477 * end up calling this function. Since the user process needs to</p><p>  478 * have a page before it can continue with its allocation, we‘ll</p><p>  479

43、 * do synchronous page flushing in that case.</p><p><b>  480 *</b></p><p>  481 * This code is heavily inspired by the FreeBSD source code. Thanks</p><p>  482

44、* go out to Matthew Dillon.</p><p><b>  483 */</b></p><p>  484 #define MAX_LAUNDER (4 * (1 << page_cluster))</p><p>  485 int page_launder(int gfp_mask, int s

45、ync)</p><p><b>  486 {</b></p><p>  487 int launder_loop, maxscan, cleaned_pages, maxlaunder;</p><p>  488 int can_get_io_locks;</p><p>  489 s

46、truct list_head * page_lru;</p><p>  490 struct page * page;</p><p><b>  491</b></p><p>  492 /*</p><p>  493 * We can only grab the IO locks (e

47、g. for flushing dirty</p><p>  494 * buffers to disk) if __GFP_IO is set.</p><p>  495 */</p><p>  496 can_get_io_locks = gfp_mask & __GFP_IO;</p><p>&l

48、t;b>  497</b></p><p>  498 launder_loop = 0;</p><p>  499 maxlaunder = 0;</p><p>  500 cleaned_pages = 0;</p><p><b>  501</b></p>

49、<p>  502 dirty_page_rescan:</p><p>  503 spin_lock(&pagemap_lru_lock);</p><p>  504 maxscan = nr_inactive_dirty_pages;</p><p>  505 while ((page_lru = inact

50、ive_dirty_list.prev) != &inactive_dirty_list &&</p><p>  506 maxscan-- > 0) {</p><p>  507 page = list_entry(page_lru, struct page, lru);</p><p>&

51、lt;b>  508</b></p><p>  509 /* Wrong page on list?! (list corruption, should not happen) */</p><p>  510 if (!PageInactiveDirty(page)) {</p><p>  511

52、 printk("VM: page_launder, wrong page on list.\n");</p><p>  512 list_del(page_lru);</p><p>  513 nr_inactive_dirty_pages--;</p><p>  514

53、 page->zone->inactive_dirty_pages--;</p><p>  515 continue;</p><p>  516 }</p><p><b>  517</b></p><p>  518 /* Page

54、 is or was in use? Move it to the active list. */</p><p>  519 if (PageTestandClearReferenced(page) || page->age > 0 ||</p><p>  520 (!page->buffers &&

55、page_count(page) > 1) ||</p><p>  521 page_ramdisk(page)) {</p><p>  522 del_page_from_inactive_dirty_list(page);</p><p>  523 add_page_to

56、_active_list(page);</p><p>  524 continue;</p><p>  525 }</p><p><b>  526</b></p><p>  527 /*</p><p>  528

57、* The page is locked. IO in progress?</p><p>  529 * Move it to the back of the list.</p><p>  530 */</p><p>  531 if (TryLockPage(page)) {</p><p&

58、gt;  532 list_del(page_lru);</p><p>  533 list_add(page_lru, &inactive_dirty_list);</p><p>  534 continue;</p><p>  535 }</p>

59、<p><b>  536</b></p><p>  537 /*</p><p>  538 * Dirty swap-cache page? Write it out if</p><p>  539 * last copy..</p><p>  540

60、 */</p><p>  541 if (PageDirty(page)) {</p><p>  542 int (*writepage)(struct page *) = page->mapping->a_ops->writepage;</p><p>  543

61、int result;</p><p><b>  544</b></p><p>  545 if (!writepage)</p><p>  546 goto page_active;</p><p><b>  547</b></

62、p><p>  548 /* First time through? Move it to the back of the list */</p><p>  549 if (!launder_loop) {</p><p>  550 list_del(page_lru);</p&g

63、t;<p>  551 list_add(page_lru, &inactive_dirty_list);</p><p>  552 UnlockPage(page);</p><p>  553 continue;</p><p>  554

64、 }</p><p><b>  555</b></p><p>  556 /* OK, do a physical asynchronous write to swap. */</p><p>  557 ClearPageDirty(page);</p>

65、<p>  558 page_cache_get(page);</p><p>  559 spin_unlock(&pagemap_lru_lock);</p><p><b>  560</b></p><p>  561 result = write

66、page(page);</p><p>  562 page_cache_release(page);</p><p><b>  563</b></p><p>  564 /* And re-start the thing.. */</p><p>  565

67、 spin_lock(&pagemap_lru_lock);</p><p>  566 if (result != 1)</p><p>  567 continue;</p><p>  568 /* writepage refused to do anythi

68、ng */</p><p>  569 set_page_dirty(page);</p><p>  570 goto page_active;</p><p>  571 }</p><p><b>  572</b></p><p&

69、gt;  573 /*</p><p>  574 * If the page has buffers, try to free the buffer mappings</p><p>  575 * associated with this page. If we succeed we either free</p><

70、p>  576 * the page (in case it was a buffercache only page) or we</p><p>  577 * move the page to the inactive_clean list.</p><p>  578 *</p><p>  579

71、 * On the first round, we should free all previously cleaned</p><p>  580 * buffer pages</p><p>  581 */</p><p>  582 if (page->buffers) {</p>

72、<p>  583 int wait, clearedbuf;</p><p>  584 int freed_page = 0;</p><p>  585 /*</p><p>  586 * Since we might be doing disk IO,

73、 we have to</p><p>  587 * drop the spinlock and take an extra reference</p><p>  588 * on the page so it doesn‘t go away from under us.</p><p>  589

74、 */</p><p>  590 del_page_from_inactive_dirty_list(page);</p><p>  591 page_cache_get(page);</p><p>  592 spin_unlock(&pagemap_lru_lock);

75、</p><p><b>  593</b></p><p>  594 /* Will we do (asynchronous) IO? */</p><p>  595 if (launder_loop && maxlaunder == 0 && sync)&l

76、t;/p><p>  596 wait = 2; /* Synchrounous IO */</p><p>  597 else if (launder_loop && maxlaunder-- > 0)</p><p>  598 wait = 1; /*

77、Async IO */</p><p>  599 else</p><p>  600 wait = 0; /* No IO */</p><p><b>  601</b></p><p>  602 /* Try to free the

78、 page buffers. */</p><p>  603 clearedbuf = try_to_free_buffers(page, wait);</p><p><b>  604</b></p><p>  605 /*</p><p>  606

79、 * Re-take the spinlock. Note that we cannot</p><p>  607 * unlock the page yet since we‘re still</p><p>  608 * accessing the page_struct here...</p><p&g

80、t;  609 */</p><p>  610 spin_lock(&pagemap_lru_lock);</p><p><b>  611</b></p><p>  612 /* The buffers were not freed. */</p>

81、;<p>  613 if (!clearedbuf) {</p><p>  614 add_page_to_inactive_dirty_list(page);</p><p><b>  615</b></p><p>  616 /* The

82、page was only in the buffer cache. */</p><p>  617 } else if (!page->mapping) {</p><p>  618 atomic_dec(&buffermem_pages);</p><p>  619

83、 freed_page = 1;</p><p>  620 cleaned_pages++;</p><p><b>  621</b></p><p>  622 /* The page has more users besides the cache and us. */&

84、lt;/p><p>  623 } else if (page_count(page) > 2) {</p><p>  624 add_page_to_active_list(page);</p><p><b>  625</b></p><p>  626

85、 /* OK, we "created" a freeable page. */</p><p>  627 } else /* page->mapping && page_count(page) == 2 */ {</p><p>  628 add_page_to_i

86、nactive_clean_list(page);</p><p>  629 cleaned_pages++;</p><p>  630 }</p><p><b>  631</b></p><p>  632 /*</p>

87、<p>  633 * Unlock the page and drop the extra reference.</p><p>  634 * We can only do it here because we ar accessing</p><p>  635 * the page struct

88、above.</p><p>  636 */</p><p>  637 UnlockPage(page);</p><p>  638 page_cache_release(page);</p><p><b>  639</b></p&g

89、t;<p>  640 /*</p><p>  641 * If we‘re freeing buffer cache pages, stop when</p><p>  642 * we‘ve got enough free memory.</p><p>  643

90、 */</p><p>  644 if (freed_page && !free_shortage())</p><p>  645 break;</p><p>  646 continue;</p><p>  647

91、 } else if (page->mapping && !PageDirty(page)) {</p><p>  648 /*</p><p>  649 * If a page had an extra reference in</p><p>  650 *

92、 deactivate_page(), we will find it here.</p><p>  651 * Now the page is really freeable, so we</p><p>  652 * move it to the inactive_clean list.</p><p>  6

93、53 */</p><p>  654 del_page_from_inactive_dirty_list(page);</p><p>  655 add_page_to_inactive_clean_list(page);</p><p>  656 UnlockPa

94、ge(page);</p><p>  657 leaned_pages++;</p><p>  658 } else {</p><p>  659 page_active:</p><p>  660 /*</p><p> 

95、 661 * OK, we don‘t know what to do with the page.</p><p>  662 * It‘s no use keeping it here, so we move it to</p><p>  663 * the active list.</p><

96、;p>  664 */</p><p>  665 del_page_from_inactive_dirty_list(page);</p><p>  666 add_page_to_active_list(page);</p><p>  667 UnlockP

97、age(page);</p><p>  668 }</p><p><b>  669 }</b></p><p>  670 spin_unlock(&pagemap_lru_lock);</p><p>  ==================== mm/vmscan.c 6

98、71 697 ====================</p><p>  [kswapd()>do_try_to_free_pages()>page_launder()]</p><p><b>  671</b></p><p>  672 /*</p><p>  673 * If we

99、 don‘t have enough free pages, we loop back once</p><p>  674 * to queue the dirty pages for writeout. When we were called</p><p>  675 * by a user process (that /needs/ a free page) and

100、 we didn‘t</p><p>  676 * free anything yet, we wait synchronously on the writeout of</p><p>  677 * MAX_SYNC_LAUNDER pages.</p><p><b>  678 *</b></p>

101、<p>  679 * We also wake up bdflush, since bdflush should, under most</p><p>  680 * loads, flush out the dirty pages before we have to wait on</p><p>  681 * IO.</p>&l

102、t;p>  682 */</p><p>  683 if (can_get_io_locks && !launder_loop && free_shortage()) {</p><p>  684 launder_loop = 1;</p><p>  685 /* If we

103、 cleaned pages, never do synchronous IO. */</p><p>  686 if (cleaned_pages)</p><p>  687 sync = 0;</p><p>  688 * We only do a few "out of order&quo

104、t; flushes. */</p><p>  689 maxlaunder = MAX_LAUNDER;</p><p>  690 /* Kflushd takes care of the rest. */</p><p>  691 wakeup_bdflush(0);</p><p>

105、  692 goto dirty_page_rescan;</p><p><b>  693 }</b></p><p><b>  694</b></p><p>  695 /* Return the number of pages moved to the inactive_clea

106、n list. */</p><p>  696 return cleaned_pages;</p><p><b>  697 }</b></p><p><b>  換入的流程圖:</b></p><p><b>  相關代碼:</b></p>&l

107、t;p><b>  /* </b></p><p>  * These routines also need to handle stuff like marking pages dirty </p><p>  * and/or accessed for architectures that don't do it in hardware (mos

108、t </p><p>  * RISC architectures). The early dirtying is also good on the i386. </p><p><b>  * </b></p><p>  * There is also a hook called "update_mmu_cache()&q

109、uot; that architectures </p><p>  * with external mmu caches can use to update those (ie the Sparc or </p><p>  * PowerPC hashed page tables that act as extended TLBs). </p><p>&

110、lt;b>  * </b></p><p>  * We enter with non-exclusive mmap_sem (to exclude vma changes, </p><p>  * but allow concurrent faults), and pte mapped but not yet locked. </p><p&

111、gt;  * We return with pte unmapped and unlocked. </p><p><b>  * </b></p><p>  * The mmap_sem may have been released depending on flags and our </p><p>  * return valu

112、e. See filemap_fault() and __lock_page_or_retry(). </p><p><b>  */ </b></p><p>  static int handle_pte_fault(struct mm_struct *mm, </p><p>  struct vm_area_struct *v

113、ma, unsigned long address, </p><p>  pte_t *pte, pmd_t *pmd, unsigned int flags) </p><p><b>  { </b></p><p>  pte_t entry; </p><p>  spinlock_t *ptl; &

114、lt;/p><p><b>  /* </b></p><p>  * some architectures can have larger ptes than wordsize, </p><p>  * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and CONFIG_32BIT=y, <

115、/p><p>  * so READ_ONCE or ACCESS_ONCE cannot guarantee atomic accesses. </p><p>  * The code below just needs a consistent view for the ifs and </p><p>  * we later double check any

116、way with the ptl lock held. So here </p><p>  * a barrier will do. </p><p><b>  */ </b></p><p>  entry = *pte; </p><p>  barrier(); </p><p&g

117、t;  if (!pte_present(entry)) { </p><p>  if (pte_none(entry)) { </p><p>  if (vma->vm_ops) </p><p>  return do_fault(mm, vma, address, pte, pmd, </p><p>  flags,

118、 entry); </p><p>  return do_anonymous_page(mm, vma, address, pte, pmd, </p><p><b>  flags); </b></p><p><b>  } </b></p><p>  return do_swap

119、_page(mm, vma, address, </p><p>  pte, pmd, flags, entry); </p><p><b>  } </b></p><p><b>  進程管理器</b></p><p><b>  軟件功能:</b></p&

120、gt;<p>  1)顯示當前系統(tǒng)所有的進程,并提供刷新操作</p><p>  2)可以結束當前選定進程</p><p><b>  3)打開系統(tǒng)記事本</b></p><p><b>  軟件截圖演示:</b></p><p><b>  1)顯示</b>&l

121、t;/p><p><b>  2)打開記事本</b></p><p>  3)找到notepad并結束進程</p><p><b>  源碼重要部分分析:</b></p><p>  private void Window_Loaded(object sender, RoutedEventArgs e)

122、 //窗體啟動時運行的代碼</p><p><b>  {</b></p><p>  processes = System.Diagnostics.Process.GetProcesses();//獲得系統(tǒng)全部進程</p><p>  this.textBlock1.Text = "進程任務管理器,這里顯示所有的系統(tǒng)進程,共&quo

123、t; + processes.Length + "個進程";</p><p>  processesCollection.Clear();//清除之前的進程Collection記錄</p><p>  foreach (System.Diagnostics.Process eachProcess in processes)</p><p><

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論