To a large extent, as the name says. I have a piece of code that looks like this:
pid_t = p; p = fork(); if (p == 0) { childfn(); } else if (p > 0) { parentfn(); } else {
I want either the parent or the child to perform (but not return) their respective functions in front of the other.
Something like calling sleep () will probably work, but is not guaranteed by any standard and will just use the details of the OS scheduler implementation ... is this possible? Will vfork work?
edit: both functions find their way to the system () call, one of which will not be returned until the other is launched. Therefore, for repeated iteration: I need to make sure that either the parent or the child only calls their respective functions (but does not return, because they will not, which is what all mutex-based solutions below offer) before the other. Any ideas? Sorry for the lack of clarity.
edit2: Having one sched_yield process call and sleep, I seem to get pretty reliable results. vfork does provide the semantics I am looking for, but has too many restrictions on what I can do in the child process (I can pretty much call call exec). So, I found some workarounds that are good enough, but there is no real solution. vfork is probably the closest to what I was looking for, but all of the solutions below will work more or less.
source share