Signals

This is a discussion on Signals within the A Brief History of Cprogramming.com forums, part of the Community Boards category; In my C class last term, we learned that UNIX handled various happenings by sending signals (for floating point errors, ...

  1. #1
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825

    Signals

    In my C class last term, we learned that UNIX handled various happenings by sending signals (for floating point errors, seg faults, etc.). How does Windows handle these things, and is there any way to catch them from a console program?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Signals are standard in C. You assign a function that you write to a signal and when it occurs that function is called, or you can raise the signal yourself and the same thing happens. But it's very easy to screw up, so make sure you understand very well what you're doing before working with signals. Here's one possible use for error recovery:
    Code:
    #include <stdio.h>
    #include <setjmp.h>
    #include <signal.h>
    
    static jmp_buf jmpbuf;
    
    static void handler ( int sig )
    {
      longjmp ( jmpbuf, sig );
    }
    
    int main ( void )
    {
      int *p = NULL;
      (void)signal ( SIGSEGV, handler ); /* Segmentation Violation Signal */
      switch ( setjmp ( jmpbuf ) ) {
      case 0:
        *p = 10;
        break;
      case SIGSEGV:
        printf ( "Segmentation violation..recovery code goes here\n" );
        break;
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Are the signal names the same?

    That looks different than what we did - we assigned a function as signal handler. Also, we were told that the signal function was unreliable, and to use sigaction instead.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You were probably using UNIX specific signal functions. The ISO C standard defines two functions, a type, and several macros for different signals. This is all in signal.h.

    >we assigned a function as signal handler
    As did I. The second argument to signal is the function to handle the first argument.

    >Also, we were told that the signal function was unreliable
    Signals in general should be avoided when possible because the problems they can cause are not worth what little gain they offer a program. But they can be useful and sometimes are the best option.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    >As did I. The second argument to signal is the function to handle the first argument.

    Right - rereading your code, I see I'm an idiot.

    What's the other function specified in ISO C? Is it sigaction?

    > the problems they can cause are not worth what little gain they offer a program.

    What kinds of problems?

    > You were probably using UNIX specific signal functions.

    Not AFAIK - the only functions we used for signal handling were signal and sigaction. However, we also used functions like sigprocmask and the like - are those UNIX specific?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What's the other function specified in ISO C?
    The two ISO C funtions are raise and signal.

    >What kinds of problems?
    Signals originally had much to be desired, but since they are based on UNIX, ISO C had to tone them down even more, so they're not very useful. Multiple signals can get lost, even if the program checks all signals the it could still terminate without handling future signals since the signal handler has to reestablish itself as the handler or future signals will be dealt with the the default way, and a few other problems having to do with UNIX and PDP-11 being the base for ISO signal design. So there's no portable way to use signals safely.

    >are those UNIX specific?
    They aren't standard, so I would have to assume so.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    OK - thanks for clearing that up, Prelude...

    seems that most of the problems you talked about would be gone if sigaction was adopted in the standard...

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    sigaction() and sigprocmask() are included in the POSIX standard, so as such, are not strictly UNIX only. Any POSIX compliant OS should implement them. DEC's VMS for example is POSIX compliant. (I believe the US government requires all tendered software systems to be POSIX compliant - perhaps someone could confirm or deny that).

    Windows is not strictly POSIX compliant although many of the same concepts are implemented in a variety of more or less subtle variations.

    Authors wishing to read up on POSIX and POSIX compliance should have a look at the book "POSIX Programmers Guide" by Donald Lewine. The POSIX threading model, (often called pThreads), is well covered by "Programming with POSIX threads" by Dave Butenhof, and the POSIX 4 realtime extensions by Bill Gallmeisters "POSIX.4".
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    Govt... I'm not sure who you had, but Vineyard taught us strictly UNIX C... mainly because that's what we used to write the programs, but there was a lot of emphasis put on the UNIX-specific material.

    And I hate him for this because there's a ton of stuff I'd love to do but have to relearn it in C because I can't use some of the UNIX stuff.

  10. #10
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > Govt... I'm not sure who you had, but Vineyard taught us

    Good call... He always said that it was a class to teach you the structure of UNIX environments (Solaris, in this case), and not C... It always seemed to me that it was the other way around - C with a bit of UNIX.

    Regardless, thanks everyone for input - maybe I'll check out those books if I'm having trouble sleeping, adrian.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about signals handling
    By smoking81 in forum Linux Programming
    Replies: 10
    Last Post: 10-01-2008, 04:38 PM
  2. kill & stop signals
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 02:04 AM
  3. problem with signals
    By cnchybrid in forum C Programming
    Replies: 8
    Last Post: 04-05-2007, 05:01 PM
  4. keyboard capturing
    By xlnk in forum Linux Programming
    Replies: 4
    Last Post: 01-31-2003, 01:02 PM
  5. queued signals problem, C programming related
    By Unregistered in forum Linux Programming
    Replies: 3
    Last Post: 01-22-2002, 12:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21