Linux中fork函数作用,深入解析Linux中的fork函数
1、定義
#include
#include
pid_t fork( void );
pid_t 是一個宏定義,其實質是int,被定義在#include中
返回值:若成功調用一次則返回兩個值,子進程返回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 will start their execution at the next statement following the fork() call.
3、為什么fork會返回兩次?
由于在復制時復制了父進程的堆棧段,所以兩個進程都停留在fork函數中,等待返回。因為fork函數會返回兩次,一次是在父進程中返回,另一次是在子進程中返回,這兩次的返回值是不一樣的。
After a new child process is created, both processes will execute the next instruction following the fork() system call.
調用fork之后,代碼有兩份,都從fork函數中返回。
Please note that Unix will make an exact copy of the parent's address space and give it to the child. Therefore, the parent and child processes have separate address spaces.
實例代碼1:
#include
#include
#include
int main(int argc,char**argv)
{
int pid=fork();
if(pid == -1)
{
print("error!");
}
else if(pid == 0)
{
print("This is the child process!");
}
else
{
print("This is the parent process! child process id = %d", pid);
}
return 0;
}
示例代碼2:#include
#include
#include
#include
#define MAX_COUNT 200
#define BUF_SIZE 100
void main(void)
{
pid_t pid;
int i;
char buf[BUF_SIZE];
fork();
pid = getpid();
for (i = 1; i <= MAX_COUNT; i++) {
sprintf(buf, "This line is from pid %d, value = %d\n", pid, i);
write(1, buf, strlen(buf));
}
}
為什么用write,而不用printf呢?because printf() is "buffered," meaning printf() will group the output of a process together,這樣,會把子進程和父進程的輸出混起來。所以用use the "unbuffered" write。
示例代碼3:
#include
#include
#define MAX_COUNT 200
void ChildProcess(void); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
int main(void)
{
pid_t pid;
pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess();
return 0;
}
void ChildProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d\n", i);
printf(" *** Child process is done ***\n");
}
void ParentProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i);
printf("*** Parent is done ***\n");
}
4、Error Codes
出錯返回錯誤信息如下:
EAGAIN :達到進程數上限.
ENOMEM :沒有足夠空間給一個新進程分配.
fork()系統在Linux中的返回值是沒有NULL的.
[fork]Linux中的fork函數詳解
---------------------------------------------------------------------------------------------------- ...
Unix/Linux中的fork函數
fork函數介紹 一個現有進程可以調用fork函數創建一個新進程.該函數定義如下: #include pid_t fork(void); // 返回:若成功則在子進程 ...
linux中的fork()函數以及標準I/O緩沖
1. fork()創建的新進程成為子進程.一次調用,兩次返回,子進程的返回值是0,而父進程的返回值是新子進程的進程ID,如果出現錯誤,fork返回一個負值. 2.?可以通過fork返回的值來判斷當前進 ...
linux中的fork函數的基本用法
代碼: #include #include #include #include
Linux編程之fork函數
在Linux中,fork函數的功能就是在一個進程中創建一個新的進程,當前調用fork函數的進程就是產生的新進程的父進程,新進程在以下也稱為子進程.在新進程生成之后就會在系統中開始執行. 函數原型:pi ...
【Linux 進程】fork函數詳解
一.fork入門知識 一個進程,包括代碼.數據和分配給進程的資源.fork()函數通過系統調用創建一個與原來進程幾乎完全相同的進程,也就是兩個進程可以做完全相同的事,但如果初始參數或者傳入的變量不同, ...
解析Linux中的VFS文件系統機制
總結
以上是生活随笔為你收集整理的Linux中fork函数作用,深入解析Linux中的fork函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 概率论与数理统计---陈希孺---书籍链
- 下一篇: x7 z8750 linux,x7-z8