2013年8月21日 星期三

User process & user thread & kernel thread & kernel process(?)

最近想讀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
對於這點我還抱著遲疑的態度@@...

Related Posts:

  • Describing Physical MemoryReference: http://www.chudov.com/tmp/LinuxVM/html/understand/node16.html NUMA(non-uniform memory access)架構下,memory被分成很多的bank(又稱為node),而每個bank被CPU access的cost不同 Linux用struct pg_data_t來代表bank(or node) 而PC是一UMA的架構,所以只有一個p… Read More
  • 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
  • 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
  • Kernel Space MemoryReference : http://www.chudov.com/tmp/LinuxVM/html/understand/node28.html Kernel的virtual memory適用linear mapping,physical address 0對應到kernel virtual memory的PAGE_OFFSET(通常是0xC000_0000) 也就是說kernel virtual address - PAGE_OFFSET就… Read More
  • Allocating and Freeing Page TablesReference : http://www.chudov.com/tmp/LinuxVM/html/understand/node26.html 因為page table的allocation跟free有很大的cost,而且又是很常要執行的操作(process create&delete) 所以Linux kernel使用quicklists來cache住用來當page table的page quicklist包含pgd_quick… Read More

0 意見:

張貼留言