How to handle interruptions

You need to close the applications properly when you interrupt your application with signals such as SIGTERM or SIGINT. SIGTERM is one of the POSIX signals that sends a signal to a process to end it. Signals are asynchronous and are vulnerable to race conditions.

  • SIGTERM: When you execute the kill command with its defaults in a Unix system, you are basically calling a SIGTERM signal to the denoted process. These signals should be handled properly so your application can release the resources or close database connections and flush the messages properly.
  • SIGINT: This signal can be caught by the application just like SIGTERM as well. The user usually calls it with Ctrl + C.
  • SIGKILL: This is the signal that is called with a kill -9 command. However, this signal cannot be caught by an application, so there is nothing much we could do about it.

SIGINT could be caught in languages that support exception handling by throwing the proper exception such as KeyboardInterrupt in Python. However, things are a little bit different in C.

Let's recall our "hello world" request-reply program to demonstrate the handling of the SIGTERM (Ctrl + C) interrupt.

/*

  Request - Reply
  
  Handling interrupts.
  
  server.c

*/

#include <string.h>
#include <stdio.h>
#include "czmq.h"

int main (int argc, char const *argv[]) {
  
  zctx_t* context = zctx_new();
  void* socket = zsocket_new(context, ZMQ_REP);
  zsocket_bind(socket, "tcp://*:5050");
  
  printf("Starting server...
");
  
  for(;;) {
    
    char* msg = zstr_recv(socket);
    
    if(!msg) {
      break;
    }
    printf("Received: %s
", msg);
    zstr_send(socket, "world");
    free(msg);
  }

  zsocket_destroy(context, socket);
  zctx_destroy(&context);
    
  return 0;
}

And the following is the client code:

/*

  Request - Reply
  
  Handling interrupts.
  
  client.c

*/


#include "czmq.h"

int main (int argc, char const *argv[]) {
  
  zctx_t* context = zctx_new();
  void* request = zsocket_new(context, ZMQ_REQ);
  
  printf("Starting client...
");
  
  zsocket_connect(request, "tcp://localhost:5050");

  for(;;) {
    zstr_send(request, "hello");
    char* reply = zstr_recv(request);
    if(!reply) {
      break;
    }
    printf("Received: %s
", reply);
    free(reply);
    sleep(1);    
  }
  
  zsocket_destroy(context, request);
  zctx_destroy(&context);

  return 0;

}

However, we are coding in C and it already has signal-handling functions defined in signal.h. We could do things closer to the traditional C style by including a signal handler, as follows:

/*

  Request - Reply
  
  Send "world" in multi-parts.
  
  server.c

*/

#include "czmq.h"
#include <signal.h>

int main (int argc, char const *argv[]) {
  
  zctx_t* context = zctx_new();
  void* socket = zsocket_new(context, ZMQ_REP);
  zsocket_bind(socket, "tcp://*:5050");
  signal(SIGINT, exit);
  
  printf("Starting server...
");
  
  for(;;) {
    
    char* msg = zstr_recv(socket);
    printf("Received: %s
", msg);
    zstr_send(socket, "world");
    free(msg);
  }

  zsocket_destroy(context, socket);
  zctx_destroy(&context);
    
  return 0;
}

Did you notice something different? Yes, we included the czmq.h header. The code has completely changed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.132.223