2011-05-28 83 views
0

我正在爲我的小型網絡服務器編寫一個cgi程序。該計劃然後分叉創造一個孩子。據我所知,父母和孩子共享相同的文件描述符,所以我期望看到孩子的輸出,但實際上並沒有發生。文件描述符 - 父母和分叉的孩子

CGI程序基本上是這樣的:

printf("Content-Type: text/plain;charset=us-ascii\n\n"); 

printf("parent"); 

pid=fork(); 

if(pid==0) printf("child"); 

wait(null); 

我期待什麼都是「父」與「子」,但實際上這只是「父母」。任何人都可以幫我解釋一下嗎?感謝任何幫助

回答

0

您應該仔細檢查是否實際上正在創建子進程。從here您應該驗證返回的PID不是-1,如果是這樣,請檢查錯誤號以獲取更多信息:

返回值

Upon successful completion, fork() shall return 0 to the child process 
and shall return the process ID of the 
child process to the parent process. 
Both processes shall continue to 
execute from the fork() function. 
Otherwise, -1 shall be returned to the 
parent process, no child process shall 
be created, and errno shall be set to 
indicate the error. 

錯誤

The fork() function shall fail if: 

[EAGAIN] 
    The system lacked the necessary resources to create another 
    process, or the system-imposed limit 
    on the total number of processes under 
    execution system-wide or by a single 
    user {CHILD_MAX} would be exceeded. 


    The fork() function may fail if: 

[ENOMEM] 
    Insufficient storage space is available. 

如果上面的內容沒有幫助,如果你可以提供更多關於你正在嘗試操作的操作系統的信息,這可能會很有用,但我可以想象這是pret ty大多數平臺上的標準代碼。

+0

我確信孩子是被創造出來的。打印出標準流沒有問題。只有當我將輸出重定向到客戶端套接字時纔會出現此問題 – totalnewbie 2011-05-29 05:38:16