欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
linux,unix,fock,C語(yǔ)言

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

  首先我們來(lái)看下fork函數的原型:

 ?。 nclude <sys/types.h>

 ?。 nclude <unistd.h>

  pid_t fork(void);

  返回值:

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

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

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

  其次我們來(lái)看下如何利用fork創(chuàng )建子進(jìn)程。

  創(chuàng )建子進(jìn)程的樣板代碼如下所示:

  pid_t child;

  if((child = fork())<0)

  /*錯誤處理*/

  else if(child == 0)

  /*這是新進(jìn)程*/

  else

  /*這是最初的父進(jìn)程*/

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

  這是因為父進(jìn)程可能存在很多過(guò)子進(jìn)程,所以必須通過(guò)這個(gè)返回的子進(jìn)程ID來(lái)跟蹤子進(jìn)程,

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

  下面我們來(lái)對比一下兩個(gè)例子:

  第一個(gè):

  #include <unistd.h>

  #include <stdio.h>

  int main()

  {

  pid_t pid;

  int count=0;

  pid = fork();

  printf( "This is first time, pid = %d\n", pid );

  printf( "This is second time, pid = %d\n", pid );

  count++;

  printf( "count = %d\n", count );

  if ( pid>0 )

  {

  printf( "This is the parent process,the child has the pid:%d\n", pid );

  }

  else if ( !pid )

  {

  printf( "This is the child Process.\n")

  }

  else

  {

  printf( "fork failed.\n" );

  }

  printf( "This is third time, pid = %d\n", pid );

  printf( "This is fouth time, pid = %d\n", pid );

  return 0;

  }

  運行結果如下:


  問(wèn)題:

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

  接著(zhù)看:

  #include <unistd.h>

  #include <stdio.h>

  int main(void)

  {

  pid_t pid;

  int count=0;

  pid = fork();

  printf( "Now, the pid returned by calling fork() is %d\n", pid );

  if ( pid>0 )

  {

  printf( "This is the parent process,the child has the pid:%d\n", pid );

  printf( "In the parent process,count = %d\n", 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 = %d\n", count );

  }

  else

  {

  printf( "fork failed.\n" );

  }

  return 0;

  }

  運行結果如下:

  現在來(lái)解釋上面提出的問(wèn)題。

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

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

  1. 在父進(jìn)程中,fork返回新創(chuàng )建子進(jìn)程的進(jìn)程ID;

  2.在子進(jìn)程中,fork返回0;

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

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

  接下來(lái)我們來(lái)看看APUE2中對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函數被調用一次,卻兩次返回。返回值唯一的區別是在子進(jìn)程中返回0,而在父進(jìn)程中返回子進(jìn)程的pid。在父進(jìn)程中要返回子進(jìn)程的pid的原因是父進(jìn)程可能有不止一個(gè)子進(jìn)程,而一個(gè)進(jìn)程又沒(méi)有任何函數可以得到他的子進(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).

  子進(jìn)程和父進(jìn)程都執行在fork函數調用之后的代碼,子進(jìn)程是父進(jìn)程的一個(gè)拷貝。例如,父進(jìn)程的數據空間、堆??臻g都會(huì )給子進(jìn)程一個(gè)拷貝,而不是共享這些內存。

  Current implementations don't perform. a complete copy of the parent's data, stack, and heap, since a fork is often followed by an exec. Instead, a technique called copy-on-write (COW) is used. These regions are shared by the parent and the child and have their protection changed by the kernel to read-only. If either process tries to modify these regions, the kernel then makes a copy of that piece of memory only, typically a "page" in a virtual memory system. Section 9.2 of Bach [1986] and Sections 5.6 and 5.7 of McKusick et al. [1996] provide more detail on this feature.

  我們來(lái)給出詳細的注釋

  #include <unistd.h>

  #include <stdio.h>

  int main(void)

  {

  pid_t pid;

  int count=0;

  /*此處,執行fork調用,創(chuàng )建了一個(gè)新的進(jìn)程, 這個(gè)進(jìn)程共享父進(jìn)程的數據和堆??臻g等,這之后的代碼指令為子進(jìn)程創(chuàng )建了一個(gè)拷貝。 fock 調用是一個(gè)復制進(jìn)程,fock 不象線(xiàn)程需提供一個(gè)函數做為入口, fock調用后,新進(jìn)程的入口就在 fock的下一條語(yǔ)句。*/

  pid = fork();

  /*此處的pid的值,可以說(shuō)明fork調用后,目前執行的是父進(jìn)程還是子進(jìn)程*/

  printf( "Now, the pid returned by calling fork() is %d\n", pid );

  if ( pid>0 )

  {

  /*當fork在子進(jìn)程中返回后,fork調用又向父進(jìn)程中返回子進(jìn)程的pid, 如是該段代碼被執行,但是注意的事,count仍然為0, 因為父進(jìn)程中的count始終沒(méi)有被重新賦值,  這里就可以看出子進(jìn)程的數據和堆??臻g和父進(jìn)程是獨立的,而不是共享數據*/

  printf( "This is the parent process,the child has the pid:%d\n", pid );

  printf( "In the parent process,count = %d\n", count );

  }

  else if ( !pid )

  { /*在子進(jìn)程中對count進(jìn)行自加1的操作,但是并沒(méi)有影響到父進(jìn)程中的count值,父進(jìn)程中的count值仍然為0*/

  printf( "This is the child process.\n");

  printf( "Do your own things here.\n" );

  count++;

  printf( "In the child process, count = %d\n", count );

  }

  else

  {

  printf( "fork failed.\n" );

  }

  return 0;

  }

  也就是說(shuō),在Linux下一個(gè)進(jìn)程在內存里有三部分的數據,就是"代碼段"、"堆棧段"和"數據段"。"代碼段",顧名思義,就是存放了程序代碼的數據,假如機器中有數個(gè)進(jìn)程運行相同的一個(gè)程序,那么它們就可以使用相同的代碼段。"堆棧段"存放的就是子程序的返回地址、子程序的參數以及程序的局部變量。而數據段則存放程序的全局變量,常數以及動(dòng)態(tài)數據分配的數據空間(比如用malloc之類(lèi)的函數取得的空間)。系統如果同時(shí)運行數個(gè)相同的程序,它們之間就不能使用同一個(gè)堆棧段和數據段。

  仔細分析后,我們就可以知道:

  一個(gè)程序一旦調用fork函數,系統就為一個(gè)新的進(jìn)程準備了前述三個(gè)段,首先,系統讓新的進(jìn)程與舊的進(jìn)程使用同一個(gè)代碼段,因為它們的程序還是相同的,對于數據段和堆棧段,系統則復制一份給新的進(jìn)程,這樣,父進(jìn)程的所有數據都可以留給子進(jìn)程,但是,子進(jìn)程一旦開(kāi)始運行,雖然它繼承了父進(jìn)程的一切數據,但實(shí)際上數據卻已經(jīng)分開(kāi),相互之間不再有影響了,也就是說(shuō),它們之間不再共享任何數據了。

  fork()不僅創(chuàng )建出與父進(jìn)程代碼相同的子進(jìn)程,而且父進(jìn)程在fork執行點(diǎn)的所有上下文場(chǎng)景也被自動(dòng)復制到子進(jìn)程中,包括:

  ——全局和局部變量

  ——打開(kāi)的文件句柄

  ——共享內存、消息等同步對象

  而如果兩個(gè)進(jìn)程要共享什么數據的話(huà),就要使用另一套函數(shmget,shmat,shmdt等)來(lái)操作?,F在,已經(jīng)是兩個(gè)進(jìn)程了,對于父進(jìn)程,fork函數返回了子程序的進(jìn)程號,而對于子程序,fork函數則返回零,這樣,對于程序,只要判斷fork函數的返回值,就知道自己是處于父進(jìn)程還是子進(jìn)程中。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Linux進(jìn)程學(xué)習總結 fork()和vfork()的學(xué)習
fork與vfork的區別
linux 進(jìn)程(二)
UNIX環(huán)境編程學(xué)習筆記(18)
父進(jìn)程退出時(shí)如何確保子進(jìn)程退出?
fork()后exit(0)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久