Function fork() analog to go

Hi, everybody!

some not long time ago, I’ve created this ticket in github https://github.com/golang/go/issues/22109

the thing is: I want some pretty nifty way to spawn part of my code as separate process, to do some unconventional stuff with it and not to mess with main process.

other languages have such a thing utilizing functions like clone(), fork() etc.

I want something similar in golang.

ps: for some unknown reason this forum in unavailable from place where i’m locating and i have to use tor to access this forum.

How does os/exec not work for your use case?

how can os/exec spawn part of my code in separate process?

Exec the same binary and use a command line option or something in the environment to direct the control flow to the code you want to run.

this is not the same as forking. (not saying this is not pretty, nor nifty, nor seamless)

It seems to fulfil your criteria:

  • to spawn part of my code as separate process
  • to do some unconventional stuff with it
  • and not to mess with main process

If it doesn’t, then I can think of two options:

  • call fork via cgo
  • use Syscall directly

github.com/jpillora/overseer has an example of this:

look, I bothering with software development for 16 years now. If I’d would like to re-execute my own executable i’d do so. and Yes, I know how to use cgo to call fork too. but simply calling fork() is not an option for me as I’m going to run my software on system without libc. this why I’m filed a feature request, so “maybe cool Go developers will create some tool, which, probably, would create hidden entry points in code, /fork/ process and redirect there when needed”. but NO, you all going to tell me to use os/exec… damn… I’ll go find some better tool to bother with, than golang.

forking a multithreaded process is difficult, error prone and unreliable.

this has been discussed in the past (reference needed) and was rejected.
I wouldn’t hold my breath on this one.

Exactly what are you trying to achieve? Would it be possible to do using the existing tools Go have? For example goroutines

You want to find a programming language which supports forking processes without using libc or POSIX fork()? OK, good luck with that. Let us know what you find.

man clone(2)

% man 2 clone
No entry for clone in section 2 of the manual

And on Linux:

CLONE(2)                   Linux Programmer's Manual                  CLONE(2)

NAME
       clone, __clone2 - create a child process

SYNOPSIS
       /* Prototype for the glibc wrapper function */

But if you want a wrapper for that which will work on Linux only, go ahead.

this man is from linux programmers manual. let me google it for you
http://man7.org/linux/man-pages/man2/clone.2.html

I posted from the Linux man page and a link to a Go library for using the Linux-only API. I guess reading isn’t your strong point.

this is how I’ve red your message in My e-mail client.

it is bad move - to add additional texts to messages, like you did.

go ahead

yes, I know about it. and it’s does not allow to do the thing i’ve described… it’s only mimics os/exec.Cmd in it’s own fashion. so it’s not me here who have problems with something.

The additional text was in the message here by the time you replied to it, and you had to come here to reply to it. (Check the timestamps.)

Anyway, I still don’t think you’ll find a language which has language-level support for the Linux-only clone API. If you do, come back and tell us which it is!

The additional text was in the message here by the time you replied to it, and you had to come here to reply to it. (Check the timestamps.)

are You kidding Me? I’ve replied with e-mail message. because, as I’ve already mentioned, it is necessary to Me using tor to access this site, and it bothers Me to use tor each time to answering to your’s, @mathew, low quality comments.

I … don’t think

without doubt.

language which has language-level support for the Linux-only clone API.

I know at least 2 languages with native support for fork()/clone(). I’m going even name you those languages, as You, apparently, do not know of their existence: C and C++

OK guys, time to dial down the antagonism a notch or two? Go might not do what you need in this case and that’s fine. There might be other solutions, or Go might be the wrong choice, and we can discuss that without getting into ad hominem.

2 Likes

Seems to me there’s a problem with the forum software allowing e-mail replies and allowing reply editing and not sending e-mail updates. Oh well, not my fault.

I know at least 2 languages with native support for fork()/clone(). I’m going even name you those languages, as You, apparently, do not know of their existence: C and C++

They don’t have language level support, which is apparently what you’re demanding of Go.

Both C and C++ let you call the clone() function as a native syscall, and so does Go — but when nathankerr suggested that, you apparently felt that wasn’t good enough. Hence my original comment that I didn’t think you’d find a language that offered anything better.