KyL
KyL

Reputation: 1077

How to stop blocking linux kernel thread

Suppose I have two linux kernel threads, master thread and worker thread. Master uses kthread_run() to create worker. While worker is accepting socket connection and blocking, master calls kthread_stop() to stop worker.

Because worker is blocking on accepting operation and cannot exit, the kthread_stop() inside master will not return.

What should I do to kill worker thread from master in graceful way? Thanks.

Upvotes: 4

Views: 1075

Answers (1)

hlitz
hlitz

Reputation: 655

You need to specify a timeout for the blocking socket the worker is reading from:

struct timeval tv;
tv.tv_sec = 0;  /* 100 ms Timeout */
tv.tv_usec = 100000;
kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));

Whenever the recv call returns you check for kthread_should_stop(). With 100ms timeout there should be almost zero polling overheads.

Upvotes: 0

Related Questions