the best form to wait the finish of execution of a child...

Florent Thoumie flz at xbsd.org
Wed Mar 30 10:42:29 PST 2005


Le Mercredi 30 mars 2005 à 10:25 -0800, Michael C. Shultz a écrit :
> On Wednesday 30 March 2005 10:17 am, zean zean wrote:
> > Hi Hackers:
> >
> > Excuse for my badly English.  which is the best form to wait  the
> > finish of execution of a child.
> >
> > My idea is:
> >
> > pid_t chilpid;
> >
> > while(childpid != wait(&status))
> > ;
> >
> > Any aid to obtain the best way is very welcome.
> >
> > PD. Excuse my ignorance and I hope they can guide me.
> >
> > Bye and thanxs ;)
> > _______________________________________________
> > freebsd-hackers at freebsd.org mailing list
> > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> > To unsubscribe, send any mail to
> > "freebsd-hackers-unsubscribe at freebsd.org"
> 
> Here is how I do it, likely someone will have a better way:
> 
> 	pid_t		pid;
> 
> 	pid	= fork();
> 	if( !pid )
> 	{
> 		execl( "/bin/mkdir", "mkdir", "directory_name", 0 );
> 	}
> 	wait( (int*)pid );

	Something like this would be better I think :


	pid_t	pid;
	int	ret;	

	pid = fork();
	if (pid < 0)
	{
		perror("fork");
		/* exit(1); if you want */
	}
	else if (pid == 0)
	{
		if (execl("/bin/mkdir", "mkdir", "directory_name", 0 ))
			perror("execl");
	}
	else
	{
		wait(&ret); /* plus return code handling, YMMV */
	}
	
	You need to check fork(2) return code or your wait(2) is useless.
	Don't mix pid_t with int just for the gain of one variable.

	I haven't played with this for some time, I guess it's correct.

-- 
Florent Thoumie
flz at xbsd.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: This is a digitally signed message part
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20050330/60bf998b/attachment.bin


More information about the freebsd-hackers mailing list