????????????
????????????????task_struct??

???????????????????????????????????????????????????????????????PID??????????????????????????????????У???????????????task_struct
???????????饗PCB??
?????????????????????????????????????????
??????????
????image
????fork()
????fork()?????????????????Ρ?????????з????????? pid??????????з???0??
????fork????????????
????#include <stdio.h>
????#include <stdlib.h>
????#include <unistd.h>
????int main(int argc?? char * argv[])
????{
????int pid;
????/* fork another process */
????pid = fork();
????if (pid < 0)
????{
????/* error occurred */
????fprintf(stderr??"Fork Failed!");
????exit(-1);
????}
????else if (pid == 0)
????{
????/* child process */
????printf("This is Child Process! ");
????}
????else
????{ 
????/* parent process  */
????printf("This is Parent Process! ");
????/* parent will wait for the child to complete*/
????wait(NULL);
????printf("Child Complete! ");
????}
????}
???????????
????????????

????fork ???0×80?ж??????????????????????????????????????????????????
????fork.c
????//fork
????#ifdef __ARCH_WANT_SYS_FORK
????SYSCALL_DEFINE0(fork)
????{
????#ifdef CONFIG_MMU
????return do_fork(SIGCHLD?? 0?? 0?? NULL?? NULL);
????#else
????/* can not support in nommu mode */
????return -EINVAL;
????#endif
????}
????#endif
????//vfork
????#ifdef __ARCH_WANT_SYS_VFORK
????SYSCALL_DEFINE0(vfork)
????{
????return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD?? 0??
????0?? NULL?? NULL);
????}
????#endif
????//clone
????#ifdef __ARCH_WANT_SYS_CLONE
????#ifdef CONFIG_CLONE_BACKWARDS
????SYSCALL_DEFINE5(clone?? unsigned long?? clone_flags?? unsigned long?? newsp??
????int __user *?? parent_tidptr??
????int?? tls_val??
????int __user *?? child_tidptr)
????#elif defined(CONFIG_CLONE_BACKWARDS2)
????SYSCALL_DEFINE5(clone?? unsigned long?? newsp?? unsigned long?? clone_flags??
????int __user *?? parent_tidptr??
????int __user *?? child_tidptr??
????int?? tls_val)
????#elif defined(CONFIG_CLONE_BACKWARDS3)
????SYSCALL_DEFINE6(clone?? unsigned long?? clone_flags?? unsigned long?? newsp??
????int?? stack_size??
????int __user *?? parent_tidptr??
????int __user *?? child_tidptr??
????int?? tls_val)
????#else
????SYSCALL_DEFINE5(clone?? unsigned long?? clone_flags?? unsigned long?? newsp??
????int __user *?? parent_tidptr??
????int __user *?? child_tidptr??
????int?? tls_val)
????#endif
????{
????return do_fork(clone_flags?? newsp?? 0?? parent_tidptr?? child_tidptr);
????}
????#endif
?????????????????????????????????????????? fork ???? vfork ??????????????????? do_fork() ???????????????????????????? do_fork()??????????????????????????
????long do_fork(unsigned long clone_flags??
????unsigned long stack_start??
????unsigned long stack_size??
????int __user *parent_tidptr??
????int __user *child_tidptr)
????{
????//?????????????????
????struct task_struct *p;
????//……
????//???????????????copy_process()??????????? task_struct ???
????p = copy_process(clone_flags?? stack_start?? stack_size??
????child_tidptr?? NULL?? trace);
????if (!IS_ERR(p)) {
????struct completion vfork;
????struct pid *pid;
????trace_sched_process_fork(current?? p);
????//????′???????????????е?pid
????pid = get_task_pid(p?? PIDTYPE_PID);
????nr = pid_vnr(pid);
????if (clone_flags & CLONE_PARENT_SETTID)
????put_user(nr?? parent_tidptr);
????//???????? vfork()??????????? vfork ???????????
????if (clone_flags & CLONE_VFORK) {
????p->vfork_done = &vfork;
????init_completion(&vfork);
????get_task_struct(p);
????}
????//?????????????????У??????? CPU????????
????wake_up_new_task(p);
????//fork ???????????????????
????if (unlikely(trace))
????ptrace_event_pid(trace?? pid);
????//????? vfork?????????????????????У????????????
????if (clone_flags & CLONE_VFORK) {
????if (!wait_for_vfork_done(p?? &vfork))
????ptrace_event_pid(PTRACE_EVENT_VFORK_DONE?? pid);
????}
????put_pid(pid);
????} else {
????nr = PTR_ERR(p);
????}
????return nr;
????}
????do_fork ????
???????? copy_process ???????????????????
????????? vfork ??????????????
???????? wake_up_new_task ??????????????????????? CPU
????????? vfork?????????????????? exec ?滻??????????
????copy_process ????
???????copy_process ?????????
????static struct task_struct *copy_process(unsigned long clone_flags??
????unsigned long stack_start??
????unsigned long stack_size??
????int __user *child_tidptr??
????struct pid *pid??
????int trace)
????{
????int retval;
????//?????????????????
????struct task_struct *p;
????//……
????//???????? task_struct
????p = dup_task_struct(current);
????//……
????//???????????? 
????rt_mutex_init_task(p);
????//?????????????????????????????
????if (atomic_read(&p->real_cred->user->processes) >=
????task_rlimit(p?? RLIMIT_NPROC)) {
????if (p->real_cred->user != INIT_USER &&
????!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
????goto bad_fork_free;
????}
????//……
????//????????????? max_threads ??????С????
????if (nr_threads >= max_threads)
????goto bad_fork_cleanup_count;
????//……
????//???????????
????spin_lock_init(&p->alloc_lock);
????//????????????
????init_sigpending(&p->pending);
????//????? CPU ?????
????posix_cpu_timers_init(p);
????//……
????//?????????????????????????????? TASK_RUNNING
????retval = sched_fork(clone_flags?? p);
????//???????н?????????????????????????????????????????