简体   繁体   中英

How to write a simple WatchDog Timer in C on linux?

TCP KEEPALIVE timer has a default duration of 2 hrs.
What are the best practices for knowing that a TCP connection/socket is down asap --

  • Is implementing a WatchDog timer the best way to go about it?

  • How do I implement a WatchDog timer to do it?
    I have browsed quite extensively (may be I am using wrong search parameters) but I have not seen any such implementation for linux GPOS, all I see are some hardware based embedded system timer, which are hardware dependant.

Usually what do you do after you detect if the socket/connection has gone dead.

  • Do you only close the socket_descriptor?
  • Does closing the socket_descriptor frees all the kernel resources associated with that connection?
  • How do you free all the resources associated/allocated at user space? Do you write a routine, to do. If yes, how do you keep track of the resources you allocate in user space?

There is no way to detect that a TCP connection is dead "ASAP". If the host on the other side is dead, it's not participating in the TCP connection dialog anymore, and the only way of noticing that is a timeout on the connection.

You can lower the keepalive time on the socket to "notice" the problem earlier, but that's not a good solution generally.

If you're trying to monitor the host, send short "ping" messages at whatever frequency suits you. If the other side doesn't answer within a given interval, you can declare it "dead".

Once you've noticed a dead connection, closing the socket is sufficient to free all kernel resources associated with that socket.

If you have other resources allocated along side that (session information for instance), you need to free those as well. It's indeed a good idea to write a function for that (and for allocating those resources when you make the connection), so that all that book-keeping is in the same spot and easy to inspect.

How you keep track of the resources allocated is entirely up to you. Holding a reference to all the "to be freed" resources in one struct , and saving that struct in a linked list or hash (indexed by the socket fd for instance) can work out pretty well.

(The term "watchdog timer", in Linux anyway, is used for hardware monitoring devices. That's not a good term to search for for networking/TCP related things.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM