FTAM/9000 Programmer's Guide

190 Chapter 4
Using Support Functions
Responding to Asynchronous Calls
#define WAIT 0 /* Tells sem_p)) to block if necessary */
#define NO_WAIT 1 /* Tells sem_p() to return immediately */
int sem_id; /* The semaphore array for async mgmt */
int sem_create(); /* Create semaphores on the system */
void sem_remove(); /* Remove semaphores from the system */
int sem_p(); /* Perform a P (get) operation */
void sem_v(); /* Perform a V (give) operation */
/*
** Signals and signal handler used to manage asynchronous event notification.
** The signal_handler logic is included following the main program.
*/
#define SIG_MAP SIGIO /* MAP notification signal */
#define SIG_NON_MAP SIGUSR1 /* Non-MAP signal (for example) */
void signal_handler(); /* Signal handler for async event mgmt */
/*
** MAIN PROGRAM
*/
main()
{
Local_event_name map_event_name; /* MAP 3.0 event name (identifier) */
Return_code map_rc; /* MAP 3.0 function return code */
Api_rc map_result; /* MAP 3.0 result structure */
struct sigvec vec; /* Signal Vector for event handler */
printf(“Demonstration program for em_hp_sigio()\n”);
/*
** Create and initialize the semaphore array for process suspension and
** resumption.
*/
printf(“Creating semaphore for process suspend & resume.\n”);
sem_id = sem_create(NUM_SEMAPHORES);
/*
** Install the signal handler to catch interrupts for each signal used
** as an event notification.
*/
printf(“Installing signal handlers for MAP and Non-MAP events\n”);
vec.sv_handler = signal_handler;
vec.sv_mask = 0;
vec.sv_flags = 0;
sigvector(SIGIO, & vec, NULL); /* MAP events */
sigvector(SIGUSR1, & vec, NULL); /* Non-MAP events */
/*
** Put the MAP 3.0 Interface into SIGIO notification mode. This causes
** MAP Event Management to generate a SIGIO signal whenever there is
** an asynchronous event to process.
**
** Note, the process to signal is the local (this) process. The process
** id is negated to distinguish it from a process group id.
*/
printf(“Calling em_hp_sigio() to enable SIGIO notification for MAP.\n”);
map_rc = em_hp_sigio(EM_SIGIO_ENABLE, getpid(), & map_result);
if (map_rc != SUCCESS)
{
fprintf(stderr, “ERROR: em_hp_sigio(ENABLE) failed, %d\n”, map_rc);
exit(1);
}