Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 8 Jul 2001 13:28:26 -0300 (ART)
From:      Fernando Gleiser <fgleiser@cactus.fi.uba.ar>
To:        Gabriel Ambuehl <gabriel_ambuehl@buz.ch>
Cc:        <freebsd-questions@FreeBSD.ORG>
Subject:   Re[2]: Passing data in C++ via stdin without waiting for the new process to  complete
Message-ID:  <20010708131733.Q48298-100000@cactus.fi.uba.ar>
In-Reply-To: <84594262844.20010708173749@buz.ch>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, 8 Jul 2001, Gabriel Ambuehl wrote:

>
> The exec() family of calls looks more promising, but those still
> block the parent process. I need a solution, that allows the parent
> process to continue its work immediately after the child has been
> spawned, no matter how long it takes the child to complete.

No. exec(2) replaces the corrent process with the new one. If you don't
want to block waiting for the child to return, don't call wait(2), and
install a signal handler to trap the SIGCHLD.

You need to do:

if ((pid=fork()) == 0) {
	execlp("path", name, arg1, ..., NULL);
	exit(2); /*cant happen */
} else if (pid == -1) {
	/* fork failed */
} else {
	/* Parent code goes here */
}


I recommend "Advanced programming in the Unix environment" by W. R. Stevens.
It is THE book about unix programming. Read chapter 8 "Process Control" to
learn everything you need to know about fork, exec and wait.


>
> One somewhat crazy way to circumvent the blocking of the parent would
> be to use a separate thread to control each of the currently running
> childs but
> I fear that this would end up in a performance problem if each call
> to
> an external executable also involves creation of a thread in the
> parent.
>
> Another approach, which perhaps would even be the faster one (how
> compares fork() to the creation of a wholly new process from
> scratch?),
> is to simple create fork() based daemons out of the external programs
> which then accept their params by TCP or UDP...
>
> > P.S Buy the book : "Advanced Programming in teh UNIX environment"
> > by W.R.Stevens ISBN:0-201-56317-7 and maybe his other books about
> > "Unix Network Programming" as well.
>
> I've already got Volume 1 of Network Programming by Stevens right
> beside me and didn't want to don't like C and thus use C++ (would
> prefer Python but that is too slow and too separated from the System
> for my current project) which at least got stuff like dynamic arrays
> built into the STL so I don't have to come up with what I consider to
> be essential data types...
>
> Best regards,
>  Gabriel
>
> -----BEGIN PGP SIGNATURE-----
> Version: PGP 6.5i
>
> iQEVAwUBO0hwQMZa2WpymlDxAQGhUwf/TGTbKLC8/htZ+duiD/gL6OYs38fK17U0
> kHohMw3EQ5fM/bK+kD34cfaWJyyqjCUgSo5NaGh9Vi2thalrippIi1GIh0AO0Q6s
> hjj26+a0PT+qwHq6XglblFc6RB3VnW/0M4t2AHpoyNs8CHax/o/xsKbSWcfW/ODq
> bD4eUcYxhu+f5RA5gsyofLczBkYAssz5At9bZVWTKwYHAonSzuUb0OfrdBCRK/Eg
> IA1SAw8kI9TJJ/vz5C+ldKCG44u9Jm/ssTvHoJv5NOa1nNlkb2dGg6EwhB2eSsbx
> fezrOd8UatPYfVFhU0OWY23s4B/aHuDb38oSkbShIrj8FS5qlFUnog==
> =F3Lv
> -----END PGP SIGNATURE-----
>
>
> To Unsubscribe: send mail to majordomo@FreeBSD.org
> with "unsubscribe freebsd-questions" in the body of the message
>


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010708131733.Q48298-100000>