您的位置:網站首頁 > 電器維修資料網 > 正文 >
FORK()函數的理解
來源: 日期:2013-11-29 9:19:25 人氣:標簽:
返回值:
負數:如果出錯,則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).
- 1
- 2
- 下一頁
【看看這篇文章在百度的收錄情況】
相關文章
- 上一篇: 光纖活動連接器的種類
- 下一篇: PADS中BGA Fanout扇出教程