优化多线程

This commit is contained in:
aixiao 2023-06-05 11:47:35 +08:00
parent 2dc3eea220
commit b8f20c3a09
7 changed files with 43 additions and 27 deletions

View File

@ -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)

BIN
forward-tunnel Normal file

Binary file not shown.

View File

@ -147,7 +147,6 @@ void *forward_tunnel(void *p)
goto shutdown; goto shutdown;
} }
if (rc && FD_ISSET(forwardsock, &fds)) { // 测试某一个描述符返回值: 若fd在描述符集中则返回非0,否则返回0 if (rc && FD_ISSET(forwardsock, &fds)) { // 测试某一个描述符返回值: 若fd在描述符集中则返回非0,否则返回0
len = read(forwardsock, buffer, sizeof(buffer)); len = read(forwardsock, buffer, sizeof(buffer));
if (len < 0) { if (len < 0) {
@ -199,10 +198,11 @@ void *forward_tunnel(void *p)
fprintf(stderr, "The server at %s:%d disconnected!\n", remote_desthost, remote_destport); fprintf(stderr, "The server at %s:%d disconnected!\n", remote_desthost, remote_destport);
goto shutdown; goto shutdown;
} }
} }
} }
shutdown: shutdown:
close(forwardsock); close(forwardsock);
if (channel) if (channel)
@ -218,13 +218,6 @@ 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";
@ -270,6 +263,13 @@ static char help_info(void)
return 0; return 0;
} }
int nice( int increment)
{
int oldprio = getpriority(PRIO_PROCESS, getpid());
printf("%d\n", oldprio);
return setpriority(PRIO_PROCESS, getpid(), oldprio + increment);
}
int main(int argc, char *argv[], char **env) int main(int argc, char *argv[], char **env)
{ {
struct sockaddr_in sin; struct sockaddr_in sin;
@ -277,7 +277,7 @@ int main(int argc, char *argv[], char **env)
socklen_t server_addr_len; socklen_t server_addr_len;
int sockopt = 1; int sockopt = 1;
int listensock = -1, forwardsock = -1; int listensock = -1, forwardsock = -1;
int daemon_is = 0; int daemon_ = 0;
int opt; int opt;
@ -285,7 +285,7 @@ int main(int argc, char *argv[], char **env)
while (-1 != (opt = getopt(argc, argv, optstring))) { while (-1 != (opt = getopt(argc, argv, optstring))) {
switch (opt) { switch (opt) {
case 'd': case 'd':
daemon_is = 1; daemon_ = 1;
break; break;
case 'r': case 'r':
server_ssh_ip = strdup(optarg); server_ssh_ip = strdup(optarg);
@ -346,7 +346,7 @@ int main(int argc, char *argv[], char **env)
goto shutdown; goto shutdown;
} }
if (listen(listensock, 1024) < 0) { if (listen(listensock, 500) < 0) {
perror("listen"); perror("listen");
goto shutdown; goto shutdown;
} }
@ -354,37 +354,43 @@ 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));
if (daemon_is == 1) {
if (daemon_ == 1) {
if (daemon(1, 1)) { if (daemon(1, 1)) {
perror("daemon"); perror("daemon");
return -1; return -1;
} }
} }
if (-1 == (nice(-20))) // 进程优先级 if (-1 == (nice(-20))) // 进程优先级
perror("nice"); perror("nice");
pthread_t thread_id = 0; pthread_t thread_id = 0;
pthread_attr_t attr; pthread_attr_t attr;
pthread_attr_init(&attr); struct sched_param param;
struct sched_param sched_param;
sigset_t signal_mask; sigset_t signal_mask;
sigemptyset(&signal_mask); sigemptyset(&signal_mask);
sigaddset(&signal_mask, SIGPIPE); // 忽略PIPE信号 sigaddset(&signal_mask, SIGPIPE); // 忽略PIPE信号
if (pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) != 0) { if (pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) != 0) {
printf("block sigpipe error\n"); 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); // 线程执行完函数后,会自行终止并释放占用的资源.
// 初始化线程属性
pthread_attr_init(&attr);
// 设置线程调度策略为SCHED_FIFO
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
// 设置线程优先级为50
param.sched_priority = 50;
pthread_attr_setschedparam(&attr, &param);
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);
@ -392,11 +398,16 @@ int main(int argc, char *argv[], char **env)
perror("accept"); perror("accept");
goto shutdown; goto shutdown;
} }
getsockname(forwardsock, (struct sockaddr *)&sin, &server_addr_len);
char *ip = inet_ntoa(sin.sin_addr);
printf("Client IP address: %s\n", ip);
pthread_create(&thread_id, &attr, &forward_tunnel, (void *)&forwardsock); pthread_create(&thread_id, &attr, &forward_tunnel, (void *)&forwardsock);
} }
pthread_attr_destroy(&attr);
pthread_join(thread_id, NULL);
pthread_exit(NULL); pthread_exit(NULL);

View File

@ -19,6 +19,11 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/poll.h>
#define MAX_EVENTS 1024
#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

BIN
forward-tunnel.o Normal file

Binary file not shown.

BIN
reverse-tunnel Normal file

Binary file not shown.

BIN
reverse-tunnel.o Normal file

Binary file not shown.