Skip to content

Instantly share code, notes, and snippets.

@ei-grad
Last active August 29, 2015 14:14
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 ei-grad/c32bf278add52bd176e9 to your computer and use it in GitHub Desktop.
Save ei-grad/c32bf278add52bd176e9 to your computer and use it in GitHub Desktop.
Create thread vs. create process in Linux (both use the clone syscall)
from os import fork
print fork()
# clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=...)
from threading import Thread
from os import getpid
import ctypes
import sys
SYS_gettid = 186
libc = ctypes.cdll.LoadLibrary('libc.so.6')
def gettid():
return libc.syscall(SYS_gettid)
def f():
sys.stdout.write("Thread pid=%d tid=%d\n" % (getpid(), gettid()))
sys.stdout.flush()
t = Thread(target=f)
t.start()
# clone(child_stack=.., flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=..., tls=..., child_tidptr=...)
sys.stdout.write("Parent pid=%d tid=%d\n" % (getpid(), gettid()))
t.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment