我正在为我的小型网络服务器编写一个cgi程序。那个程序然后分叉创建一个孩子。据我所知,父和它的孩子共享相同的文件描述符,所以我希望看到孩子的输出,实际上没有发生。
cgi程序基本上是这样的:
printf("Content-Type: text/plain;charset=us-ascii\n\n");
printf("parent");
pid=fork();
if(pid==0) printf("child");
wait(null);
我所期望的是“父母”和“孩子”,但事实上它只是“父母”。谁能帮我解释一下?感谢任何帮助
答案 0 :(得分:0)
您应该仔细检查是否实际创建了子进程。从here开始,您应该验证返回的pid不是-1,如果是,请检查errno以获取更多信息:
返回值
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.
如果上述方法没有帮助,那么如果您可以提供有关您尝试执行此操作的操作系统的更多信息可能会有用,但我认为这是大多数平台上非常标准的代码。< / p>