![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 21
Rep Power: 0
![]() |
Knowing when a program is closed
If I have a program, and someone closes it, how can I execute one last command before it actually closes? For instance, I have two programs that communicate with each other. if the user closes one of the programs, I need to send a msg to the other program before it closes, telling the other program to close as well. Is there an easy way to do this?
|
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Differs per OS, i believe. For what OS is the program? Is it graphical / consolebased?
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2005
Posts: 21
Rep Power: 0
![]() |
linux/unix and is console based
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
ps -ef | grep nameOfProg
pipe that into an awk script, pull out the PID and do a kill -9 on it prior to the close of the other program.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 143
Rep Power: 0
![]() |
Do a man on atexit
Supposing the first application is not being abnormally terminated, simply register you function(s) and be on your way.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Well you catch the SIGTERM signal that is sent to application 1 to tell it to shut down, and then you can do whatever clean up work you'd like to do e.g send a signal like SIGUSR1 to application 2 which will inform it that application 1 has closed down.
This won't work if you send SIGKILL to application 1, mind - there is no way to catch a SIGKILL signal, so it will die on you in a puff of smoke and have no time to notify app 2. Edit: Another why might be to use a heartbeat UDP packet - application 1 sends a packet to application 2 over a UDP socket every 5 seconds. If application 2 doesn't receive a packet after, say, 15 seconds, then you know that application 1 has died. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Sep 2005
Posts: 21
Rep Power: 0
![]() |
ok so I put the section of code that uses the pipe into a try block.. then I can use a catch block to look for the SIGTERM signal then exit? Will the system throw the exception to the catch block or do I have to somehow check for SIGTERM then throw the exception myself?
|
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 143
Rep Power: 0
![]() |
Quote:
If by threads, your SIG* won't be as easy to implement as you might think. If by sockets, there are other means to terminate (you should be monitoring your fds). More information will lead to more specific answers.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: Sep 2005
Posts: 21
Rep Power: 0
![]() |
I'm using a named pipe created with mkfifo, does that help?
|
|
|
|
|
|
#10 |
|
Professional Programmer
|
you gotta catch the kill signal, and do something instead. whether the kill signal comes from the user hitting ctrl-c or it getting a kill from the OS.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|