Calling system() in multi-threads.
Corinna Vinschen
corinna-cygwin@cygwin.com
Wed Jul 16 15:36:42 GMT 2025
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.
Corinna
More information about the Cygwin
mailing list