Skip to content

Instantly share code, notes, and snippets.

Christian Stigen Larsen cslarsen

Block or report user

Report or block cslarsen

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@cslarsen
cslarsen / pids.py
Created Jun 8, 2016
Prints PIDs for given program basename (or wildcard if not found)
View pids.py
#!/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 Jun 8, 2016
BASH program that waits until given PIDs have terminated. Rename to pwait and chmod +x.
View pwait.sh
#!/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
View stirling.py
# 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 Mar 19, 2015
Usage: v <some file>:lineno or v <somefile> +lineno or <somefile> lineno (i think)... searches for files and opens them in vim
View v
#!/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 Dec 3, 2014
Prints TODO and FIXME in code, along with who's probably responsible from git blame. Requires parallel to work.
View git-todo.sh
#!/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 Aug 29, 2015
Start vim fast w/several options, put in ~/bin/v and chmod +x ~/bin/v and put in PATH.
View v.py
#!/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 Apr 27, 2014
One way of sending raw Ethernet packets in Python
View sendeth.py
"""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 Jan 29, 2014
How to embed binary objects in your executable.
View Makefile
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 Dec 15, 2015
How to create heterogeneous arrays in Go / golang.
View heterogeneous-array.go
/*
* 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 Nov 19, 2012
Example of a race condition with and wihout locking (pthread or x86 TSL)
View lab0.cpp
/*
* =================================================
* DAT320 Operating Systems, University of Stavanger
*
* LAB 0: PROTECTION AND THREADS
* Written by Christian Stigen Larsen, 2012-08-24
* =================================================
*
* ABOUT
* -----
You can’t perform that action at this time.