How to properly process signals when using a workflow template?

I have a simple server that looks something like this:

void *run_thread(void *arg) {
    // Communicate via a blocking socket
}

int main() {
    // Initialization happens here...

    // Main event loop
    while (1) {
        new_client = accept(socket, ...);
        pthread_create(&thread, NULL, &run_thread, *thread_data*);
        pthread_detach(thread);
    }

    // Do cleanup stuff:
    close(socket);
    // Wait for existing threads to finish
    exit(0); 
)

Thus, when SIGINT or SIGTERM is received, I need to exit the main event loop to go to the cleanup code. Moreover, the main call expects an accept () call, so it cannot check any other variable to see if it should break ;.

, , : http://devcry.blogspot.com/2009/05/pthreads-and-unix-signals.html ( , ). , , : accept() , , break;?

+3
2

select(listeninig-socket-here) accept(). accept() - , . select(), SIGTERM ( ), select select interrupted system call.

+2

I skwllsp , select call accept. , , , , . , , pthread_cancel . pthread_cancel, , , ( ), , .

,

+1

Source: https://habr.com/ru/post/1745965/


All Articles