Creating and killing processes
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Creating and killing processes




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Creating and killing processes  
hepp


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-02-04 11: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?





[ Post a follow-up to this message ]



    Re: Creating and killing processes  
Kieran Simkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-02-04 11: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/







[ Post a follow-up to this message ]



    Re: Creating and killing processes  
Fletcher Glenn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-02-04 11: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






[ Post a follow-up to this message ]



    Re: Creating and killing processes  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-02-04 11: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 ***





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:58 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register