Calling system() in multi-threads.
Takashi Yano
takashi.yano@nifty.ne.jp
Thu Jul 17 14:14:21 GMT 2025
Hi Corinna,
On Wed, 16 Jul 2025 17:36:42 +0200
Corinna Vinschen wrote:
> On Jul 16 23:52, Takashi Yano via Cygwin wrote:
> > Hi Corinna,
> >
> > On Wed, 18 Jun 2025 20:31:27 +0900
> > Takashi Yano wrote:
> > > On Tue, 17 Jun 2025 15:42:26 -0700
> > > Mark Geisert wrote:
> > > > Hi Takashi,
> > > >
> > > > On 6/17/2025 5:54 AM, Takashi Yano via Cygwin wrote:
> > > > > Hi,
> > > > >
> > > > > If system() is called in parallel in threads, system()
> > > > > fails with exit code 127.
> > > > >
> > > > > Just compile pthread_system.c attached and run.
> > > > >
> > > > > I believe system() is multi-thread safe so the STC
> > > > > should work.
> > > >
> > > > I regret to inform that system() is not currently MT-safe. I discovered
> > > > this a few years ago when porting YAFU (Yet Another Factoring Utility).
> > > >
> > > > I ended up supplying a wrapper that serialized the system() call itself
> > > > but allowed multiple wrappers (and thus child processes) to be running
> > > > at the same time. I never got around to supplying a fix for Cygwin.
> > > > I've attached the code.
> > > > HTH somehow,
> > >
> > > Thanks for the information. I guess the cause is that accessing
> > > child_info_spawn NO_COPY ch_spawn;
> > > in spwan.cc conflicts with other threads. I wonder ch_spawn should
> > > be thread-specific. Or should be guarded by lock.
> >
> > Do you have any idea?
>
> Locking would be super-simple.
>
> But theoretically it should be possible to use a local child_info_spawn
> variable at this point. The ch_spawn child_info_spawn instance is not
> copied to the child anyway, so that should be safe. The same goes for
> posix_spawn() then, btw.
>
> I checked the sources and I don't see any dependency to ch_spawn
> from a spawning process, in contrast to an exec'ing process. That
> doesn't mean there is none, just that I didn't find any.
Thanks!
As a starting point, I tried tntroducing locking. It almost works
as expected, however, sometimes my STC in my first report is hangs
if N is large e.g. 100. The patch is as attached.
What am I missing?
--
Takashi Yano <takashi.yano@nifty.ne.jp>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Add-lock-to-ch_spawn.patch
URL: <https://cygwin.com/pipermail/cygwin/attachments/20250717/a01ea3f6/attachment.ksh>
More information about the Cygwin
mailing list