1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 // Process stores the information about a process created by StartProcess.
18 handle
uintptr // handle is accessed atomically on Windows
19 isdone
uint32 // process has been successfully waited on, non zero if true
20 sigMu sync
.RWMutex
// avoid race between wait and signal
23 func newProcess(pid
int, handle
uintptr) *Process
{
24 p
:= &Process
{Pid
: pid
, handle
: handle
}
25 runtime
.SetFinalizer(p
, (*Process
).Release
)
29 func (p
*Process
) setDone() {
30 atomic
.StoreUint32(&p
.isdone
, 1)
33 func (p
*Process
) done() bool {
34 return atomic
.LoadUint32(&p
.isdone
) > 0
37 // ProcAttr holds the attributes that will be applied to a new process
38 // started by StartProcess.
39 type ProcAttr
struct {
40 // If Dir is non-empty, the child changes into the directory before
41 // creating the process.
43 // If Env is non-nil, it gives the environment variables for the
44 // new process in the form returned by Environ.
45 // If it is nil, the result of Environ will be used.
47 // Files specifies the open files inherited by the new process. The
48 // first three entries correspond to standard input, standard output, and
49 // standard error. An implementation may support additional entries,
50 // depending on the underlying operating system. A nil entry corresponds
51 // to that file being closed when the process starts.
54 // Operating system-specific process creation attributes.
55 // Note that setting this field means that your program
56 // may not execute properly or even compile on some
58 Sys
*syscall
.SysProcAttr
61 // A Signal represents an operating system signal.
62 // The usual underlying implementation is operating system-dependent:
63 // on Unix it is syscall.Signal.
64 type Signal
interface {
66 Signal() // to distinguish from other Stringers
69 // Getpid returns the process id of the caller.
70 func Getpid() int { return syscall
.Getpid() }
72 // Getppid returns the process id of the caller's parent.
73 func Getppid() int { return syscall
.Getppid() }
75 // FindProcess looks for a running process by its pid.
77 // The Process it returns can be used to obtain information
78 // about the underlying operating system process.
80 // On Unix systems, FindProcess always succeeds and returns a Process
81 // for the given pid, regardless of whether the process exists.
82 func FindProcess(pid
int) (*Process
, error
) {
83 return findProcess(pid
)
86 // StartProcess starts a new process with the program, arguments and attributes
87 // specified by name, argv and attr.
89 // StartProcess is a low-level interface. The os/exec package provides
90 // higher-level interfaces.
92 // If there is an error, it will be of type *PathError.
93 func StartProcess(name
string, argv
[]string, attr
*ProcAttr
) (*Process
, error
) {
94 return startProcess(name
, argv
, attr
)
97 // Release releases any resources associated with the Process p,
98 // rendering it unusable in the future.
99 // Release only needs to be called if Wait is not.
100 func (p
*Process
) Release() error
{
104 // Kill causes the Process to exit immediately.
105 func (p
*Process
) Kill() error
{
109 // Wait waits for the Process to exit, and then returns a
110 // ProcessState describing its status and an error, if any.
111 // Wait releases any resources associated with the Process.
112 // On most operating systems, the Process must be a child
113 // of the current process or an error will be returned.
114 func (p
*Process
) Wait() (*ProcessState
, error
) {
118 // Signal sends a signal to the Process.
119 // Sending Interrupt on Windows is not implemented.
120 func (p
*Process
) Signal(sig Signal
) error
{
124 // UserTime returns the user CPU time of the exited process and its children.
125 func (p
*ProcessState
) UserTime() time
.Duration
{
129 // SystemTime returns the system CPU time of the exited process and its children.
130 func (p
*ProcessState
) SystemTime() time
.Duration
{
131 return p
.systemTime()
134 // Exited reports whether the program has exited.
135 func (p
*ProcessState
) Exited() bool {
139 // Success reports whether the program exited successfully,
140 // such as with exit status 0 on Unix.
141 func (p
*ProcessState
) Success() bool {
145 // Sys returns system-dependent exit information about
146 // the process. Convert it to the appropriate underlying
147 // type, such as syscall.WaitStatus on Unix, to access its contents.
148 func (p
*ProcessState
) Sys() interface{} {
152 // SysUsage returns system-dependent resource usage information about
153 // the exited process. Convert it to the appropriate underlying
154 // type, such as *syscall.Rusage on Unix, to access its contents.
155 // (On Unix, *syscall.Rusage matches struct rusage as defined in the
156 // getrusage(2) manual page.)
157 func (p
*ProcessState
) SysUsage() interface{} {