signal handler

This is a discussion on signal handler within the C Programming forums, part of the General Programming Boards category; Hello I am starting to understand signal processing in UNIX. I still have some doubts ... In the following program ...

  1. #1
    Registered User falconetti's Avatar
    Join Date
    Nov 2001
    Posts
    15

    signal handler

    Hello

    I am starting to understand signal processing in UNIX. I still have some doubts ...

    In the following program (not written by me, of course) why is sigint_handler() declared inside main() ?

    Code:
        #include <stdio.h>
        #include <stdlib.h>
        #include <errno.h>
        #include <signal.h>
    
        int main(void)
        {
            void sigint_handler(int sig); /* prototype */
            char s[200];
        
                    /* set up the handler */
            if (signal(SIGINT, sigint_handler) == SIG_ERR) {
                perror("signal");
                exit(1);
            }
        
            printf("Enter a string:\n");
        
            if (gets(s) == NULL)
                perror("gets");
            else 
                printf("You entered: \"%s\"\n", s);
        
            return 0;
        }
        
            /* this is the handler */
        void sigint_handler(int sig)
        {
            printf("Not this time!\n");
        }
    By the way, could someone give some easy to understand example of the use of the pointer returned by signal ().

    Thanks in advance !

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It is being prototyped because:
    a) It's not declared in an included header.
    b) It is included, but they just feel like prototyping it.
    c) It is prototyped inside the function so the function can use it, and the actual prototyped function is or will be declared later.

    In this case, since 'main' uses this function before it's been declared, they're prototyping it. They could have prototyped it outside of the function. Perhaps they want it only available to the main function. Example:

    main { ... }
    fun1{ ... }
    fun2{ ... }
    fun3{ ... }

    If we don't prototype anything, and locally prototyep 'fun3' in 'main', then only 'main' will be able to use 'fun3', where as, if you had prototyped 'fun3' at the top of the file, all funcitons in the file could call it.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-07-2009, 11:05 AM
  2. Signal handler function - pointer to this gets lost
    By maus in forum C++ Programming
    Replies: 1
    Last Post: 07-01-2009, 10:10 AM
  3. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM
  4. using a signal handler to kill processes
    By dinjas in forum C Programming
    Replies: 2
    Last Post: 03-16-2005, 12:58 PM
  5. signal handling
    By trekker in forum C Programming
    Replies: 2
    Last Post: 07-05-2002, 03:52 AM

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