1.fork()簡(jiǎn)介
函數(shù)原型:
pid_t fork(void);//pid_t為int類(lèi)型,進(jìn)行了重載
pid_t getpid();// 獲取當(dāng)前進(jìn)程的 pid 值。
pid_t getppid(); //獲取當(dāng)前進(jìn)程的父進(jìn)程 pid 值。
用于創(chuàng)建一個(gè)進(jìn)程,所創(chuàng)建的進(jìn)程復(fù)制父進(jìn)程的代碼段/數(shù)據(jù)段/BSS段/堆/棧等所有用戶空間信息;在內(nèi)核中操作系統(tǒng)重新為其申請(qǐng)了一個(gè)PCB,并使用父進(jìn)程的PCB進(jìn)行初始化;
如圖所示 :我們將A 進(jìn)程, 也就是調(diào)用 fork 的進(jìn)程稱之為父進(jìn)程, 而新的進(jìn)程(B 進(jìn)程)稱之為子進(jìn)程。
關(guān)于fork 可以命令,查看詳細(xì)說(shuō)明及用法:
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)用的一個(gè)奇妙之處就是它僅僅被調(diào)用一次,卻能夠返回兩次,它可能有三種不同的返回值:
在父進(jìn)程中,fork返回新創(chuàng)建子進(jìn)程的進(jìn)程ID;
在子進(jìn)程中,fork返回0;
如果出現(xiàn)錯(cuò)誤,fork返回一個(gè)負(fù)值;
因此我們可以通過(guò)fork返回的值來(lái)判斷當(dāng)前進(jìn)程是子進(jìn)程還是父進(jìn)程。(注:fork 調(diào)用生成的新進(jìn)程與其父進(jìn)程誰(shuí)先執(zhí)行不一定,哪個(gè)進(jìn)程先執(zhí)行要看系統(tǒng)的進(jìn)程調(diào)度策略)
舉個(gè)例子來(lái)解釋fpid的值為什么在父子進(jìn)程中不同:“相當(dāng)于鏈表,進(jìn)程形成了鏈表,父進(jìn)程的fpid(p 意味point)指向子進(jìn)程的進(jìn)程id, 因?yàn)樽舆M(jìn)程沒(méi)有子進(jìn)程,所以其fpid為0.
3, fork()例程
看到這里大家對(duì)fork()有個(gè)大致了解了,讓我們來(lái)看個(gè)例題:
#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
運(yùn)行:
./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)生兩個(gè)pid(進(jìn)程)
審核編輯:劉清
-
pcb
+關(guān)注
關(guān)注
4326文章
23161瀏覽量
399989 -
Linux系統(tǒng)
+關(guān)注
關(guān)注
4文章
596瀏覽量
27510 -
PID控制
+關(guān)注
關(guān)注
10文章
460瀏覽量
40281 -
Linux驅(qū)動(dòng)
+關(guān)注
關(guān)注
0文章
43瀏覽量
10015 -
Fork
+關(guān)注
關(guān)注
0文章
14瀏覽量
3334
原文標(biāo)題:【Linux應(yīng)用開(kāi)發(fā)】fork()函數(shù)詳解
文章出處:【微信號(hào):嵌入式加油站,微信公眾號(hào):嵌入式加油站】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
Linux下多進(jìn)程編程之fork()函數(shù)語(yǔ)法
Linux進(jìn)程之fork()函數(shù)詳解及經(jīng)典筆試題
Linux中fork()函數(shù)詳解
linux內(nèi)核do_fork函數(shù)創(chuàng)建新進(jìn)程
Fork/Join的框架機(jī)制詳解
最常見(jiàn)的fork用法是什么
Android開(kāi)發(fā)手冊(cè)—API函數(shù)詳解
![Android開(kāi)發(fā)手冊(cè)—API<b class='flag-5'>函數(shù)</b><b class='flag-5'>詳解</b>](https://file.elecfans.com/web2/M00/4A/0A/pYYBAGKhvI2AaWdfAAA9AF9KZg8578.png)
基于Android開(kāi)發(fā)手冊(cè)—API函數(shù)詳解
![基于Android開(kāi)發(fā)手冊(cè)—API<b class='flag-5'>函數(shù)</b><b class='flag-5'>詳解</b>](https://file.elecfans.com/web2/M00/4A/1D/pYYBAGKhvJqAS3i4AAAp6AAG78k580.png)
linux中fork()函數(shù)詳解
Linux中fork()函數(shù)詳解
fork函數(shù)的作用_fork函數(shù)創(chuàng)建進(jìn)程
![<b class='flag-5'>fork</b><b class='flag-5'>函數(shù)</b>的作用_<b class='flag-5'>fork</b><b class='flag-5'>函數(shù)</b>創(chuàng)建進(jìn)程](https://file.elecfans.com/web1/M00/D1/76/o4YBAF_F-O6AdhTLAABSBJdKVjM046.jpg)
Linux內(nèi)核GPIO操作函數(shù)的詳解分析
Linux中可怕的fork炸彈介紹
![Linux中可怕的<b class='flag-5'>fork</b>炸彈介紹](https://file1.elecfans.com/web2/M00/88/80/wKgaomRq19mAJnT0AAAm_WugADY738.png)
評(píng)論