Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 13th, 2005, 4:23 PM   #1
Kazy
Newbie
 
Join Date: Sep 2005
Posts: 21
Rep Power: 0 Kazy is on a distinguished road
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?
Kazy is offline   Reply With Quote
Old Sep 13th, 2005, 4:24 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Differs per OS, i believe. For what OS is the program? Is it graphical / consolebased?
Polyphemus_ is offline   Reply With Quote
Old Sep 13th, 2005, 4:27 PM   #3
Kazy
Newbie
 
Join Date: Sep 2005
Posts: 21
Rep Power: 0 Kazy is on a distinguished road
linux/unix and is console based
Kazy is offline   Reply With Quote
Old Sep 13th, 2005, 4:49 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Sep 13th, 2005, 5:59 PM   #5
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 143
Rep Power: 0 L7Sqr is an unknown quantity at this point
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]
L7Sqr is offline   Reply With Quote
Old Sep 13th, 2005, 6:06 PM   #6
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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.
Cerulean is offline   Reply With Quote
Old Sep 13th, 2005, 8:21 PM   #7
Kazy
Newbie
 
Join Date: Sep 2005
Posts: 21
Rep Power: 0 Kazy is on a distinguished road
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?
Kazy is offline   Reply With Quote
Old Sep 14th, 2005, 7:15 AM   #8
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 143
Rep Power: 0 L7Sqr is an unknown quantity at this point
Quote:
two programs that communicate with each other.
You need to provide the way they are communicating.
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]
L7Sqr is offline   Reply With Quote
Old Sep 14th, 2005, 12:47 PM   #9
Kazy
Newbie
 
Join Date: Sep 2005
Posts: 21
Rep Power: 0 Kazy is on a distinguished road
I'm using a named pipe created with mkfifo, does that help?
Kazy is offline   Reply With Quote
Old Sep 14th, 2005, 1:09 PM   #10
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
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.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC