system() , exec(), pid_t and wait() question ?

system() , exec(), pid_t and wait() question ?

Post by huang ka » Mon, 22 Jun 1998 04:00:00



Hi,
    I'm using `system("xterm&");  ` to start an xterm in my program, and
I want get the new program's pid , so I can check if it has exited. How
can I get the pid of it in my program ?

pid=system("xterm&"); //????

Kevin

 
 
 

system() , exec(), pid_t and wait() question ?

Post by Sascha Demetri » Mon, 22 Jun 1998 04:00:00



: Hi,
:     I'm using `system("xterm&");  ` to start an xterm in my program, and
: I want get the new program's pid , so I can check if it has exited. How
: can I get the pid of it in my program ?

: pid=system("xterm&"); //????
hint:  man fork; man execvp; man waitpid
*NOTE* that `waitpid()' terminates with `errno == EINTR' (interrupted
systemcall) if the waiting process catches a signal, so you should do
something like:
  do { if ((rv = waitpid(pid, 0, 0)) != -1) break; } while (errno == EINTR);

solong,
        Sascha

--
Sascha Demetrio
TU Darmstadt - Germany
email: demetrio<et>rbg<dot>informatik<dot>tu<dash>darmstadt<dot>de

 
 
 

system() , exec(), pid_t and wait() question ?

Post by David Z. Maz » Mon, 22 Jun 1998 04:00:00


hk> I'm using `system("xterm&"); ` to start an xterm in my program,
hk> and I want get the new program's pid , so I can check if it has
hk> exited. How can I get the pid of it in my program ?
hk>
hk> pid=system("xterm&"); //????

system() doesn't return until the program exits (assuming it starts).
The system(3) manpage explains how you can tell whether the program
actually started or not.

NB: In all likelihood, you're not going to be able to find a program
called 'xterm&' on your system.  If you want to start an xterm as
another process and have both running simultaneously, look into the
fork() and exec() calls instead of using system().  (fork() will
return the new pid to the parent process.  It will return zero to the
child, which can exec() the other program.)

--
 _____________________________
/                             \       "Dad was reading a book called
|          David Maze         |     _Schroedinger's Kittens_.  A*

| http://www.veryComputer.com/|               -- Abra Mitchell
\_____________________________/

 
 
 

system() , exec(), pid_t and wait() question ?

Post by huang ka » Mon, 22 Jun 1998 04:00:00




> : Hi,
> :     I'm using `system("xterm&");  ` to start an xterm in my program, and
> : I want get the new program's pid , so I can check if it has exited. How
> : can I get the pid of it in my program ?

> : pid=system("xterm&"); //????
> hint:  man fork; man execvp; man waitpid
> *NOTE* that `waitpid()' terminates with `errno == EINTR' (interrupted
> systemcall) if the waiting process catches a signal, so you should do
> something like:
>   do { if ((rv = waitpid(pid, 0, 0)) != -1) break; } while (errno == EINTR);

> solong,
>         Sascha

> --
> Sascha Demetrio
> TU Darmstadt - Germany
> email: demetrio<et>rbg<dot>informatik<dot>tu<dash>darmstadt<dot>de

  Thanks for your help , but there still have a question : how can I get the
pid ?
from your code I know I can wait till a process finished . And if I want start
the process and get the pid of it  , how can I do that ?
  I don't understand  what fork() can do .Pls help me out.

Kevin

 
 
 

system() , exec(), pid_t and wait() question ?

Post by Alexander Vi » Mon, 22 Jun 1998 04:00:00




[snip]

Quote:>  Thanks for your help , but there still have a question : how can I get the
>pid ?
>from your code I know I can wait till a process finished . And if I want start
>the process and get the pid of it  , how can I do that ?
>  I don't understand  what fork() can do .Pls help me out.

Look: if a process calls fork() the system makes an exact copy of that
process. Both copies run in parallel. The only difference between them is
that old one gets PID of new (as the return value of fork()), while new
gets zero. Now, execvp(...) replaces the content of process with new
program.
        So the typical variant would be:

pid=fork();
if (pid==-1) {
        /* error: can't fork. Most likely - too many processes. */
        ....

Quote:} else if (pid==0) {

        /* old process will bypass it */
        execvp("/usr/bin/foo","/usr/bin/foo",arg1,arg2,arg3,NULL);
        /* we shouldn't be here - exec.. doesn't return if it was
           successful. Most likely - no such program. Flame on.
        */
        ....
Quote:} else {

        /* old process; PID of new one is in pid */
        /* wait for completion of the child. Or don't - up to you */
        ....

Quote:}

--
Luser, n.:
        Human-like creature that doesn't dare to use elevator, because of
its belief that only horrible geeks can master arcane and obscure art of
using control panel.
 
 
 

system() , exec(), pid_t and wait() question ?

Post by David D. Gitchel » Mon, 22 Jun 1998 04:00:00


Well stated, Alexander, but one should also stress to the beginner
that there MUST be an exit() call following an exec(), in the event
that exec() fails.  If the exec() call fails, the child process is
never overlaid with the new program and continues to run as a copy
of, and in parallel with, the parent from the point of the exec.

This is one of the typical ways the beginner "fork bombs" him/herself
into oblivion.  It's a tad frustrating when one can't login to one's
own computer because there are too many processes running...


>         So the typical variant would be:

> pid=fork();
> if (pid==-1) {
>         /* error: can't fork. Most likely - too many processes. */
>         ....
> } else if (pid==0) {
>         /* old process will bypass it */
>         execvp("/usr/bin/foo","/usr/bin/foo",arg1,arg2,arg3,NULL);
>         /* we shouldn't be here - exec.. doesn't return if it was
>            successful. Most likely - no such program. Flame on.
>         */
>         ....

        exit(1); /* JUST IN CASE EXEC FAILS! */

Quote:> } else {
>         /* old process; PID of new one is in pid */
>         /* wait for completion of the child. Or don't - up to you */
>         ....
> }

Enjoy!
-- Dave
------------------------------------------------------------
#include <std_disclaimer.h> /* I speak for myself, only. */|
------------------------------------------------------------
LCDR David D. Gitchell, USN (Retired),   Hutchinson, KS, USA

 
 
 

system() , exec(), pid_t and wait() question ?

Post by Robert Hyat » Mon, 22 Jun 1998 04:00:00



: Hi,
:     I'm using `system("xterm&");  ` to start an xterm in my program, and
: I want get the new program's pid , so I can check if it has exited. How
: can I get the pid of it in my program ?

: pid=system("xterm&"); //????

: Kevin

why not do a fork(), and then let the child exec() xterm.  Stash the
pid that fork() returns and check that.

Or, just catch SIGCHLD, which should get triggered when the xterm
started by system() exits, although if you start multiple things, you
might have problems figuring out which exited using the system()
call.

Fork is cleaner

ie if (!(pid=fork()) {
  exec("/xx/yy/xterm",NULL);

Quote:}

then you can use pid as an argument to waitpid() with the WNOHANG
option to see if it has exited, or omit the WNOHANG if you simply
want to sit until it terminates...

Personally, I normally use vfork() for this, but under linux, with its
copy-on-write memory management, vfork() doesn't actually exist, and is
an alias for fork().

--
Robert Hyatt                    Computer and Information Sciences

(205) 934-2213                  115A Campbell Hall, UAB Station
(205) 934-5473 FAX              Birmingham, AL 35294-1170

 
 
 

system() , exec(), pid_t and wait() question ?

Post by Robert Hyat » Mon, 22 Jun 1998 04:00:00



:   Thanks for your help , but there still have a question : how can I get the
: pid ?
: from your code I know I can wait till a process finished . And if I want start
: the process and get the pid of it  , how can I do that ?
:   I don't understand  what fork() can do .Pls help me out.

: Kevin

See my previous post.  fork() returns the pid of the newly created
process.  waitpid() can be used to either wait on this pid, or to simply
check to see if it has exited without* if it hasn't...

--
Robert Hyatt                    Computer and Information Sciences

(205) 934-2213                  115A Campbell Hall, UAB Station
(205) 934-5473 FAX              Birmingham, AL 35294-1170

 
 
 

system() , exec(), pid_t and wait() question ?

Post by david parso » Tue, 23 Jun 1998 04:00:00




Quote:>Hi,
>    I'm using `system("xterm&");  ` to start an xterm in my program, and
>I want get the new program's pid , so I can check if it has exited. How
>can I get the pid of it in my program ?

>pid=system("xterm&"); //????

     if ((pid = fork()) > 0)
        /* I'm the parent; pid is the pid of the child */
    else if (pid == 0) {
        /* I'm the child, launch Xterm */
        execl("wherever.xterm.lives", "xterm", 0);
        perror("xterm");
        exit(0);
    }
    else
        /* can't fork */
        perror("fork");

                  ____
    david parsons \bi/ Shouldn't this be in comp.unix.programming.without.posix?
                   \/