您好,登錄后才能下訂單哦!
1、線程池網絡服務
:針對多線程網絡服務模式的一些不足之處而提出的改進模式。
池是一個很重要的概念,其基本理念是:先創建一批資源,當有用戶到來時,直接分配以創建好的資源,它的主要目的是減少系統在頻繁創建資源時的開銷。
實現原理:主服務線程創建既定數量的服務線程,當有客戶端到來時,則從線程池中找出空閑的服務線程,為其服務,服務完畢后,線程不進行釋放,重新放回線程池;若當前線程池已滿,則將當前的客戶端加入等待隊列。
模型如下:
2、代碼實現
同樣用處理整數運算來模擬線程池的并發處理
(1)、utili.h
#include<unistd.h> #include<stdio.h> #include<string.h> #include<stdlib.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<pthread.h> #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 5 typedef 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; i<THREAD_POOL_NUM; ++i){ pthread_create(&tid[i], NULL, Thread_Handler, &i); sleep(1); } for(i=0; i<THREAD_POOL_NUM; ++i){ pool[i].sockConn = 0; pool[i].flag = IDEL; } int sockConn; while(1){ printf("Server Wait Client Connect.......\n"); sockConn = accept(sockSer, (struct sockaddr*)&addrCli, &len); if(sockConn == -1){ printf("Server Accept Client Connect Fail.\n"); continue; }else{ printf("Server Accept Client Connect Success.\n"); printf("Client IP:>%s\n", inet_ntoa(addrCli.sin_addr)); printf("Client Port:>%d\n",ntohs(addrCli.sin_port)); } for(i=0; i<THREAD_POOL_NUM; ++i){ if(pool[i].flag == IDEL){ pool[i].flag = BUSY; pool[i].sockConn = sockConn; break; } } } close(sockSer); return 0; }
(3)、cli.c
#include"utili.h" void InputData(OperStruct *pt); void InputData(OperStruct *pt){ printf("please input op1 and op2 : "); scanf("%d %d", &(pt->op1), &(pt->op2)); } //Cli int 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; }
運行結果
服務器端
客戶端1
客戶端2
3、分析總結
(1)、其優點:性能高效
(2)、可能存在的問題:新用戶如果在等待隊列里耗時過長,會影響用戶體驗,針對此問題,改進方案如下:
a、動態創建新的服務線程,服務結束后,該線程加入線程池,這種改進的好處是,用戶體驗得到提升,潛在問題是,在長時間,大規模的并發用戶狀態下,線程會產生很多,最終會因為資源消耗過多,系統退出。
b、增加一個線程資源回收機制,當線程池的規模達到一定程度或滿足某種既定規則時,會主動殺死一些線程,以達到系統穩定和用戶體驗之間折中。
模型分析
當有客戶端來,有2種做法,i>、創建線程為其服務;ii>、加入等待隊列;這2種都不太合適,采用折中法,有一個上限值,即就是規定一個創建線程的最大數,當來一個用戶,還沒達到線程最大數時,為其創建線程,若達到了,則加入等待隊列;
對線程資源的回收:i>、立馬回收,ii>、暫時不回收;當空閑的線程數達到某一下限值時,此時再將線程回收;
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。