进程管理
对于进程的两种虚拟化方式
On modern operating systems, processes provide two virtualizations: a virtualized processor and virtual memory.
如何生成一个新的进程
A process begins its life when, not surprisingly, it is created. In Linux, this occurs by means of the fork() system call, which creates a new process by duplicating an existing one.
Often, immediately after a fork it is desirable to execute a new, different program.The exec() family of function calls creates a new address space and loads a new program into it.
进程描述符(task_struct)
The kernel stores the list of processes in a circular doubly linked list called the task list. Each element in the task list is a process descriptor of the type struct task_struct, which is defined in
进程描述符包含的内容
The process descriptor contains the data that describes the executing program—open files, the process’s address space, pending signals, the process’s state, and much more
进程描述符在vm中的位置
进程所处的五种状态
TASK_RUNNING |
The process is runnable; it is either currently running or on a run- queue waiting to run |
TASK_INTERRUPTIBLE |
The process is sleeping (that is, it is blocked), waiting for some condition to exist.The process also awakes prematurely and becomes runnable if it receives a signal. |
TASK_UNINTERRUPTIBLE |
This state is identical to TASK_INTERRUPTIBLE except that it does not wake up and become runnable if it receives a signal. |
__TASK_TRACED |
The process is being traced by another process, such as a debug- ger, via ptrace |
__TASK_STOPPED |
Process execution has stopped; the task is not running nor is it eligible to run. |
进程状态流转图
进程上下文(process context)
When a program executes a system call (see Chapter 5,“System Calls”) or triggers an exception, it enters kernel-space.At this point, the kernel is said to be “executing on behalf of the process” and is in process context.
System calls and exception handlers are well-defined interfaces into the kernel.A process can begin executing in kernel-space only through one of these interfaces—all access to the kernel is through these interfaces.
进程家族
All processes are descendants of the init process, whose PID is one.
进程创建
Unix takes the unusual approach of separating these steps into two distinct functions: fork() and exec(). The first, fork(), creates a child process that is a copy of the current task. It differs from the parent only in its PID (which is unique), its PPID (parent’s PID, which is set to the original process), and certain resources and statistics, such as pending signals, which are not inherited.The second function,exec(), loads a new executable into the address space and begins executing it.
linux中的线程实现
Threads are a popular modern programming abstraction.They provide multiple threads of execution within the same program in a shared memory address space.They can also share open files and other resources.
To these other operating systems, threads are an abstraction to provide a lighter, quicker execution unit than the heavy process.To Linux, threads are simply a manner of sharing resources between processes (which are already quite lightweight).
内核线程(kernel thread)
kernel threads do not have an address space
They operate only in kernel-space and do not context switch into user-space.
Linux delegates several tasks to kernel threads, most notably the flush tasks and the ksoftirqd task.(功能)
Kernel threads are created on system boot by other kernel threads. Indeed, a kernel thread can be created only by another kernel thread