Xv6 多程序程式設計

2021-10-19 12:36:15 字數 2138 閱讀 1619

參考: xv6-riscv-book 1.1 processes and memory

系統呼叫

描述int fork()建立乙個程序(通過複製當前程序)返回子程序 pid

int exit(int status)終止當前程序,status 會被報告給 wait(),無返回值

int wait(int *status)等待乙個子程序退出,把退出的狀態(exit de status) 寫到 status,返回退出的子程序 pid

int exec(char *file, char *ar**)載入乙個檔案,並以指定引數執行之。錯誤才返回

fork 系統呼叫通過複製當前程序,建立乙個程序,返回子程序 pid。

wait 會等待當前程序的某個子程序退出(呼叫 exit)。

書上有關使用fork的**的完整實現(在 xv6 下執行。help: xv6 編寫使用者程式):

// usefork.c for xv6

#include

"kernel/types.h"

#include

"kernel/stat.h"

#include

"user/user.h"

intmain()

else

if(pid ==0)

else

exit(0);}

注意,在 xv6 裡提供的 printf 執行緒不安全,執行程式列印出的字元可能隨機混合在一起:

$ usefork   # 這個還稍好

parent: child=c5

hild: exiting

child 5 is done

$ usefork # 這個就非常亂了

cphairledn:t :e xcihtiilndg=

7child 7 is done

在真實的*nix系統上(這裡以 macos 11,gcc 10 為例),這段**的寫法是:

// usefork.c for macos gcc

#include

#include

#include

intmain()

else

if(pid ==0)

else

return0;

}

真實系統中執行的效果會好一些,一般沒有字元混合的情況:

$ gcc-10 usefork.c ; ./a.out 

parent: child=3598

child: exiting

child 3598 is done

exec 系統呼叫載入乙個可執行檔案,用其替換自身程式,以指定引數執行之。

// useexec.c for xv6

#include

"kernel/types.h"

#include

"kernel/stat.h"

#include

"user/user.h"

intmain()

編譯執行:

$ useexec

hello

// useexec.c for macos

#include

#include

#include

intmain()

編譯執行:

$ gcc-10 useexec.c ; ./a.out 

hello

正文結束。

# by cdfmlr 2021-02-18

echo "see you.?"

XV6記憶體布局

1 規定系統最大物理記憶體為16mb。2 應用程式使用0 640k虛擬記憶體,640k 1m是對映io空間,1m以上的高位記憶體只有核心可以使用,4064 4096最高32m位址空間對映到不同的裝置。3 每個應用程式都有自己的頁表,頁表的前160項 0 640k 記憶體是自己對映的,從640k到最高...

安裝並啟動xv6

從github上拉取xv6的原始碼 git clone git sudo apt get install qemu輸入下面的命令 objdump i第二行應該輸出 elf32 i386 輸入下面的命令,gcc m32 print libgcc file name應輸出 usr lib gcc i48...

xv6磁碟驅動及快取

前面的博文中詳細講述了xv6的檔案系統,其中使用位圖塊來進行磁碟block的 管理,但是對於block內容進行讀寫則需要更底層的磁碟驅動程式,同時考慮到磁碟讀寫的速度非常慢 相對於記憶體讀寫 因此我們有必要對磁碟的資料塊進行快取。整個磁碟的快取是採用類似物件池 object pool 的框架來實現的...