Question
Consider the following simple server in the internet domain using TCP. How I can add a SIGCHLD handler to the SPECIFIC program, to reap zombie
Consider the following simple server in the internet domain using TCP. How I can add a SIGCHLD handler to the SPECIFIC program, to reap zombie processes in C?
#include
void error(char *msg) { perror(msg); exit(1); }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; int n; pid_t pid; int status; if (argc < 2) { fprintf(stderr,"ERROR, no port provided "); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr);
while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); if((pid=fork())== -1) { pid = wait(&status); close(newsockfd); continue; } else if(pid>0) { //wait(NULL); close(newsockfd); continue; }
while(1) { bzero(buffer,256);
n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket");
if (n == 0) {printf("This Client is gone "); break;} printf("Here is the message: %s ",buffer);
n = write(newsockfd, buffer, strlen(buffer)); if (n < 0) error("ERROR writing to socket"); } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started