Process

process

是执行中程序的实例,系统中的每一个程序都运行在某个进程的上下文中,上下文是由程序正确运行所需状态组成的,这个状态包括存放内存中程序的代码和数据,它的栈,通用目的寄存器的内容,程序计数器,环境变量,以及打开文件描述符的集合。每次用户通过shell输入一个可执行目标文件,shell就会创建新的进程。

The classic definition of a process is an instance of a program in execution. Each program in the system runs in the context of some process.The context consists of the state that the program needs to run correctly. This state includes the program's code and data stored in memory, its stack, the contents of its general-purpose registers, its program counter, environment variables, and the set of open file descriptors.

进程是一个独立的逻辑控制流,轮流使用处理器的,每个进程执行流的一部分,然后被强占(preempted),暂时被挂起。time slicing, concurrency

每个process有私有的地址空间,代码段都从0x400000开始。

内核调度一个新的进程,运行之后,它就会抢占当前进程,并使用一种上下文切换机制来将控制转移到新进程中。

加载并运行程序使用execve函数。execve调用一次从不返回。加载filename之后,调用启动代码,设置栈,将控制传递给新程序的main。

the typical difference is that threads (of the same process) run in a shared memory space, while processes run in separate memory spaces. thread有自己的tid,栈,sp,pc,general register and flag, the code, data and heap areas are shared

struct

在Linux操作系统上运行的所有进程都是通过task_struct结构来管理的,其也被称为“进程描述符”。一个进程描述符包含了单个进程在运行期间所有必要的信息,比如进程标识、进程的属性、构建进程的资源等。如果知道进程的结构,就能了解进程的执行对性能的重要性。

process control

进程控制:获取进程id。进程有三种状态:运行,停止(suspended) 收到SIGSTOP, SIGTSTP,SIGTTIN or SIGTTOU,直到收到SIGCONT继续。终止,收到终止进程的信号,从主程序返回,调用exit函数。 进程创建用fork, 当一个进程由于某种原因终止时,内核并不是立刻把它从系统中清除,相反进程将会,被保持在一种已经终止的状态,直到被父进程回收。waitpid 等待子进程终止。进程休眠就是sleep。

  • run
  • suspend: A process stops as a result of receiving a SIGSTOP, SIGTSTP, SIGTI1N, or SIGTTOU signal, and it remains stopped until it receives a SIGCONT signal, at which point it becomes running again.
  • terminate: exit

system call

fork函数调用一次,返回两次,并发执行,相同但是独立的地址空间。相同代码和数据段、堆、共享库以及用户栈,共享文件,最大区别是父和子有不同的pid。

对所有fork 拓扑排序,可以得到一个输出顺序。

fork为当前进程调用,是内核,为新进程创建各种数据结构,并分配给他一个唯一的pid.为了给这个新进程创建虚拟内存,它创建当前进程的mm_struct, 区域结构和页表的原样副本,他将两个进程中的每个页面都标记为只读,并将两个进程中每个区域结构都标记为私有的写时复制 copy on write。

Copy-on-write or COW: when a write request is made, the data are copied into a new storage area, and then the original data are modified.

context switch

registers是唯一被所有过程共享的资源,当调用时,把所有原始值压入栈中,改变寄存器的值,在返回之前从栈中弹出旧值。

It consists of the values of objects such as the general-purpose registers, the floating-point registers, the program counter, user's stack, status registers, kernel's stack, and various kernel data structures such as a page table that characterizes the address space, a process table that contains information about the current process, and a file table that contains information about the files that the process has opened.

Context Switch 流程:上下文切换,第一,保存当前进程的上下文,第二,恢复某个先前被抢占的进程,被保存的上下文,第三,将控制传递给这个新恢复的进程。 (1) saves the context of the current process, (2) restores the saved context of some previously preempted process, and (3) passes control to this newly restored process.

The context is the state that the kernel needs to restart a preempted process. It consists of the values of objects such as the general-purpose registers, the floating-point registers, the program counter, user's stack, status registers, kernel's stack, and various kernel data structures such as a page table that characterizes the address space, a process table that contains information about the current process, and a file table that contains information about the files that the process has opened.

zombie process

Zombie process what is zombie process? When does it become zombie process?

当进程由于某种原因终止时,内核不会立即清除,而是保持在一种已经终止的状态直至被父进程回收,一个终止未被回收的叫zombie。用waitpid函数等待子进程终止或停止。

On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution (via the exit system call) but still has an entry in the process table: it is a process in the "Terminated state". This occurs for child processes, where the entry is still needed to allow the parent process to read its child's exit status: once the exit status is read via the wait system call, the zombie's entry is removed from the process table and it is said to be "reaped".


Children
  1. Signal