Example (signal -- Handle Interrupt Signals)

In the following example, the call to signal in main establishes the function handler to process the interrupt signal raised by abort. The handler prints a message and returns to the system.

#if  (1 == __TOS_OS2__)
      #define  INCL_DOSFILEMGR       /* For OS/2 */
      #include <os2.h>
#else
      #include <windows.h>        /* For Windows */
#endif
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void handler(int sig)
{
#if  (1 == __TOS_OS2__)
   UCHAR FileData[100];
   ULONG Wrote;
   strcpy((char *)FileData, "Signal occurred.\n\r");
   DosWrite(2,  (PVOID)FileData, strlen((const char*)FileData), &Wrote);
#else
   UCHAR FileData[100] = "Signal occurred.";
   DWORD Wrote;
   WriteFile(GetStdHandle(STD_ERROR_HANDLE), FileData, strlen(FileData),
             &Wrote, NULL);
#endif
}
int main(void)
{
   if (SIG_ERR == signal(SIGABRT, handler)) {
      perror("Could not set SIGABRT");
      return EXIT_FAILURE;
   }
   abort();                             /* signal raised by abort */
   return 0;                        /* code should not reach here */
   /****************************************************************
      The output should be:
      Signal occurred.
   ****************************************************************/
}