Linux 環境中fork()函數的使用

1.函數簡介:fork()英文單詞意思是叉子,分叉;那麼在計算機中fork()就是分叉函數。

Linux中的fork()函數名、頭文件、作用、返回值:

NAME
       fork - create a child process

SYNOPSIS
       #include <unistd.h>    // 頭文件

       pid_t fork(void);    // 函數原型

DESCRIPTION
       fork()  creates a new process by duplicating the calling process.  The    
       new process is referred to as the child process.  The calling  process
       is referred to as the parent process.
RETURN VALUE
       On  success,  the  PID of the child process is returned in the parent,
       and 0 is returned in the child.  On failure, -1  is  returned  in  the
       parent, no child process is created, and errno is set appropriately.

總結:fork()函數作用爲創建一個新進程/子進程;

          fork()成功時, 返回值pid_t>0,則在父進程中返回子進程的PID; 返回值pid_t = 0時,則在子進程中返回0

          fork()失敗時,返回值爲pid_t=-1,在父進程中返回,未創建子進程,併合理設置錯誤信息。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章