优化多线程: 线程优先级、线程分离、线程先到先得(实时调度)算法

This commit is contained in:
aixiao 2023-05-05 10:45:19 +08:00
parent 250a958832
commit 2dc3eea220
3 changed files with 39 additions and 12 deletions

View File

@ -2,7 +2,7 @@ CROSS_COMPILE ?=
CC := $(CROSS_COMPILE)gcc CC := $(CROSS_COMPILE)gcc
STRIP := $(CROSS_COMPILE)strip STRIP := $(CROSS_COMPILE)strip
CFLAGS = -Wall -g -O3 CFLAGS = -Wall -g -O3
LIB = -lssh2 -pthread LIB = -lssh2 -pthread -static
OBJ = tunnel OBJ = tunnel
SSH2_LIB := $(shell pkg-config --static --libs --cflags libssh2) SSH2_LIB := $(shell pkg-config --static --libs --cflags libssh2)
@ -12,7 +12,7 @@ all:forward-tunnel reverse-tunnel
forward-tunnel: forward-tunnel.o forward-tunnel: forward-tunnel.o
$(CC) $(CFLAGS) -o forward-tunnel $^ $(SSH2_LIB) $(LIB) $(CC) $(CFLAGS) -o forward-tunnel $^ $(SSH2_LIB) $(LIB)
$(STRIP) forward-tunnel : $(STRIP) forward-tunnel
reverse-tunnel: reverse-tunnel.o reverse-tunnel: reverse-tunnel.o
$(CC) $(CFLAGS) -o reverse-tunnel $^ $(SSH2_LIB) $(LIB) $(CC) $(CFLAGS) -o reverse-tunnel $^ $(SSH2_LIB) $(LIB)

View File

@ -218,6 +218,13 @@ shutdown:
} }
int nice( int increment)
{
int oldprio = getpriority(PRIO_PROCESS, getpid());
printf("%d\n", oldprio);
return setpriority(PRIO_PROCESS, getpid(), oldprio + increment);
}
static char help_info(void) static char help_info(void)
{ {
static const char name[] = "STunnel"; static const char name[] = "STunnel";
@ -346,14 +353,7 @@ int main(int argc, char *argv[], char **env)
fprintf(stderr, "Waiting for TCP connection on %s:%d...\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); fprintf(stderr, "Waiting for TCP connection on %s:%d...\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
pthread_t thread_id = 0;
sigset_t signal_mask;
sigemptyset(&signal_mask);
sigaddset(&signal_mask, SIGPIPE); // 忽略PIPE信号
if (pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) != 0) {
printf("block sigpipe error\n");
}
if (daemon_is == 1) { if (daemon_is == 1) {
if (daemon(1, 1)) { if (daemon(1, 1)) {
perror("daemon"); perror("daemon");
@ -361,6 +361,30 @@ int main(int argc, char *argv[], char **env)
} }
} }
if (-1 == (nice(-20))) // 进程优先级
perror("nice");
pthread_t thread_id = 0;
pthread_attr_t attr;
pthread_attr_init(&attr);
struct sched_param sched_param;
sigset_t signal_mask;
sigemptyset(&signal_mask);
sigaddset(&signal_mask, SIGPIPE); // 忽略PIPE信号
if (pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) != 0) {
printf("block sigpipe error\n");
}
pthread_attr_setschedpolicy(&attr, SCHED_FIFO); // 先到先得(实时调度)算法.
//pthread_attr_getschedparam(&attr, &sched_param);
sched_param.sched_priority = 99;
pthread_attr_setschedparam(&attr, &sched_param); // SCHED_FIFO 和 SCHED_RR 策略的静态优先级范围为 1 到 99 ,其余的优先级为 0 政策。
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); // 系统所有线程之间争夺 CPU 资源.
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); // 线程执行完函数后,会自行终止并释放占用的资源.
while (1) { while (1) {
server_addr_len = sizeof(server_addr); server_addr_len = sizeof(server_addr);
forwardsock = accept(listensock, (struct sockaddr *)&sin, &server_addr_len); forwardsock = accept(listensock, (struct sockaddr *)&sin, &server_addr_len);
@ -369,11 +393,10 @@ int main(int argc, char *argv[], char **env)
goto shutdown; goto shutdown;
} }
pthread_create(&thread_id, NULL, &forward_tunnel, (void *)&forwardsock); pthread_create(&thread_id, &attr, &forward_tunnel, (void *)&forwardsock);
} }
pthread_join(thread_id, NULL);
pthread_exit(NULL); pthread_exit(NULL);

View File

@ -16,7 +16,11 @@
#include <signal.h> #include <signal.h>
#include <pthread.h> #include <pthread.h>
#include <sys/time.h>
#include <sys/resource.h>
#define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0) #define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
#ifndef INADDR_NONE #ifndef INADDR_NONE
#define INADDR_NONE (in_addr_t)-1 #define INADDR_NONE (in_addr_t)-1
#endif #endif