Skip to content

Instantly share code, notes, and snippets.

@nelsnelson
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelsnelson/9260454 to your computer and use it in GitHub Desktop.
Save nelsnelson/9260454 to your computer and use it in GitHub Desktop.
Example of ulimiting some forks
$ /bin/bash ulimiter.sh 40 forks.py
0: 9388
2: 9390
1: 9389
3: 9391
4: 9392
6: 9394
5: 9393
7: 9395
8: 9396
9: 9397
10: 9398
11: 9399
12: 9400
13: 9401
14: 9402
15: 9403
16: 9404
17: 9405
18: 9406
19: 9407
20: 9408
21: 9409
22: 9410
23: 9411
24: 9412
25: 9413
26: 9414
27: 9415
Traceback (most recent call last):
File "tmp.py", line 13, in <module>
raise e
OSError: [Errno 11] Resource temporarily unavailable
$
#! /usr/bin/env bash
if [ ! -z ${1} ]; then
ulimit -Su $1
ulimit -Hu $1
fi
python=`which python`
$python $2 $1
#! /usr/bin/env python
import os, sys, time
n = int(sys.argv[1])
for i in range(0, n):
try:
pid = os.fork()
if pid == 0:
continue
print "{}: {}".format(i, pid)
break
except OSError as e:
if e.errno == 11:
raise e
time.sleep(4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment