Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call to fork in Sys.command #801

Open
smondet opened this issue Dec 17, 2021 · 1 comment
Open

Call to fork in Sys.command #801

smondet opened this issue Dec 17, 2021 · 1 comment

Comments

@smondet
Copy link

smondet commented Dec 17, 2021

The explict call to fork is guarded here: fork.c#L28:

if (caml_domain_is_multicore()) {
  caml_failwith
    ("Unix.fork may not be called while other domains were created");
}

But in runtime/sys.c:L497 there is a call to int system(const char *command) whose manpage says:

 The system() library function uses fork(2) to create a child
 process that executes the shell command specified in command
 using execl(3) […]

Should this one also failwith?

@xavierleroy
Copy link
Contributor

If there are multiple domains running, in the child process created by fork there is only one domain running and the OCaml runtime system is in an inconsistent state. So, the child cannot run OCaml code, and that's why Unix.fork must fail in multi-domain programs.

However, the child process can still run C code, esp. if the C code does little more than calling exec, as in the case for the system C standard library function.

Plus, this man page is misleading: system can also be implemented with posix_spawn, which in turn may not involve a full fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants