Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #include #include #include #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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To SQL

Authors: Frank Lusardi

1st Edition

0070390029, 978-0070390027

More Books

Students also viewed these Databases questions

Question

3. How would you address the problems that make up the situation?

Answered: 1 week ago

Question

2. What recommendations will you make to the city council?

Answered: 1 week ago