Can one thread open a socket and other thread close it?

Posted by Pkp on Stack Overflow See other posts from Stack Overflow or by Pkp
Published on 2012-09-06T04:20:02Z Indexed on 2012/09/06 9:38 UTC
Read the original article Hit count: 154

Filed under:
|
|

I have some kernel threads in Linux kernel, inside my KLM.
I have a server thread, that listens to the channel, Once it sees there is an incoming connection, it creates an accept socket, accepts the connection and spawns a child thread. It also passes the accepted socket to the child kernel thread as the (void *) argument.

The code is working fine. I had a design question.
Suppose now the threads have to be terminated, main and the child threads, what would be the best way to close the accept socket. I can see two ways,
1] The main thread waits for all the child threads to exit, each of the child threads close the accept sockets while exiting, the last child thread passes a signal to the main thread for it to exit . Here even though the main thread was the one that created the accept socket, the child threads close that socket, and they do this before the main thread exits. So is this acceptable? Any problems you guys forsee here?
2] Second is the main thread closes all the accept sockets it created before it exits. But there may be a possibility(corner case) that the main thread gets an exception and will have to close, so if it closes the accept sockets before exiting, the child threads using that socket will be in danger.

Hence i am using the first case i mentioned.Let me know what you guys think?

© Stack Overflow or respective owner

Related posts about c

    Related posts about linux