ILE C/C++ Programmer's Guide

Example: Setting Up a Signal Handler

The following example shows how to set up a signal handler. The example illustrates that when there is no signal handler set up the default action for SIGIO is SIG_IGN. The exception is ignored. When a signal handler is set up for SIGIO, the signal handler is called.

Instructions

  1. To create the program T1520SIG, enter:

    CRTBNDC PGM(MYLIB/T1520SIG) SRCFILE(QCPPLE/QACSRC)
    Figure 182 shows the source code.
  2. To run program T1520SIG, enter:

    CALL PGM(MYLIB/T1520SIG)
    The output appears in a series of screens:
    +--------------------------------------------------------------------------------+
    |  The first read starts                                                         |
    |  Read record 1                                                                 |
    |  Read record 2                                                                 |
    |  Read record 3                                                                 |
    |  Read record 4                                                                 |
    |  Read record 5                                                                 |
    +--------------------------------------------------------------------------------+

    +--------------------------------------------------------------------------------+
    |  The first read finishes                                                       |
    |  The second read starts                                                        |
    |  Read record 1                                                                 |
    |  Read record 2                                                                 |
    |  Read record 3                                                                 |
    |  Read record 4                                                                 |
    |  Read record 5                                                                 |
    |  In SIGIO handler                                                              |
    |  Exception message ID is CPF5001                                               |
    |  Signal raised is 9                                                            |
    |  The second read finishes                                                      |
    |  The third read starts                                                         |
    |  Read record 1                                                                 |
    |  Read record 2                                                                 |
    |  Read record 3                                                                 |
    |  Read record 4                                                                 |
    |  Read record 5                                                                 |
    |  In SIGALL handler                                                             |
    |  Exception message ID is CPF5001                                               |
    |  Signal raised is 9                                                            |
    |  The third read finishes                                                       |
    |  Press ENTER to end terminal session.                                          |
    | ===>                                                                           |
    | F3=Exit F4=End of File F6=Print F9=Retrieve F17=Top                            |
    | F18=Bottom  F19=Left   F20=Right F21=User Window                               |
    +--------------------------------------------------------------------------------+

Source Code Sample that Sets Up Signal Handlers

Figure 182. T1520SIG -- ILE C Source that Sets Up Signal Handlers




#include <stdio.h>
#include <signal.h>
#include <recio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME "QTEMP/MY_FILE"
#define RCD_LEN 80
#define NUM_RCD 5
/* The signal handler for SIGIO. */
static void handler_SIGIO(int sig)
{
printf("In SIGIO handler\n");
printf("Exception message ID is %7.7s\n", _EXCP_MSGID);
printf("Signal raised is %d\n", sig);
}
/* The signal handler for SIGALL. */
static void handler_SIGALL(int sig)
{
_INTRPT_Hndlr_Parms_T data;
_GetExcData(&data);
printf("In SIGALL handler\n");
printf("Exception message ID is %7.7s\n", data.Msg_Id);
printf("Signal raised is %d\n", sig);
}
int main(void)
{
_RFILE *fp;
int i;
char buf[RCD_LEN];
char cmd[100];
/* Create a file. */
sprintf(cmd, "CRTPF FILE(%s) RCDLEN(%d)", FILE_NAME, RCD_LEN);
system(cmd);
/* Open the file for write. */
if ( (fp = _Ropen(FILE_NAME, "wr")) == NULL )
{
printf("Open for write fails\n");
exit(1);
}
/* Write some data into the file. */
memset(buf, '1', RCD_LEN);
for ( i = 0; i < NUM_RCD; i++ )
{
_Rwrite(fp, buf, RCD_LEN);
}
_Rclose(fp);
/* Open the file for the first read. */
if ( (fp = _Ropen(FILE_NAME, "rr")) == NULL )
{
printf("Open for the first read fails\n");
exit(2);
}
/* Read until end-of-file. */


/* Since there is no signal handler set up and the default */
/* action for SIGIO is SIG_IGN, the EOF exception is ignored. */
i = 1;
printf("The first read starts\n");
while ( _Rreadn(fp, buf, RCD_LEN, __DFT)->num_bytes != EOF )
{
printf("Read record %d\n", i++);
}
_Rclose(fp);
printf("The first read finishes\n");
/* Set up a signal handler for SIGIO. */
signal(SIGIO, handler_SIGIO);
/* Open the file for the second read. */
if ( (fp = _Ropen(FILE_NAME, "rr")) == NULL )
{
printf("Open for the second read fails\n");
exit(3);
}
/* Read until end of file. */
/* Since a signal handler is set up for SIGIO, the signal */
/* handler is called when the EOF exception is generated. */
i = 1;
printf("The second read starts\n");
while ( _Rreadn(fp, buf, RCD_LEN, __DFT)->num_bytes != EOF )
{
printf("Read record %d\n", i++);
}
_Rclose(fp);
printf("The second read finishes\n");
/* Set up a signal handler for SIGALL. */
signal(SIGALL, handler_SIGALL);
/* Open the file for the third read. */
if ( (fp = _Ropen(FILE_NAME, "rr")) == NULL )
{
printf("Open for the third read fails\n");
exit(4);
}
/* Read until end of file. */
/* Since there is no signal handler for SIGIO but there is a */
/* signal handler for SIGALL, the signal handler for SIGALL */
/* is called when the EOF exception is generated. But */
/* the signal ID passed to the SIGALL signal handler is still */
/* equal to SIGIO. */
i = 1;
printf("The third read starts\n");
while ( _Rreadn(fp, buf, RCD_LEN, __DFT)->num_bytes != EOF )
{
printf("Read record %d\n", i++);
}
_Rclose(fp);
printf("The third read finishes\n");
}


[ Top of Page | Previous Page | Next Page | Table of Contents ]