Golang中实现典型的fork调用

栏目: Go · 发布时间: 7年前

内容简介:Golang中没有提供

English Version

https://github.com/moby/moby/tree/master/pkg/reexec

Golang中没有提供 fork 调用,只有

这三个都类似于 fork + exec ,但是没有类似C中的fork调用,在fork之后根据返回的pid 然后进入不同的函数。原因在:

https://stackoverflow.com/questions/28370646/how-do-i-fork-a-go-process/28371586#28371586

简要翻译一下:

fork

先看一下C里的传统使用方式:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>

void child() {
    printf("child process\n");
}

int main() {
    printf("main process\n");
    pid_t pid = fork();
    int wstatus;

    if (pid == 0) {
        child();
    } else {
        printf("main exit\n");
        waitpid(pid, &wstatus, 0);
    }
}

运行一下:

$ gcc main.c && ./a.out 
main process
main exit
child process

我们看看 Docker 提供的实现的使用方式:

package main

import (
    "log"
    "os"

    "github.com/docker/docker/pkg/reexec"
)

func init() {
    log.Printf("init start, os.Args = %+v\n", os.Args)
    reexec.Register("childProcess", childProcess)
    if reexec.Init() {
        os.Exit(0)
    }
}

func childProcess() {
    log.Println("childProcess")
}

func main() {
    log.Printf("main start, os.Args = %+v\n", os.Args)
    cmd := reexec.Command("childProcess")
    cmd.Stdin = os.Stdin
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    if err := cmd.Start(); err != nil {
        log.Panicf("failed to run command: %s", err)
    }
    if err := cmd.Wait(); err != nil {
        log.Panicf("failed to wait command: %s", err)
    }
    log.Println("main exit")
}

跑一下:

$ go run main.go
2018/03/08 19:52:39 init start, os.Args = [/tmp/go-build209640177/b001/exe/main]
2018/03/08 19:52:39 main start, os.Args = [/tmp/go-build209640177/b001/exe/main]
2018/03/08 19:52:39 init start, os.Args = [childProcess]
2018/03/08 19:52:39 childProcess
2018/03/08 19:52:39 main exit

其原理是 init 一定会在 main 之前执行。而初次执行程序的时候 os.Args[0] 是可执行文件的名字。 但是 reexec.Command 却可以修改子进程的 os.Args[0] 。所以子进程会直接找到 reexec.Init 上 reexec.Register 所注册的函数,然后执行,返回true。然后调用 os.Exit(0)


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

TED Talks Storytelling: 23 Storytelling Techniques from the Best

TED Talks Storytelling: 23 Storytelling Techniques from the Best

Akash Karia / CreateSpace Independent Publishing Platform / 2015-1-11 / USD 6.99

"Every speaker can put these ideas into practice immediately -- and they should!" ~ Dr. Richard C. Harris, Certified World Class Speaking Coach "An insightful read" ~Dennis Waller, Top 500 Revie......一起来看看 《TED Talks Storytelling: 23 Storytelling Techniques from the Best》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具