Skip to content

Instantly share code, notes, and snippets.

@jjylik
Created August 21, 2022 11:51
Show Gist options
  • Save jjylik/8598e187360a2fc8b299e4484021c662 to your computer and use it in GitHub Desktop.
Save jjylik/8598e187360a2fc8b299e4484021c662 to your computer and use it in GitHub Desktop.
Fork syscall in go
package main
import (
"fmt"
"syscall"
)
func main() {
a := 10
// NOTE: this does not actually work in any non-trivial applications.
id, _, _ := syscall.Syscall(syscall.SYS_FORK, 0, 0, 0)
if id == 0 {
fmt.Println("In child:", a, &a)
a++
fmt.Println("In child:", a, &a)
} else {
fmt.Println("In parent:", a, &a)
a = a + 10
fmt.Println("In parent:", a, &a)
}
// In parent: 10 0xc0000180b0
// In parent: 20 0xc0000180b0
// In child: 10 0xc0000180b0
// In child: 11 0xc0000180b0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment