File:  [OS/2 SDKs] / os2sdk / demos / examples / signal / signal.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:26:07 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: os2sdk-1988, HEAD
Microsoft OS/2 SDK 03-01-1988

/*
 * signal.c
 *
 * This example shows the useage of the signal handling capabilities of
 * OS/2.  The signal handler is declared as a far pascal routine.  It
 * expects two paramters on the stack: the signal number and, optionally,
 * a user defined argument.  The latter has meaning only when the signal was
 * generated by a DosFlagProcess api call.
 *
 * In general the signal handler can be thought of as an interrupt to
 * the current running thread of a process.  Thread scheduling is still
 * active.
 *
 * After a signal has been caught, the handler must "acknowledge" the signal
 * before a second one can be handled.
 *
 * Created by Microsoft Corp. 1986
 */

#define INCL_DOSSIGNALS
#define INCL_DOSMISC

#include <os2def.h>
#include <bse.h>
#include <stdio.h>

void APIENTRY sig_handler();

void main() {
    
    char	  c;
    PFNSIGHANDLER address;
    USHORT	  oldaction;
    unsigned	  result;

    if (result = DosSetSigHandler(sig_handler, &address,
				  &oldaction, SIGA_ACCEPT, SIGA_IGNORE))

	fprintf(stderr,"dossetsighandler failed %d\n", result);

    if (result = DosSetSigHandler(sig_handler, &address,
				  &oldaction, SIGA_ACCEPT, SIGA_ERROR))

	fprintf(stderr,"dossetsighandler failed %d\n", result);

    if( DosGetMachineMode( &c))
	if (result = DosSetSigHandler(sig_handler, &address,
				      &oldaction, SIGA_ACCEPT,
				      SIGA_ACKNOWLEDGE ))
	    fprintf(stderr,"dossetsighandler failed %d\n", result);

    do
	printf("Type a SPACE to terminate demo\n");
    while(getchar() != 0x20);

    fprintf(stderr,"Program terminating\n");
    exit(0);
}
/*
 * The signal handler routine must ACKNOWLEDGE the signal before it can
 * capture another (of the same number).  Since this routine runs as
 * thread 0 it can be preempted by another.  If it is
 * important that all signals be caught, then Thread 0 should be run as
 * a high priority thread and always snoozing.	Of course, the
 * acknowlegement should be the first thing done in the handler.
 */
void APIENTRY sig_handler(sig_arg, sig_num)
USHORT sig_arg, sig_num;
{
    USHORT signum, sigarg;
    PFNSIGHANDLER address;
    int count;

    fprintf(stderr, "signum %u sigarg %u\n", sig_num, sig_arg);

    if (count = DosSetSigHandler( (void (pascal far *)())0L,
				  &address,
				  &sigarg, SIGA_ACKNOWLEDGE, sig_num))

	fprintf(stderr,"dossetsighandler failed %d\n", count);
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.