Detecting of clicking on STOP-button

Is there a way I can check if an user hits the STOP-button or the BACK-button?
I want to know this because my program crashes if someone hits those buttons while running.
Thus I must create a routine that starts when hitting one of the buttons.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What is your "program"?  if it is JAVA applet, then some of its methods will be invoked by the browser.
Michel021497Author Commented:
It's a C program. So no JAVA.
Michel021497Author Commented:
Edited text of question
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Where does your C program crash?
Well, yours is a c program, then where does it run?  Is it a CGI that runs on the server (unlikely) or a browser plugin (also unlikely) or an activex control?
Michel021497Author Commented:
The program writes in a file. Because it's not posible that 2 persons write in one file at the same time, I made a lock on the file.
 But if the STOP button is hit, the file is never unlocked.
So no one is able to use the program anymore after such action.
Can you put a timeout on the lock?
Can you limit the lock window to times when the user is not upoading data?
You didn't answer my question, is it a CGI program or an activex control?
Michel021497Author Commented:
It's a CGI program.

When you click the browser's stop (or back) button, the browser closes the connection with the server. What happens after that isn't really defined by the CGI protocol as far as I can tell. On Apache the server will respond to your CGI program with a SIGPIPE if you try to write to stdout; you can catch that and do cleanup. (The default for an unhandled SIGPIPE is just to terminate the process, which would explain your lock problem.) If you're on a different server, you might try hooking all the signals and see if any get caught.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Actually, I thought flock or lockf would release the lock when the process teminates.
No, no and no! This is not the answer, i'm afraid.

What happens after the client closes the connection is not defined by the CGI protocol because it has nothing to do with CGI; it has to do with handling socket connections.

And, you want get any SIGPIPE on any connection side, unless you _write_ to the socket.

Here is a scheme to handle the client closing the connection:

1. catch SIGALRM;
2. catch SIGPIPE; (handling EPIPE is another option)

3. setup a timer which interrupts your CGI process every N seconds;

4. in the SIGALRM handler, write - say - a new-line char to the socket; if the connection is closed, you'll get a SIGPIPE;

5. in the SIGPIPE handler, handle cleanup, because the socket has died.

The socket FAQ pages are the best reference on this kind of stuff (sorry, cannot remember the address).

Rgds, julio
I think I said the same thing, didn't I? Stdout is mapped to a socket. You'll get a SIGPIPE if you write to stdout. The alarm isn't needed unless you're doing long stretches with no output and want to detect the stop, but it does generalize the solution nicely. (Never mind my lock comment, I was confusing this with another message.)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.