I'm writing a cgi program for my small webserver. That program then forks to create a child. As far as I know, parent and its children share the same file descriptor, so I expected to see the child's o开发者_JAVA技巧utput, which actually didn't happen.
the cgi program is basically like this:
printf("Content-Type: text/plain;charset=us-ascii\n\n");
printf("parent");
pid=fork();
if(pid==0) printf("child");
wait(null);
What i expected are both "parent" and "child", but in fact it was just "parent". Could anyone help me to explain? Appreciate any help
You should double check that a child process is actually being created. From here you should verify that the returned pid is not -1 and if so, check the errno for more information:
RETURN VALUE
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.
ERRORS
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.
If the above does not help, it would probably be useful if you could give some more information on the operating system you're trying to do this on, but I'd imagine this is pretty standard code across most platforms.
精彩评论