Skip to content

Instantly share code, notes, and snippets.

/* Find sum of an array using forked child processes.
$ gcc -std=gnu99 -Wall -Werror -pedantic parallel-sum-fork.c -lrt && /usr/bin/time ./a.out
http://stackoverflow.com/questions/19934294/how-to-calculate-the-sum-of-the-elements-of-an-array-of-integers-using-p-process
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#!/usr/bin/env python
"""
http://stackoverflow.com/questions/19959271/in-python-how-can-i-use-timeout-in-subprocess-and-get-the-output
"""
from subprocess import Popen, PIPE
from threading import Timer
from timeit import default_timer as timer
start = timer()
# start process
#!/usr/bin/env python
"""Demo: use a socket as subprocesses stdin/stdout.
http://stackoverflow.com/a/19961290
"""
import socket
from contextlib import closing
from subprocess import Popen, PIPE
host = "stackoverflow.com"
#!/usr/bin/env python
"""Make zombie processes that are garbage collected."""
import gc
import os
import time
from subprocess import Popen
def check_zombies(expected_nzombies):
#NOTE: don't use Popen() here
output = os.popen(r"ps aux | grep Z | grep -v grep").read()
#!/usr/bin/env python
"""Make zombies using `.wait()`.
http://stackoverflow.com/questions/2760652
"""
import os
from threading import Timer
from subprocess import Popen
n = 10
/** Pipeline between three processes.
http://stackoverflow.com/q/20056084/
See @chill's answer: http://stackoverflow.com/a/8092270
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#!/usr/bin/env python
"""Print words containing the same letter at least three times in a row.
http://stackoverflow.com/questions/20170022/elongated-word-check-in-sentence
"""
import re
words = open('/usr/share/dict/words')
#NOTE: support only ascii letters
elongated = re.compile(r'(?i)([a-z])\1{2,}').search
@zed
zed / write-n-ids.c
Last active December 29, 2015 05:39
/** Fork N children, each child writes his ID to the pipe, the parent
reads the IDs from the pipe.
http://stackoverflow.com/questions/20160438/fork-multiple-children-then-use-pipe-with-select
*/
#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@zed
zed / paul.py
Created November 25, 2013 06:07
#!/usr/bin/env python
"""Try to cause memory error due to subprocess' fork/clone behaviour.
It should produce:
OSError: [Errno 12] Cannot allocate memory
http://stackoverflow.com/questions/20111242/how-to-avoid-errno-12-cannot-allocate-memory-errors-caused-by-using-subprocess
"""
import subprocess
/** Prefix notation calculator implemented in terms of fork/pipe
without recursion/stack.
http://stackoverflow.com/questions/20165145/prefix-notation-calculation-using-fork-and-pipes-in-c-without-recursion-or-st
It accepts input on stdin and writes the final result to stdout.
It uses gmplib (-lgmp) to support arbitrary precision integer arithmetic.
*/