c - Interrupt a accept() that is waiting, just changing a global variable value -
i'm doing project university, chat system server , multiple clients.
in specification document there written:
the main thread controlled loop on global var go, , @ each cycle waits connection request user
and also:
server stops sigterm or sigint. when signal received, global var go set zero, , threads exit loop cycle
so, thread main
create socket, bind port , @ every new connection accept()
, create thread worker
takes care of communication client.
my problem exiting thread main
changing global var. in thread main
if do
while (go && accept(params)) {}
it doesn't enter in while loop.
so @ moment code
while (go) { accept(params); // stuff }
and, well, when set go
0 waits on accept
, have create new connection, accepts connection , exit thread 'cause go
becomes zero.
same problem thread worker
while (go && read(socket_id, buffer, sizeof(char)) > 0) {}
it waits char socket exit loop.
i don't see way exit loops using go
var. think i've close socket, or find way, wrong?
i'm not interested in other ways exit threads, know if it's possible exit loops changing go var
thanks!
assuming setup signal handler sigint
, sigterm
toggle go
!=0
want unset sa_restart
flag handler(s), make accept()
return -1
on signal's reception.
to setup signal handler using sigaction()
. struct sigaction
pass in not set member sa_flags
hold sa_restart
.
for detailed description system call influenced sa_restart
flag see section "interruption of system calls , library functions signal handlers" of man 7 signal
.
independendly make sure go
defined sig_atomic_t
.
Comments
Post a Comment