GithubHelp home page GithubHelp logo

mek-yt / go-fork Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kraken-hpc/go-fork

0.0 0.0 0.0 20 KB

go-fork provides fork-like behavior for go processes. go-fork can also be used to spawn functions in Linux namespaces.

License: BSD 3-Clause "New" or "Revised" License

Go 100.00%

go-fork's Introduction

go-fork

Go fork is a package to provide fork-like emulation for Go. In reality, it's not very fork-like, but provides for a common thing fork() is used for, namely making a subroutine (goroutine) run in a new process spaces.

Go, by its natively threaded nature, is not fork-safe. This pkg does not implement a real fork.

Instead, this package makes a way to reroute go to execute a goroutine other than main() in a new process. This allows easy creation of some fork-like behaviors without actually using forks.

Moreover, while go-fork children do not share any memory space with go-fork parents, the parent can pass arbitrary data to go-fork via function arguments.

Here are a couple of particularly important diffierence between real forks and go-forks:

  • Go fork does not continue execution from the fork, but rather starts the specified goroutine from whatever point fork.Init() is called (usually early in main() or in init()).
  • Go fork doesn't share memory, file pointers, mutexes, or really anything with the child process. It passes function arguments to the child by encoding them to/decoding them from a temporary file.

How it works

To use go-fork you must do two things:

  1. Register functions to be forkable (this must be done before trying to fork).b
  2. Call fork.Init() somewhere early in the code. If the process is a child, execution will be taken over from this point, and the code will os.Exit when it's done, never returning execution to anything after fork.Init() is called.

It should be noted that go-fork is not able to detect function calling errors at build time. Errors like incorrect argument assignments are runtime errors.

go-fork determines that an run is a fork by looking for special values in its environment, which it will immediately unset once read.

go-fork passes arguments by encoding/gob endoding/decoding them in a temporary file. This fill will be cleaned up immediately after it is read.

Example:

func init() {
	fork.RegisterFunc("child", child)
	fork.Init()
}

func child(n int) {
	fmt.Printf("child(%d) pid: %d\n", n, os.Getpid())
}

func main() {
	fmt.Printf("main() pid: %d\n", os.Getpid())
	if err := fork.Fork("child", 1); err != nil {
		log.Fatalf("failed to fork: %v", err)
	}
}

This will output:

$ go run example/example.go 
main() pid: 164120
child(1) pid: 164125

go-fork's People

Contributors

jlowellwofford avatar mek-yt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.