Skip to content

Commit

Permalink
Drop goroutines for /proc traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Dec 31, 2024
1 parent 0da6137 commit effd081
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions restartable.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,10 @@ func runProcessMonitor(lister ProcessLister, opts Opts, openProc func(int) (Proc
fmt.Printf("%s\t%s\t%s\t%-20s\t%20s\t%s\n", "PID", "PPID", "UID", "User", "Service", "Command")
}

channel := make(map[int]chan *ProcessInfo, len(pids))
for _, pid := range pids {
channel[pid] = make(chan *ProcessInfo, 1)
}

for _, pid := range pids {
go func(pid int) {
defer close(channel[pid])
channel := make(chan *ProcessInfo, len(pids))
go func() {
defer close(channel)
for _, pid := range pids {
fs, err := openProc(pid)
if err != nil {
if !errors.Is(err, unix.ENOENT) {
Expand All @@ -336,17 +332,14 @@ func runProcessMonitor(lister ProcessLister, opts Opts, openProc func(int) (Proc
info, err := getProcessInfo(fs, opts.verbose, opts.user)
if err != nil {
log.Print(err)
} else if info != nil {
channel <- info
}
channel[pid] <- info
}(pid)
}
}
}()

services := make(map[string]bool)
for _, pid := range pids {
proc := <-channel[pid]
if proc == nil {
continue
}
for proc := range channel {
if opts.short < 3 {
fmt.Printf("%d\t%d\t%d\t%-20s\t%20s\t%s\n", proc.Pid, proc.Ppid, proc.Uid, getUser(proc.Uid), proc.Service, proc.Command)
} else if proc.Service != "-" {
Expand Down

0 comments on commit effd081

Please sign in to comment.