From: "Gábor Lénárt" <lgb@lgb.hu>
To: Arjan van de Ven <arjan@infradead.org>
Cc: linux-kernel@vger.kernel.org
Subject: Re: OT: fork(): parent or child should run first?
Date: Wed, 11 Jan 2006 14:02:55 +0100 [thread overview]
Message-ID: <20060111130255.GC30219@lgb.hu> (raw)
In-Reply-To: <1136983910.2929.39.camel@laptopd505.fenrus.org>
Hello,
Ok, you're absolutly right here. My problem is to find some solution and not
to change the behaviour of fork() of course :) It's quite annoying to
introduce some kind of IPC between parent and childs just for transferring a
single pid_t ;-) Using exit status would be great (I would transfer "n")
because it can be got by eg waitpid() in signal handler, however exit status
is limited it status & 0377 which is too short range for us ;-( As a
solution I've created a pid_t array and a counter which is filled by signal
handler with pid_t got by waitpid() and then I use it in the main loop to do
the cleanup in parent. Well, this is quite good, the only problem of mine
that some kind of race condition may occur when altering/using these
structures between the main loop and signal handler ...
Again, thanks for the info.
- Gábor
On Wed, Jan 11, 2006 at 01:51:50PM +0100, Arjan van de Ven wrote:
> On Wed, 2006-01-11 at 13:37 +0100, Gábor Lénárt wrote:
> > Hello,
> >
> > The following problem may be simple for you, so I hope someone can answer
> > here. We've got a complex software using child processes and a table
> > to keep data of them together, like this:
> >
> > childs[n].pid=fork();
> >
> > where "n" is an integer contains a free "slot" in the childs struct array.
> >
> > I also handle SIGCHLD in the parent and signal handler searches the childs
> > array for the pid returned by waitpid(). However here is my problem. The
> > child process can be fast, ie exits before scheduler of the kernel give
> > chance the parent process to run, so storing pid into childs[n].pid in the
> > parent context is not done yet. Child may exit, than scheduler gives control
> > to the signal handler before doing the store of the pid (if child run for
> > more time, eg 10 seconds it works of course). So it's impossible to store
> > child pids and search by that information in eg the signal handler? It's
> > quite problematic, since the code uses blocking I/O a lot, so other
> > solutions (like searching in childs[] in the main program and not in signal
> > handler) would require to recode the whole project. The problem can be
> > avoided with having a fork() run the PARENT first, but I thing this is done
> > by the scheduler so it's a kernel issue. Also the problem that source should
> > be portable between Linux and Solaris ...
>
> 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 ;)
>
>
--
- Gábor
next prev parent reply other threads:[~2006-01-11 13:02 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-01-11 12:37 OT: fork(): parent or child should run first? Gábor Lénárt
2006-01-11 12:51 ` Arjan van de Ven
2006-01-11 13:02 ` Gábor Lénárt [this message]
2006-01-11 13:25 ` Bernd Petrovitsch
2006-01-11 13:49 ` Ian Campbell
2006-01-11 13:55 ` Bernd Petrovitsch
2006-01-11 14:05 ` Ian Campbell
2006-01-11 15:19 ` linux-os (Dick Johnson)
2006-01-12 1:33 ` David Schwartz
2006-01-12 1:38 ` Kurt Wall
2006-01-12 17:23 ` Hugh Dickins
2006-01-12 20:19 ` Gábor Lénárt
2006-01-11 13:34 ` Roland Kuhn
2006-01-11 17:11 ` Lee Revell
2006-01-11 13:03 ` Miquel van Smoorenburg
2006-01-11 13:08 ` Jan-Benedict Glaw
2006-01-12 1:33 ` David Schwartz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20060111130255.GC30219@lgb.hu \
--to=lgb@lgb.hu \
--cc=arjan@infradead.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox