Skip to content

Instantly share code, notes, and snippets.

@sorenisanerd
Created July 29, 2015 11:38
Show Gist options
  • Save sorenisanerd/4441ffaaf6a7f81818a3 to your computer and use it in GitHub Desktop.
Save sorenisanerd/4441ffaaf6a7f81818a3 to your computer and use it in GitHub Desktop.
ZMQ_LINGER problem reproducer
#include <zmq.h>
#include <malloc.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
void *ctx, *socket;
int rc, pid;
int msgsize = 0;
sscanf(argv[1], "%d", &msgsize);
if ((pid = fork()) == 0) {
printf("Sender pid: %d\n", getpid());
ctx = zmq_ctx_new();
char *buf;
buf = malloc(msgsize);
memset (buf, 'A', msgsize);
socket = zmq_socket (ctx, ZMQ_PUSH);
zmq_connect(socket, "tcp://127.0.0.1:12345");
rc = zmq_send (socket, buf, msgsize, 0);
printf("Sent message of %d bytes\n", rc);
zmq_close (socket);
zmq_term (ctx);
free(buf);
printf("Sender exiting\n");
} else {
char *buf;
int nbytes;
printf("Receiver pid: %d\n", getpid());
buf = malloc(msgsize);
ctx = zmq_ctx_new();
socket = zmq_socket (ctx, ZMQ_PULL);
zmq_bind(socket, "tcp://127.0.0.1:12345");
nbytes = zmq_recv (socket, buf, msgsize, 0);
printf("Received msg of %d bytes\n", nbytes);
zmq_close (socket);
zmq_term (ctx);
free(buf);
printf("Receiver exiting\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment