Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating and killing processes

5 views
Skip to first unread message

hepp

unread,
Aug 31, 2004, 1:47:04 PM8/31/04
to
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?

Kieran Simkin

unread,
Aug 31, 2004, 2:24:09 PM8/31/04
to
"hepp" <ovesve...@hotmail.com> wrote in message
news:cf221c81.04083...@posting.google.com...

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/


Fletcher Glenn

unread,
Aug 31, 2004, 5:43:15 PM8/31/04
to

Have you looked at popen()? You can always terminate the attached
application using pclose().

--

Fletcher Glenn

Barry Margolin

unread,
Sep 1, 2004, 12:42:19 AM9/1/04
to
In article <4134F0F1...@removethisfoglight.com>,
Fletcher Glenn <flet...@removethisfoglight.com> wrote:

> hepp wrote:
> > 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?
>
> Have you looked at popen()?

He said "by using the popen C function", so obviously he has.

> You can always terminate the attached
> application using pclose().

No it doesn't. pclose() *waits* for the process to terminate, it
doesn't kill it.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

0 new messages