Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling fork before main

Does the POSIX standard allow calling fork() before main() - for example, in a C++ static instance, or in a __attribute__((constructor)) C function?

like image 555
user6285020 Avatar asked Jun 14 '16 16:06

user6285020


People also ask

What happens when you call fork?

When a process calls fork, it is deemed the parent process and the newly created process is its child. After the fork, both processes not only run the same program, but they resume execution as though both had called the system call.

What happens when you call a fork () in a thread?

fork creates a new process. The parent of a process is another process, not a thread. So the parent of the new process is the old process. Note that the child process will only have one thread because fork only duplicates the (stack for the) thread that calls fork .

Do you have to fork before exec?

No, you cannot create a new process in UNIX, you can only duplicate your current process (using fork ). If you want the new process to do something other than exactly what the current process is doing, you then replace it (using exec ). You do not have to fork before calling exec .

Which process runs first in fork?

The original process is called the parent process and the second process is called the child process. The child process is an almost exact copy of the parent process. Both processes continue executing from the point where the fork( ) calls returns execution to the main program.


2 Answers

There is no indication in the fork manpage that it's prohibited, nor can I think of a reason for it to be.

Indeed, there's nothing special about main as far as POSIX is concerned; it's just that C chooses to begin its programs in a function with that name, and C++ sort of almost nearly does the same. But as far as POSIX is concerned, once your process is up, your process is up. It could have been written in any old language, and fork would still have to work.

In C (not C++!) it is impossible for you to write code that executes before main (because initialisers for static variables must be constant in that context), so for C it's a bit of a moot point. However, stepping outside of the C abstraction for a moment, there's still nothing in POSIX stopping your compiler vendor from including code in the C runtime that performs a fork before entering main. Recall that the "true" entrypoint is not actually main; the "true" entrypoint does some library initialisations and whatnot before invoking main to begin your part of the program.

like image 71
Lightness Races in Orbit Avatar answered Sep 21 '22 08:09

Lightness Races in Orbit


Does the POSIX standard allow calling fork() before main() [...] ?

It depends on what you mean.

C programs

POSIX, through its incorporation of the C standard, specifies that C program startup occurs when the environment (operating system) calls main(). The POSIX-defined semantics of the C program start at that point; by definition, they do not include prior calls to any other function. If any such calls in fact occur then they are not part of "the program" in this sense, so the program cannot call fork() before main().

On the other hand, POSIX does not explicitly forbid the process in which a C program runs to execute fork() or any other function prior to the commencement of the defined C semantics. In the sense that what is not forbidden is allowed, POSIX does allow it. If your question is about whether GCC's __attribute__((constructor)) or similar facilities violate POSIX then no, they don't, though their behavior is not defined by POSIX.

On the third hand, POSIX does not affirmatively allow it, and does not define any way for C source code to specify that it should happen. If your question is about whether POSIX defines a specific facility that __attribute__((constructor)) or similar facilities leverage to provide their advertised behavior, or whether it requires that conforming systems must provide a mechanism by which such facilities can operate, then no, it doesn't. Not providing such a mechanism would not inherently cause a system to fail to conform to POSIX.

C++ programs

POSIX is defined in terms of the shell command language and ISO C. It does not require implementations to provide any C++ support at all, but of course it does not forbid it, either. Thus, the second and third paragraphs of the previous section apply to all aspects of C++ programs.

For what it's worth, however, like C, C++ specifies that programs start in main(). This has similar meaning in C++ as in C, but unlike C, C++ implementations may provide mechanisms by which user-provided code can be made to run before the first statement of main(). Specifically, the constructor for a variable with static duration may run before the first statement of main() is executed, but whether it does so is implementation-defined (C++11, 3.6.2/4). In this sense, therefore, it is implementation-defined whether the C++ program semantics can include a call to fork() before the first statement of main().

Note also that the C++ standard is carefully worded to avoid speaking to whether such initializations can be before main() is entered -- that's probably immaterial, but there are at least a couple of ways in which it would be possible to tell the difference.

Other programs

POSIX has even less to say about other kinds of programs than it has to say about C++ programs, except for shell scripts. Of course, shell scripts generally don't have a main() at all, and they have no direct access to the C library by which to explicitly fork().

like image 22
John Bollinger Avatar answered Sep 22 '22 08:09

John Bollinger