1.fork()簡介
函數(shù)原型:
pid_t fork(void);//pid_t為int類型,進行了重載
pid_t getpid();// 獲取當前進程的 pid 值。
pid_t getppid(); //獲取當前進程的父進程 pid 值。
用于創(chuàng)建一個進程,所創(chuàng)建的進程復制父進程的代碼段/數(shù)據(jù)段/BSS段/堆/棧等所有用戶空間信息;在內(nèi)核中操作系統(tǒng)重新為其申請了一個PCB,并使用父進程的PCB進行初始化;
如圖所示 :我們將A 進程, 也就是調(diào)用 fork 的進程稱之為父進程, 而新的進程(B 進程)稱之為子進程。
關(guān)于fork 可以命令,查看詳細說明及用法:
man 3 fork
NAME
fork, wait, waitpid - basic process management
SYNOPSIS
@load "fork"
pid = fork()
ret = waitpid(pid)
ret = wait();
DESCRIPTION
The fork extension adds three functions, as follows.
fork() Thisfunctioncreatesa new process. The return value is the zero in the child and the process-id number of the child in the parent, or -1 upon error. In the latter case, ERRNO indicates
the problem.In the child, PROCINFO["pid"] and PROCINFO["ppid"] are updated to reflect the correct values.
waitpid()
This function takes a numeric argument, which is the process-id to wait for. The return value is that of the waitpid(2) system call.
wait() This function waits for the first child to die.The return value is that of the wait(2) system call.
BUGS
There is no corresponding exec() function.
The interfaces could be enhanced to provide more facilities, including pulling out the various bits of the return status.
EXAMPLE
@load "fork"
...
if ((pid = fork()) == 0)
print "hello from the child"
else
print "hello from the parent"
SEE ALSO
GAWK: Effective AWK Programming, filefuncs(3am), fnmatch(3am), inplace(3am), ordchr(3am), readdir(3am), readfile(3am), revoutput(3am), rwarray(3am), time(3am).
fork(2), wait(2), waitpid(2).
AUTHOR
Arnold Robbins,arnold@skeeve.com.
COPYING PERMISSIONS
Copyright 2012, 2013, Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this manual page provided the copyright notice and this permission notice are preserved on all copies.
2.fork()特性
fork調(diào)用的一個奇妙之處就是它僅僅被調(diào)用一次,卻能夠返回兩次,它可能有三種不同的返回值:
在父進程中,fork返回新創(chuàng)建子進程的進程ID;
在子進程中,fork返回0;
如果出現(xiàn)錯誤,fork返回一個負值;
因此我們可以通過fork返回的值來判斷當前進程是子進程還是父進程。(注:fork 調(diào)用生成的新進程與其父進程誰先執(zhí)行不一定,哪個進程先執(zhí)行要看系統(tǒng)的進程調(diào)度策略)
舉個例子來解釋fpid的值為什么在父子進程中不同:“相當于鏈表,進程形成了鏈表,父進程的fpid(p 意味point)指向子進程的進程id, 因為子進程沒有子進程,所以其fpid為0.
3, fork()例程
看到這里大家對fork()有個大致了解了,讓我們來看個例題:
#include
#include
#include
int main(int argc, const char *argv[])
{
int num = 10;
pid_t pid = fork();
if(pid==0)
{
while (1)
{
num = 100;
printf("The father pid=%d The child pid=%d num=%d ", getppid(),getpid(), num);
sleep(3);
}
}
else
{
while (1)
{
printf("The father pid=%d num=%d ", getpid(), num);
sleep(5);
}
}
return 0;
}
保存為fork_test.c
gcc-o fork_test.a fork_test.c
運行:
./fork_test.a
The father pid=15131 num=10
The father pid=15131 The child pid=15132 num=100
The father pid=15131 The child pid=15132 num=100
The father pid=15131 num=10
The father pid=15131 The child pid=15132 num=100
The father pid=15131 The child pid=15132 num=100
The father pid=15131 num=10
The father pid=15131 The child pid=15132 num=100
可以看到產(chǎn)生兩個pid(進程)
審核編輯:劉清
-
pcb
+關(guān)注
關(guān)注
4319文章
23099瀏覽量
397889 -
Linux系統(tǒng)
+關(guān)注
關(guān)注
4文章
593瀏覽量
27397 -
PID控制
+關(guān)注
關(guān)注
10文章
460瀏覽量
40095 -
Linux驅(qū)動
+關(guān)注
關(guān)注
0文章
43瀏覽量
9965 -
Fork
+關(guān)注
關(guān)注
0文章
14瀏覽量
3302
原文標題:【Linux應用開發(fā)】fork()函數(shù)詳解
文章出處:【微信號:嵌入式加油站,微信公眾號:嵌入式加油站】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論