Unix Programming - Creating and killing processes

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2004 > Creating and killing processes





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Creating and killing processes
hepp

2004-09-02, 6:51 pm

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

2004-09-02, 6:51 pm

"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/


Fletcher Glenn

2004-09-02, 6:51 pm



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()? You can always terminate the attached
application using pclose().

--

Fletcher Glenn

Barry Margolin

2004-09-02, 6:51 pm

In article <4134F0F1.7090109@removethisfoglight.com>,
Fletcher Glenn <fletcher@removethisfoglight.com> wrote:

> hepp wrote:
>
> 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, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com