17、fork函数
1、定义 #include <unistd.h> #include<sys/types.h> pid_t fork( void ); pid_t 是一个宏定义,其实质是 int ,被定义在 #include<sys/types.h> 中 返回值:若成功调用一次则返回两个值,子进程返回 0 ,父进程返回子进程 ID ;否则,出错返回 -1 2、函数说明: 一个现有进程可以调用 fork 函数创建一个新进程。由 fork 创建的新进程被称为子进程( child process )。 fork 函数被调用一次但返回两次。两次返回的唯一区别是子进程中返回 0 值而父进程中返回子进程 ID 。 子进程是父进程的副本, 它将获得父进程数据空间、堆、栈等资源的副本 。注意,子进程持有的是上述存储空间的“副本”,这意味着父子进程间不共享这些存储空间。 File locks and pending signals are not inherited. 【 3 】 If the call to fork() is executed successfully, Unix will ① make two identical copies of address spaces, one for the parent and the other for the child. ② Both processes