操作系统课程设计之Pintos
資源下載地址:https://download.csdn.net/download/sheziqiong/86784890
資源下載地址:https://download.csdn.net/download/sheziqiong/86784890
Pintos 的安裝和配置
sudo apt-get install qemu
git clone git://pintos-os.org/pintos-anon
編輯/utils/pintos(257 行):替換 kernel.bin 為完整路徑的 kernel.bin
編輯/utils/pintos(621 行):替換 qemu 為 qemu-system-x86_64
編輯/utils/Pintos.pm(362 行):替換 loader.bin 為完整路徑的 loader.bin
Threads
Mission1
該部分內(nèi)容主要是需要我們修改 timer_sleep 函數(shù)
首先查看原本 timer_sleep 代碼
通過(guò)查看相關(guān)教程及分析,可知 start 獲取了起始時(shí)間, 然后斷言必須可以被中斷, 不然會(huì)一直死循環(huán)下去。
while (timer_elapsed (start) < ticks)thread_yield();追蹤 thread_yield()函數(shù)及 schedule()函數(shù),schedule 負(fù)責(zé)切換下一個(gè)線程進(jìn)行 run,thread_yield 負(fù)責(zé)把當(dāng)前線程放到就緒隊(duì)列里,然后重新 schedule(當(dāng) ready 隊(duì)列為空時(shí)線程會(huì)繼續(xù)在 CPU 執(zhí)行)。 而 timer_sleep 則是在 ticks 時(shí)間內(nèi),如果線程出于 running 狀態(tài)就不斷把線程放到就緒隊(duì)列不讓他執(zhí)行。由此可以發(fā)現(xiàn)它存在的問(wèn)題:
線程不斷在 CPU 就緒隊(duì)列和 running 隊(duì)列之間來(lái)回,占用了 CPU 資源。因此我們決定添加喚醒機(jī)制。
函數(shù)實(shí)現(xiàn)
修改 thread 結(jié)構(gòu)體:
struct thread{/* Owned by thread.c. */tid_t tid; /* Thread identifier. */enum thread_status status; /* Thread state. */char name[16]; /* Name (for debugging purposes). */uint8_t *stack; /* Saved stack pointer. */int priority; /* Priority. */struct list_elem allelem; /* List element for all threads list. *//* Shared between thread.c and synch.c. */struct list_elem elem; /* List element. */#ifdef USERPROG/* Owned by userprog/process.c. */uint32_t *pagedir; /* Page directory. */ #endif/* Owned by thread.c. */unsigned magic; /* Detects stack overflow. */int64_t ticks_blocked; /* Time for blocked. */};添加了一個(gè)變量 ticks_blocked 用于記錄剩余阻塞時(shí)間。在 timer_sleep 函數(shù)中,將該線程阻塞并設(shè)置阻塞時(shí)間。這一過(guò)程需要解除中斷。
修改 timer_sleep 函數(shù)
/* Timer interrupt handler. */
static void
timer_interrupt (struct intr_frame *args UNUSED)
{
ticks++;
thread_tick ();
thread_foreach(check_blocked_time,NULL);
}
在頭文件也要添加關(guān)于 check_blocked_time 的聲明
void check_blocked_time(struct thread *t, void *aux);此時(shí) Mission1 通過(guò)部分
pass tests/threads/alarm-single pass tests/threads/alarm-multiple pass tests/threads/alarm-simultaneous FAIL tests/threads/alarm-priority pass tests/threads/alarm-zero pass tests/threads/alarm-negative然后完成線程優(yōu)先級(jí)的問(wèn)題。通過(guò)發(fā)現(xiàn) thread.c 中的 next_thread_to_run()函數(shù),發(fā)現(xiàn)其中存在一個(gè) ready_list。
static struct thread * next_thread_to_run (void) {if (list_empty (&ready_list))return idle_thread;elsereturn list_entry (list_pop_front (&ready_list), struct thread, elem); }查找 list.c 文件,發(fā)現(xiàn)了 list_max 函數(shù),可用于根據(jù)比較函數(shù)查找 ready_list 中優(yōu)先級(jí)最高的線程。構(gòu)造比較函數(shù),利用 list_max 和 list_entry 將優(yōu)先級(jí)最高的線程移除并返回。
bool thread_compare_priority (const struct list_elem *a,const struct list_elem *b,void *aux UNUSED){return list_entry(a,struct thread,elem)->priority < list_entry(b,struct thread,elem)->priority; } static struct thread * next_thread_to_run (void) {if (list_empty (&ready_list))return idle_thread;else{struct list_elem *max_priority = list_max (&ready_list,thread_compare_priority,NULL);list_remove (max_priority);return list_entry (max_priority,struct thread,elem);} }此時(shí),Mission1 部分全部通過(guò)。
pass tests/threads/alarm-single pass tests/threads/alarm-multiple pass tests/threads/alarm-simultaneous pass tests/threads/alarm-priority pass tests/threads/alarm-zero pass tests/threads/alarm-negativeMission2
從 priority-fifo 測(cè)試看起,改測(cè)試創(chuàng)建了一個(gè)一個(gè)優(yōu)先級(jí) PRI_DEFAULT+2 的主線程,并用這個(gè)線程創(chuàng)建了 16 個(gè)優(yōu)先級(jí) PRI_DEFAULT+1 的子線程,然后把主線程的優(yōu)先級(jí)設(shè)置為優(yōu)先級(jí) PRI_DEFAULT。
測(cè)試需要把 16 個(gè)線程跑完后結(jié)束主線程,但操作系統(tǒng)中線程是并行執(zhí)行的,有可能最開(kāi)始的一個(gè)線程在設(shè)置完優(yōu)先級(jí)之后立刻結(jié)束了,而此時(shí)其他線程并未結(jié)束。因此在線程設(shè)置完優(yōu)先級(jí)之后應(yīng)該立刻重新調(diào)度,需要在 thread_set_priority()函數(shù)里添加 thread_yield()函數(shù)。
結(jié)語(yǔ)
斯坦福操作系統(tǒng)課程設(shè)計(jì)的難度很大,無(wú)論是從環(huán)境的配置和搭建,文檔的查詢(xún)和翻譯,相關(guān)資料的搜集和理解,都需要花費(fèi)很多的功夫。通過(guò) pintos 的自學(xué),我能夠更清晰地理解操作系統(tǒng)中的線程到底是怎么一回事,需要考慮哪些因素:優(yōu)先級(jí)的循環(huán)更新,嵌套調(diào)度,時(shí)間片的考量,關(guān)于線程鎖的概念,以及更復(fù)雜的隊(duì)列調(diào)度算法。受限于時(shí)間原因,這里只完成到 thread 部分。
資源下載地址:https://download.csdn.net/download/sheziqiong/86784890
資源下載地址:https://download.csdn.net/download/sheziqiong/86784890
總結(jié)
以上是生活随笔為你收集整理的操作系统课程设计之Pintos的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 前端学习(1904)vue之电商管理系统
- 下一篇: 西电计科院微机原理与系统设计课程笔记(车