Thread: Signal handling

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    40

    Signal handling

    Hello again!

    Unfortunately I encountered problems after finding my function.

    What I want to do is to handle the situation where the user tries to end the program by "Ctrl-C" so a temp-file is erased before exiting the program.

    I found the function signal(), but I'm not sure how to use it. It seems that you can set it to do some default action when you receive a Ctrl-C for example, but how do I handle it? Can I get it to call a different function for example?

    That is if I set a global boolean to true when I have created the tempfile, but before I am done with what I want to do I can erase the tempfile before exiting? (And how would I do that?)

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Sorry, the function was sighandler (it has to work in Unix aswell as Linux)

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you make a signal handler for SIGINT which is the signal sent when you press CTRL-C (nominally, technically it is possible to change that into some other key, but very rarely done in real life)

    I wouldn't make a flag, I would just delete any file(s) that you want to have deleted. If the file isn't there, you don't cause any problem by trying to delete a file that doesn't exist. And it makes the code simpler. It's not like it's your "normal path of execution", so the fact that it is a tiny bit slower than testing a flag and deleting a file only when the flag is set is pretty much irelevant.

    --
    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.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Mats is right. I have found that the best use for signals is when you need a program to behave in a specific way without human intervention, such as forcing a timeout. The setup is somewhat complicated.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    if u want 2 call a function on recieving a signal ..

    first send the signal using kill()

    catch the signal in the desired process using signal() which takes the SIGNAL u want 2 send and d signal handler as arguments.

    now define this signalhandler func in ur program and thr call any function u want 2 ..
    jus like dat http://www.ecst.csuchico.edu/~beej/g...c/signals.html

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Thank you!

    The example on that page was great. It does exactly what I wanted in my program so I'll use that.

    The reason I want to use a boolean is that the tempfile is used to store information from an "original" which I will remove some information from. The program gets information from a user about systems in use. This is stored in a file called .active. When a user wants to remove a system from that list I copy the original file to .tmp, and then I copy all the systems back to .active except the one to remove. After that it removes the .tmp file.

    But the other day I started the program in "remove"-mode, and pressed "Ctrl-C", and when I ran it again it didn't run because the .tmp-file already existed (I don't want to accidentally remove a .tmp-file not created by my program so I check if it exists), so if the user presses Ctrl-C I want to restore the .active-file and remove the .tmp-file before exiting. So that's why I want the boolean (or some other parameter), to know if the .tmp-file has been created before the user has pressed Ctrl-C.

    Thanks again for the help. This is a great forum. A lot of talented programmers here :-)

  7. #7
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I don't know if it would have been an acceptable solution for you in this case, but there's the tmpfile function declared in stdio.h with can be used if you only need a temporary file. It's supossed to get deleted when the program terminates normally. However, I'm not sure if the file created by tmpfile is deleted is the program is interrupted by a SIGINT signal, since I have some doubt this is considered as a "normal termination".
    I hate real numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM
  3. Signal Handling
    By DarrenY in forum C Programming
    Replies: 4
    Last Post: 06-04-2006, 11:30 AM
  4. Signal Handling - Are they part of ANSI C?
    By Stanley S in forum C Programming
    Replies: 3
    Last Post: 12-21-2005, 07:49 AM
  5. signal handling
    By trekker in forum C Programming
    Replies: 2
    Last Post: 07-05-2002, 02:52 AM