How to tell if a process is running on a mobile device - mobile

I have the handle of process 'A' on a Pocket PC 2003 device. I need to determine if that process is still running from process 'B'. Process 'B' is written in Embedded Visual C++ 4.0.

GetExitCodeProcess will return STILL_ACTIVE if the process was running when the function was called.

Process handles are waitable. They are signalled - will release any waiting thread - when the process exits. You can use them with WaitForSingleObject, WaitForMultipleObjects, etc.

Related

who send sighup signal to childs when session leader exit, the c library or kernel?

As I know from man 3 exit :
If the exiting process is a session leader and its controlling
terminal is the controlling terminal of the session, then each process
in the foreground process group of this controlling terminal is sent a
SIGHUP signal
but who send the signal? it's c library or kernel ?
what clean work, c library do, and what clean work kernel do.
It's the kernel which sends the SIGHUP signal to the foreground process group when the controlling process (i.e. a session leader with a controlling terminal) exits -- see disassociate_ctty() which is called from do_exit().
I have no idea if job control could be implemented at all in userland, even theoretically -- FWIW it's in the kernel in all the implementations I know of.
However, notice that many shells (like bash) supplement the job control interface implemented by the operating system with their own non-standard quirks and features, making people wrongly assume that they're part of the same interface.

Call I call a function to self reset?

Let's say I get myself into a situation where I do not know how to recover. What would be the best way to self restart the process? What I'm looking for is something similar to closing itself and launching itself again. On some arduino's I can call NVIC_SystemReset however I'd like a function for windows, mac and linux.
I was thinking perhaps the only way is to execute a detached process and let myself shut down? With shellexecute on windows and execl on linux?
As mentioned by #Lundin, this is for processor not micro-controller.
This answer might not be correct in your case.
1) Create a proxy process to delegate to main process.
2) This proxy process will redirect to main process.
3) If your main process fails/return due to any reason then restart the main process. Otherwise if it normal exit of main program then end the proxy process.

How to tell when my Windows app is being terminated?

Is there any way my Windows program (C/C++) can receive a notification when it is being killed from Taskmgr.exe? It does not appear to receive any special Windows Messages - it just terminates.
I don't want to stop it from terminating, I just want to write a notification of some kind that it was manually terminated.
Thanks.
If it's a full windows app, you should get WM_QUIT in your message pump right before the application quits.
As MSDN states: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632641(v=vs.85).aspx
This isn't posted to a window's message queue, you can only retrieve it in your main message pump.
This is only when it quits cleanly. If the process is killed, this never happens.
A way you can detect it being killed on next launch, is to have a file be created on start-up and destroyed on shutdown, If the file still exists on the next start up you know that the process was killed, but not whether it was killed due to an error or because it was killed at a users request.
If you need to know immediately when your process is killed the only way I know of is to use another process as a watchdog. If you use OpenProcess() to get a handle to the process in question, you can wait on that handle (via WaitForSingleObject or similar), and the handle will be signalled when the process terminates. You'll need to do some coordination with the target process in order to track whether the shutdown was clean or forcible.

Linux automatically restarting application on crash - Daemons

I have an system running embedded linux and it is critical that it runs continuously. Basically it is a process for communicating to sensors and relaying that data to database and web client.
If a crash occurs, how do I restart the application automatically?
Also, there are several threads doing polling(eg sockets & uart communications). How do I ensure none of the threads get hung up or exit unexpectedly? Is there an easy to use watchdog that is threading friendly?
You can seamlessly restart your process as it dies with fork and waitpid as described in this answer. It does not cost any significant resources, since the OS will share the memory pages.
Which leaves only the problem of detecting a hung process. You can use any of the solutions pointed out by Michael Aaron Safyan for this, but a yet easier solution would be to use the alarm syscall repeatedly, having the signal terminate the process (use sigaction accordingly). As long as you keep calling alarm (i.e. as long as your program is running) it will keep running. Once you don't, the signal will fire.
That way, no extra programs needed, and only portable POSIX stuff used.
The gist of it is:
You need to detect if the program is still running and not hung.
You need to (re)start the program if the program is not running or is hung.
There are a number of different ways to do #1, but two that come to mind are:
Listening on a UNIX domain socket, to handle status requests. An external application can then inquire as to whether the application is still ok. If it gets no response within some timeout period, then it can be assumed that the application being queried has deadlocked or is dead.
Periodically touching a file with a preselected path. An external application can look a the timestamp for the file, and if it is stale, then it can assume that the appliation is dead or deadlocked.
With respect to #2, killing the previous PID and using fork+exec to launch a new process is typical. You might also consider making your application that runs "continuously", into an application that runs once, but then use "cron" or some other application to continuously rerun that single-run application.
Unfortunately, watchdog timers and getting out of deadlock are non-trivial issues. I don't know of any generic way to do it, and the few that I've seen are pretty ugly and not 100% bug-free. However, tsan can help detect potential deadlock scenarios and other threading issues with static analysis.
You could create a CRON job to check if the process is running with start-stop-daemon from time to time.
use this script for running your application
#!/bin/bash
while ! /path/to/program #This will wait for the program to exit successfully.
do
echo “restarting” # Else it will restart.
done
you can also put this script on your /etc/init.d/ in other to start as daemon

Suspend JamVM and Restart It?

I am working on JamVM, a popular small Java VM, and I would like to stop it (suspend all the threads), execute a function within the VM (a C function) and then restart all the threads.
I tried to handle a signal (Ctrl-C), execute my function, but this didn't work, because every signal the VM receives, it transmits it to the running java program...
So do you have a way to stop a VM, and then restart it? (with or without signals, it doesn't matter)

Resources