Created
August 21, 2022 11:51
-
-
Save jjylik/8598e187360a2fc8b299e4484021c662 to your computer and use it in GitHub Desktop.
Fork syscall in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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