Thread: signals interrupt and quit issue

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    signals interrupt and quit issue

    I thought that this will run until I sent a signal from my keyboard!

    The code is from another person and he said that he tested on a mac.

    I run it in Linux, because I think this can't be done from my windows 7.

    As you can see I have commented fflush(stdout), but uncommeting it has no effect.

    The code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    
    void handlerC(int sig)
    {
            printf("Crtl-C pressed\n");
            printf("Received signal nr %i.\n",sig);
            psignal(sig,"handler");
    }
    
    void handlerQ(int sig)
    {
            printf("Received signal nr %i.\n",sig);
            psignal(sig,"handler");
            printf("bb\n");
            exit(1);
    }
    
    
    void waitforever()
    {
            signal(SIGINT,handlerC); // Ctrl-C
            signal(SIGQUIT,handlerQ); //ctrl-\
            while(1)
            {
                    putchar('.');
                    //fflush(stdout);
                    sleep(2);
            }
    }
    
    int main(int argc, char* argv[])
    {
            waitforever();
            return 0; // will never be called
    }
    Output
    Code:
    linux05:/home/users/std10093>./px
    .linux05:/home/users/std10093>
    So, only one dot is being printed and then the loop exits and we stop. Why? :/
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may want to up your compiler warning level. Here is what my compiler had to say about your code:
    main.cpp|25|warning: multi-line comment [-Wcomment]|
    main.cpp||In function ‘void handlerC(int)’:|
    main.cpp|6|warning: no previous declaration for ‘void handlerC(int)’ [-Wmissing-declarations]|
    main.cpp||In function ‘void handlerQ(int)’:|
    main.cpp|13|warning: no previous declaration for ‘void handlerQ(int)’ [-Wmissing-declarations]|
    main.cpp||In function ‘void waitforever()’:|
    main.cpp|22|warning: no previous declaration for ‘void waitforever()’ [-Wmissing-declarations]|
    main.cpp|34|warning: unused parameter ‘argc’ [-Wunused-parameter]|
    main.cpp|34|warning: unused parameter ‘argv’ [-Wunused-parameter]|
    ||=== Build finished: 0 errors, 6 warnings ===|
    That first warning is the key! After taking care of that and uncommenting the fflush() it works.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by std10093 View Post
    Output
    Code:
    linux05:/home/users/std10093>./px
    .linux05:/home/users/std10093>
    So, only one dot is being printed and then the loop exits and we stop. Why? :/
    I think you inadvertently commented out your loop:

    Code:
    signal(SIGQUIT,handlerQ); //ctrl-\
            while(1)
            {
    backslash followed by newline is interpreted as extending the single-line comment onto the next line. At least it is by GCC. If you compile with -Wall it will give you a warning about this.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I compiled without -Wall.

    I deserved what happened to me! But from our mistakes, we learn!

    Thank you both !
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Q to quit help
    By macman in forum C++ Programming
    Replies: 9
    Last Post: 04-12-2005, 03:30 PM
  2. I quit!
    By Luigi in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2002, 09:30 AM
  3. Are Your Sure You Want To Quit? Help Please
    By paulroseby in forum C Programming
    Replies: 4
    Last Post: 10-23-2002, 04:34 PM
  4. Last try, then i quit...
    By Silentsharp in forum C++ Programming
    Replies: 15
    Last Post: 04-06-2002, 11:06 PM