performance/signaloverhead.c
///////////////////////////////////////////////////////////////////////////////
// Filename: signaloverhead.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: measure the overhead of synchronizing tasks with signals
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/02/06 00:27:43 muellerg: created
// 96/02/27 00:06:14 muellerg: updated, now uses the functions
//                             synchronization_XXX
//
///////////////////////////////////////////////////////////////////////////////
// Feature test switches ///////////////////////////// Feature test switches //
    /* NONE */
// System headers /////////////////////////////////////////// System headers //
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
// Local headers ///////////////////////////////////////////// Local headers //
#include "../common.h"
// Macros /////////////////////////////////////////////////////////// Macros //
    /* NONE */
// File scope objects /////////////////////////////////// File scope objects //
const int NUMBER_REPEAT = 100000;           /* how often are signals send? */
// External variables, functions, and classes ///////////// External objects //
    /* NONE */
// Signal catching functions ///////////////////// Signal catching functions //
    /* NONE */
// Structures, unions, and class definitions /////////////////// Definitions //
    /* NONE */
// Functions and class implementation /// Functions and class implementation //
    /* NONE */
// Main /////////////////////////////////////////////////////////////// Main //
int
main(int argc, char *argv[])
{
    error.set_program_name(argv[0]);    
    cout << "This program measures how long it takes to send a signal" << endl;
    cout << "from one process to another and getting a reply signal ";
    cout << "back." << endl;
    cout << endl;
    pid_t childpid;
    pid_t parentpid=getpid();
    // set up signal handler    
    synchronization_init();
    // spawn child 
    if ( (childpid = fork()) < 0)
        error.system("fork error");
    else
        if (childpid == 0)
        {   
            // child
            // inform parent that we are existing
            synchronization_signal_parent(parentpid);
            for(int i=0; i < NUMBER_REPEAT; i++)
            {
                // wait for signal
    
                synchronization_wait();
                // and send signal to parent
                synchronization_signal_parent(parentpid);
            }
            return(EXIT_SUCCESS);   // and exit..
        }
    // parent
    measurement mes(argv[0], "", 1);
    // wait for "I am alive" signal of child
    synchronization_wait();
    // start timer
    mes.start(-1, NUMBER_REPEAT);
    for(int i=0; i < NUMBER_REPEAT; i++)
    {
        // send signal
        synchronization_signal_child(childpid);
        // wait for signal of child (receive acknowledgment)
        synchronization_wait();
    }
    // end timer
    mes.end();
    // write ops/s on cout
    mes.writeout_logfile(false, true, true);
    return(EXIT_SUCCESS);
}