Skip to content

Instantly share code, notes, and snippets.

View cslarsen's full-sized avatar

Christian Stigen Larsen cslarsen

View GitHub Profile
@cslarsen
cslarsen / pids.py
Created June 8, 2016 08:16
Prints PIDs for given program basename (or wildcard if not found)
#!/usr/bin/env python3
from collections import defaultdict
import os
import sys
def read_cmdline(file):
try:
with open(file, "rb") as f:
cmd = str(f.read(), encoding="utf-8")
@cslarsen
cslarsen / pwait.sh
Created June 8, 2016 07:42
BASH program that waits until given PIDs have terminated. Rename to pwait and chmod +x.
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: pwait <PID>"
echo "Waits until PID has terminated, then continues."
echo "Example: pwait 123 && echo DONE"
exit 1
fi
for pid in "$@"; do
# Fast way to compute the Stirling numbers of the second kind.
# https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind
from math import factorial
def binom(a,b):
return factorial(a) / (factorial(b)*factorial(a-b))
def stirling_fast(n,k):
"Returns the Stirling number of the second kind."
@cslarsen
cslarsen / v
Created March 19, 2015 08:00
Usage: v <some file>:lineno or v <somefile> +lineno or <somefile> lineno (i think)... searches for files and opens them in vim
#!/usr/bin/env python
"""
Starts vim with several possible input formats.
Examples:
v foo/bar/baz.c # full correct path
v baz.c # finds baz.c and edits the first
v baz.c:123 # same but start at line 123
v baz.c +123 # same as above
@cslarsen
cslarsen / git-todo.sh
Created December 3, 2014 15:54
Prints TODO and FIXME in code, along with who's probably responsible from git blame. Requires parallel to work.
#!/bin/bash
# Show all TODO and FIXME from current directory along with who's
# responsible.
#
# Requires parallel to work (unless you want to make everything slow)
( find . \( -name '*.cpp' -o \
-name '*.py' -o \
-name '*.h' -o \
@cslarsen
cslarsen / v.py
Last active August 29, 2015 14:00
Start vim fast w/several options, put in ~/bin/v and chmod +x ~/bin/v and put in PATH.
#!/usr/bin/env python
"""
Starts vim with several possible input formats.
Examples:
v foo/bar/baz.c # full correct path
v baz.c # finds baz.c and edits the first
v baz.c:123 # same but start at line 123
v baz.c +123 # same as above
@cslarsen
cslarsen / sendeth.py
Created April 27, 2014 07:14
One way of sending raw Ethernet packets in Python
"""Demonstrates how to construct and send raw Ethernet packets on the
network.
You probably need root privs to be able to bind to the network interface,
e.g.:
$ sudo python sendeth.py
"""
from socket import *
@cslarsen
cslarsen / Makefile
Created January 29, 2014 11:29
How to embed binary objects in your executable.
TARGETS = a.o foo.o a
run: a
./a
all: $(TARGETS)
foo.o: foo.txt
ld -r -b binary -o $@ $<
@cslarsen
cslarsen / heterogeneous-array.go
Last active January 1, 2023 13:41
How to create heterogeneous arrays in Go / golang.
/*
* Example of heterogeneous arrayo in golang.
*
* The trick is simply to create an array that accepts elements that conform
* to the naked interface (an interface with no requirements).
*
* Expected output:
*
* $ go run array.go
* [1 2 3.14 hey {10 20}]
@cslarsen
cslarsen / lab0.cpp
Created November 19, 2012 20:26
Example of a race condition with and wihout locking (pthread or x86 TSL)
/*
* =================================================
* DAT320 Operating Systems, University of Stavanger
*
* LAB 0: PROTECTION AND THREADS
* Written by Christian Stigen Larsen, 2012-08-24
* =================================================
*
* ABOUT
* -----