當前位置: 首頁>>代碼示例>>Golang>>正文


Golang os.ProcessState類代碼示例

本文整理匯總了Golang中os.ProcessState的典型用法代碼示例。如果您正苦於以下問題:Golang ProcessState類的具體用法?Golang ProcessState怎麽用?Golang ProcessState使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了ProcessState類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: runprog

func runprog(argv []string) (filesize int64, mem int64, err error) {
	var attr os.ProcAttr
	var stat *os.ProcessState
	var proc *os.Process

	exepath, err := exec.LookPath(argv[0])
	if err != nil {
		err = errors.New("can't find exe file.")
		return
	}

	proc, err = os.StartProcess(exepath, argv, &attr)
	if err != nil {
		return
	}

	fi, err := os.Stat(exepath)
	if err != nil {
		return
	}

	filesize = fi.Size()
	stat, err = proc.Wait()
	mem = int64(stat.SysUsage().(*syscall.Rusage).Maxrss)
	return
}
開發者ID:shell909090,項目名稱:performance,代碼行數:26,代碼來源:perf.go

示例2: GetErrorLevel

func GetErrorLevel(processState *os.ProcessState) (int, bool) {
	if processState.Success() {
		return 0, true
	} else if t, ok := processState.Sys().(syscall.WaitStatus); ok {
		return t.ExitStatus(), true
	} else {
		return 255, false
	}
}
開發者ID:Matsuyanagi,項目名稱:nyagos,代碼行數:9,代碼來源:interpreter.go

示例3: Wait

// Wait calls Wait on the underlying exec.Cmd's Process and, if the
// operating system supports it, returns the exit status.
//
// If an error occurs when waiting for the underlying process, the exit
// status will be -2, and the error will be returned.  If the operating
// system does not support determining the exit status, but the program
// exited successfully, the exit status will be 0.  If the operating
// system does not support determining the exit status and the program
// exited unsuccessfully, the exit status will be -1.
func (p *Proc) Wait() (exitStatus int, err error) {
	var ps *os.ProcessState
	ps, err = p.Cmd.Process.Wait()
	if err != nil {
		return -2, err
	}
	ws, ok := ps.Sys().(syscall.WaitStatus)
	if ok {
		return ws.ExitStatus(), nil
	}
	if ps.Success() {
		return 0, nil
	}
	return -1, nil
}
開發者ID:pennello,項目名稱:go_prun,代碼行數:24,代碼來源:proc.go

示例4: Stop

// Stop the worker process
func (w *Worker) Stop(replyChan chan<- CommandReply) {
	//err := syscall.Kill(worker.Pid, syscall.SIGTERM)
	proc, err := os.FindProcess(w.Pid)
	if nil != err {
		w.Logger.Printf("worker.Stop(): Cannot find worker process %d: %s\n", w.Pid, err)
		//w.exitChannel <- w.dtoppedCommand(err, state, stalled)
		replyChan <- CommandReply{Reply: fmt.Sprintf("[%s] worker process %d already stopped", w.Taskname, w.Pid)}
		return
	}

	// attempt stopping the worker with a SIGTERM first
	err = proc.Signal(syscall.SIGTERM)
	if err != nil {
		if err.Error() == "os: process already finished" {
			w.Logger.Println("worker.Stop() SIGTERM sent to already dead process")
		} else {
			w.Logger.Printf("worker.Stop(): Error sending SIGTERM to worker process %d: %s\n", w.Pid, err)
		}
	}

	var msg string
	var cmd Command
	var state *os.ProcessState
	gracePeriod := time.Duration(w.GracePeriod) * time.Millisecond

	// wait until the process returns gracefully from the SIGTERM, or times out + is killed after a grace period
	select {
	case <-time.After(gracePeriod):
		w.Logger.Printf("Grace Period (%s) expired, killing worker process %d", gracePeriod, w.Pid)
		err = proc.Kill()
		msg = fmt.Sprintf("Worker process %d was still around after %s, killed.", w.Pid, gracePeriod)
		if nil != err {
			msg = fmt.Sprintf("worker.Stop(): Failed to kill process %d - %s", w.Pid, err.Error())
		}
		cmd = <-w.exitChannel // coming from waitOnProcess()
		cmd.Params["killed"] = true
	case cmd = <-w.exitChannel: // wait for the original waitpid() syscall in waitOnProcess() to return
		cmd.Params["killed"] = false
		err = nil
		msg = fmt.Sprintf("Worker process %d terminated gracefully", w.Pid)
		if state2, ok := cmd.Params["state"]; ok {
			state, ok = state2.(*os.ProcessState)
		}
		if err2, ok := cmd.Params["error"]; ok {
			if err, ok = err2.(error); ok {
				msg = fmt.Sprintf("Worker process %d terminated with error: (%T) %#v (%s)", w.Pid, err, err, state.String())
			}
		}
	}
	cmd.Params["stalled"] = w.HasStalled()
	//w.Logger.Println(msg)
	replyChan <- CommandReply{Reply: msg, Error: err}
	w.TaskFeedbackChannel <- cmd
}
開發者ID:mahasak,項目名稱:workerpoolmanager,代碼行數:55,代碼來源:worker.go

示例5: getExitCode

func getExitCode(state *os.ProcessState) int {
	return state.Sys().(syscall.WaitStatus).ExitStatus()
}
開發者ID:pombredanne,項目名稱:geard,代碼行數:3,代碼來源:utils.go

示例6: exitStatus

func exitStatus(p *os.ProcessState) int {
	ws := p.Sys().(syscall.WaitStatus)
	return ws.ExitStatus()
}
開發者ID:rainycape,項目名稱:gondola,代碼行數:4,代碼來源:dev.go


注:本文中的os.ProcessState類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。