[zeromq-dev] ZMQ_POLL disabling signal management?

erupter at libero.it erupter at libero.it
Mon Aug 20 14:31:14 CEST 2012


I have to admit I'm no expert in linux programming: to manage signals I'm using 
the examples provided in the ZMQ Guide.
What I'm noticing is that if I use the poll, my signal handlers are ignored.
The very same code, when I comment the poll instruction, exits cleanly.
When the instruction is uncommented, it doesn't.

I can't seem to spot any lampant error in the code I propose below.
The relevant instruction at line 75 is commented.
Only way I can get the supposed behaviour while the poll instruction is in 
effect, is by using a delay instruction (commented at line 107).
How am I supposed to work around this?

#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include "zhelpers.h"
#include <czmq.h>
#include <signal.h>
#include <unistd.h>
    
static int s_interrupted = 0;
static void s_signal_handler (int signal_value)
{
    if (signal_value == 2)
        s_interrupted = 1;
}
static void s_catch_signals (void)
{
    struct sigaction action;
    action.sa_handler = s_signal_handler;
    action.sa_flags = 0;
    sigemptyset (&action.sa_mask);
    sigaction (SIGINT, &action, NULL);
    sigaction (SIGTERM, &action, NULL);
}


zmq_pollitem_t local_zmq_sockets[5];


int main (void)
{
    zmq_msg_t request;
    void *context = zmq_init (1);
    const char * chptr = 0;
    int zerr = 0;
    // Socket to talk to clients
    void *responder = zmq_socket (context, ZMQ_REP);
    zerr = zmq_bind (responder, "tcp://*:5555");
    if (zerr != 0)
    {
        zerr = zmq_errno();
        chptr = zmq_strerror(zerr);
        printf ("\n%s\n", chptr);
        return 0;
    }
    
    void *publisher = zmq_socket (context, ZMQ_PUB);
    zerr = zmq_bind ( publisher, "tcp://*:5563");
    if (zerr != 0)
    {
        zerr = zmq_errno();
        chptr = zmq_strerror(zerr);
        printf ("\n%s\n", chptr);
        return 0;
    }
    
    s_catch_signals ();
    local_zmq_sockets[0].socket = responder;
    local_zmq_sockets[0].events = ZMQ_POLLIN;
    local_zmq_sockets[1].socket = publisher;
    local_zmq_sockets[1].events = ZMQ_POLLOUT;
    s_catch_signals ();
   
    while (1) {

        if (s_interrupted == 1) {
            printf ("\nSIGTERM interrupt received, killing server…\n");
            break;
        }

        // Wait for next request from client

       
        //zerr = zmq_poll(local_zmq_sockets,1,5);
        if (zerr == -1)
        {       
            zerr = zmq_errno();
            chptr = zmq_strerror(zerr);
            printf ("\n%s\n", chptr);
            break;
        }
        if (local_zmq_sockets[0].revents & ZMQ_POLLIN) {
            zmq_msg_init (&request);
            zmq_recv (responder, &request, 0);
            zmq_msg_close(&request);
            
            // Send reply back to client
            zmq_msg_t reply;
            zmq_msg_init_size (&reply, 5);
            memcpy (zmq_msg_data (&reply), "Prova", 5);
            zmq_send (responder, &reply, 0);
            zmq_msg_close (&reply);
            
            // Write two messages, each with an envelope and content
            s_sendmore (publisher, "A");
            s_send (publisher, "We don't want to see this");
            s_sendmore (publisher, "B");
            s_send (publisher, "We would like to see this");
        }
        
        if (local_zmq_sockets[1].revents & ZMQ_POLLOUT) {
            int a = 1;
        }
        
        // Do some 'work'
        //s_sleep (100);


    }
    // We never get here but if we did, this would be how we end
    zmq_close (responder);
    zmq_close (publisher);
    zmq_term (context);
    return 0;
}


Regards
Claudio



More information about the zeromq-dev mailing list