Thread: Signal handling

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0

    Signal handling


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<signal.h>
    
    void kill(int handler)
    {
    	puts("Caught SIGINT");
    	exit(0);
    }
    
    int main()
    {
    	char input[30];
    	signal(SIGINT,kill);
    	fgets(input,30,stdin);
    	puts(input);
    	getchar();
    	return 0;
    }
    I'm trying to catch the signal when I press ctrl-C. When I run the program message pops up saying
    First-chance exception at 0x77166b2f in hello world1.exe: 0x40010005: Control-C.
    Why doesn't my function catch the signal?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    What OS are you running?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Windows 7, like always. But I'm expecting to get Linux soon when I'm done with this computer.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Signals are a UNIX thing, not a Windows thing.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Then what's the point of having the signals header file?

    And can I ask, is C used more for operating systems not including windows?
  10. #6
  11. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    Originally Posted by miz6565
    Then what's the point of having the signals header file?
    Because it's part of the Standard C Library. It's a bit of an oddity, really, and probably shouldn't have been because of its UNIX specificity, but at the time the language was first standardized (circa 1987), C was mostly used under UNIX, and Windows was still just getting started; the standard libraries as defined at the time were pretty much straight ports of the UNIX versions, and often had UNIX-like quirks.

    Originally Posted by miz6565
    And can I ask, is C used more for operating systems not including windows?
    Sort of... C and UNIX are closely related, C having been designed in part for the purpose of writing the portable version of UNIX for the PDP-11 way back in 1970. Thus, even though C is (or at least was, I don't know if it's still as important nowadays) used in developing Windows itself, the language remains closely tied to UNIX and it's descendants (which include Linux, FreeBSD, and MacOS X).
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    If you look up signals at the Microsoft Developers' Network (MSDN), which is Microsoft's on-line reference library, its "manual" as in "RTFM!" ("Read The Manual!"), you will see that signals were added in order to maintain ANSI compatibility, but that the support is rather spotty and is apt to do things differently than a UNIX/Linux system would do.

    Save UNIX/Linux programming until you have a Linux system to work on. The restrictions, work-arounds, and odd behavior that you would learn trying it under Windows would only give you bad habits that you would have to un-learn later.

IMN logo majestic logo threadwatch logo seochat tools logo