最近想讀Linux中,user/kernel、process/thread之間的memory關係
目前看起來user space有process、thread之分
而kernel space應該就只有稱kernel thread而沒kernel process
那kernel thread的功用又是什麼呢?
找了一些資料後自己歸納以下三點功用:
1. 處理只能在kernel space做的事情 -- device driver
2. 處理只能在kernel space做的事情,協助user program -- system call
3. daemon的使用,因為kernel thread的context switch相對於user process來說是比較便宜的,所以對需要固定時間就執行一次的daemon來說,以kernel thread的形式來呈現是最好的
創建user process的方法是有三個API: fork, vfork, clone
創建user thread的方法主要是使用thread library: pthread_create
創建kernel thread的方法有兩種API: kernel_thread, kthread_create
而以上這些API還有library function最後都會call到do_fork
而do_fork所做的事就是填task_struct,這邊有一個我覺得很酷的觀念要講一下
*** 只要填好task_struct就是把該task給創建出來了 !!! ***
可以知道其實user process, user thread, user thread就只是在task_struct的填法不一樣而已
===== kernel_thread vs kthread_create =====
在driver module中若使用kernel_thread來開kernel thread就要在thread function中使用daemonize
我自己的理解是,插入module使用insmod是在userspace呼叫的
然後kernel_thread最後使用do_fork,此function會複製parent的資訊進child中
使用daemonize的目的就是要把userspace的資訊拿掉不要複製進目標kernel thread中
而kthread_create只是把要執行的內容掛在一個list中,而kthreadd(pid=2)
此kthreadd是kernel thread,是一支daemon
他會去抓掛入的list,最後也是使用kernel_thread來開啟kernel thread
但由於此時呼叫的人變成kthreadd,已經是kernel thread了
所以就不用再另外呼叫daemonize來把userspace的資訊釋放
對於user thread還有一點我覺得有點酷
The user thread scheduler is the thread scheduler, which is within the process.
也就是說user thread scheduling是由該thread的process來負責,而非kernel scheduler
對於這點我還抱著遲疑的態度@@...
User process & user thread & kernel thread & kernel process(?)
Related Posts:
Memory zoneReference : Understaning the Linux Kernel Chap.8 Ideal computer architecture上,任意page frame都是可以被access的 但實際上的電腦架構在硬體上會有些限制住page frame被使用 Linux kernel must deal with two hardware constraints of the 80 × 86 architecture: • The D… Read More
紀錄一下/proc/pid/maps的使用在跑起來的一支程式,可用cat /proc/pid/maps 來看他的memory layout (似乎只看的到user space的) 以下是這段code看到的資訊: address - The starting and ending address of the region in the process's address space permissions - rwx就不用講了,p代表的是private(not shared),… Read More
About kernel structureDOS was designed for running only one dsedicated process. => Is the perfect for some real-time, dedicated applications.UNIX, however, was designed for multi-user, multi-tasking applications.Kernel runs in… Read More
Booting ARM system from bootloader to linux kernel有空來讀一下 會以u-boot code當作bootloader base 開到start_kernel之前的組語部分可以參考 http://gicl.cs.drexel.edu/people/sevy/linux/ARM_Linux_boot_sequence.html… Read More
Linux memory layout每一個user process看到的vitrual memory共有4G(32 bit machine) 其中通常切成3G:1G 0x00000000-0xbfffffff: user process(3GB) 0xc0000000-0xffffffff: kernel space (1GB) 而user process(3G)是每支user process不同的,甚至每支user thread也是不同 &… Read More
0 意見:
張貼留言