Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Thread pool network service

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

1. Thread pool network service

: An improved multi-thread network service model is proposed to overcome some shortcomings of multi-thread network service model.

Pool is a very important concept, its basic idea is: first create a batch of resources, when there are users, direct allocation to create good resources, its main purpose is to reduce the system in the frequent creation of resources overhead.

Implementation principle: The main service thread creates a given number of service threads. When a client arrives, it finds an idle service thread from the thread pool and serves it. After the service is completed, the thread is not released and is put back into the thread pool. If the current thread pool is full, the current client is added to the waiting queue.

The model is as follows:

2、代码实现

同样用处理整数运算来模拟线程池的并发处理

(1)、utili.h

#include#include#include#include#include#include#include#include#define SERVER_PORT 8090#define SERVER_IP "127.0.0.1"#define LISTEN_QUEUE 5#define BUFFER_SIZE 255#define CMD_SIZE 20 #define THREAD_POOL_NUM 5typedef enum{ADD,SUB,MUL,DIV,MOD, QUIT}OPER_TYPE;typedef enum{IDEL, BUSY}THREAD_TAG;typedef struct OperStruct{ int op1; int op2; OPER_TYPE oper;}OperStruct;

(2)、ser.c

#include"../utili.h"typedef struct PoolStruct{ int sockConn; THREAD_TAG flag; }PoolStruct;typedef PoolStruct threadpool[THREAD_POOL_NUM];threadpool pool;pthread_t tid[THREAD_POOL_NUM];void* Thread_Handler(void *arg);void* Thread_Handler(void *arg){ int index = *(int *)arg; printf("[%d] thread start up.\n", index); OperStruct op; int result; while(1){ if(pool[index].flag == BUSY){ printf("[%d] thread start wroking.\n", index); int res = recv(pool[index].sockConn, &op, sizeof(op), 0); if(res == -1){ printf("recv data fail.\n"); continue; } 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){ break; } res = send(pool[index].sockConn, &result, sizeof(result), 0); if(res == -1){ printf("send data fail.\n"); continue; } }else{ printf("[%d] thread sleep.\n",index); sleep(1); } } close(pool[index].sockConn); pthread_exit(0);} int main(void){ int sockSer = socket(AF_INET, SOCK_STREAM, 0); if(sockSer == -1){ perror("socket"); return -1; } 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 len = sizeof(struct sockaddr); int res = bind(sockSer, (struct sockaddr*)&addrSer, len); if(res == -1){ perror("bind"); close(sockSer); return -1; } listen(sockSer, LISTEN_QUEUE); int i; for(i=0; iop2));}//Cliint main(void){ int sockCli = socket(AF_INET, SOCK_STREAM, 0); if(sockCli == -1){ perror("socket"); return -1; } struct sockaddr_in addrSer; addrSer.sin_family = AF_INET; addrSer.sin_port = htons(SERVER_PORT); addrSer.sin_addr.s_addr = inet_addr(SERVER_IP); socklen_t len = sizeof(struct sockaddr); int res = connect(sockCli, (struct sockaddr*)&addrSer, len); if(res == -1){ perror("connect"); close(sockCli); return -1; }else{ printf("Client Connect Server Success.\n"); } char cmd[2]; OperStruct op; int result; while(1){ printf("Please input operator : "); scanf("%s",cmd); if(strcmp(cmd, "+") == 0){ op.oper = ADD; InputData(&op); }else if(strcmp(cmd,"-") == 0){ op.oper = SUB; InputData(&op); }else if(strcmp(cmd,"*") == 0){ op.oper = MUL; InputData(&op); }else if(strcmp(cmd,"/") == 0){ op.oper = DIV; InputData(&op); }else if(strcmp(cmd, "quit") == 0){ op.oper = QUIT; }else{ printf("Cmd invalid.\n"); } res = send(sockCli, &op, sizeof(op), 0); if(res == -1){ printf("send data fail.\n"); continue; } if(op.oper == QUIT) break; res = recv(sockCli, &result, sizeof(result), 0); if(res == -1){ printf("recv data fail.\n"); continue; } printf("result = %d\n", result); } close(sockCli); return 0;}

运行结果

服务器端

client 1

client 2

3. Analysis summary

(1)Its advantages: high performance

(2)Possible problems: If new users spend too much time in the waiting queue, it will affect the user experience. To solve this problem, the improvement scheme is as follows:

A, dynamically create a new service thread, after the service ends, the thread joins the thread pool, the benefit of this improvement is that the user experience is improved, the potential problem is that in a long-term, large-scale concurrent user state, threads will be generated a lot, and eventually the system will exit because of excessive resource consumption.

b. Add a thread resource recycling mechanism. When the size of the thread pool reaches a certain level or meets certain established rules, it will actively kill some threads to achieve a compromise between system stability and user experience.

model analysis

When there is a client, there are two ways, i>, create threads for its service;ii>, join the waiting queue; these two are not suitable, adopt a compromise method, there is an upper limit, that is, specify the maximum number of threads created, when a user, has not reached the maximum number of threads, create threads for it, if reached, join the waiting queue;

Reclaim thread resources: i>, immediately reclaim, ii>, temporarily not reclaim; when the number of idle threads reaches a certain lower limit, then reclaim the thread;

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report