Has anyone hacked together support for creating daemons
with Go? This seems quite hard to do since forking
is non-trivial (e.g. with pakackage init-functions
spawning new go-routines).
- Taru Karttunen
Can't you just make a program and then make a script to launch
multiple instances of it or background it etc.?
- jessta
--
=====================
http://jessta.id.au
r1, r2, err1 = RawSyscall(SYS_FORK, 0, 0, 0)
- Charle Demers
import (
"os"
"syscall"
)
func main() {
var pid, err uintptr
pid, err = fork();
// pid = 0, I'm the child, pid != 0, I'm the parent and pid is the
child process pid.
if err != 0 {
os.Exit(int(err))
}
}
func fork() (pid uintptr, err uintptr) {
var r1, r2, err1 uintptr
darwin := syscall.OS == "darwin"
r1, r2, err1 = syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0)
if err != 0 {
return 0, err1
}
// Handle exception for darwin
if darwin && r2 == 1 {
r1 = 0;
}
return r1, 0
}
- Charle Demers
This won't work reliably in a real program. Once you've started
another thread, fork+exit in parent is either wrong or impossible,
depending on the operating system. Since imported packages
can start goroutines as part of initialization, and those will likely
cause new threads to be created soon after starting main, just
calling fork is not going to work in general.
By far the easiest solution for now is to use the shell's & feature.
I think at some point there will be an os/background package
that you can import and then use to "fork" into the background,
but it is subtle. I tried to put it together a few weeks ago and
got stuck on some detail (I don't remember what) and set it aside.
Russ
I'll check out this os/background package is ready.
Impressive work by the way, keep it up!
Cheers!
On Feb 7, 12:54 am, Russ Cox <r...@golang.org> wrote: