I created a server process which forks and uses a child process to handle the client's request. I was just making sure that I properly reap any zombie processes which I may have. Is this accomplished through the waitpid() method? Here is a copy of my server code:
Code:
msock = myTCPserverSocket(port, argv[0]);
/*
* Now accept connections and service clients forever
*/
while(1) {
/*Accept the client's socket*/
socket = myTCPaccept(msock, argv[0]);
if( (pid=fork()) > 0) { //Parent Process
myTCPclose(socket); //Child process is now the only one with access to this socket
if( (wpid = waitpid(pid, &stat, 0)) != pid) {
fprintf(stderr, "Signal was received by parent while waiting...Error\n");
fprintf(stderr, "ERRNO: %s\n", strerror(errno));
exit(1);
}
}
else if (pid==0) { //Child Process
myTCPclose(msock); //Close the "master socket" as we only care about the socket which was newly created in this process
pid = getpid();
if((serverGet = myHTTPserverGet(socket, path, from, userAgent, &sleepTime)) < 0) {
fprintf(stdout, "Trouble in myHTTPserverGet.\nReturn Value = %d\n", serverGet);
exit(1);
}
sleep(sleepTime);
/*Send the error code from myHTTPserverGet and path of the file to be written back to the client to myHTTPserverResponse*/
if((serverResp =myHTTPserverResponse(socket, userAgent, serverGet, path, sleepTime, pid)) < 0) {
fprintf(stdout, "Trouble in myHTTPserverResponse.\nReturn Value = %d\n", serverResp);
exit(1);
}
exit(1);
}
else { //Error in Fork Call
fprintf(stderr, "There was an error in the fork call\n");
fprintf(stderr, "ERRNO: %s\n", strerror(errno));
exit(1);
}
// myTCPclose(socket);
}
}