1753781 Members
7444 Online
108799 Solutions
New Discussion юеВ

Re: Few Questions on C++

 
SOLVED
Go to solution
Lalit Seth
Frequent Advisor

Few Questions on C++

Hi,

1) how do i get the PID from a process of which i know the name only

2) how can i send a signal to a process of which I have the PID

Many Thanks
6 REPLIES 6
Lalit Seth
Frequent Advisor

Re: Few Questions on C++

I want to acheive these in C++ Code.
Geetha.R
Occasional Advisor

Re: Few Questions on C++

You can use system command.
system("ps -eaf |cut -1"); (cut to just get the PID column).
I am not sure about the second question.
Lalit Seth
Frequent Advisor

Re: Few Questions on C++

Thanks

This results in printing the output to console. instead is there any way i can store the info in some C++ variable or in structure.
Geetha.R
Occasional Advisor

Re: Few Questions on C++

system("ps -eaf |cut -1 > file1.txt");
you can redirect the output to a file like this & you can read the file & store the PID in a variable.
I am not sure if there are any shortcut methods available for this.
Fred Ruffet
Honored Contributor

Re: Few Questions on C++

A more C/C++ way to do it :

have a look at "man 2 kill" and "man pstat".
"man signal will also help you.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Craig Smith_13
Frequent Advisor
Solution

Re: Few Questions on C++

One way is to use popen() instead:

#include

FILE *fp;
char result[81];


fp = popen(" your command line ");

while (fgets(result, 80, fp) != NULL) {
}