Thread: How to write/use watchdog in C program

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    5

    How to write/use watchdog in C program

    Hi can anybody tell me how to handle the watchdog in C program, I mean if my program hang how can i reboot the whole device automatically. My program is written in C-Language. I am very new in C-language. I was googling about this but I could not find any proper example anywhere. Hope somebody can help me or at least provide me an example "how to handle this kind of situation in C". Thanks in advanced.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Linux has a watchdog already. However, that checks that the OS is not hanging, not that your app is not hanging.

    I think the easiest way to check if your app is hanging or not is to use some sort of mechanism to tell another process "I'm still alive" and if that's not happening, let the monitor app to restart the system (or the process?)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    >I think the easiest way to check if your app is hanging or not is to use some sort of mechanism to >tell another process "I'm still alive" and if that's not happening, let the monitor app to restart the >system (or the process?)


    Thanks for reply, can you provide me an example how to write such a machanism

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tuhin_iub View Post
    >I think the easiest way to check if your app is hanging or not is to use some sort of mechanism to >tell another process "I'm still alive" and if that's not happening, let the monitor app to restart the >system (or the process?)


    Thanks for reply, can you provide me an example how to write such a machanism
    That would be a few dozen lines of code, and I'm not going to write something like that for someone else without a linux system to test it on - and I don't currently have a Linux system available.

    What do you think you should be doing? Or are you another of those people that have promised that you can do something you actually can't and you are now in trouble to try to solve it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    Quote Originally Posted by matsp View Post
    That would be a few dozen lines of code, and I'm not going to write something like that for someone else without a linux system to test it on - and I don't currently have a Linux system available.

    What do you think you should be doing? Or are you another of those people that have promised that you can do something you actually can't and you are now in trouble to try to solve it?

    --
    Mats
    Can you point me out somewhere else/ or any url which has similar topics like how to handle this kind of situation. It seems like you don't know.

  6. #6
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Quote Originally Posted by tuhin_iub View Post
    Can you point me out somewhere else/ or any url which has similar topics like how to handle this kind of situation. It seems like you don't know.
    Actually it is quite apparent that you don't know what you are doing and how to do this. So what you should do is figure out this relatively simple program, or learn how to use google effectively.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    Quote Originally Posted by MeTh0Dz View Post
    Actually it is quite apparent that you don't know what you are doing and how to do this. So what you should do is figure out this relatively simple program, or learn how to use google effectively.
    Sorry for my bad english. English is not my first language. Anyway actually I was goggling couple of days about this situation. problem is I did not understand which area I should focus properly. I have always found watchdog in google search. But I don't know that is actually possible to use. It seems like you are much more knowledgeable then me. A simple example or you can tell me wich are I need to look at in C programming language. Thanks in advanced

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, here's the rough steps:
    1. Set up a pipe
    You can use a named pipe for an independent monitor process or anonymous if you use fork+exec to create the two different processes.

    2. Then the monitor process waits on the pipe using select, with a timeout - how large that timeout should be depends on how long you are willing to wait for your application, which in turn depends on the type of system you are designing. Don't make it TOO short, or you'll run into trouble.

    http://linux.die.net/man/2/select

    3. If the select call times out, then you know that the process didn't send a message, so you restart the system [code]system("shutdown -r now");/code] assuming you have the rights to do that [and if you don't, you are stuffed].

    The target application should write to the pipe more often than the timeout. Depending on your applications design and behaviour, you could have a watch-dog tickler in the main loop, or scattered a bit here and there.

    If you start the monitored process from the monitor process, you could also set up a SIGCHILD handler and detect if the child process falls over - then you have a choice of restarting the whole system or just restarting the process itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    Quote Originally Posted by matsp View Post
    Ok, here's the rough steps:
    1. Set up a pipe
    You can use a named pipe for an independent monitor process or anonymous if you use fork+exec to create the two different processes.

    2. Then the monitor process waits on the pipe using select, with a timeout - how large that timeout should be depends on how long you are willing to wait for your application, which in turn depends on the type of system you are designing. Don't make it TOO short, or you'll run into trouble.

    http://linux.die.net/man/2/select

    3. If the select call times out, then you know that the process didn't send a message, so you restart the system [code]system("shutdown -r now");/code] assuming you have the rights to do that [and if you don't, you are stuffed].

    The target application should write to the pipe more often than the timeout. Depending on your applications design and behaviour, you could have a watch-dog tickler in the main loop, or scattered a bit here and there.

    If you start the monitored process from the monitor process, you could also set up a SIGCHILD handler and detect if the child process falls over - then you have a choice of restarting the whole system or just restarting the process itself.

    --
    Mats
    Lots of thanks to you. I think I understand what I need to do. I will let you know the what is the outcome. But it will take more than couple of hours, I am very slow in writing code and testing. Again thanks for your valuable advice.

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by tuhin_iub View Post
    Sorry for my bad english. English is not my first language.
    Nobody cares. Most of us are used to dealing with people that speak english as a second (or third etc.) language. Your failure is not of a grammatical nature. If you cant be bothered to at least attempt to solve the problem, do not expect everyone else to fall all over themselves helping you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread