Re: fork ???



Hello,

fork() does not run a different program, but makes another copy of the
same program!

To use it you would do something like...


     #include <sys/types.h>
     #include <unistd.h>

       .
       .
       .

     /* Stuff before the fork here */

       .
       .
       .

     pid_t p;

     switch ( p = fork() ) {

       case 0:
         /* I am the child! */
         break;

       case -1:
         /* An error occured! */
         break;

       default:
         /* I am the parent, an 'p' holds the pid of the child */
     }

       .
       .
       .

     /* Other stuff here */
   
       .
       .
       .


So what you have here, is that you are running one program until
fork() is called.  At that point another copy of the same program
is made.  (And now you have two.)  The only difference between the
two is what is returned by fork().  The original program (what we
call the parent) will have fork() return a positive value which
will be the 'pid' of the child.  The copy (what we call the child)
will have fork() return '0'.

(And of course you'd have to replace my comments with something
useful.)

But, from reading your message, this is probably not what you
are looking for.  fork() will *not* run a different program,
but the same program!

If you want to execute another program, then you could always
use one of these functions...

  int execl(const char * path, char * arg0, ..., const char * argn, char * /*NULL*/);

  int execle(const char * path, char * arg0, ..., const char * argn, char * /*NULL*/, char * const
envp);

  int execlp(const char * file, char * arg0, ..., const char * argn, char * /*NULL*/);

  int execv(const char * path, char * const argv[]);

  int execvp(const char * file, char * const argv[]);

  int execve(const char * path, char * const argv[], char * const envp[]);

And each of these requires you to...

  #include <unistd.h>

Basically, the way that you use them is to use fork() to split your program
into two, and then have one of the copies (let's just say the child) call
one of the 'exec' functions.  For example...

   
  #include <sys/types.h> /* For fork()
                          */
  #include <unistd.h>    /* For execl(), fork()
                          */

  #include <stdlib.n>    /* For exit()
                          */

  void main(int argc, char * argv[])
  {

    pid_t p;

     switch ( p = fork() ) {

       case 0:
         /* I am the child! */
         /*
          * I'll just run the program `ls -laF' and then exit.
          */
         execl("/usr/bin/ls", "ls", "-alF", NULL);
         exit(1);
         break;

       case -1:
         /* An error occured! */
         /* Something should really be done here */
         break;

       default:
         /* I am the parent, an 'p' holds the pid of the child */
         /*
          * No need to put anything here.  Since the child process
            exits after it runs `ls -laF', I can put the code for
            the rest of the program after this switch statement.
          */
     }

     /* Rest of the code goes here */

  }

In case you are wondering, you need to do the fork(), (and not just
call exec from your original program) because exec will overlay its
calling process with with the new executable module (in this case `ls').

(Oh ya, and I really didn't bother about error handling so that the
code would be a bit more readable.  But you should include it!)

Hope that helps.

See ya

     Charles Iliya Krempeaux

Magnus Wirström wrote:
> 
> Hi
> Do anyone have a good example of how to use fork() ? What I am trying to
> do is to execute another program from my app ... I used system() before
> but it freezes up my app until the other program exits and do not
> continue running the gnome app widget after it ...  that was ok before
> but not anymore.I have read the man for it but I really don't get how to
> tell it what program to run ...
> 
> So I am happy if anyone could share a peice of code that explains how to
> do this in a easy way !!! If you need more info please contact me
> 
> Thanks
> /Magnus




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]