Skip to the content.

fork

首先学会用户态的使用

整体流程

大多数代码都是在 kernel/fork.c 中,大约存在 3000 行左右

单独拎出来地址空间的拷贝:

几个问题

exit

Man exit_group

This system call is equivalent to _exit(2) except that it terminates not only the calling thread, but all threads in the calling process’s thread group.

difference between exit and exit_group

所以区别就是 : exit_group 会发送 KILL 信号给整个 process group 的开始结束

/*
 * Nuke all other threads in the group.
 */
int zap_other_threads(struct task_struct *p)
{
	struct task_struct *t = p;
	int count = 0;

	p->signal->group_stop_count = 0;

	while_each_thread(p, t) {
		task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
		count++;

		/* Don't bother with already dead threads */
		if (t->exit_state)
			continue;
		sigaddset(&t->pending.signal, SIGKILL);
		signal_wake_up(t, 1);
	}

	return count;
}

vfork

the parent is suspended until the child terminates or calls exec()

具体来说,通过 wait_for_vfork_done() 来实现

As with fork(2), the child process created by vfork() inher‐ its copies of various of the caller’s process attributes (e.g., file descriptors, signal dispositions, and current working directory);

Until that point, the child shares all memory with its parent, including the stack.

SYSCALL_DEFINE0(vfork)
{
	struct kernel_clone_args args = {
		.flags		= CLONE_VFORK | CLONE_VM,
		.exit_signal	= SIGCHLD,
	};

	return kernel_clone(&args);
}

CLONE_VM 表示共享地址空间,其他的 flags 都没有,表示其他的各种资源都是拷贝的。

扩展

fork 设计问题

本站所有文章转发 CSDN 将按侵权追究法律责任,其它情况随意。