diff --git a/README.rst b/README.rst index e81f075..80524bc 100644 --- a/README.rst +++ b/README.rst @@ -64,6 +64,8 @@ Current Status - disk_usage (linux, freebsd, windows) - boot_time (linux, freebsd, windows(but little broken)) - users (linux, freebsd) + - pids (freebsd) + - pid_exists (freebsd) - not yet @@ -72,11 +74,14 @@ Current Status - disk_io_counters - net_io_counters - net_connections - - pids - - pid_exists + - Process class + +- future work + - process_iter - wait_procs - - process class + + License ------------ diff --git a/process.go b/process.go index 8904008..563ce28 100644 --- a/process.go +++ b/process.go @@ -74,3 +74,32 @@ type Io_countersStat struct { Read_bytes int32 Write_bytes int32 } + +func Pids() ([]int32, error) { + ret := make([]int32, 0) + procs, err := processes() + if err != nil { + return ret, nil + } + + for _, p := range procs { + ret = append(ret, p.Pid) + } + + return ret, nil +} + +func Pid_exists(pid int32) (bool, error) { + pids, err := Pids() + if err != nil { + return false, err + } + + for _, i := range pids { + if i == pid { + return true, err + } + } + + return false, err +} diff --git a/process_test.go b/process_test.go index fb21ec0..5b50e4f 100644 --- a/process_test.go +++ b/process_test.go @@ -1,16 +1,27 @@ package gopsutil import ( - "encoding/json" - "fmt" "testing" ) -func Test(t *testing.T) { - v, err := findProcess(0) +func Test_Pids(t *testing.T) { + ret, err := Pids() if err != nil { t.Errorf("error %v", err) } - d, _ := json.Marshal(v) - fmt.Printf("%s\n", d) + if len(ret) == 0 { + t.Errorf("could not get pids %v", ret) + } +} + +func Test_Pid_exists(t *testing.T) { + ret, err := Pid_exists(1) + if err != nil { + t.Errorf("error %v", err) + } + + if ret == false { + t.Errorf("could not get init process %v", ret) + } + }