程序建立與執行緒建立

2021-09-25 10:44:10 字數 1622 閱讀 4139

1.程序建立fork

asmlinkage int sys_fork(struct pt_regs *regs)

long do_fork(unsigned long clone_flags,

unsigned long stack_start,

struct pt_regs *regs,

unsigned long stack_size,

int __user *parent_tidptr,

int __user *child_tidptr)

/* * determine whether and which event to report to ptracer. when

* called from kernel_thread or clone_untraced is explicitly

* requested, no event is reported; otherwise, report if the event

* for the type of forking is enabled.

*/if (likely(user_mode(regs)) && !(clone_flags & clone_untraced))

p = copy_process(clone_flags, stack_start, regs, stack_size, //為子程序複製出乙份程序資訊

child_tidptr, null, trace);

/* * do this prior waking up the new thread - the thread pointer

* might get invalid after that point, if the thread exits quickly.

*/if (!is_err(p))

wake_up_new_task(p); //將子程序加入到排程器中,為其分配cpu,準備執行

/* forking complete and child started to run, tell ptracer */

if (unlikely(trace))

ptrace_event(trace, nr);

if (clone_flags & clone_vfork)

} else

return nr;

}

vfork()的實現比fork()多了兩個標誌位,分別是clone_vfork與clone_vm。從上面do_fork可以看到,clone_vfork表示父程序會被掛起,直至子程序釋放虛擬記憶體資源。

asmlinkage int sys_vfork(struct pt_regs *regs)

2.核心執行緒建立

static void create_kthread(struct kthread_create_info *create)

}

int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)

程序建立多少執行緒,程序建立的過程,執行緒建立的過程

程序能建立多少執行緒與程序邏輯位址空間 32位位址長度的話,就是4gb 更具體地說應該是有程序位址空間中記憶體對映區的大小有關,執行緒棧是通過mmap系統呼叫在記憶體對映區建立的。和執行緒棧 1mb 的大小有關。程序建立的過程 unix和linux採用另一種方式 它把建立程序分成兩步,fork 和e...

多執行緒 執行緒與程序 執行緒排程 建立執行緒

步驟 定義thread類的子類,並重寫該類的run 方法,該run 方法的方法體就代表了執行緒需要完成的任務,因此把run 方法稱為執行緒執行體。建立thread子類的例項,即建立了執行緒物件 呼叫執行緒物件的start 方法來啟動該執行緒 測試 自定義執行緒類 public class mythr...

執行緒 程序 的建立

簡單多執行緒示例 include include using namespace std dword winapi f hello lpvoid dword winapi f hi lpvoid 形參必須是這個樣子 int main int argc,char argv dword winapi f...