Menu

Signals with CommonC++ lib

Help
2000-10-31
2000-11-06
  • Crawford Lodge

    Crawford Lodge - 2000-10-31

    I am trying to use the signal handling features
    of CommonC++ and I am having difficulty.  I have a simple program that starts a signal handling thread:

    main.cpp:

    #include <iostream>
    #include <SignalHandler.h>
    #include <cc++/thread.h>

    // global variables
    SignalHandler *sh  = NULL;

    int
    main()
    {
        cout << "main: starting signal handling thread" << endl;
        sh = new SignalHandler();
        sh->Start();

        // do nothing
        for ( ;; )
        {
            ccxx_sleep( 3000 );
            //pause();
        }

        cout << "main ending" << endl;

        return 0;

    }

    SignalHandler.h:

    #include <cc++/thread.h>
    #include <iostream>

    class SignalHandler : public virtual Thread
    {

    public:
        SignalHandler();
        virtual ~SignalHandler();
        void Run( void );

    private:
        //
        // signal handler for all signals
        //
        void OnSignal( int signo );

        //
        // signal handler for SIGHUP
        //
        void OnHangup( void );

    };

    SignalHandler.cpp:

    #include "SignalHandler.h"
    #include <iostream>

    //
    // CTOR
    //
    SignalHandler::SignalHandler() : Thread()
    {
    }

    //
    // DTOR
    //
    SignalHandler::~SignalHandler()
    {
        Terminate();
        delete this;
    }

    //
    // Run method
    //  -- just wait for signals
    //
    void
    SignalHandler::Run( void )
    {

        setSignal( 15,true );
        setSignal( 2,true );
        setSignal( 1,true );
        siginstall( 15 );
        siginstall( 2 );
        siginstall( 1 );

        for ( ;; )
        {
            pause();
        }
    }

    //
    // signal handler for SIGHUP
    //
    void
    SignalHandler::OnHangup()
    {
        cout << "OnHangup() - got a SIGHUP" << endl;
    }

    //
    // signal handler for all signals
    //
    void
    SignalHandler::OnSignal( int signo )
    {
                cout << "OnSignal() - got signal " << signo << endl;
    }

    It works okay when I send signals to the child thread,
    the problem is that I want to be able to send signals to
    the main thread (one with lowest PID)

    For example:
    [crawford@floyd signals]$ ./test
    main --- starting signal handling thread

    on another terminal:
    [crawford@floyd signals]$ ps aux | grep test
    crawford 23532  0.1  0.1   420  168 pts/17   S    13:37   0:00 ./test
    crawford 23533  0.0  0.1   420  168 pts/17   S    13:37   0:00 ./test
    crawford 23534  0.0  0.1   420  168 pts/17   S    13:37   0:00 ./test
    crawford 23536  0.0  0.4  1360  516 pts/11   S    13:38   0:00 grep test
    [crawford@floyd signals]$ kill -15 23534

    on original term i see:
    OnSignal() - got signal 15

    but if i send a kill to pid=23532 the program ends with signal 15 ( which is what OnSignal() in thread.cpp is set to do ).

    how do i change this?

    Thanx in advance for any help/examples you can provide.

    crawford.lodge@xmlglobal.com

     
    • David Sugar

      David Sugar - 2000-11-06

      While using signal handling in this fashion is not entirely portable it's certainly workable in the Linux kernel handling of threads.  What is happening is that the "main" thread is actually constructed under an internal "object", _mainthread, which cannot be derived.  The signal handling of the "main" thread is hence different than in any other thread and can only be trapped with the use of the signal or sigaction call.  However, the use of signals in this manner is non-portable in any case and really depends on a side-effect of the Linux thread implimentation, nor can signals be used at all in Win32 and perhaps other platforms.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.