Socket programming practices (8)

Source: Internet
Author: User
How to Avoid zombie processes by using multiple processes on the server

1) Solve botnets by ignoring sigchld Signals

Add

signal(SIGCHLD, SIG_IGN);

2) Solve botnets through the wait/waitpid Method

  signal(SIGCHLD,onSignalCatch);    void onSignalCatch(int signalNumber)  {      wait(NULL);  }

Server code:

// Return a socket that have start listened.int mkatcpserver (int serverport, int backlog = somaxconn) {int sockfd = socket (af_inet, sock_stream, 0); If (sockfd =-1) {err_exit ("socket error");} // Add address reused int on = 1; if (setsockopt (sockfd, sol_socket, so_reuseaddr, & on, sizeof (on )) =-1) {err_exit ("setsockopt so_reuseaddr error");} // band a local address and port struct sockaddr_in serveraddr; serveraddr. sin_family = af_inet; serveraddr. sin_port = htons (SERVERPORT); serveraddr. sin_addr.s_addr = inaddr_any; // band an any IP address if (BIND (sockfd, (struct sockaddr *) & serveraddr, sizeof (serveraddr) =-1) {err_exit ("BIND error");} // start to listen. if (Listen (sockfd, backlog) =-1) {err_exit ("Listen error");} return sockfd;} void onsignalcatch (INT signalnumber) {waitpid (-1, null, wnohang);} int main () {// install sigchld signal processing function signal (sigchld, onsignalcatch); int serversockfd = mkatcpserver (8002); struct sockaddr_in peeraddr; socklen_t peerlen = sizeof (peeraddr); While (true) {// accept the link int peersockfd = accept (serversockfd, (struct sockaddr *) & peeraddr, & peerlen ); if (peersockfd =-1) {err_exit ("Accept error ");}..... // other code is similar to the previous one

Disable multiple clients at the same time

The problem is described as follows:



Client code implementation code

// Other Code such as // ...int main () {int serversocket [10]; int socketcount = 10; for (INT I = 0; I <socketcount; ++ I) {serversocket [I] = mkatcpclient (8002, "127.0.0.1");} Sleep (100); Return 0 ;}

When you press Ctrl + C during the running process, you can see that 10 sub-processes are started on the server and all clients are disconnected, the number of zombie processes generated is amazing (It also proves that the sigchld signal is unreliable )!

 


Solution:

// Some server code is as follows: // others such as the previous... void onsignalcatch (INT signalnumber) {int ret = 0; // pay attention to this !!!! While (ret = waitpid (-1, null, wnohang )! =-1);} int main () {signal (sigchld, onsignalcatch); int serversockfd = mkatcpserver (8002 );...

We can see that the server process has been monitored and no zombie process has been found!



Socket programming practices (8)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.