2011-05-28 85 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