How to fork or create a subprocess?

90 views
Skip to first unread message

adam.gr...@gmail.com

unread,
Sep 29, 2016, 2:01:51 PM9/29/16
to golang-nuts
I have searched everywhere for the answer to this question, so my apologies if I missed it somewhere on this forum.

I'm looking to create a subprocess when my Golang executable launches. This is no different than calling the fork() function in C.

In fact, the following code represents exactly what I want to do in Go:
#include <stdio.h>
#include <stdlib.h>


int main() {
 
int pid = fork();


 
if (pid == 0) {
    printf
("This is the child process.\n");
 
}
 
else {
    printf
("This is the parent process.\n");
 
}


 
return 0;
}

Any assistance would be greatly appreciated!

Thanks,
Adam

Aram Hăvărneanu

unread,
Sep 29, 2016, 2:08:54 PM9/29/16
to adam.gr...@gmail.com, golang-nuts
You can't use fork in multithreaded programs (except to call exec),
and all Go programs are multithreaded.

You can use os/exec to start new programs, however.

--
Aram Hăvărneanu
Reply all
Reply to author
Forward
0 new messages