Skip to content

Instantly share code, notes, and snippets.

@deepcube
deepcube / connect_die.c
Last active December 16, 2015 09:59
Test whether ZMQ_ROUTER sockets grow as peers connect and disconnect. It looks like they don't. To run, download all three files into the same directory and run: sh disconnect_test.sh
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zmq.h>
#define zcheck_err(eval, condition, fmt, args...) \
((void)({ \
errno = 0; \
if (!(condition)) { \
@deepcube
deepcube / many.c
Last active December 16, 2015 10:49
Continually create and destroy contexts and sockets to check for memory leaks. None found. To run download both files into a directory and run: sh many_test.sh
#include <czmq.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <zmq.h>
#define zcheck_err(eval, condition, fmt, args...) \
((void)({ \
errno = 0; \
if (!(condition)) { \
@deepcube
deepcube / czmq_connect_die.c
Created April 19, 2013 20:32
Rewrite of https://gist.github.com/deepcube/5417288 in czmq. To run download all files in a directory and run: sh czmq_disconnect_test.sh
#include <czmq.h>
#include <stdio.h>
#include <stdlib.h>
#include <zmq.h>
#define zcheck_err(eval, condition, fmt, args...) \
((void)({ \
errno = 0; \
if (!(condition)) { \
fprintf(stderr, "%s: " fmt "%s%s\n", prog_name, ##args, errno ? ": " : "", errno ? zmq_strerror(errno) : ""); \
@deepcube
deepcube / offsets.awk
Last active December 17, 2015 09:09
Given a file, print each word followed by byte offsets at which it is found. offsets.c and offsets.bash should be interchangeable. ./offsets < file | awk -f offsets.awk | sort | column -t
# offsets.awk
#
# given a list of word offset pairs, accumulate offsets and print
# word [offset,...] pairs
# use with offsets.c
# no good way to shebang awk, so just do awk -f manually
{
a[$1] = a[$1] (a[$1] ? "," : "") $2;
}
@deepcube
deepcube / zerr.h
Created May 24, 2013 16:43
zeromq macros I use, see comments for descriptions. gcc specific due to branching macros. _GNU_SOURCE specific due to program_invocation_name.
#ifndef ZERR_H
#define ZERR_H
#ifndef _GNU_SOURCE
#define _GNU_SOURCE // for program_invocation_name
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@deepcube
deepcube / broker.c
Created May 24, 2013 16:49
zeromq broker. Frontend client prepends messages with the ID of the final destination. Broker receives message through a ZMQ_ROUTER so message now has initial source ID prepended. Broker swaps first two frames and sends message out backend ZMQ_ROUTER, which routes message to final destination. Clients used as destinations must identify to broker…
#include <czmq.h>
#include <stdlib.h>
#include <zmq.h>
#include "zerr.h"
/*
* read message, switch first two frames (from and to), send it back out
* arg is the socket to which the message should be sent. For the front_end arg
* is back_end, and vice versa.
#!/bin/sh
#
# Simulate add/remove events by writing directly
# into the uevent files.
if [ "$#" -ne 1 ] || [ "$1" != add ] && [ "$1" != remove ]; then
# warning: can't trust $0, better off just hard coding the name
# echo "usage: simevent add|remove" 1>&2
printf "usage: %s add|remove\n" "${0##*/}" 1>&2
exit 1
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
uintmax_t calls;
void print(int *chain, int len)
{
for (int i = 0; i < len; i++)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node Node;
struct Node {
Node *next; /* next Node in the chain */
Node *first; /* Node we started the chain on */
int pos; /* position in chain */
};
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define x(s,d,...)if(s){o=errno;fputs(*v,stderr);fprintf(stderr,": "__VA_ARGS__\
);errno=o;perror(0);d;}
#define l while