Zombie Process
"...which would make the child process zombie"
go job()
does not start a separate process, it starts another goroutine. You have a main
goroutine, and a job
goroutine in this program. Language spec:
A "go" statement starts the execution of a function call as an independent concurrent thread of control, or goroutine, within the same address space.
Therefore you will not be creating a Unix zombie process.
Main Not Exiting Before Job
"Why does this NOT happen: the main exits after it receives the done and before job return...?"
Reason #1: The Receive Operations are Blocking Both Goroutines
(1) Because there's no send statement on channel done
in job
.
<-done
is a receive operation. See "Receive operator" in Go spec:
For an operand ch
of channel type, the value of the receive operation <-ch
is the value received from the channel ch
.
See also "Send statements" in Go spec:
ch <- 3 // send value 3 to channel ch
You need a send statement. E.g.:
done <- true
More explanatory text and examples are available in the Go Tour and Effective Go.
...And there's a second reason:
Reason #2: There's Nothing To Do Between Send and Return
(2) Because, even if you change the receive operation to a send statement, there's nothing to do in the job goroutine between when it sends on the channel and it returns, so it's not likely for any time to pass from one to the next. If you want that to happen, add a sleep in job using the time
package:
done <- true
time.Sleep(time.Second * 30)
return