1
0
mirror of https://github.com/fumiama/simple-kanban.git synced 2026-07-02 00:50:26 +08:00

解决handle signal不完善导致的线程退出

This commit is contained in:
fumiama
2021-05-04 14:42:35 +08:00
parent fe7625d5da
commit e09996b955

View File

@@ -67,7 +67,7 @@ off_t size_of_file(const char* fname);
int s0_init(THREADTIMER *timer); int s0_init(THREADTIMER *timer);
int s1_get(THREADTIMER *timer); int s1_get(THREADTIMER *timer);
int s2_set(THREADTIMER *timer); int s2_set(THREADTIMER *timer);
int s3_setData(THREADTIMER *timer); int s3_set_data(THREADTIMER *timer);
int bind_server(uint16_t port, u_int try_times) { int bind_server(uint16_t port, u_int try_times) {
int fail_count = 0; int fail_count = 0;
@@ -197,7 +197,7 @@ int s2_set(THREADTIMER *timer) {
} }
} }
int s3_setData(THREADTIMER *timer) { int s3_set_data(THREADTIMER *timer) {
timer->status = 0; timer->status = 0;
uint32_t file_size = *(uint32_t*)(timer->data); uint32_t file_size = *(uint32_t*)(timer->data);
printf("Set data size: %u\n", file_size); printf("Set data size: %u\n", file_size);
@@ -251,24 +251,22 @@ int check_buffer(THREADTIMER *timer) {
case 0: return s0_init(timer); break; case 0: return s0_init(timer); break;
case 1: return s1_get(timer); break; case 1: return s1_get(timer); break;
case 2: return s2_set(timer); break; case 2: return s2_set(timer); break;
case 3: return s3_setData(timer); break; case 3: return s3_set_data(timer); break;
default: return -1; break; default: return -1; break;
} }
} }
void handle_quit(int signo) { void handle_quit(int signo) {
printf("Handle sig %d\n", signo); printf("Handle quit with sig %d\n", signo);
pthread_exit(NULL); pthread_exit(NULL);
} }
#define timerPointerOf(x) ((THREADTIMER*)(x)) #define timer_pointer_of(x) ((THREADTIMER*)(x))
#define touchTimer(x) timerPointerOf(x)->touch = time(NULL) #define touch_timer(x) timer_pointer_of(x)->touch = time(NULL)
void accept_timer(void *p) { void accept_timer(void *p) {
pthread_detach(pthread_self()); pthread_detach(pthread_self());
THREADTIMER *timer = timerPointerOf(p); THREADTIMER *timer = timer_pointer_of(p);
signal(SIGQUIT, handle_pipe);
signal(SIGPIPE, handle_pipe);
while(!pthread_kill(*(timer->thread), 0)) { while(!pthread_kill(*(timer->thread), 0)) {
sleep(MAXWAITSEC); sleep(MAXWAITSEC);
puts("Check accept status"); puts("Check accept status");
@@ -301,12 +299,12 @@ void kill_thread(THREADTIMER* timer) {
} }
void handle_pipe(int signo) { void handle_pipe(int signo) {
printf("Pipe error: %d", signo); printf("Pipe error: %d\n", signo);
} }
void handle_accept(void *p) { void handle_accept(void *p) {
pthread_detach(pthread_self()); pthread_detach(pthread_self());
int accept_fd = timerPointerOf(p)->accept_fd; int accept_fd = timer_pointer_of(p)->accept_fd;
if(accept_fd > 0) { if(accept_fd > 0) {
puts("Connected to the client."); puts("Connected to the client.");
signal(SIGQUIT, handle_quit); signal(SIGQUIT, handle_quit);
@@ -315,20 +313,20 @@ void handle_accept(void *p) {
if (pthread_create(&thread, NULL, (void *)&accept_timer, p)) puts("Error creating timer thread"); if (pthread_create(&thread, NULL, (void *)&accept_timer, p)) puts("Error creating timer thread");
else puts("Creating timer thread succeeded"); else puts("Creating timer thread succeeded");
send_data(accept_fd, "Welcome to simple kanban server.", 33); send_data(accept_fd, "Welcome to simple kanban server.", 33);
timerPointerOf(p)->status = -1; timer_pointer_of(p)->status = -1;
char *buff = calloc(BUFSIZ, sizeof(char)); char *buff = calloc(BUFSIZ, sizeof(char));
if(buff) { if(buff) {
timerPointerOf(p)->data = buff; timer_pointer_of(p)->data = buff;
while(*(timerPointerOf(p)->thread) && (timerPointerOf(p)->numbytes = recv(accept_fd, buff, BUFSIZ, 0)) > 0) { while(*(timer_pointer_of(p)->thread) && (timer_pointer_of(p)->numbytes = recv(accept_fd, buff, BUFSIZ, 0)) > 0) {
touchTimer(p); touch_timer(p);
buff[timerPointerOf(p)->numbytes] = 0; buff[timer_pointer_of(p)->numbytes] = 0;
printf("Get %zd bytes: %s\n", timerPointerOf(p)->numbytes, buff); printf("Get %zd bytes: %s\n", timer_pointer_of(p)->numbytes, buff);
puts("Check buffer"); puts("Check buffer");
if(!check_buffer(timerPointerOf(p))) break; if(!check_buffer(timer_pointer_of(p))) break;
} }
printf("Break: recv %zd bytes\n", timerPointerOf(p)->numbytes); printf("Break: recv %zd bytes\n", timer_pointer_of(p)->numbytes);
} else puts("Error allocating buffer"); } else puts("Error allocating buffer");
kill_thread(timerPointerOf(p)); kill_thread(timer_pointer_of(p));
} else puts("Error accepting client"); } else puts("Error accepting client");
} }
@@ -355,6 +353,8 @@ void accept_client() {
timer->data = NULL; timer->data = NULL;
timer->is_open = 0; timer->is_open = 0;
timer->fp = NULL; timer->fp = NULL;
signal(SIGQUIT, handle_quit);
signal(SIGPIPE, handle_pipe);
if (pthread_create(timer->thread, NULL, (void *)&handle_accept, timer)) puts("Error creating thread"); if (pthread_create(timer->thread, NULL, (void *)&handle_accept, timer)) puts("Error creating thread");
else puts("Creating thread succeeded"); else puts("Creating thread succeeded");
} else puts("Allocate timer error"); } else puts("Allocate timer error");
@@ -373,8 +373,8 @@ FILE *open_file(char* file_path, int lock_type, char* mode) {
printf("Error: "); printf("Error: ");
fp = NULL; fp = NULL;
} }
printf("Open dict in mode %d\n", lock_type); printf("Open file in mode %d\n", lock_type);
} else puts("Open dict error"); } else puts("Open file error");
return fp; return fp;
} }
@@ -385,7 +385,7 @@ int close_file_and_send(THREADTIMER *timer, char *data, size_t numbytes) {
} }
void close_file(FILE *fp) { void close_file(FILE *fp) {
puts("Close dict"); puts("Close file");
if(fp) { if(fp) {
flock(fileno(fp), LOCK_UN); flock(fileno(fp), LOCK_UN);
fclose(fp); fclose(fp);
@@ -415,8 +415,6 @@ int main(int argc, char *argv[]) {
if(fp) { if(fp) {
data_path = argv[as_daemon?5:4]; data_path = argv[as_daemon?5:4];
fclose(fp); fclose(fp);
signal(SIGQUIT, handle_pipe);
signal(SIGPIPE, handle_pipe);
if(bind_server(port, times)) if(listen_socket(times)) accept_client(); if(bind_server(port, times)) if(listen_socket(times)) accept_client();
} }
} else printf("Error opening kanban file: %s\n", argv[as_daemon?4:3]); } else printf("Error opening kanban file: %s\n", argv[as_daemon?4:3]);