快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门新葡亰游戏881:Linux进程线程学习笔记:进程创建



各位同砚,转换下思维,这里说的是“进程”, 不是“线程”,OK,我们开始

“进程”二字彷佛总有那么些“只可领悟弗成言传”的韵味,维基百科是这样来解释的:澳门新葡亰游戏881

进程(英语:Process,台湾译作行程)是谋略机中已运行法度榜样的物理。进程本身不会运行,是线程的容器。法度榜样本身只是指令的聚拢,进程才是法度榜样(那些指令)的真正运行。多少进程有可能与同一个法度榜样相关系,且每个进程皆可以同步(循序)或不合步(平行)的要领自力运行(多线程即每一个线程都代表一个进程)。今世谋略机系统可在同一段光阴内加载多个法度榜样和进程到存储器中,并借由光阴共享(或称多义务),以在一个处置惩罚器上体现出同时(平行性)运行的感到。同样的,应用多线程技巧的操作系统或谋略机架构,同样法度榜样的平行进程,可在多 CPU 主机或收集上真正同时运行(在不合的 CPU 上)。进程为现今分时系统的基础运作单位。

也有同伙如斯来阐述,

一个可以履行的法度榜样;

和该进程相关联的整个数据(包括变量,内存空间,缓冲区等等);

法度榜样的履行高低文(execution context)

我更盼望将这些简化一下(或许不太准确):指令和履行指令所需的情况,指令可以理解成“代码”,情况可以理解成“高低文”

系统用一个叫做“进程表”的器械来掩护中系统中的进程,进程表中的一个条款掩护着存储着一个进程的相关信息,比如进程号,进程状态,寄存器值等等...

当分配给进程A的“光阴片”应用完时,CPU会进行高低文切换以便运行其他进程,比如进程B,这里所谓的“高低文切换”,主要便是在操作那个“进程表”,其将进程A的相关信息(高低文)保存到其对应的进程表项中, 与之相反,其会从对应于进程B的进程表项中读取相关信息并运行之。

那么,假如进程A新建了一个进程C呢?教程表会多这样一个表项,并且该表项拥有一个独一的ID,也便是进程号(PID),进程表项的其他值大年夜部分与进程A的相同,详细说来,便是C澳门新葡亰游戏881和A共享代码段,并且C将A的数据空间,客栈等复制一份 ,然后从A创建C的地方开始运行。

A和C的相似度极大年夜,除了以下方面(来自这里: http://opengroup.org/onlinepubs/007908775/xsh/fork.html ):

The new process (child process) is an exact copy of the calling process (parent process) except as detailed below.

The child process has a unique process ID.

The child process ID also does not match any active process group ID.

The child process has a different parent process ID (that is, the process ID of the parent process).

The child process has its own copy of the parent's file descriptors. Each of the child's file descriptors澳门新葡亰游戏881 refers to the same open file description with the corresponding file descriptor of the parent.

The child process has its own copy of the parent's open directory streams. Each open directory stream in the child process may share directory stream positioning with the corresponding directory stream of the parent.

The child process may have its own copy of the parent's message catalogue descriptors.

The child process' values of tms_utime, tms_stime, tms_cutime and tms_cstime are set to 0.

The time left until an alarm clock signal is reset to 0.

All semadj values are cleared.

File locks set by the parent process are not inherited by the child process.

The set of signals pending for the child process is initialised to the empty set.

Interval timers are reset in the child process.

If the Semaphores option is supported, any semaphores that are open in the parent process will also be open in the child process.

If the Process Memory Locking option is supported, the child process does not inherit any address space memory locks established by the parent process via calls to mlockall() or mlock().

Memory mappings created in the parent are retained in the child process. MAP_PRIVATE mappings inherited from the parent will also be MAP_PRIVATE mappings in the child, and any modifications to the data in these mappings made by the parent prior to calling fork()will be visible to the child. Any modifications to the data in MAP_PRIVATE mappings made by the parent after fork() returns will be visible only to the parent. Modifications to the data in MAP_PRIVATE mappings made by the child will be visible only to the child.

If the Process Scheduling option is supported, for the SCHED_FIFO and SCHED_RR scheduling policies, the child process inherits the policy and priority settings of the parent process during a fork() function. For other scheduling policies, the policy and priority settings on fork()are implementation-dependent.

If the Timers option is supported, per-process timers created by the pa澳门新葡亰游戏881rent are not inherited by the child process.

If the Message Passing option is supported, the child process has its own copy of the message queue descriptors of the parent. Each of the message descriptors of the child refers to the same open message queue description as the corres澳门新葡亰游戏881ponding message descriptor of the parent.

If the Asynchronous Input and Output option is supported, no asynchronous input or asynchronous output operations are inherited by the child process.

从代码角度来看,创建一个新进程的函数声明如下:

pid_t fork(void);

您可能还会对下面的文章感兴趣: