Skip to content

Instantly share code, notes, and snippets.

Created October 8, 2018 20:24
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Calling fork() from Swift
// Note: calling fork() can be unsafe and dangerous, that
// is why Swift doesn't let you invoke it directy. Be very
// careful about how you use fork(). For some more details,
// see and
import Darwin
let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
let forkPtr = dlsym(RTLD_DEFAULT, "fork")
typealias ForkType = @convention(c) () -> Int32
let fork = unsafeBitCast(forkPtr, to: ForkType.self)
switch fork() {
case -1:
case 0:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment