> I could use os.Wait() with a specific pid and launch it in goroutines,
> but it seems very complicated for what it is.
I don't understand the problem. Just wait for all pids you launched
sequentially.
--
Marcin Kowalczyk
> I tried os.Wait() with -1/0 as pid but it didn't really understand
> what was the result, I had a lot of notifications but none for my
> childs processes.
I don't know what you're doing there, but this works fine here:
package main
import "os"
func sleep() {
os.ForkExec("/bin/sh", []string{"/bin/sh", "-c", "sleep 3"},
[]string{}, "", nil)
}
func main() {
sleep()
sleep()
for i := 0; i != 2; i++ {
msg, _ := os.Wait(-1, 0)
println(msg.Pid, " exited")
}
}
--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/blog
http://niemeyer.net/twitter
Using os.Wait(-1, os.WUNTRACED) in the provided snippet works fine for
me as well.
With Ubuntu 10.10 on amd64 it works fine:
22442 exited
22444 exited
Would be worth debugging further to see where the issue is.
On Tue, 28 Dec 2010, creack wrote:
> I am using ForkExec in loop and I would like to wait for all them
> after the loop. In C, this would be with WAIT_ANY, but I didn't
> managed to get it in Go.
I was told that using wait with PID 0 or -1 is dangerous in Go, due to
the way the runtime works. Using one goroutine per fork is the easiest
way:
import (
"fmt"
"os"
)
func doSomething(arg string, status chan *os.Waitmsg) {
pid, err := os.ForkExec( ... )
if err != nil { ... }
w, err := os.Wait(pid, 0)
// ... check value of err
status <- w
}
func main() {
var num int
status := make(chan *os.Waitmsg)
for _, arg := range os.Args[1:] {
go doSomething(arg, status)
num++
}
for i := 0; i < num; i++ {
w := <-status
if w != nil && w.Exited() {
fmt.Printf("Child (pid %v) exited with status
%v\n", w.Pid, w.ExitStatus())
}
}
}
--Benny.
David
--
David Roundy