Unix to Windows Porting Dictionary for HPC

RSS

Links

Function List

signal


Unix

header file: signal.h

typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);

Windows

Header file: signal.h

void (__cdecl *signal(int sig,void (__cdecl *func ) (int [, int ] )))(int);

Purpose

The signal function associates a signal handler with the specified signal.

Discussion

The signal function allows the user to associate a specific signal handling routine with the specified signal. The signal handling routine could be a normal system supplied routine (SIG_INT, SIG_IGN, etc) or a user written routine. Note that before it invokes a signal handler, the operating system sets the handler for that signal back to the system default handler. If you are entering a user written handler, you will need to reset the action back to the user handler before you exit the user handler if you want the user written handler to handle the next signal.

Example of Use in Windows

Orignal POSIX code:

#include <signal.h>
// Example usage of the signal call for signal redirection
// My signal handler function

void my_handler (int sig) {
    printf ("Intercepted SIG_INT");
    exit(0);
}

int main ( void ) 
{
// Set up to redirect SIG_INT to my own handler
    signal (SIGINT, my_handler);
    printf ("I should now catch SIG_INT\n");
    sleep(5);
    printf (" No SIG_INT detected within 5 seconds\n");

// Now set up SIG_INT to be ignored
    signal (SIGINT, SIG_IGN);
    printf ("I should ignore SIG_INT\n");
    sleep(5);
    printf ("No SIG_INT detected within 5 seconds\n");

// Finally set up SIG_INT to its default behavior
    signal (SIGINT, SIG_DFL);
    printf ("The system should do the default for SIG_INT\n");
    sleep(5);
    printf ("No SIG_INT detected within 5 seconds\n");
    return 0;
}
    

Windows conversion:

// Example Windows signal redirection

#include "stdafx.h"  //Signal.h is contained in stdafx.h

using namespace System;

// My signal handler function

void My_Handler (int sig) 
{
    printf ("Intercepted SIG_INT...\n");
    Sleep (5000);    //Windows Sleep function is in milliseconds
}

int main ( void ) 
{
        typedef void (*My_HandlerPointer) (int);

// Set up to redirect SIG_INT to my own handler

        My_HandlerPointer previousHandler
        previousHandler = signal (SIGINT, My_Handler);
        printf ("I should now catch SIG_INT\n");
        Sleep(5000);
        printf (" No SIG_INT detected within 5 seconds\n");

// Now set up SIG_INT to be ignored

        previousHandler = signal (SIGINT, SIG_IGN);
        printf ("I should ignore SIG_INT\n");
        Sleep(5000);
        printf ("No SIG_INT detected within 5 seconds\n");

// Finally set up SIG_INT to its default behavior

        previousHandler = signal (SIGINT, SIG_DFL);
        printf ("The system should do the default for SIG_INT\n");
        Sleep(5000);
        printf ("No SIG_INT detected within 5 seconds\n");
        return 0;
}
    
blog comments powered by Disqus
Valid HTML 4.01 Transitional Valid CSS!