1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
process.go 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2024-05-06 12:13 . process 获取进程相关信息
package system
import "errors"
// Process is the generic interface that is implemented on every platform
// and provides common operations for processes.
type Process interface {
// Pid is the process ID for this process.
Pid() int32
// PPid is the parent process ID for this process.
PPid() int32
// Name name running this process. This is not a path to the
Name() string
Path() string
}
// Processes returns all processes.
//
// This of course will be a point-in-time snapshot of when this method was
// called. Some operating systems don't provide snapshot capability of the
// process table, in which case the process table returned might contain
// ephemeral entities that happened to be running when this was called.
func Processes() ([]Process, error) {
return getProcesses()
}
// FindProcessByPId looks up a single process by pid.
//
// Process will be nil and error will be nil if a matching process is
// not found.
func FindProcessByPId(pid int32) (Process, error) {
ps, err := getProcesses()
if err != nil {
return nil, err
}
for _, p := range ps {
if p.Pid() == pid {
return p, nil
}
}
return nil, errors.New("process not found")
}
// FindProcessByName looks up a single process by pid.
//
// Process will be nil and error will be nil if a matching process is
// not found.
func FindProcessByName(name string) (Process, error) {
ps, err := getProcesses()
if err != nil {
return nil, err
}
for _, p := range ps {
if p.Name() == name {
return p, nil
}
}
return nil, errors.New("process not found")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.123

搜索帮助