Documentation ¶
Overview ¶
包gproc实现了对进程的管理和通信功能。
Index ¶
- func AddSigHandler(handler SigHandler, signals ...os.Signal)
- func AddSigHandlerShutdown(handler ...SigHandler)
- func IsChild() bool
- func Listen()
- func MustShell(ctx context.Context, cmd string, out io.Writer, in io.Reader)
- func MustShellExec(ctx context.Context, cmd string, environment ...[]string) string
- func MustShellRun(ctx context.Context, cmd string)
- func PPid() int
- func PPidOS() int
- func Pid() int
- func SearchBinary(file string) string
- func SearchBinaryPath(file string) string
- func Send(pid int, data []byte, group ...string) error
- func SetPPid(ppid int) error
- func Shell(ctx context.Context, cmd string, out io.Writer, in io.Reader) error
- func ShellExec(ctx context.Context, cmd string, environment ...[]string) (result string, err error)
- func ShellRun(ctx context.Context, cmd string) error
- func StartTime() time.Time
- func Uptime() time.Duration
- type Manager
- func (m *Manager) AddProcess(pid int)
- func (m *Manager) Clear()
- func (m *Manager) GetProcess(pid int) *Process
- func (m *Manager) KillAll() error
- func (m *Manager) NewProcess(path string, args []string, environment []string) *Process
- func (m *Manager) Pids() []int
- func (m *Manager) Processes() []*Process
- func (m *Manager) RemoveProcess(pid int)
- func (m *Manager) Send(data []byte)
- func (m *Manager) SendTo(pid int, data []byte) error
- func (m *Manager) SignalAll(sig os.Signal) error
- func (m *Manager) Size() int
- func (m *Manager) WaitAll()
- type MsgRequest
- type MsgResponse
- type Process
- type SigHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddSigHandler ¶
func AddSigHandler(handler SigHandler, signals ...os.Signal)
AddSigHandler 为一个或多个自定义信号添加自定义处理函数。
func AddSigHandlerShutdown ¶
func AddSigHandlerShutdown(handler ...SigHandler)
AddSigHandlerShutdown 添加自定义信号处理器以处理关闭信号: syscall.SIGINT(中断信号) syscall.SIGQUIT(退出并生成 core 文件信号) syscall.SIGKILL(强制终止信号,无法被捕获或忽略) syscall.SIGTERM(软件终止信号) syscall.SIGABRT(异常终止信号,如调用 abort 函数时触发)
func MustShellExec ¶
MustShellExec 的执行方式与 ShellExec 相同,但当出现任何错误时,它会触发 panic。
func MustShellRun ¶
MustShellRun 的行为与 ShellRun 相同,但当出现任何错误时,它会触发 panic。
func PPidOS ¶
func PPidOS() int
PPidOS 返回当前进程的系统父进程ID。 注意,PPidOS 和 PPid 函数之间的区别在于:PPidOS 返回的是系统的父进程ID, 但若存在自定义父进程ID,PPid 函数可能会返回由 gproc 提供的自定义进程ID。
func SearchBinary ¶
SearchBinary 在当前工作目录和PATH环境变量中搜索二进制文件 `file`。
func SearchBinaryPath ¶
SearchBinaryPath在PATH环境变量中搜索二进制文件`file`。
func Shell ¶
Shell 同步执行命令 `cmd`,并使用给定的输入管道 `in` 和输出管道 `out`。 命令 `cmd` 从输入管道 `in` 读取输入参数,并自动将其输出写入输出管道 `out`。
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 是一个进程管理器,用于维护多个进程。
func (*Manager) AddProcess ¶
AddProcess 将一个进程添加到当前管理器中。 如果给定 `pid` 的进程不存在,则不做任何操作。
func (*Manager) GetProcess ¶
GetProcess 获取并返回一个 Process 对象。 如果未找到给定 `pid` 的进程,则返回 nil。
func (*Manager) NewProcess ¶
NewProcess 创建并返回一个 Process 对象。
func (*Manager) RemoveProcess ¶
RemoveProcess 从当前管理器中移除一个进程。
type MsgRequest ¶
type MsgRequest struct { SenderPid int // Sender PID. ReceiverPid int // Receiver PID. Group string // 消息组名称。 Data []byte // Request data. }
MsgRequest 是用于进程间通信的请求结构体。
func Receive ¶
func Receive(group ...string) *MsgRequest
接收区块并通过本地TCP监听从其他进程接收消息。 注意,只有当调用此函数时,才会启用TCP监听服务。
type MsgResponse ¶
type MsgResponse struct { Code int // 1: 表示成功;其它值:表示错误 Message string // Response message. Data []byte // Response data. }
MsgResponse 是用于进程间通信的响应结构体。
type Process ¶
Process是用于单个进程的结构体。
func NewProcess ¶
NewProcess 创建并返回一个新的 Process。
func NewProcessCmd ¶
NewProcessCmd根据给定的命令和可选的环境变量数组创建并返回一个进程。