I use the risky fork(2) in my code. In the child, I found syscall.Getpid()
and C.getpid() return different result. It's demonstrated in the following
code.
On fedora 17, x86_64, it will return something like,
child process (syscall): pid 28354, ppid 28352
child process (C): pid 28352, ppid 28352
Child exits 0
I'm not sure if it can be taken as a bug?
(The background problem: my requirement is an RPC server to serve some
requests(run some code) on behalf different user. I would like to
circumvent the fork(2) usage but found no elegant way by myself. Do I have
to invoke another program, such as su, to accomplish the work in such a
scenario? (I want to avoid it because I have to add command line interface
for each piece of code.))
-- demo code --
package main
// #include <unistd.h>
import "C"
import (
"fmt"
"log"
"syscall"
)
func main() {
r1, _, err := syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0)
//r1, _, err := syscall.Syscall(syscall.SYS_FORK, 0, 0, 0)
if err != 0 {
log.Fatal(err)
}
// r1 := C.fork()
if r1 != 0 {
var wstatus syscall.WaitStatus
pid1, err := syscall.Wait4(int(r1), &wstatus, 0, nil)
if err != nil || pid1 < 0 {
log.Fatal(err)
}
exitStatus := wstatus.ExitStatus()
fmt.Printf("Child exits %d\n", exitStatus)
return
}
// child
fmt.Printf("child process (syscall): pid %d, ppid %d\n",
syscall.Getpid(), syscall.Getppid())
fmt.Printf("child process (C): pid %d, ppid %d\n",
C.getpid(), C.getppid())
}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.