1.9 进程pid,运行耗时 运行退出状态

Posted cucy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1.9 进程pid,运行耗时 运行退出状态相关的知识,希望对你有一定的参考价值。

package main

import (
    "fmt"
    "os/exec"
    "runtime"
    "time"
)

func main() {

    var cmd string
    if runtime.GOOS == "windows" {
        cmd = "timeout"
    } else {
        cmd = "sleep"
    }

    proc := exec.Command(cmd, "1")
    proc.Start()

    // Wait function will
    // wait till the process ends.
    proc.Wait()

    // After the process terminates
    // the *os.ProcessState contains
    // simple information
    // about the process run
    fmt.Printf("PID: %d\n", proc.ProcessState.Pid())
    fmt.Printf("Process took: %dms\n", proc.ProcessState.SystemTime()/time.Microsecond)
    fmt.Printf("Exited sucessfuly : %t\n", proc.ProcessState.Success())
}

/*
PID: 6337
Process took: 755ms
Exited sucessfuly : true
*/
package main

import (
    "fmt"
    "os/exec"
    "runtime"
)

func main() {

    var cmd string
    if runtime.GOOS == "windows" {
        cmd = "timeout"
    } else {
        cmd = "sleep"
    }
    proc := exec.Command(cmd, "1")
    proc.Start()

    // No process state is returned
    // till the process finish.
    fmt.Printf("Process state for running process: %v\n", proc.ProcessState)

    // The PID could be obtain
    // event for the running process
    fmt.Printf("PID of running process: %d\n\n", proc.Process.Pid)
}

/*
Process state for running process: <nil>
PID of running process: 6410

*/

以上是关于1.9 进程pid,运行耗时 运行退出状态的主要内容,如果未能解决你的问题,请参考以下文章

Centos7系统进程管理

从子进程中检索 PID 和退出状态

僵尸进程

僵尸进程

Linux进程管理

LINUX PID 1和SYSTEMD PID 0 是内核的一部分,主要用于内进换页,内核初始化的最后一步就是启动 init 进程。这个进程是系统的第一个进程,PID 为 1,又叫超级进程(代码片段