小明永久免费大陆在线观看-小明永久免费视频-小明永久视频免费播放-小嫩妇好紧好爽再快视频-小嫩嫩12欧美-小日本xxx

首頁 新聞 > 科技 > 正文

FORK()函數的理解

對于剛剛接觸Unix/Linux操作系統,在Linux下編寫多進程的人來說,fork是最難理解的概念之一:它執行一次卻返回兩個值。

本文引用地址:http://www.eepw.com.cn/article/148649.htm

首先我們來看下fork函數的原型:

#i nclude

#i nclude

pid_t fork(void);

返回值:

負數:如果出錯,則fork()返回-1,此時沒有創建新的進程。最初的進程仍然運行。

零:在子進程中,fork()返回0

正數:在負進程中,fork()返回正的子進程的PID

其次我們來看下如何利用fork創建子進程。

創建子進程的樣板代碼如下所示:

pid_t child;

if((child = fork())0)

/*錯誤處理*/

else if(child == 0)

/*這是新進程*/

else

/*這是最初的父進程*/

fock函數調用一次卻返回兩次;向父進程返回子進程的ID,向子進程中返回0,

這是因為父進程可能存在很多過子進程,所以必須通過這個返回的子進程ID來跟蹤子進程,

而子進程只有一個父進程,他的ID可以通過getppid取得。

下面我們來對比一下兩個例子:

第一個:

#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;

}

運行結果如下:

問題:

這個結果很奇怪了,為什么printf的語句執行兩次,而那句“count++;”的語句卻只執行了一次

接著看:

#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;

}

運行結果如下:

現在來解釋上面提出的問題。

看這個程序的時候,頭腦中必須首先了解一個概念:在語句pid=fork()之前,只有一個進程在執行這段代碼,但在這條語句之后,就變成兩個進程在執行了,這兩個進程的代碼部分完全相同,將要執行的下一條語句都是if ( pid>0 )……。

兩個進程中,原先就存在的那個被稱作“父進程”,新出現的那個被稱作“子進程”。父子進程的區別除了進程標志符(process ID)不同外,變量pid的值也不相同,pid存放的是fork的返回值。fork調用的一個奇妙之處就是它僅僅被調用一次,卻能夠返回兩次,它可能有三種不同的返回值:

1. 在父進程中,fork返回新創建子進程的進程ID;

2.在子進程中,fork返回0;

3.如果出現錯誤,fork返回一個負值;

fork出錯可能有兩種原因:(1)當前的進程數已經達到了系統規定的上限,這時errno的值被設置為EAGAIN。(2)系統內存不足,這時errno的值被設置為ENOMEM。

接下來我們來看看APUE2中對fork的說明:

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創建的新進程叫做自進程。fork函數被調用一次,卻兩次返回。返回值唯一的區別是在子進程中返回0,而在父進程中返回子進程的pid。在父進程中要返回子進程的pid的原因是父進程可能有不止一個子進程,而一個進程又沒有任何函數可以得到他的子進程的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).

關鍵詞: 理解 函數 FORK

最近更新

關于本站 管理團隊 版權申明 網站地圖 聯系合作 招聘信息

Copyright © 2005-2018 創投網 - m.zhigu.net.cn All rights reserved
聯系我們:33 92 950@qq.com
豫ICP備2020035879號-12

 

日韩av片无码一区二区不卡 | 又黄又爽又无遮挡免费的网站 | 日韩精品成人无码专区免费 | www夜片内射视频在观看视频 | 久久精品国产亚洲av忘忧草18 | 国产午夜精品一区二区三区 | 亚洲aⅴ在线无码播放毛片一线天 | 免费观看的毛片手机视频 | 亚洲精品沙发午睡系列 | 国产成人精品一区二区免费 | 亚洲综合无码无在线观看 | 国产av无码专区亚洲av蜜芽 | 免费国产黄频在线观看视频 | 又爆又大又粗又硬又黄的a片 | 日本亲与子乱人妻hd | 天天爱天天做天天爽 | 全免费a级毛片免费看视频 国产成人无码精品久久久免费 | 久久99亚洲网美利坚合众国 | 国产精品 高清 尿 小便 嘘嘘 | 国产亚洲成av人片在线观看 | 一本色道久久88加勒比—综合 | 久久久久女人精品毛片 | 久久精品免费一区二区喷潮 | 免费无码又爽又刺激高潮视频 | 手机看片福利一区二区三区 | 十八禁无码免费网站 | 狠狠色噜噜狠狠狠888米奇视频 | 亚洲av无码国产精品色午友在线 | 91久久精品在这里色伊人6882 | 手机看片久久国产免费 | 久久精品国产99国产精品 | 激情国产白嫩美女在线观看 | 国产免费午夜a无码v视频 | 欧美黑吊大战白妞 | 亚洲国产成人久久综合一区 | 国产免费看插插插视频 | 国产精品久久久久久久久久免费 | 大陆国语对白国产av片 | 91久久精品视频 | 一本大道在线无码一区 | 国精产品一区二区三区 |