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 ;)
> > _______________________________________________
> > [email protected] mailing list
> > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> > To unsubscribe, send any mail to
> > "[EMAIL PROTECTED]"
> 
> 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
[EMAIL PROTECTED]

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to