1

I would like to close a socket but before notify all threads blocking on accept/connect, therefore I am sending a user specific signal, however not knowing the tid of the signal receiving threads. I cannot use syscall kill(pid, signal)

Bionix1441
  • 2,135
  • 1
  • 30
  • 65
  • use `shutdown()` as described in http://stackoverflow.com/questions/2486335/wake-up-thread-blocked-on-accept-call – Fazlin Jul 04 '16 at 14:59
  • You could just send them a SIGPIPE if they try to write to the closed socket, or arrange to have any reads return an indicator that the socket is closed. – William Pursell Jul 04 '16 at 14:59
  • But how about UDP sockets, I cannot use SIGPIPE – Bionix1441 Jul 04 '16 at 15:07
  • Err ... pid? thread? "pid" is *process*-id, not thread-id. – alk Jul 04 '16 at 15:29
  • Note a UDP connection could be used from a remote computer - How do you want to send a signal there? It would be much better to put the close notification *into* the socket data stream, probably as OOB. Your receiving ends get a SIGURG then, regardless of where they are. – tofro Jul 04 '16 at 15:32
  • @tofro Could you please clarify with an example. I do not have any knowledge of out-of-band data – Bionix1441 Jul 04 '16 at 15:42
  • Just realized its UDP - there is no OOB there. In this case, I would put the closing message into a specific packet type. – tofro Jul 04 '16 at 15:46
  • So before closing the socket I should send one packet with the specific signal right ? – Bionix1441 Jul 04 '16 at 15:48
  • 3
    You cannot "send a signal in a packet" - But you can very well define a packet type that tells the receiver "we're gonna close soon". That is, however not commonly done - Receivers need to be able to live with the fact that a socked is closed (due to server crash, network problems, whatever). So you can simply close the socket, receivers will be able to detect. – tofro Jul 04 '16 at 16:49

2 Answers2

2

Why not just shutdown the socket without notifying? Each thread that is blocked on accept or read from that socket will return with the corresponding error that you can parse to take the necessary actions

jap jap
  • 320
  • 2
  • 15
0

Yes . You can send signal to all the running threads through the system call : kill () . Use it like this : Kill( 0, signal_id ) Here '0' represents broadcast signal to all the process . Process' which are registered to the broadcasted signal , executed their signal handler for further action .

  • `kill()` sends a signal to a process. If this process is multi-threaded exactly one of the process' threads will receive the signal (if not blocked). So `kill()` is not an option to address signals to specific ***threads***. – alk Jul 06 '16 at 19:58