mirror of
https://github.com/gatieme/LDD-LinuxDeviceDrivers.git
synced 2026-09-24 22:43:42 +08:00
...
This commit is contained in:
File diff suppressed because it is too large
Load Diff
+23
-17
@@ -10,7 +10,7 @@ void do_exit(long code)
|
||||
profile_task_exit(tsk);
|
||||
|
||||
/*
|
||||
http://lxr.free-electrons.com/source/kernel/kcov.c?v=4.6;#L104
|
||||
http://lxr.free-electrons.com/source/kernel/kcov.c?v=4.6;#L104
|
||||
https://lwn.net/Articles/671640
|
||||
*/
|
||||
kcov_task_exit(tsk);
|
||||
@@ -18,7 +18,7 @@ void do_exit(long code)
|
||||
/* http://lxr.free-electrons.com/source/include/linux/blkdev.h?v=4.6;#L1095 */
|
||||
WARN_ON(blk_needs_flush_plug(tsk));
|
||||
|
||||
/* oops消息
|
||||
/* oops消息
|
||||
中断上下文不能执行do_exit函数
|
||||
也不能终止PID为0的进程。
|
||||
*/
|
||||
@@ -33,28 +33,30 @@ void do_exit(long code)
|
||||
* continuing. Amongst other possible reasons, this is to prevent
|
||||
* mm_release()->clear_child_tid() from writing to a user-controlled
|
||||
* kernel address.
|
||||
*
|
||||
* 设定进程可以使用的虚拟地址的上限(用户空间)
|
||||
*
|
||||
* 设定进程可以使用的虚拟地址的上限(用户空间)
|
||||
* http://lxr.free-electrons.com/ident?v=4.6;i=set_fs
|
||||
*/
|
||||
set_fs(USER_DS);
|
||||
/*
|
||||
/*
|
||||
possibly stop for a ptrace event notification
|
||||
由于进程如果是在ptrace调试事件中终止的
|
||||
因此需要将信息发送给ptrace的进程
|
||||
函数定义在 http://lxr.free-electrons.com/source/include/linux/ptrace.h?v=4.6#L133 */
|
||||
ptrace_event(PTRACE_EVENT_EXIT, code);
|
||||
|
||||
|
||||
/*
|
||||
heck creds for do_exit()
|
||||
http://lxr.free-electrons.com/source/kernel/cred.c?v=4.6#L805
|
||||
http://lxr.free-electrons.com/source/kernel/cred.c?v=4.6#L805
|
||||
*/
|
||||
validate_creds_for_do_exit(tsk);
|
||||
/*
|
||||
* We're taking recursive faults here in do_exit. Safest is to just
|
||||
* leave this task alone and wait for reboot.
|
||||
*/
|
||||
if (unlikely(tsk->flags & PF_EXITING)) {
|
||||
|
||||
/*current->flags的PF_EXITING标志表示进程正在被删除 */
|
||||
if (unlikely(tsk->flags & PF_EXITING)) { /* 检查PF_EXITING标志是否未被设置 */
|
||||
pr_alert("Fixing recursive fault but reboot is needed!\n");
|
||||
/*
|
||||
* We can do this unlocked here. The futex code uses
|
||||
@@ -67,19 +69,23 @@ void do_exit(long code)
|
||||
*/
|
||||
/* 设置进程标识为PF_EXITPIDONE*/
|
||||
tsk->flags |= PF_EXITPIDONE;
|
||||
/* 设置进程状态为不可中断的等待状态 */
|
||||
set_current_state(TASK_UNINTERRUPTIBLE);
|
||||
/* 调度其它进程 */
|
||||
schedule();
|
||||
}
|
||||
/*
|
||||
tsk->flags |= PF_EXITING;
|
||||
http://lxr.free-electrons.com/source/kernel/signal.c#L2383
|
||||
*/
|
||||
exit_signals(tsk); /* sets tsk->flags PF_EXITING */
|
||||
exit_signals(tsk); /* sets tsk->flags PF_EXITING 设置PF_EXITING标志 */
|
||||
/*
|
||||
* tsk->flags are checked in the futex code to protect against
|
||||
* an exiting task cleaning up the robust pi futexes.
|
||||
*/
|
||||
/* 内存屏障,用于确保在它之后的操作开始执行之前,它之前的操作已经完成 */
|
||||
smp_mb();
|
||||
/* 一直等待,直到获得current->pi_lock自旋锁 */
|
||||
raw_spin_unlock_wait(&tsk->pi_lock);
|
||||
|
||||
if (unlikely(in_atomic())) {
|
||||
@@ -95,7 +101,7 @@ void do_exit(long code)
|
||||
|
||||
/*
|
||||
cct_update_integrals - update mm integral fields in task_struct
|
||||
更新进程的运行时间
|
||||
更新进程的运行时间, 获取current->mm->rss_stat.count[member]计数
|
||||
http://lxr.free-electrons.com/source/kernel/tsacct.c?v=4.6#L152
|
||||
*/
|
||||
acct_update_integrals(tsk);
|
||||
@@ -114,11 +120,11 @@ void do_exit(long code)
|
||||
tsk->exit_code = code;
|
||||
taskstats_exit(tsk, group_dead);
|
||||
|
||||
/* 释放存储空间
|
||||
放弃进程占用的mm,如果没有其他进程使用该mm,则释放它。
|
||||
/* 释放存储空间
|
||||
放弃进程占用的mm,如果没有其他进程使用该mm,则释放它。
|
||||
*/
|
||||
exit_mm(tsk);
|
||||
|
||||
/* 输出进程会计信息 */
|
||||
if (group_dead)
|
||||
acct_process();
|
||||
trace_sched_process_exit(tsk);
|
||||
@@ -139,9 +145,9 @@ void do_exit(long code)
|
||||
*
|
||||
* because of cgroup mode, must be called before cgroup_exit()
|
||||
*/
|
||||
/* When a child task exits, feed back event values to parent events.
|
||||
http://lxr.free-electrons.com/source/kernel/events/core.c#L8987
|
||||
*/
|
||||
/* When a child task exits, feed back event values to parent events.
|
||||
http://lxr.free-electrons.com/source/kernel/events/core.c#L8987
|
||||
*/
|
||||
perf_event_exit_task(tsk);
|
||||
|
||||
cgroup_exit(tsk);
|
||||
@@ -221,4 +227,4 @@ void do_exit(long code)
|
||||
for (;;)
|
||||
cpu_relax(); /* For when BUG is null */
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(do_exit);
|
||||
EXPORT_SYMBOL_GPL(do_exit);
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Take down every thread in the group. This is called by fatal signals
|
||||
* as well as by sys_exit_group (below).
|
||||
*/
|
||||
void
|
||||
do_group_exit(int exit_code)
|
||||
{
|
||||
struct signal_struct *sig = current->signal;
|
||||
|
||||
BUG_ON(exit_code & 0x80); /* core dumps don't get here */
|
||||
/*
|
||||
检查current->sig->flags的SIGNAL_GROUP_EXIT标志是否置位
|
||||
或者current->sig->group_exit_task是否不为NULL
|
||||
*/
|
||||
if (signal_group_exit(sig))
|
||||
exit_code = sig->group_exit_code; /* group_exit_code存放的是线程组终止代码 */
|
||||
else if (!thread_group_empty(current)) { /* 检查线程组链表是否不为空 */
|
||||
struct sighand_struct *const sighand = current->sighand;
|
||||
|
||||
spin_lock_irq(&sighand->siglock);
|
||||
if (signal_group_exit(sig))
|
||||
/* Another thread got here before we took the lock. */
|
||||
exit_code = sig->group_exit_code;
|
||||
else {
|
||||
sig->group_exit_code = exit_code;
|
||||
sig->flags = SIGNAL_GROUP_EXIT;
|
||||
zap_other_threads(current); /* 遍历整个线程组链表,并杀死其中的每个线程 */
|
||||
}
|
||||
spin_unlock_irq(&sighand->siglock);
|
||||
}
|
||||
|
||||
do_exit(exit_code);
|
||||
/* NOTREACHED */
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
| 日期 | 内核版本 | 架构| 作者 | GitHub| CSDN |
|
||||
| ------------- |:-------------:|:-------------:|:-------------:|:-------------:|:-------------:|
|
||||
| 2016-05-12 | [Linux-4.5](http://lxr.free-electrons.com/source/?v=4.5) | X86 & arm | [gatieme](http://blog.csdn.net/gatieme) | [LinuxDeviceDrivers](https://github.com/gatieme/LDD-LinuxDeviceDrivers) | [Linux-进程管理与调度](http://blog.csdn.net/gatieme/article/category/6225543) |
|
||||
|
||||
#进程复制Duplication--fork,vfork,clone
|
||||
-------
|
||||
Unix标准的复制进程的系统调用时fork(即分叉),但是Linux,BSD等操作系统并不止实现这一个,确切的说linux实现了三个,fork,vfork,clone(确切说vfork创造出来的是轻量级进程,也叫线程,是共享资源的进程)
|
||||
|
||||
| 系统调用 | 描述 |
|
||||
|:-------------:|:-------------:|
|
||||
| fork | fork创造的子进程是父进程的完整副本,复制了父亲进程的资源,包括内存的内容task_struct内容 |
|
||||
| vfork | vfork创建的子进程与父进程共享数据段,而且由vfork()创建的子进程将先于父进程运行 |
|
||||
|
||||
|
||||
vfork()用法与fork()相似.但是也有区别,具体区别归结为以下3点
|
||||
|
||||
1. fork() 子进程拷贝父进程的数据段,代码段.
|
||||
vfork() 子进程与父进程共享数据段.
|
||||
|
||||
2. fork() 父子进程的执行次序不确定.
|
||||
vfork():保证子进程先运行,在调用exec或_exit之前与父进程数据是共享的,在它调用exec
|
||||
或_exit之后父进程才可能被调度运行。如果在调用这两个函数之前子进程依赖于父进程的进一步动作,则会导致死锁。当需要改变共享数据段中变量的值,则拷贝父进程
|
||||
|
||||
vfork用于创建一个新进程,而该新进程的目的是exec一个新进程,vfork和fork一样都创建一个子进程,但是它并不将父进程的地址空间完全复制到子进程中,不会复制页表。因为子进程会立即调用exec,于是也就不会存放该地址空间。不过在子进程中调用exec或exit之前,他在父进程的空间中运行。
|
||||
为什么会有vfork,因为以前的fork当它创建一个子进程时,将会创建一个新的地址空间,并且拷贝父进程的资源,而往往在子进程中会执行exec调用,这样,前面的拷贝工作就是白费力气了,这种情况下,聪明的人就想出了vfork,它产生的子进程刚开始暂时与父进程共享地址空间(其实就是线程的概念了),因为这时候子进程在父进程的地址空间中运行,所以子进程不能进行写操作,并且在儿子“霸占”着老子的房子时候,要委屈老子一下了,让他在外面歇着(阻塞),一旦儿子执行了exec或者exit后,相当于儿子买了自己的房子了,这时候就相当于分家了。
|
||||
vfork和fork之间的另一个区别是: vfork保证子进程先运行,在她调用exec或exit之后父进程才可能被调度运行。如果在调用这两个函数之前子进程依赖于父进程的进一步动作,则会导致死锁。由此可见,这个系统调用是用来启动一个新的应用程序。其次,子进程在vfork()返回后直接运行在父进程的栈空间,并使用父进程的内存和数据。这意味着子进程可能破坏父进程的数据结构或栈,造成失败。
|
||||
为了避免这些问题,需要确保一旦调用vfork(),子进程就不从当前的栈框架中返回,并且如果子进程改变了父进程的数据结构就不能调用exit函数。子进程还必须避免改变全局数据结构或全局变量中的任何信息,因为这些改变都有可能使父进程不能继续。通常,如果应用程序不是在fork()之后立即调用exec(),就有必要在fork()被替换成vfork()之前做仔细的检查。
|
||||
用vfork函数创建子进程后,子进程往往要调用一种exec函数以执行另一个程序,当进程调用一种exec函数时,该进程完全由新程序代换,而新程序则从其main函数开始执行,因为调用exec并不创建新进程,所以前后的进程id 并未改变,exec只是用另一个新程序替换了当前进程的正文,数据,堆和栈段。
|
||||
Reference in New Issue
Block a user