Thread Pool Network Service model

Source: Internet
Author: User
Tags bind joins mul sleep socket strcmp port number htons

The former: Thread pool Network Service is an improved model for the lack of multi-threaded Network service model, the basic idea is to create a batch of resources, when the user arrives, directly assign the resources that have been created; The main purpose of the thread pool is to reduce the overhead of the system when it frequently creates resources.

the implementation principle of thread pool Network service:

  1> a  fixed number of service threads created by the primary service thread while listening on the specified port;

  2>  If there is a new connection coming, find the idle service thread from the thread pool, service it, service finished, thread does not release, re-put back to thread pool;

  3>   If the current thread pool is full, The current connection is added to the wait queue or a new thread is created for it to service (as the implementation does);

the flowchart for thread pool Network service is as follows:

Benefits of Thread Pool service: High performance

The core design idea of the thread pool is that the system creates a certain number of service threads at initialization, and puts them in idle state, if there is a new user coming, the system first looks for the idle thread, if any, assigns the service thread to it immediately, if not, joins the new user to the service queue and after the other thread service completes. Reassign the service thread to it.

The following improvements are available for new users who are waiting for the service team joins to take too long to affect the user experience:

1> dynamic creation of a new service thread, after the end of the service, the thread pool, the benefits of this method of improvement is the user experience is improved, but the potential problem is the size of the thread for a long time, large concurrent user state, will become very large, and finally due to the resource consumption is too large, the system exits;

2> Add a thread resource recycling mechanism, when the thread pool size reaches a certain level or satisfies certain rules, it will kill some threads actively to achieve the compromise between the user experience and the stability of the system operation;

The thread pool design scheme:

   The 1> system, when initialized, opens up Thread_max threads that make up the service thread pool.

   2> Each thread has two states, idle, working state, each thread has a piece of its own public data area, this data region holds two variables, one is used to mark its own state, 0: Idle 1: Work, another variable corresponds to the client socket size, if the thread is idle, The socket size is the initial value.

   3> When a new user arrives, the system determines whether there are idle threads in the current system by looking for a common data region, and if so, assigns the service thread directly to it.

   4> If there is currently no idle thread, the system will not do any processing, prompt the current system is busy, close the new customer socket

The following is an example of a thread pool:

The client code #include "unp.h" void Input (Oper *op);

void Help ();  int main (int ac, char *av[]) {int sockcli = socket (af_inet, sock_stream, 0);
    Get the client socket if (SOCKCLI = =-1) {perror ("socket");   } struct sockaddr_in addrser;   The address structure of the server addrser.sin_family = af_inet;   Set the protocol family for the server Addrser.sin_port = htons (Server_port);   Set the port number of the server ADDRSER.SIN_ADDR.S_ADDR = inet_addr (SERVER_IP);   Set the IP address of the server socklen_t Addrlen = sizeof (struct sockaddr);   Get the address structure size of the server int res = connect (sockcli, struct sockaddr*) &addrser, Addrlen);
    Connect the server if (res = =-1) {perror ("connect");
    }else{printf ("Client Connect server ok.\n");   } Char cmd[20];        Used to store the service name requested by the Customer Oper op;     The structure of the customer's request (including the type of service requested by the customer, input data) int result;
        The service result of the request sent back by the server//the client sends the request and receives the request result while (1) {printf ("cmd:>");

        scanf ("%s", cmd);
            if (strcmp (cmd, "add") = = 0) {Op.oper = add; InpuT (&OP);
            }else if (strcmp (cmd, "sub") = = 0) {Op.oper = sub;
        Input (&OP);
            }else if (strcmp (cmd, "mul") = = 0) {op.oper = Mul;
        Input (&OP);
            }else if (strcmp (cmd, "div") = = 0) {op.oper = div;
                while (1) {Input (&OP);
                    if (Op.op2 = = 0) {//divisor is 0 o'clock re-enter perror ("OP2 = = 0 Error");
                    printf ("Please input again.\n");
                Continue
            } break;
            }}else if (strcmp (cmd, "help") = = 0) {help ();
        Continue
        }else if (strcmp (cmd, "quit") = = 0) {op.oper = quit;
        }else{printf ("Your input is invalid.\n");  } int res = Send (SOCKCLI, (char *) &op, sizeof (OP), 0);
        Send the client's request to the server if (res = =-1) {perror ("send");
        } if (Op.oper = = QUIT) {break;

    }    Recv (SOCKCLI, &result, sizeof (int), 0);
    The receiving server sends back the result printf ("result =%d\n", result);   } close (SOCKCLI);
Close the client socket return 0;
    } void Input (Oper *op)//Get operation two number {printf ("Please input OP1 add op2.\n");
scanf ("%d,%d", &OP->OP1, &OP->OP2);
    } void Help ()//hint Information {printf ("#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*\n");
    printf (^^ ^^ ^^ ^^ ^version^ ^^ ^^ ^^ ^^: ^^ ^^ ^v1.0^ ^^ ^^ ^^ ^^ ^\n ");
    printf (the "^^ ^^ ^^ ^^ ^author^ ^^ ^^ ^^ ^^ ^: ^^ ^^ ^haha^ ^^ ^^ ^^ ^^ the ^\n");
    printf ("~*~*~*~*~cmd~*~*~*~*~*~*~ ~*~*~describe~*~*~\n");
    printf ("~*~*~*~*~add~*~*~*~*~*~*~ ~*~*~op1 + op2~*~*\n");
    printf ("~*~*~*~*~sub~*~*~*~*~*~*~ ~*~*~op1-op2~*~*\n");
    printf ("~*~*~*~*~mul~*~*~*~*~*~*~ ~*~*~op1 * op2~*~*\n");
    printf ("~*~*~*~*~div~*~*~*~*~*~*~ ~*~*~op1/op2~*~*\n");
printf ("#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*\n"); }
The server-side code #include ".
    /unp.h "#define THREAD_MAX 3 typedef struct thread_arg{BOOL flag;
int sockconn;

}thread_arg;
typedef struct THREAD_ARG Threadpool[thread_max];


ThreadPool Pool;
    void* handler (void *arg) {int index = * (int *) arg;

    printf ("[%d] thread startup.\n", index);

    struct Thread_arg *pthread = &pool[index];

    int sockconn = pthread->sockconn;
    Char sendbuf[100];
    Oper op;
    int result;
        while (1) {if (Pthread->flag) {printf ("[%d] thread start work.\n", index);
        int res = recv (pool[index].sockconn, (char *) &op, sizeof (OP), 0);
        if (res = =-1) {perror ("recv");
        } if (Op.oper = = ADD) {result = Op.op1 + op.op2;
        }else if (op.oper = = SUB) {result = OP.OP1-OP.OP2;
        }else if (op.oper = = MUL) {result = Op.op1 * OP.OP2;
        }else if (op.oper = = DIV) {result = OP.OP1/OP.OP2; }else if (Op.oper = = QUIT) {struct sockaddr_in addrcli;
            socklen_t addrlen = sizeof (struct sockaddr);
            Getpeername (Pool[index].sockconn, (struct sockaddr*) &addrcli, &addrlen);
            printf ("client[%d] quit.\n", addrcli.sin_port);
            Pthread->flag = 0;
            Close (Sockconn);
        Continue
        } Send (Pool[index].sockconn, (char *) &result, sizeof (int), 0);
            }else{printf ("[%d] thread is idel.\n", index);
        Sleep (1);
    }}} int main (int ac, char *av[]) {int sockser = socket (af_inet, sock_stream, 0);
    if (Sockser = =-1) {perror ("socket");
    } int yes = 1;
    if (setsockopt (Sockser, Sol_socket, so_reuseaddr, &yes, sizeof (int)) = =-1) {perror ("setsockopt");
    } struct sockaddr_in addrser, addrcli;
    addrser.sin_family = af_inet;
    Addrser.sin_port = htons (Server_port);

    ADDRSER.SIN_ADDR.S_ADDR = inet_addr (SERVER_IP); Socklen_t Addrlen = sizeof (struct sockaddr);
    int res = bind (Sockser, (struct sockaddr*) &addrser, Addrlen);
    if (res = =-1) {perror ("bind");


    } Listen (Sockser, queue_size);
    pthread_t Tid[thread_max];
        for (int i = 0; i < Thread_max; ++i) {pool[i].flag = 0;
    Pool[i].sockconn = 0;
        } for (int i = 0; i < Thread_max; ++i) {pthread_create (&tid[i], NULL, Handler, &i);
    Sleep (1);
    } int sockconn;
    Int j =-1;
        while (1) {sockconn = Accept (Sockser, (struct sockaddr*) &addrcli, &addrlen);
        if (Sockconn = =-1) {perror ("accept");
        } printf ("client[%d] Connect server ok.\n", addrcli.sin_port);
                for (int i = 0; i < Thread_max; ++i) {if (Pool[i].flag = = 0) {pool[i].sockconn = Sockconn;
                Pool[i].flag = 1;
            Break
            } ++j; if (J >= Thread_max) {pthread_create (&AMP;TID[J], NULL, Handler, &j);
                Pool[j].sockconn = Sockconn;
                Pool[j].flag = 1;
                Sleep (1);
            Break
    }}} close (Sockser);
return 0; }
//header file #include <iostream> #include <unistd.h> #include <signal.h> #include <stdio.h> #include <arpa/inet.h> #include <netinet/in.h> #include <string.h> #include <    stdlib.h> #include <sys/socket.h> #define SERVER_IP "127.0.0.1" #define SERVER_PORT 7070 #define Queue_size

5 typedef enum{add, SUB, MUL, DIV, quit}oper_state;
    typedef struct oper{oper_state Oper;
    int OP1;
int OP2; }oper; 

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.