| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package main
- import (
- "fmt"
- "os"
- "strconv"
- "syscall"
- )
- // 前台执行job
- func cmd_fg(fields []string, stdin *os.File, stdout *os.File, stderr *os.File) {
- if len(fields) == 1 { // 如果没有指定job,则选编号最小的
- if len(jobs) == 0 { // 如果没有job
- panic("fg: no current job") // 则提示错误
- }
- for i := range jobs {
- fields = append(fields, "%"+strconv.Itoa(i))
- break
- }
- }
- for _, jid := range fields[1:] { // 遍历参数
- id, err := strconv.Atoi(jid[1:]) // 字符串转数字
- if err != nil {
- panic("fg: job not found: " + jid)
- }
- _, ok := jobs[id] // 是否有指定编号的job
- if !ok {
- panic("fg: %" + jid[1:] + ": no such job")
- }
- cur = job{jobs[id].wait, "running", jobs[id].work, jobs[id].cmd} // 前台执行
- delete(jobs, id) // 删除job
- cur.cmd.Process.Signal(syscall.SIGCONT) // 向进程发送SIGCONT信号,继续执行
- fmt.Fprintf(stdout, "[%d] %d continued\t%s\n", id, cur.cmd.Process.Pid, cur.work)
- term = make(chan bool) // 初始化停止信号管道
- <-term // 等待接收信号,实现前台执行
- close(term) // 关闭停止管道
- cur.cmd = nil // 前台没有在执行命令
- }
- }
|