Skip to content

Instantly share code, notes, and snippets.

@lee8oi
Last active January 9, 2025 15:31
Show Gist options
  • Select an option

  • Save lee8oi/ec404fa99ea0f6efd9d1 to your computer and use it in GitHub Desktop.

Select an option

Save lee8oi/ec404fa99ea0f6efd9d1 to your computer and use it in GitHub Desktop.

Revisions

  1. lee8oi revised this gist Mar 19, 2015. 1 changed file with 3 additions and 14 deletions.
    17 changes: 3 additions & 14 deletions startProcess.go
    Original file line number Diff line number Diff line change
    @@ -18,22 +18,11 @@ func Start(args ...string) (p *os.Process, err error) {
    return nil, err
    }

    func Wait(pid int) error {
    proc, err := os.FindProcess(pid)
    if err == nil {
    ps, err := proc.Wait()
    if ps.Exited() && err == nil {
    return nil
    }
    }
    return err
    }

    func main() {
    if proc, err := Start("ping", "-c 3", "www.google.com"); err == nil {
    Wait(proc.Pid)
    proc.Wait()
    }
    if p, err := Start("zsh"); err == nil {
    Wait(p.Pid)
    if proc, err := Start("zsh"); err == nil {
    proc.Wait()
    }
    }
  2. lee8oi renamed this gist Mar 19, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. lee8oi revised this gist Mar 19, 2015. 1 changed file with 10 additions and 11 deletions.
    21 changes: 10 additions & 11 deletions ForkExec.go
    Original file line number Diff line number Diff line change
    @@ -3,20 +3,19 @@ package main
    import (
    "os"
    "os/exec"
    "syscall"
    )

    func Start(args ...string) (pid int, err error) {
    func Start(args ...string) (p *os.Process, err error) {
    if args[0], err = exec.LookPath(args[0]); err == nil {
    var procAttr syscall.ProcAttr
    procAttr.Files = []uintptr{uintptr(syscall.Stdin),
    uintptr(syscall.Stdout), uintptr(syscall.Stderr)}
    p, err := syscall.ForkExec(args[0], args, &procAttr)
    var procAttr os.ProcAttr
    procAttr.Files = []*os.File{os.Stdin,
    os.Stdout, os.Stderr}
    p, err := os.StartProcess(args[0], args, &procAttr)
    if err == nil {
    return p, nil
    }
    }
    return 0, err
    return nil, err
    }

    func Wait(pid int) error {
    @@ -31,10 +30,10 @@ func Wait(pid int) error {
    }

    func main() {
    if pid, err := Start("ping", "-c 3", "www.google.com"); err == nil {
    go Wait(pid)
    if proc, err := Start("ping", "-c 3", "www.google.com"); err == nil {
    Wait(proc.Pid)
    }
    if pid, err := Start("bash"); err == nil {
    Wait(pid)
    if p, err := Start("zsh"); err == nil {
    Wait(p.Pid)
    }
    }
  4. lee8oi revised this gist Mar 19, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions ForkExec.go
    Original file line number Diff line number Diff line change
    @@ -31,10 +31,10 @@ func Wait(pid int) error {
    }

    func main() {
    if pid, err := Start("bash"); err == nil {
    Wait(pid)
    if pid, err := Start("ping", "-c 3", "www.google.com"); err == nil {
    go Wait(pid)
    }
    if pid, err := Start("ping", "-c3", "www.google.com"); err == nil {
    if pid, err := Start("bash"); err == nil {
    Wait(pid)
    }
    }
  5. lee8oi revised this gist Mar 19, 2015. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions ForkExec.go
    Original file line number Diff line number Diff line change
    @@ -27,12 +27,14 @@ func Wait(pid int) error {
    return nil
    }
    }
    return (err)
    return err
    }

    func main() {
    pid, _ := Start("bash")
    Wait(pid)
    pid, _ = Start("ping", "-c3", "www.google.com")
    Wait(pid)
    if pid, err := Start("bash"); err == nil {
    Wait(pid)
    }
    if pid, err := Start("ping", "-c3", "www.google.com"); err == nil {
    Wait(pid)
    }
    }
  6. lee8oi revised this gist Mar 19, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion ForkExec.go
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,8 @@ func Wait(pid int) error {
    }

    func main() {
    pid, _ := Start("ping", "-c3", "www.google.com")
    pid, _ := Start("bash")
    Wait(pid)
    pid, _ = Start("ping", "-c3", "www.google.com")
    Wait(pid)
    }
  7. lee8oi revised this gist Mar 19, 2015. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions ForkExec.go
    Original file line number Diff line number Diff line change
    @@ -11,27 +11,26 @@ func Start(args ...string) (pid int, err error) {
    var procAttr syscall.ProcAttr
    procAttr.Files = []uintptr{uintptr(syscall.Stdin),
    uintptr(syscall.Stdout), uintptr(syscall.Stderr)}
    p, err := syscall.ForkExec(args[0], args[1:], &procAttr)
    p, err := syscall.ForkExec(args[0], args, &procAttr)
    if err == nil {
    return p, nil
    }
    }
    panic(err)
    return
    return 0, err
    }

    func Wait(pid int) {
    func Wait(pid int) error {
    proc, err := os.FindProcess(pid)
    if err == nil {
    ps, err := proc.Wait()
    if ps.Exited() && err == nil {
    return
    return nil
    }
    }
    panic(err)
    return (err)
    }

    func main() {
    pid, _ := Start("bash")
    pid, _ := Start("ping", "-c3", "www.google.com")
    Wait(pid)
    }
  8. lee8oi revised this gist Mar 19, 2015. 1 changed file with 7 additions and 10 deletions.
    17 changes: 7 additions & 10 deletions ForkExec.go
    Original file line number Diff line number Diff line change
    @@ -6,20 +6,17 @@ import (
    "syscall"
    )

    func Start(args ...string) (pid int) {
    if binary, lookErr := exec.LookPath(args[0]); lookErr == nil {
    args[0] = binary
    func Start(args ...string) (pid int, err error) {
    if args[0], err = exec.LookPath(args[0]); err == nil {
    var procAttr syscall.ProcAttr
    procAttr.Files = []uintptr{uintptr(syscall.Stdin),
    uintptr(syscall.Stdout), uintptr(syscall.Stderr)}
    p, execErr := syscall.ForkExec(args[0], args[1:], &procAttr)
    if execErr != nil {
    panic(execErr)
    p, err := syscall.ForkExec(args[0], args[1:], &procAttr)
    if err == nil {
    return p, nil
    }
    pid = p
    } else {
    panic(lookErr)
    }
    panic(err)
    return
    }

    @@ -35,6 +32,6 @@ func Wait(pid int) {
    }

    func main() {
    pid := Start("bash")
    pid, _ := Start("bash")
    Wait(pid)
    }
  9. lee8oi revised this gist Mar 19, 2015. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions ForkExec.go
    Original file line number Diff line number Diff line change
    @@ -26,11 +26,9 @@ func Start(args ...string) (pid int) {
    func Wait(pid int) {
    proc, err := os.FindProcess(pid)
    if err == nil {
    for {
    ps, err := proc.Wait()
    if ps.Exited() && err == nil {
    return
    }
    ps, err := proc.Wait()
    if ps.Exited() && err == nil {
    return
    }
    }
    panic(err)
  10. lee8oi revised this gist Mar 19, 2015. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions ForkExec.go
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    package main

    import (
    "fmt"
    "os"
    "os/exec"
    "syscall"
    @@ -24,9 +23,7 @@ func Start(args ...string) (pid int) {
    return
    }

    func main() {
    pid := Start("bash")
    fmt.Println(pid)
    func Wait(pid int) {
    proc, err := os.FindProcess(pid)
    if err == nil {
    for {
    @@ -38,3 +35,8 @@ func main() {
    }
    panic(err)
    }

    func main() {
    pid := Start("bash")
    Wait(pid)
    }
  11. lee8oi revised this gist Mar 19, 2015. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions ForkExec.go
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ package main

    import (
    "fmt"
    "os"
    "os/exec"
    "syscall"
    )
    @@ -26,6 +27,14 @@ func Start(args ...string) (pid int) {
    func main() {
    pid := Start("bash")
    fmt.Println(pid)
    for {
    proc, err := os.FindProcess(pid)
    if err == nil {
    for {
    ps, err := proc.Wait()
    if ps.Exited() && err == nil {
    return
    }
    }
    }
    }
    panic(err)
    }
  12. lee8oi created this gist Mar 19, 2015.
    31 changes: 31 additions & 0 deletions ForkExec.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    package main

    import (
    "fmt"
    "os/exec"
    "syscall"
    )

    func Start(args ...string) (pid int) {
    if binary, lookErr := exec.LookPath(args[0]); lookErr == nil {
    args[0] = binary
    var procAttr syscall.ProcAttr
    procAttr.Files = []uintptr{uintptr(syscall.Stdin),
    uintptr(syscall.Stdout), uintptr(syscall.Stderr)}
    p, execErr := syscall.ForkExec(args[0], args[1:], &procAttr)
    if execErr != nil {
    panic(execErr)
    }
    pid = p
    } else {
    panic(lookErr)
    }
    return
    }

    func main() {
    pid := Start("bash")
    fmt.Println(pid)
    for {
    }
    }