Thread: SIGINT - how to delete files after sigint has been send to interrupt the process

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    SIGINT - how to delete files after sigint has been send to interrupt the process

    Hi, i have a simple question. I have a program that makes a directory (temporary) and in it some files that are necessary for computation while program is running. Now suppose a user decides to interrupt the program at some stage. what happens is that the program stops but my folder and files remain.

    Q: How to override SIGINT command so that before the the execution stops the folder is deleted ?

    thank you

    baxy

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    signal(2): ANSI C signal handling - Linux man page

    You also need this
    "Async-signal-safe functions"
    signal(7) - Linux manual page

    You can call unlink() to remove files with a known name, and rmdir() to remove the whole directory once it is empty.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    Thank you for kicking me in the right direction

    However I have another related question

    Code:
    #include <stdio.h>
    #include <signal.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    void sig_stop(int signo){
      if (signo == SIGINT){
        printf("You are done:  SIGINT\n");
        exit(0);
    	}
    }
    
    int main(){
    	int i=0;
      if (signal(SIGINT, sig_stop) == SIG_ERR)
      printf("\nerror SIGINT\n");
    
      while(i<10){ 
        i++;
        printf("i:%d\n",i);
        sleep(1);
    }
      return 0;
    }


    So the above code is catching the SIGINT, and i know that the function prototipe for interrupt "= sig_end(int sig)" but is there a way to pass more arguments ?? so that i can do something before i exit() the program ??

    PS

    I want to avoid any global variables if possible. If not, then the solution is straightforward ...
    Last edited by baxy; 05-14-2013 at 10:40 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The only parameter you get in a signal handler is the signal number.
    So if you want more data, it has to be global.

    You also need to make sure that any data you set up is consistent at all times (as much as possible anyway).
    So for example, your list of filenames to be deleted should be in something like
    Code:
    // 50 files, max path length is 300
    char filesToDelete[50][300] = { { 0 } };
    Yes it wastes some space, but it's a lot safer than some fancy scheme involving malloc and lots of pointers which would be very hard to make signal-safe.


    When you're in a signal handler, you're in a kind of "limbo" state.
    You can only use a certain set of functions (see the link previously posted), and you shouldn't spend an excessive amount of time doing work.

    You can't call printf() for example, as per your post (you have to use write() if you want to write a message), and you should use _exit() rather than exit() if you want to bail out of a program inside the signal handler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    Thank you, i think i get it now , thanks !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-16-2010, 01:37 AM
  2. CreateProcess() - send to process
    By kovitch in forum C++ Programming
    Replies: 2
    Last Post: 01-26-2010, 08:04 AM
  3. send() system call using _asm and interrupt
    By raghu2383 in forum C Programming
    Replies: 3
    Last Post: 09-25-2008, 02:38 AM
  4. Child process can't send signal to itself.
    By arunj in forum C Programming
    Replies: 2
    Last Post: 03-23-2008, 11:19 AM
  5. Linux: Send keyboard input to background process
    By xErath in forum Tech Board
    Replies: 2
    Last Post: 12-09-2004, 07:02 PM