Re: Creating and killing processes

From: Kieran Simkin (kieran_at_digital-crocus.com)
Date: 08/31/04


Date: Tue, 31 Aug 2004 18:24:09 GMT


"hepp" <ovesvenssons@hotmail.com> wrote in message
news:cf221c81.0408310947.376d7dae@posting.google.com...
>I am writing a Unix GUI application in C++ that needs to start a
> process by executing a system command, read the output from this
> process and be able to terminate it whenever the user wants to.
>
> First I tried to open a pipe to the process by using the popen C
> function and get hold of the pid by executing some kind of "ps" system
> command, but the problem is that I start a Java process and there are
> many other Java processes running on the same machine so I can't
> figure out which one is which.
>
> Is there any way to start a process and get both the pid and the
> output stream?

See the code below. Use pipe() and/or freopen() to replace the child's i/o
streams before you exec*(). If you don't want to wait() on the child, you
can usually let the OS know about this using signal(SIGCHLD, SIG_IGN).

Hope this helps.

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

int main (void) {
    pid_t pid;

    pid=fork()
    if (pid==0) {
        /* child */
        /* replace stdin, stdout and stderr with a pipe you have previously
setup here
            and clean up the environment for security's sake */
        execlp(); // execute whatever you want to execute

    } else if (pid != -1) {
        /* parent */
        printf("child pid is%i\n",pid);
        while (wait(NULL) != pid) {
            /* wait() on child */
        }
    } else {
        printf("Failed to fork\n");
        return(1);
    }
    return(0);
}

~Kieran Simkin
Digital Crocus
http://digital-crocus.com/



Relevant Pages

  • Re: Creating and killing processes
    ... >> process by executing a system command, ... >> process and be able to terminate it whenever the user wants to. ... but the problem is that I start a Java process and there are ... > application using pclose(). ...
    (comp.unix.programmer)
  • Creating and killing processes
    ... process by executing a system command, ... function and get hold of the pid by executing some kind of "ps" system ... but the problem is that I start a Java process and there are ... Is there any way to start a process and get both the pid and the ...
    (comp.unix.programmer)
  • Re: Creating and killing processes
    ... > process by executing a system command, ... > process and be able to terminate it whenever the user wants to. ... but the problem is that I start a Java process and there are ...
    (comp.unix.programmer)
  • Re: dynamic arrays for reading file 2
    ... to speak of the fact that the exit status of wc isn't normally available ... the system command tends to be that of the shell instead of that of the ... commands executed by the shell). ... For unix/C, if I wanted to use wc, I would pipe the output with ...
    (comp.lang.fortran)
  • pipes problem
    ... The follow sets $stat to 1, ... # $stat = close PIPE; ... the exit sataus of the child process is haaarvested by the parent ... My goal here is to execute a system command and be able to capture the ...
    (comp.lang.perl.misc)