Skip to content

Instantly share code, notes, and snippets.

View gsauthof's full-sized avatar

Georg Sauthoff gsauthof

View GitHub Profile
@gsauthof
gsauthof / addressoffn.cc
Last active July 2, 2016 10:59
Address of function example
/*
Illustrate how the & operator can be omitted when getting a function pointer.
In reply to: https://www.youtube.com/watch?v=z-kUhwANrIw
2016, Georg Sauthoff
*/
@gsauthof
gsauthof / fib.c
Created July 25, 2016 06:57
Syntactic variation of a Fibonacci example
// in reply to http://verse.systems/blog/post/2016-07-18-Software-Engineering-Teaching/
unsigned fibonacci(unsigned n) {
unsigned a = 0, b = 1;
unsigned sum = 0;
for ( ; n > 0; --n) {
unsigned t = b;
b += a;
a = t;
sum += a;
% curl -L -o signature.png \
% 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Beethoven_Signature.svg/150px-Beethoven_Signature.svg.png'
% compile twice: pdflatex sig_overlay.tex
\documentclass{article}
\usepackage{blindtext}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\node [xshift=150mm,yshift=-80mm]
@gsauthof
gsauthof / boost_hex.cc
Last active October 3, 2016 20:00
comparison of boost hex algorithm and printf conversion specifier
// re: http://stackoverflow.com/questions/28489153/how-to-portably-compute-a-sha1-hash-in-c/39833022#39833022
#include <boost/uuid/sha1.hpp>
#include <boost/detail/endian.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/range/iterator_range_core.hpp>
#include <boost/endian/conversion.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
@gsauthof
gsauthof / feedgen_bench.py
Last active February 12, 2017 16:27
Costs of ordered attributes in generated feeds
# in reply to: https://groups.google.com/d/msg/django-developers/6mm42rrQXww/Yee5KJjVBQAJ
# (pull request: write feeds with ordered attributes)
# cf. https://github.com/django/django/pull/8044
#
# Results:
# - generating one feed: ~ 0.01 s (current Laptop, i7, SSD)
# - sorting + OrderedDict() in SimpleXMLGenerator::startElement(): + ~ 27 %
# - sorting + OrderedDict() iff there are attributes: + ~ 18 %
# - replacing {} with [] for attributes in feedgenerator: + ~ 7 %
@gsauthof
gsauthof / last_lines.py
Created October 31, 2017 13:36
asyncssh compatible file-like object for capturing the most recent lines
class Last_Lines:
def __init__(self, n, binary=True):
self.n = n
self.binary = binary
self.lines = []
# asyncssh calls fstat() on fileno() and checks if it is a regular file
# thus we open any regular file to fake it ...
self.dev_null = open('/etc/resolv.conf')
def write(self, s):
lines = s.splitlines(True)
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int pid = vfork();
if (pid == 0) {
_exit(0);
}
@gsauthof
gsauthof / redact_env.c
Created July 27, 2019 11:59
Redact environment variable under Linux
/* Demonstrate how to overwrite the original value of
an environment variable in the original environment vector
that is easily observable from other processes
(with sufficient permissions, i.e. same user or root).
*/
/** {{{ MIT No Attribution
Copyright 2019 Georg Sauthoff <mail@georg.so>
@gsauthof
gsauthof / futex_demo.c
Created October 25, 2019 17:25
Fix deadlock, busy waiting and comments in futex example from http://man7.org/linux/man-pages/man2/futex.2.html
/* futex_demo.c
Usage: futex_demo [nloops]
(Default: 5)
Demonstrate the use of futexes in a program where parent and child
use a pair of futexes located inside a shared anonymous mapping to
synchronize access to a shared resource: the terminal. The two
processes each write 'num-loops' messages to the terminal and employ
a synchronization protocol that ensures that they alternate in
@gsauthof
gsauthof / orphan.c
Created November 27, 2016 09:44
Illustrating how orphaned children aren't necessarily adopted 2 by PID 1
/* For illustrating how orphaned children aren't necessarily adopted
by PID 1.
See also: http://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits/36945270#36945270
2016, Georg Sauthoff <mail@georg.so>
*/
#include <unistd.h>