You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"fmt""os""syscall"
)
funcmain() {
r1, r2, err:=syscall.Syscall(syscall.SYS_FORK, 0, 0, 0)
iferr!=0 {
panic(err)
}
fmt.Println("Pid of sub process is", r1)
ifr2==0 { // parentfmt.Println("os.Getpid() of parent process is", os.Getpid())
} else { // subfmt.Println("os.Getpid() of sub process is", os.Getpid())
}
}
What did you expect to see?
Pid of sub process is 8048
os.Getpid() of parent process is 8047
Pid of sub process is 8048
os.Getpid() of sub process is 8048
What did you see instead?
Pid of sub process is 8048
os.Getpid() of parent process is 8047
Pid of sub process is 8048
os.Getpid() of sub process is 8047
The text was updated successfully, but these errors were encountered:
It looks like on macos, Go hooks into libc's getpid implementation. I am seeing some references to libc attempting to cache the result instead of making the syscall each time, but I cannot find anything authoritative for macos. My guess is since you are directly issuing the SYS_FORK syscall, this cache is not being invalidated, hence os.Getpid returns the wrong thing. Note that if you directly do syscall.Syscall(syscall.SYS_GETPID, 0, 0, 0) it gives the right answer.
odeke-em
changed the title
affected/package: os.Getpid() returns parent's pid even called from sub process
os: Getpid() returns parent's PID even called from sub process
Apr 2, 2023
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Run this code:
What did you expect to see?
Pid of sub process is 8048
os.Getpid() of parent process is 8047
Pid of sub process is 8048
os.Getpid() of sub process is 8048
What did you see instead?
Pid of sub process is 8048
os.Getpid() of parent process is 8047
Pid of sub process is 8048
os.Getpid() of sub process is 8047
The text was updated successfully, but these errors were encountered: