Announcement

Collapse
No announcement yet.

[os] Signal Mangement with sigaction - SINGINT ( CTRL-D ) SAMPLE

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [os] Signal Mangement with sigaction - SINGINT ( CTRL-D ) SAMPLE

    PHP Code:
    #include <signal.h>
    #include <st***.h>
    #include <stdlib.h>

    /***
     *               _            _                          
     *              (_)          (_)                          
     *    __  ____ _ _  _____   ___  ___   ___ ___  _ __ ___  
     *    \ \/ / _` | |/ _ \ \ / / |/ _ \ / __/ _ \| '_ ` _ \
     *     >  < (_| | | (_) \ V /| | (_) | (_| (_) | | | | | |
     *    /_/\_\__, |_|\___/ \_/ |_|\___(_)___\___/|_| |_| |_|
     *          __/ |                                        
     *         |___/                                          
     *
     *  Alter SIGINT signal from shell. It will ask you if you are sure
     *  to kill the process.
     *
     *  v 1.0
     *
     *
     */


    void xflush FILEstream ) {
        
        
    int temp;
        
    temp fgetc stream );
        
        for (; 
    temp != '\n' ;temp fgetc stream )){}
        
        
    }



    void ask int signal) {
        
        
    char stringa;
        
        
    stringa = (char *) malloc (20);
        
        if ( 
    stringa == NULL) { fprintf(stderr,"Impossibile completarel'operazione\n");exit(-2);}
        
        
    fprintf stdout"\nDo you want to quit me? y\\n\n");
        
    fscanfstdin"%19s"stringa);
        
    xflush (stdin);
        
        for (;(
    strcmp(stringa,"y") != 0) &&  (strcmp(stringa,"n") != 0);){
        
            
    fprintf stdout"Wrong Answer \n");
            
    fscanfstdin"%19s"stringa);
            
    xflush (stdin);
        }
        
            if ( 
    strcmp(stringa,"y") == 0){
                
    fprintf(stdout,"Signal processed. Goodbye friend. I'm dying ...\n");
                
    kill (getpid(),SIGKILL);
                
    fprintf(stderr"You can't kill me!\n");
            }
        
    }



    int main void ) {
        
        
    struct sigaction sigint_action;
            
    sigint_action.sa_handler ask;
            
    sigint_action.sa_flags 0;
            
        
        
    sigaction SIGINT, &sigint_actionNULL);
        
        
    fprintf(stdout,"Press ctrl-c if you want to kill me....\n");
        
        for(;-
    1;){
            
    pause ();
            
    fprintf(stdout,"Signal processed but i'm still here. Thank you. Press ctrl-c if you want to kill me\n");
        }
          
        
        return 
    0;

    | VFX Artist, C++ Programmer, HW Overclocker | Web: xgiovio.com Email: xgiovio@gmail.com Twitter: @xgiovio
Working...
X

Google Profile


My name is Giovanni Di Grezia, but people call me xgiovio.

Here is my homepage:.

I'm a VFX Artist and Software Developer.

Giovanni Di Grezia