衍生新进程

这是来自GoByExample的例子,代码在https://gobyexample.com/spawning-processes

它能够执行任意Go或者非Go程序,并且等待放回结果,外部进程结束后继续执行本程序。

代码实现

package main
import "fmt"
import "io/ioutil"
import "os/exec"
func main() {
    dateCmd := exec.Command("date")
    dateOut, err := dateCmd.Output()
    if err != nil {
        panic(err)
    }
    fmt.Println("> date")
    fmt.Println(string(dateOut))
    grepCmd := exec.Command("grep", "hello")
    grepIn, _ := grepCmd.StdinPipe()
    grepOut, _ := grepCmd.StdoutPipe()
    grepCmd.Start()
    grepIn.Write([]byte("hello grep\ngoodbye grep"))
    grepIn.Close()
    grepBytes, _ := ioutil.ReadAll(grepOut)
    grepCmd.Wait()
    fmt.Println("> grep hello")
    fmt.Println(string(grepBytes))
    lsCmd := exec.Command("bash", "-c", "ls -a -l -h")
    lsOut, err := lsCmd.Output()
    if err != nil {
        panic(err)
    }
    fmt.Println("> ls -a -l -h")
    fmt.Println(string(lsOut))
}

运行结果

$ go run spawning-processes.go
> date
Wed Oct 10 09:53:11 PDT 2012
> grep hello
hello grep
> ls -a -l -h
drwxr-xr-x  4 mark 136B Oct 3 16:29 .
drwxr-xr-x 91 mark 3.0K Oct 3 12:50 ..
-rw-r--r--  1 mark 1.3K Oct 3 16:28 spawning-processes.go

归纳总结

因此如果你的程序需要执行外部命令,可以直接使用exec.Command()来Spawn进程,并且根据需要获得外部程序的返回值。

下一节:进程的概念大家都很熟悉,但你是否能准确说出僵尸进程的含义呢?还有COW(Copy On Write)、Flock(File Lock)、Epoll和Namespace的概念又是否了解过呢?