kernel---进程创建研究之 fork()

来源:互联网 发布:淘宝联盟5.0 编辑:程序博客网 时间:2024/06/06 10:40

首先提取出do_fork(),

from linux/kernel/fork.c




/* *  Ok, this is the main fork-routine. * * It copies the process, and if successful kick-starts * it and waits for it to finish using the VM if required. */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){struct task_struct *p;int trace = 0;long nr;/* * Do some preliminary argument and permissions checking before we * actually start allocating stuff */if (clone_flags & CLONE_NEWUSER) {if (clone_flags & CLONE_THREAD)return -EINVAL;/* hopefully this check will go away when userns support is * complete */if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SETUID) ||!capable(CAP_SETGID))return -EPERM;}/* * We hope to recycle these flags after 2.6.26 */if (unlikely(clone_flags & CLONE_STOPPED)) {static int __read_mostly count = 100;if (count > 0 && printk_ratelimit()) {char comm[TASK_COMM_LEN];count--;printk(KERN_INFO "fork(): process `%s' used deprecated ""clone flags 0x%lx\n",get_task_comm(comm, current),clone_flags & CLONE_STOPPED);}}/* * When called from kernel_thread, don't do user tracing stuff. */if (likely(user_mode(regs)))trace = tracehook_prepare_clone(clone_flags);p = <span style="color:#FF0000;">copy_process</span>(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)) {struct completion vfork;trace_sched_process_fork(current, p);nr = task_pid_vnr(p);if (clone_flags & CLONE_PARENT_SETTID)put_user(nr, parent_tidptr);if (clone_flags & CLONE_VFORK) {p->vfork_done = &vfork;init_completion(&vfork);}audit_finish_fork(p);tracehook_report_clone(regs, clone_flags, nr, p);/* * We set PF_STARTING at creation in case tracing wants to * use this to distinguish a fully live task from one that * hasn't gotten to tracehook_report_clone() yet.  Now we * clear it and set the child going. */p->flags &= ~PF_STARTING;if (unlikely(clone_flags & CLONE_STOPPED)) {/* * We'll start up with an immediate SIGSTOP. */sigaddset(&p->pending.signal, SIGSTOP);set_tsk_thread_flag(p, TIF_SIGPENDING);__set_task_state(p, TASK_STOPPED);} else {wake_up_new_task(p, clone_flags);}tracehook_report_clone_complete(trace, regs,clone_flags, nr, p);if (clone_flags & CLONE_VFORK) {freezer_do_not_count();wait_for_completion(&vfork);freezer_count();tracehook_report_vfork_done(p, nr);}} else {nr = PTR_ERR(p);}return nr;}

call  copy_process( );

在此函数中有几个重要函数的调用:

(1)dup_task_struct( ); 

在copy_process( )中调用形式为p = dup_task_struct(current);

current为当前进程描述符。

static struct task_struct *dup_task_struct(struct task_struct *orig){struct task_struct *tsk;struct thread_info *ti;unsigned long *stackend;int err;prepare_to_copy(orig);<span style="color:#FF0000;">tsk = alloc_task_struct();</span>if (!tsk)return NULL;<span style="color:#FF0000;">ti = alloc_thread_info(tsk);</span>if (!ti) {free_task_struct(tsk);return NULL;} err = arch_dup_task_struct(tsk, orig);if (err)goto out;tsk->stack = ti;err = prop_local_init_single(&tsk->dirties);if (err)goto out;setup_thread_stack(tsk, orig);clear_user_return_notifier(tsk);stackend = end_of_stack(tsk);*stackend = STACK_END_MAGIC;/* for overflow detection */#ifdef CONFIG_CC_STACKPROTECTORtsk->stack_canary = get_random_int();#endif/* One for us, one for whoever does the "release_task()" (usually parent) */atomic_set(&tsk->usage,2);atomic_set(&tsk->fs_excl, 0);#ifdef CONFIG_BLK_DEV_IO_TRACEtsk->btrace_seq = 0;#endiftsk->splice_pipe = NULL;account_kernel_stack(ti, 1);return tsk;out:free_thread_info(ti);free_task_struct(tsk);return NULL;}

函数中复制了进程描述符

# define alloc_task_struct()    kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)

static inline struct thread_info *alloc_thread_info(struct task_struct *tsk)
{
#ifdef CONFIG_DEBUG_STACK_USAGE
    gfp_t mask = GFP_KERNEL | __GFP_ZERO;
#else
    gfp_t mask = GFP_KERNEL;
#endif
    return (struct thread_info *)__get_free_pages(mask, THREAD_SIZE_ORDER);
}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 无经验想在工地承包点小活怎么办 一级建造师挂靠后中标后怎么办 用360对系统修补漏洞很慢怎么办 如果美国和俄罗斯开战中国会怎么办 戴牙冠前临时补牙材料掉了怎么办 设备间在业主家里每次上锁怎么办 成都安全员证原件丢了并过期怎么办 记不施工员证书号了怎么办 优易学车学员版登录不了怎么办 先科移动dvd主板坏了怎么办 离职单位不出劳动解除书怎么办 离职后一级建造师注册证怎么办 京牌货车报废挂靠公司不给办怎么办 二建挂靠注册证书到期了怎么办 二建证书挂靠公司不给钱怎么办 二建拿到证书原单位不解锁怎么办 凯云软件清单锁定只读了怎么办 苹果笔记本鼠标触摸板没反应怎么办 苹果笔记本键盘和触摸板失灵怎么办 苹果手机输入密码显示已停用怎么办 苹果7p手机刷机黑屏了怎么办 苹果5s来电接听屏幕卡顿怎么办? 手机摔了一下触屏失灵怎么办 小米手机摔了一下触屏失灵怎么办 苹果6sp触摸ic坏了怎么办 苹果4s屏幕摔裂了怎么办 新换的手机内屏颜色太亮怎么办 苹果手机摔了一下屏幕失灵怎么办 苹果手机6s屏幕坏了怎么办 苹果6s屏幕摔坏了怎么办 苹果手机屏幕进油了屏幕变暗怎么办 苹果手机不小心屏幕进油了怎么办 苹果6充电插口螺丝口坏了怎么办 苹果5s手机安装屏幕翘边怎么办 苹果手机摔了一下触摸屏失灵怎么办 苹果手机摔了下触摸屏失灵怎么办 苹果6老是屏幕失灵或者闪屏怎么办 华为荣耀8手触摸屏乱跳失灵怎么办 苹果5s屏幕有半边竖纹怎么办 苹果5s电源键坏了怎么办 苹果6s手机触屏失灵怎么办