summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go73
-rw-r--r--main/main.go47
-rw-r--r--process/process.go35
3 files changed, 82 insertions, 73 deletions
diff --git a/main.go b/main.go
deleted file mode 100644
index 44ef0ab..0000000
--- a/main.go
+++ /dev/null
@@ -1,73 +0,0 @@
-package main
-
-import (
- "fmt"
- "io/ioutil"
- "log"
- "regexp"
-)
-
-const (
- STOP = 0
- GETPIDS = 1
-)
-
-type Program struct {
- pid int
- command string
-}
-
-func gstatGatherPids(res chan<- string) {
- re, _ := regexp.Compile("^[0-9]+$")
-
- dir, err := ioutil.ReadDir("/proc/")
- if err != nil {
- log.Fatal(err)
- }
-
- for _, fi := range dir {
- name := fi.Name()
- if re.MatchString(name) {
- res <- name
- }
- }
-}
-
-func gstatGather(action <-chan int, done chan<- bool, res chan<- string) {
- for {
- switch <-action {
- case STOP:
- {
- done <- true
- break
- }
- case GETPIDS:
- {
- gstatGatherPids(res)
- done <- true
- }
- }
- }
-}
-
-func main() {
- action := make(chan int)
- done := make(chan bool)
- res := make(chan string)
-
- go gstatGather(action, done, res)
-
- action <- GETPIDS
- for {
- switch <-done {
- case false:
- {
- fmt.Println(<-res)
- }
- case true:
- {
- break
- }
- }
- }
-}
diff --git a/main/main.go b/main/main.go
new file mode 100644
index 0000000..b8b5d39
--- /dev/null
+++ b/main/main.go
@@ -0,0 +1,47 @@
+package main
+
+import (
+ "fmt"
+ "github.com/buetow/gstat/process"
+)
+
+const (
+ STOP = 0
+ GETPIDS = 1
+)
+
+func gstatGather(action <-chan int, res chan<- process.Process) {
+ for {
+ switch <-action {
+ case STOP:
+ {
+ break
+ }
+ case GETPIDS:
+ {
+ process.Gather(res)
+ }
+ }
+ close(res)
+ }
+}
+
+func main() {
+ action := make(chan int)
+ res := make(chan process.Process)
+
+ go gstatGather(action, res)
+
+ action <- GETPIDS
+ for {
+ process, more := <-res
+ if more {
+ fmt.Println(process.Cmdline)
+ } else {
+ break
+ }
+ }
+ action <- STOP
+
+ fmt.Println("Good bye")
+}
diff --git a/process/process.go b/process/process.go
new file mode 100644
index 0000000..4420b44
--- /dev/null
+++ b/process/process.go
@@ -0,0 +1,35 @@
+package process
+
+import (
+ "fmt"
+ "io/ioutil"
+ "log"
+ "regexp"
+ "strconv"
+)
+
+type Process struct {
+ Pid int
+ Cmdline string
+}
+
+func Gather(res chan<- Process) {
+ re, _ := regexp.Compile("^[0-9]+$")
+
+ dir, err := ioutil.ReadDir("/proc/")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ for _, direntry := range dir {
+ name := direntry.Name()
+ if re.MatchString(name) {
+ pid, _ := strconv.Atoi(name)
+ bytes, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
+ if err != nil {
+ log.Fatal(err)
+ }
+ res <- Process{Pid: pid, Cmdline: string(bytes)}
+ }
+ }
+}