TCP/IP network programming (transcription note 3)--zombie process and multitasking concurrent server

Source: Internet
Author: User

<title>TCP/IP network programming (transcription note 3) – Zombie process and multitasking concurrent server</title> TCP/IP network programming (transcription note 3) – Zombie process and multitasking concurrent server table of Contents
    • The production of zombie processes
    • Avoid zombie processes
    • Signal
    • Multi-tasking Concurrent server
The production of zombie processes
    1. Child process exits first, parent process does not exit ==> zombie process
    2. The parent process exits first, the child process does not exit the ==> child process is reclaimed by the No. 0 process and does not produce a zombie process
pid_t pid = fork (); if (PID = = 0) {  child    printf (' Child:%d\n', Getpid ());    printf ("Child exited\n");    Else {    printf ("Parent:%d\n", Getpid ());    Sleep (30);}
Avoid zombie processes

Notifies the parent process when the child process exits:
Parent process Call

int status;wait (&status); wifexited (status); Wexitstatus (status);

This allows you to wait for the child process to exit and get his return value
If the parent process exits with a command called exit, then the child process is reclaimed by process # No. 0

When you call wait, the parent process blocks itself until the child process exits, and if you do not want to block, you can use

Waitpid ( -1, &status, Wnohang);
Signal
void keyboard(int Sig) {if(sig = = SIGINT) {printf ("Ctrl + C pressed\N"); }}void Timeout(int Sig) {if(sig = = SIGALRM) {printf (" Time Out ...\N");        Exit (0); }}int Main() {... signal (SIGINT, keyboard);    Signal (SIGALRM, timeout); Alarm (3);// when the time is over, a signal is generated and the Timeoutu function is called automatically...}

Use Sigaction, signal obsolete

struct sigaction Act; act.sa_flags = 0;sigemptyset (&act.sa_mask); act.sa_handler = timeout; sigaction (SIGALRM, &act, 0);

Using signals to eliminate zombie processes
Principle: As long as the child process exit to tell the parent process a sound, "I want to quit" can eliminate the zombie process, that wait/waitpid is, the call, the child process exit signal isSIGCHLD

Multi-tasking Concurrent server
void Read_childproc(int Sig) {int Status;if(sig = = SIGCHLD) {if(wifexited (status)) {printf ("Child quit ...%d\N", Wexitstatus (status)); }        }}int Main(int argc,Char*argv[]) {int Server_sock;int Client_sock;struct sockaddr_in server_addr;struct sockaddr_in client_addr;// SocketServer_sock = socket (af_inet, sock_stream, 0);// Bindmemset (&server_addr, 0,sizeof(SERVER_ADDR));        server_addr.sin_family = af_inet;        SERVER_ADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);        Server_addr.sin_port = htons (Atoi (argv[1])); Bind (Server_sock, (struct sockaddr*) &server_addr,sizeof(SERVER_ADDR));// ListenListen (Server_sock, 5);// sigaction        struct sigaction Act; Act.sa_handler = Read_childproc;Char buf[Buf_size];int Str_len;pid_t PID; while(1) {socklen_t client_size=sizeof(CLIENT_ADDR); Client_sock = Accept (Server_sock, (struct sockaddr*) &client_addr, &client_size); PID = fork ();if(PID = = 0) {// Child process replication, this time opened 4 FD, requires a child process close one, the parent process close one, look at the image belowClose (Server_sock); while((Str_len = Read (Client_sock, buf, buf_size))! = 0)                        {Write (Client_sock, buf, Str_len);                        } close (Server_sock); printf"Client Disconnected ...\N"); }Else{// The parent process hands the client to the child process and continues to listen to the server FDClose (Client_sock);        }} close (Server_sock); Close (Client_sock);return0;}


TCP/IP network programming (transcription note 3)--zombie process and multitasking concurrent server

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.