C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-31-2008, 08:46 AM   #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.
tuhin_iub is offline   Reply With Quote
Old 10-31-2008, 09:03 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 10-31-2008, 09:06 AM   #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
tuhin_iub is offline   Reply With Quote
Old 10-31-2008, 09:12 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 10-31-2008, 09:20 AM   #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.
tuhin_iub is offline   Reply With Quote
Old 10-31-2008, 09:24 AM   #6
The MethLab
 
MeTh0Dz's Avatar
 
Join Date: Oct 2008
Posts: 110
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.
__________________
C Coder, etc etc.
MeTh0Dz is offline   Reply With Quote
Old 10-31-2008, 09:30 AM   #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
tuhin_iub is offline   Reply With Quote
Old 10-31-2008, 09:34 AM   #8
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 10-31-2008, 09:45 AM   #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.
tuhin_iub is offline   Reply With Quote
Old 11-03-2008, 04:15 AM   #10
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,929
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.
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is offline   Reply With Quote
Reply

Tags
c-language, linux, watchdog

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue with program that's calling a function and has a loop tigerfansince84 C++ Programming 9 11-12-2008 01:38 PM
Need help with a program, theres something in it for you engstudent363 C Programming 1 02-29-2008 01:41 PM
This is a simple program.. Help me please I think it has an error.. lesrhac03 C Programming 4 02-21-2008 10:39 AM
My program, anyhelp @licomb C Programming 14 08-14-2001 10:04 PM


All times are GMT -6. The time now is 09:12 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22