久久人妻av一区二区软件 ,欧美日韩中文亚洲另类春色,国产欧美亚洲精品a,亚洲a∨无码男人的天堂,精品亚洲人伦一区二区三区,国产美女精品一区二区,欧美精品aaa久久久影院,成人精品天堂一区二区三区
首頁 新聞 > 科技 > 正文

FORK()函數(shù)的理解

對(duì)于剛剛接觸Unix/Linux操作系統(tǒng),在Linux下編寫多進(jìn)程的人來說,fork是最難理解的概念之一:它執(zhí)行一次卻返回兩個(gè)值。

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

首先我們來看下fork函數(shù)的原型:

#i nclude

#i nclude

pid_t fork(void);

返回值:

負(fù)數(shù):如果出錯(cuò),則fork()返回-1,此時(shí)沒有創(chuàng)建新的進(jìn)程。最初的進(jìn)程仍然運(yùn)行。

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

正數(shù):在負(fù)進(jìn)程中,fork()返回正的子進(jìn)程的PID

其次我們來看下如何利用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)程可能存在很多過子進(jìn)程,所以必須通過這個(gè)返回的子進(jìn)程ID來跟蹤子進(jìn)程,

而子進(jìn)程只有一個(gè)父進(jìn)程,他的ID可以通過getppid取得。

下面我們來對(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é)果如下:

問題:

這個(gè)結(jié)果很奇怪了,為什么printf的語句執(zhí)行兩次,而那句“count++;”的語句卻只執(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)在來解釋上面提出的問題。

看這個(gè)程序的時(shí)候,頭腦中必須首先了解一個(gè)概念:在語句pid=fork()之前,只有一個(gè)進(jìn)程在執(zhí)行這段代碼,但在這條語句之后,就變成兩個(gè)進(jìn)程在執(zhí)行了,這兩個(gè)進(jìn)程的代碼部分完全相同,將要執(zhí)行的下一條語句都是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。

接下來我們來看看APUE2中對(duì)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創(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)程又沒有任何函數(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).

關(guān)鍵詞: 理解 函數(shù) FORK

最近更新

關(guān)于本站 管理團(tuán)隊(duì) 版權(quán)申明 網(wǎng)站地圖 聯(lián)系合作 招聘信息

Copyright © 2005-2018 創(chuàng)投網(wǎng) - www.bbcnxku.cn All rights reserved
聯(lián)系我們:33 92 950@qq.com
豫ICP備2020035879號(hào)-12

 

主站蜘蛛池模板: 亚洲香蕉网久久综合影视| 69天堂人成无码麻豆免费视频| 无码av波多野结衣| 国产愉拍精品手机| 午夜dy888国产精品影院| 99久久99视频只有精品| av天堂久久天堂色综合| 亚国产亚洲亚洲精品视频| 中文字幕无码人妻丝袜| 国内精品久久久久久99| 国产精品国产三级国产普通话| 少妇熟女久久综合网色欲| 午夜视频久久久久一区| 精品少妇无码一区二区三批 | 精品无人区卡一卡二卡三乱码| av无码久久久久不卡网站蜜桃| 久久精品国产只有精品2020| av天堂亚洲狼人在线| 成人免费无码大片a毛片直播| 99这里视频只精品2019| 日本免费一区二区三区中文字幕 | 特黄做受又硬又粗又大视频小说| 97无码精品综合| 亚洲另类欧美综合久久图片区| 亚洲国产一区二区三区四区电影网| 狂野av人人澡人人添| 西欧free性满足hd| 久久这里只有是精品23| 国语对白刺激精品视频| 在线点播亚洲日韩国产欧美| 亚洲国产色播av在线| 欧美亚洲综合成人专区| 东北老头嫖妓猛对白精彩| 曰韩a∨无码一区二区三区| 99精品国产在热久久无毒| 天天狠天天添日日拍捆绑调教| 一个本道久久综合久久88| 99久久成人国产精品免费| 亚欧乱色国产精品免费九库| 伊人久久大香线蕉无码不卡| 亚洲国产成人久久精品软件|