FORK()函數(shù)的理解
對(duì)于剛剛接觸Unix/Linux操作系統(tǒng),在Linux下編寫(xiě)多進(jìn)程的人來(lái)說(shuō),fork是最難理解的概念之一:它執(zhí)行一次卻返回兩個(gè)值。
本文引用地址:http://www.eepw.com.cn/article/148649.htm首先我們來(lái)看下fork函數(shù)的原型:
#i nclude
#i nclude
pid_t fork(void);
返回值:
負(fù)數(shù):如果出錯(cuò),則fork()返回-1,此時(shí)沒(méi)有創(chuàng)建新的進(jìn)程。最初的進(jìn)程仍然運(yùn)行。
零:在子進(jìn)程中,fork()返回0
正數(shù):在負(fù)進(jìn)程中,fork()返回正的子進(jìn)程的PID
其次我們來(lái)看下如何利用fork創(chuàng)建子進(jìn)程。
創(chuàng)建子進(jìn)程的樣板代碼如下所示:
pid_t child;
if((child = fork())0)
/*錯(cuò)誤處理*/
else if(child == 0)
/*這是新進(jìn)程*/
else
/*這是最初的父進(jìn)程*/
fock函數(shù)調(diào)用一次卻返回兩次;向父進(jìn)程返回子進(jìn)程的ID,向子進(jìn)程中返回0,
這是因?yàn)楦高M(jìn)程可能存在很多過(guò)子進(jìn)程,所以必須通過(guò)這個(gè)返回的子進(jìn)程ID來(lái)跟蹤子進(jìn)程,
而子進(jìn)程只有一個(gè)父進(jìn)程,他的ID可以通過(guò)getppid取得。
下面我們來(lái)對(duì)比一下兩個(gè)例子:
第一個(gè):
#include
#include
int main()
{
pid_t pid;
int count=0;
pid = fork();
printf( This is first time, pid = %dn, pid );
printf( This is secONd time, pid = %dn, pid );
count++;
printf( count = %dn, count );
if ( pid>0 )
{
printf( This is the parent process,the child has the pid:%dn, pid );
}
else if ( !pid )
{
printf( This is the child Process.n)
}
else
{
printf( fork failed.n );
}
printf( This is third time, pid = %dn, pid );
printf( This is fouth time, pid = %dn, pid );
return 0;
}
運(yùn)行結(jié)果如下:
問(wèn)題:
這個(gè)結(jié)果很奇怪了,為什么printf的語(yǔ)句執(zhí)行兩次,而那句“count++;”的語(yǔ)句卻只執(zhí)行了一次
接著看:
#include
#include
int main(void)
{
pid_t pid;
int count=0;
pid = fork();
printf( Now, the pid returned by calling fork() is %dn, pid );
if ( pid>0 )
{
printf( This is the parent procESS,the child has the pid:%dn, pid );
printf( In the parent process,count = %dn, count );
}
else if ( !pid )
{
printf( This is the child process.n);
printf( Do your own things here.n );
count ++;
printf( In the child process, count = %dn, count );
}
else
{
printf( fork failed.n );
}
return 0;
}
運(yùn)行結(jié)果如下:
現(xiàn)在來(lái)解釋上面提出的問(wèn)題。
看這個(gè)程序的時(shí)候,頭腦中必須首先了解一個(gè)概念:在語(yǔ)句pid=fork()之前,只有一個(gè)進(jìn)程在執(zhí)行這段代碼,但在這條語(yǔ)句之后,就變成兩個(gè)進(jìn)程在執(zhí)行了,這兩個(gè)進(jìn)程的代碼部分完全相同,將要執(zhí)行的下一條語(yǔ)句都是if ( pid>0 )……。
兩個(gè)進(jìn)程中,原先就存在的那個(gè)被稱作“父進(jìn)程”,新出現(xiàn)的那個(gè)被稱作“子進(jìn)程”。父子進(jìn)程的區(qū)別除了進(jìn)程標(biāo)志符(process ID)不同外,變量pid的值也不相同,pid存放的是fork的返回值。fork調(diào)用的一個(gè)奇妙之處就是它僅僅被調(diào)用一次,卻能夠返回兩次,它可能有三種不同的返回值:
1. 在父進(jìn)程中,fork返回新創(chuàng)建子進(jìn)程的進(jìn)程ID;
2.在子進(jìn)程中,fork返回0;
3.如果出現(xiàn)錯(cuò)誤,fork返回一個(gè)負(fù)值;
fork出錯(cuò)可能有兩種原因:(1)當(dāng)前的進(jìn)程數(shù)已經(jīng)達(dá)到了系統(tǒng)規(guī)定的上限,這時(shí)errno的值被設(shè)置為EAGAIN。(2)系統(tǒng)內(nèi)存不足,這時(shí)errno的值被設(shè)置為ENOMEM。
接下來(lái)我們來(lái)看看APUE2中對(duì)fork的說(shuō)明:
The new process created by fork is called the child process. This function is called once but returns twice. The only difference in the returns is that the return value in the child is 0, whereas the return value in the parent is the process ID of the new child. The reason the child"s process ID is returned to the parent is that a process can have more than one child, and there is no function that allows a process to o^ain the process IDs of its children. The reason fork returns 0 to the child is that a process can have only a single parent, and the child can always call getppid to o^ain the process ID of its parent. (Process ID 0 is reserved for use by the kernel, so it"s not possible for 0 to be the process ID of a child.)
被fork創(chuàng)建的新進(jìn)程叫做自進(jìn)程。fork函數(shù)被調(diào)用一次,卻兩次返回。返回值唯一的區(qū)別是在子進(jìn)程中返回0,而在父進(jìn)程中返回子進(jìn)程的pid。在父進(jìn)程中要返回子進(jìn)程的pid的原因是父進(jìn)程可能有不止一個(gè)子進(jìn)程,而一個(gè)進(jìn)程又沒(méi)有任何函數(shù)可以得到他的子進(jìn)程的pid。
Both the child and the parent continue executing with the instruction that follows the call to fork. The child is a copy of the parent. For example, the child gets a copy of the parent"s data space, heap, and stack. Note that this is a copy for the child; the parent and the child do not share these portions of memory. The parent and the child share the text segment (Section 7.6).
您可能也感興趣:
今日熱點(diǎn)
為您推薦
8家險(xiǎn)企股權(quán)被掛牌轉(zhuǎn)讓,為何險(xiǎn)企股權(quán)不再被追捧?
深圳最低工資標(biāo)準(zhǔn)調(diào)整為2360元/月 失業(yè)保險(xiǎn)金為2124元/月
遼寧實(shí)施失業(yè)保險(xiǎn)省級(jí)統(tǒng)籌 對(duì)缺口核定等作出詳細(xì)規(guī)定
更多
- 蕪湖釋放創(chuàng)新“N次方”效應(yīng) 數(shù)字賦能驅(qū)動(dòng)產(chǎn)業(yè)升級(jí)
- 重慶:激發(fā)人才創(chuàng)新活力,到2025年創(chuàng)新要素活躍度顯著增強(qiáng)
- 西寧加快知識(shí)產(chǎn)權(quán)強(qiáng)市建設(shè)步伐 去年兌現(xiàn)資助資金200萬(wàn)元
- 無(wú)錫錫山區(qū)全面啟動(dòng)實(shí)施“雙招雙引” 引進(jìn)高端創(chuàng)新資源
- 重慶巴南區(qū)落實(shí)創(chuàng)新驅(qū)動(dòng)發(fā)展戰(zhàn)略 以科技創(chuàng)新引領(lǐng)五大產(chǎn)業(yè)集...
- 朝陽(yáng)北票經(jīng)開(kāi)區(qū)加快推進(jìn)數(shù)字經(jīng)濟(jì)發(fā)展 推動(dòng)體制機(jī)制創(chuàng)新
- 湖北省先進(jìn)低碳冶金產(chǎn)業(yè)技術(shù)創(chuàng)新聯(lián)合體組建 打造五千億級(jí)產(chǎn)...
- 重慶渝北將優(yōu)化創(chuàng)新生態(tài)環(huán)境 培育打造一批引領(lǐng)型的科創(chuàng)載體
更多
- 山東抓投資抓項(xiàng)目 新興領(lǐng)域投資規(guī)模持續(xù)擴(kuò)大
- 2021年各地引資成績(jī)單亮眼 迸發(fā)出巨大經(jīng)濟(jì)活力
- 多地重大項(xiàng)目投資規(guī)模力度明顯加大 新基建項(xiàng)目成重要發(fā)力點(diǎn)
- 年內(nèi)湖北新開(kāi)工城建項(xiàng)目160個(gè) 集中在道路交通等方面
- 去年我國(guó)對(duì)外投資合作平穩(wěn)發(fā)展 境外經(jīng)貿(mào)合作區(qū)建設(shè)成效顯著
- 荊州集中簽約39個(gè)項(xiàng)目 重點(diǎn)涵蓋高端裝備制造、電子信息等領(lǐng)域
- 廣東打出一系列穩(wěn)外貿(mào)政策“組合拳” 外貿(mào)逆勢(shì)創(chuàng)新高
- 蘭州新區(qū)再獲“最具投資吸引力新區(qū)” 去年新增市場(chǎng)主體5427戶
排行
最近更新
- 蕪湖釋放創(chuàng)新“N次方”效應(yīng) 數(shù)字賦能驅(qū)動(dòng)產(chǎn)業(yè)升級(jí)
- 去年快遞服務(wù)全程時(shí)限水平有較大提升 干線運(yùn)輸時(shí)限縮短
- 住建部:推動(dòng)長(zhǎng)江經(jīng)濟(jì)帶高質(zhì)量發(fā)展,打造區(qū)域協(xié)調(diào)發(fā)展新樣板
- 江蘇發(fā)布重大項(xiàng)目清單 增資擴(kuò)產(chǎn)項(xiàng)目明顯增多
- 山東抓投資抓項(xiàng)目 新興領(lǐng)域投資規(guī)模持續(xù)擴(kuò)大
- Gucci在2月將投放 10 個(gè)“SuperGucci”NFT
- 美國(guó)銀行:美國(guó)CBDC將保持美元作為世界儲(chǔ)備貨幣的地位
- 美股三大指數(shù)尾盤(pán)集體轉(zhuǎn)漲
- 光伏發(fā)電概念股有哪些?光伏發(fā)電概念龍頭股一覽
- The new iPad屏幕對(duì)比評(píng)測(cè)
- 電源接通延時(shí)器
- 汽車(chē)電子穩(wěn)定系統(tǒng)(ESP)詳解
- 聯(lián)想B520一體機(jī)拆解
- 焦炭相關(guān)股票有哪些?焦炭概念股票龍頭一覽
- 增強(qiáng)信號(hào) 3G無(wú)線上網(wǎng)卡改裝拆解全攻略
- 盤(pán)點(diǎn)全球五大智能手機(jī)生產(chǎn)商 華為聯(lián)想入圍
- 三星新平板 Galaxy Note 10.1全拆解
- 智能監(jiān)控防盜報(bào)警系統(tǒng)
- 格力電器未來(lái)三年股東回報(bào)規(guī)劃:每年累計(jì)分紅不低于當(dāng)年凈利潤(rùn)50%
- 消息稱京東科技計(jì)劃2022年在港IPO,募資10億至20億美元
- 恒大集團(tuán):呼吁境外債權(quán)人不采取任何激進(jìn)的法律行動(dòng)
- 海航董事長(zhǎng)劉璐因個(gè)人原因辭職,在海航已近28年
- 青青稞酒預(yù)計(jì)2021年?duì)I收增長(zhǎng)30%-40%,四季度凈虧損超1500萬(wàn)
- 2022年在港上市募資10億美元?京東科技:不予置評(píng)
- 財(cái)政部修訂出臺(tái)《財(cái)政行政處罰聽(tīng)證實(shí)施辦法》
- 賣(mài)房子需要交什么稅?賣(mài)房子必須攜帶哪些證件?
- 醫(yī)??ǖ氖褂梅秶心男??補(bǔ)牙可以使用醫(yī)保報(bào)銷(xiāo)嗎?
- 小產(chǎn)權(quán)房能過(guò)戶嗎?購(gòu)買(mǎi)小產(chǎn)權(quán)房有什么風(fēng)險(xiǎn)?
- 奕東電子的實(shí)際控制人是誰(shuí)?奕東電子股票上市了嗎?
- 百合股份是一家什么公司?百合股份的股票何時(shí)上市?
今日要聞
- 蕪湖釋放創(chuàng)新“N次方”效應(yīng) 數(shù)字賦能驅(qū)動(dòng)產(chǎn)業(yè)升級(jí)
- 去年快遞服務(wù)全程時(shí)限水平有較大提升 干線運(yùn)輸時(shí)限縮短
- 住建部:推動(dòng)長(zhǎng)江經(jīng)濟(jì)帶高質(zhì)量發(fā)展,打造區(qū)域協(xié)調(diào)發(fā)展新樣板
- 山東抓投資抓項(xiàng)目 新興領(lǐng)域投資規(guī)模持續(xù)擴(kuò)大
- Gucci在2月將投放 10 個(gè)“SuperGucci”NFT
- 美國(guó)銀行:美國(guó)CBDC將保持美元作為世界儲(chǔ)備貨幣的地位
- 美股三大指數(shù)尾盤(pán)集體轉(zhuǎn)漲
- The new iPad屏幕對(duì)比評(píng)測(cè)
- 電源接通延時(shí)器
- 汽車(chē)電子穩(wěn)定系統(tǒng)(ESP)詳解