News for hackers. Stuff that matters.
Re:In C, how do you know if the dynamic allocation succeeded? by saagarjha on Friday November 05, 2021 @07:14PM (#29125744)
I mean, posix_spawn exists. It's a messy function, but its job is messy for exactly the reasons you describe. (FWIW, there are very few things you can legally perform between fork and exec.)
Re:In C, how do you know if the dynamic allocation succeeded? by marcan_42 on Friday November 05, 2021 @09:47PM (#29126793)
Are there any things that are illegal between fork and exec? It is perfectly legal to exec whenever you want, and it it perfectly legal to fork whenever you want. I'm not aware of any requirements around the fork/exec sequence.
Re:In C, how do you know if the dynamic allocation succeeded? by tsimionescu on Saturday November 06, 2021 @01:12AM (#29127702)
For a single-threaded process, sure. But in a multi threaded context, almost any operation done in the child after fork() can royally mess up the system. There are many versions of libcs where malloc() after fork() is likely to deadlock between the parent and child processes, as they share the internal malloc() locks.
Re:In C, how do you know if the dynamic allocation succeeded? by marcan_42 on Saturday November 06, 2021 @10:31PM (#29136001)
Ah, yes, multithreading. That always interacts poorly with old UNIX APIs...
Re:In C, how do you know if the dynamic allocation succeeded? by spacechild1 on Saturday November 06, 2021 @05:44AM (#29128828)
From the fork() man page:

“After a fork() in a multithreaded program, the child can safely call only async-signal-safe functions (see signal-safety(7)) until such time as it calls execve(2)“

For example, you can't do any dynamic memory allocation or call a function that may allocate.

Re:In C, how do you know if the dynamic allocation succeeded? by jart on Saturday November 06, 2021 @01:58AM (#29127874)
There's no restrictions in that sense. However the computer might not do what you intended if you were to do something like fputc('x',stdout);fork();fflush(0);
Re:In C, how do you know if the dynamic allocation succeeded? by nextaccountic on Friday November 05, 2021 @11:25PM (#29127238)
I think you mean between vfork and exec. The Linux manpage says:

Standard description

(From POSIX.1) The vfork() function has the same effect as fork(2), except that the behavior is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit(2) or one of the exec(3) family of functions.

Re:In C, how do you know if the dynamic allocation succeeded? by jart on Saturday November 06, 2021 @02:00AM (#29127883)
Note that modern POSIX recommends just calling fork() because "[vfork] was previously under-specified". The glibc man pages shouldn't say undefined since that's too strong a word. There is so much you can actually do in practice during vfork(). Pretty much all system calls are just as safe. It's mostly a question of whether or not your userspace libraries might clobber some global, due to how vfork puts the process in a state where implementation details that are normally private become part of the public surface area of these apis, and not many library authors are disposed to offer those kinds of assurances.