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

From: Florent Thoumie (flz_at_xbsd.org)
Date: 03/30/05

  • Next message: Peter Jeremy: "Re: A few thoughts.."
    To: "Michael C. Shultz" <ringworm01@gmail.com>
    Date: Wed, 30 Mar 2005 20:42:23 +0200
    
    
    

    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@freebsd.org mailing list
    > > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
    > > To unsubscribe, send any mail to
    > > "freebsd-hackers-unsubscribe@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@xbsd.org
    
    



  • Next message: Peter Jeremy: "Re: A few thoughts.."

    Relevant Pages

    • Re: the best form to wait the finish of execution of a child...
      ... >Excuse for my badly English. ... >finish of execution of a child. ...
      (freebsd-hackers)
    • the best form to wait the finish of execution of a child...
      ... finish of execution of a child. ... pid_t chilpid; ... Excuse my ignorance and I hope they can guide me. ...
      (freebsd-hackers)
    • Re: Traffic cops 26/10/2005
      ... >>> Mind you, I don't for one second think you really believe it. ... >>> were YOUR kids, you'd think up every excuse in the book, wouldn't you? ... >>It wasn't my child, because my child has been brought up to know the ...
      (uk.legal)
    • Re: Awful
      ... NEVER any excuse - no one prattling on about the stress/high pressure ... One or the other shook their child to ...
      (uk.politics.misc)
    • No excuse for having child porn
      ... If people viewing child porn are just "satisfying a personal sexual ... And what's this thing about "restaurants with schoolgirls" and St ... 'NO EXCUSE' FOR HAVING CHILD PORN ... problem of society sexualising schoolchildren. ...
      (uk.legal)