Re: Creating and killing processes
From: Kieran Simkin (kieran_at_digital-crocus.com)
Date: 08/31/04
- Next message: James Antill: "Re: Large File Support Standards"
- Previous message: Andre Majorel: "Re: Xah Lee's Unixism"
- In reply to: hepp: "Creating and killing processes"
- Next in thread: Fletcher Glenn: "Re: Creating and killing processes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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/
- Next message: James Antill: "Re: Large File Support Standards"
- Previous message: Andre Majorel: "Re: Xah Lee's Unixism"
- In reply to: hepp: "Creating and killing processes"
- Next in thread: Fletcher Glenn: "Re: Creating and killing processes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|