Messages in this thread | | | From | Roland Kuhn <> | Subject | Re: OT: fork(): parent or child should run first? | Date | Wed, 11 Jan 2006 14:34:19 +0100 |
| |
Hi Arjan!
On 11 Jan 2006, at 13:51, Arjan van de Ven wrote:
> you just cannot depend on which would run first, child or parent. Even > if linux would do it the other way around, you have no guarantee. > Think > SMP or Dual Core processors and time slices and cache misses... your > code just HAS to be able to cope with it. Even on solaris ;)
That means that the starting of the child process needs to be synchronized by the application itself. I tried it once but then I discovered that my case was easily solved in a completely different way (it was a very small project). However, which one is the easiest/ fastest way to do this synchronization? Using SysV-Semaphores? Pipes? Would something like this work?
int fd[2], pid;
pipe(fd); pid = fork(); if (pid < 0) { error(); } else if (pid == 0) { close(fd[1]); read(fd[0]); close(fd[0]); child_code(); } else { store_pid(); close(fd[0]); close(fd[1]); // this should wake up the child, right? }
This should ensure that store_pid() is executed before child_code()...
Ciao, Roland
-- TU Muenchen, Physik-Department E18, James-Franck-Str., 85748 Garching Telefon 089/289-12575; Telefax 089/289-12570 -- UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. -Doug Gwyn -----BEGIN GEEK CODE BLOCK----- Version: 3.12 GS/CS/M/MU d-(++) s:+ a-> C+++ UL++++ P+++ L+++ E(+) W+ !N K- w--- M + !V Y+ PGP++ t+(++) 5 R+ tv-- b+ DI++ e+++>++++ h---- y+++ ------END GEEK CODE BLOCK------
[unhandled content-type:application/pgp-signature] | |