Skip to content

Instantly share code, notes, and snippets.

View orbekk's full-sized avatar

Kjetil Ørbekk orbekk

  • NTNU
  • Trondheim, Norway
View GitHub Profile
@orbekk
orbekk / gist:2351825
Created April 10, 2012 14:40
Java Data Parallelism
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
public class Par {
int parallelism = Runtime.getRuntime().availableProcessors() + 1;
ExecutorService executor = Executors.newCachedThreadPool();
class WithCallback {
Callback callback = null;
void done() {
callback.run();
}
protected abstract void run();
}
@orbekk
orbekk / bash-challenge-1.txt
Created August 7, 2013 22:45
Bash Challenge #1
Write a script pardo.sh. It should take a target number of processes N, and a list of commands. The commands should be executed in parallel on up to N processes.
Usage: pardo.sh NUM_PROCESSES COMMAND...
Test as follows:
time /tmp/pardo.sh 2 "sleep 2" "sleep 4" "sleep 4" "sleep 2"
This should take 6 seconds.
@orbekk
orbekk / bash-challenge-1-solution.sh
Created August 8, 2013 00:00
Bash Challenge #1 Solution
#!/bin/bash
if [[ -z $1 ]]; then
echo "Usage: $(basename $0) NUM_PROCESSES COMMAND..."
fi
NUM_PROCESSES=$1
shift
SEMAPHORE=/tmp/semaphore.$RANDOM
@orbekk
orbekk / gist:6206917
Created August 11, 2013 21:26
Bash quoting example
#!/bin/bash
function my_print() {
for arg in "$@"; do
echo -n "{$arg} "
done
echo
}
var1="Two bits"
s = r"print \"s = r\\\"\" + s + \"\\\"\"\nprint s.decode(\"string-escape\")"
print "s = r\"" + s + "\""
print s.decode("string-escape")
@orbekk
orbekk / maybe.cpp
Last active December 26, 2015 08:19 — forked from ehamberg/maybe.cpp
template <class T>
struct Maybe {
public:
Maybe() {}
explicit Maybe(T v) : state_(v) {}
bool isJust() {
return state_;
}
#!/bin/bash
#
# This script syncs authorized keys (found in the $authorized_keys_file below)
# to a list of remote hosts. It does not touch existing keys unless overwrite
# is set to true, but creates a special section containing the keys.
declare -r begin_marker="### BEGIN MANAGED_BY_KJ_SYNC_AUTHORIZED_KEYS.SH ###"
declare -r end_marker="### END MANAGED_BY_KJ_SYNC_AUTHORIZED_KEYS.SH ###"
declare -r overwrite=true
declare -r tmpdir=$(mktemp -d /tmp/kj_sync_authorized_keys.XXXXX)