sk-matrix-project/app/wails/pkg/system/process.go

57 lines
1.1 KiB
Go
Raw Normal View History

2023-07-09 23:04:17 +08:00
package system
2023-07-17 15:53:59 +08:00
import (
"github.com/shirou/gopsutil/process"
"os"
)
func (i *InfoUtils) GetCurrentPid() int {
return os.Getpid()
}
2023-07-09 23:04:17 +08:00
2023-07-15 23:29:03 +08:00
func (i *InfoUtils) GetAllProcessPid() []int32 {
2023-07-09 23:04:17 +08:00
pidList, _ := process.Pids()
return pidList
}
2023-07-17 15:53:59 +08:00
type ProcessDTO struct {
Pid int32 `json:"pid"`
Name string `json:"name"`
Parent int32 `json:"parent"`
Status string `json:"status"`
CreateTime int64 `json:"createTime"`
2023-07-09 23:04:17 +08:00
}
2023-07-17 10:05:45 +08:00
2023-07-17 15:53:59 +08:00
func (i *InfoUtils) process2ProcessDTO(p *process.Process) *ProcessDTO {
d := &ProcessDTO{
Pid: p.Pid,
Name: "",
Parent: 0,
Status: "",
CreateTime: 0,
}
d.Name, _ = p.Name()
parent, _ := p.Parent()
if parent != nil {
d.Parent = parent.Pid
}
d.CreateTime, _ = p.CreateTime()
d.Status, _ = p.Status()
return d
}
func (i *InfoUtils) GetProcessInfo(pid int32) *ProcessDTO {
info, _ := process.NewProcess(pid)
return i.process2ProcessDTO(info)
2023-07-17 10:05:45 +08:00
}
func (i *InfoUtils) GetProcesses() []*ProcessDTO {
info, _ := process.Processes()
dto := make([]*ProcessDTO, 0)
2023-07-17 15:53:59 +08:00
for _, p := range info {
dto = append(dto, i.process2ProcessDTO(p))
2023-07-17 10:05:45 +08:00
}
return dto
}