Getting pid of a process!!
Hi to all,
I would like to know if there's a way to get the pid of a process by using it's name. Also, how can I know of the processes that are running and their pids? Warm regards, Visham |
Code:
ps ax | grep [process name] the first number on the left is the pid |
On Slackware, there is a /sbin/pidof which returns the pid of a program.
|
Hi,
many thx for the reply... But what command do I use for getting the pid of a process, which i know by name, in a C program? Warm regards, Visham |
Here's one way, a rewriting of Solaris pgrep/pkill commands to Linux:
http://cvs.sourceforge.net/viewcvs.p...19&view=markup |
Hi,
I found sth called pidof...it's a shell script command that returns the pid of a process when you know its name. So in order to use it this command in a C program, we can use the system command: void main() { ... system("pidof -s name_of_process"); ... } Thx to all for ur input.. Warm regards, Visham |
Calling a shell script from a C program instead of using the C APIs defeats some or all of the interest of writing a C program vs a shell script.
Also, if you still want to use pidof instead of what I sent you (pgrep C source code), you'd rather use popen() instead of system() to get pidof output. |
Hi,
I did look at the pgrep.c file...but i'm araid i don't know hw to include it in my code..i'm actualy modifying iptables kernel code, where I need to find the pid of another process. I found it easier to write just one line in the existing code rather than having to paste the source code of pgrep.c. i'm using RH9 by the way. Can you suggest me a way of how to use prep.c while causing minimal intrusion in the existing iptables kernel code? I can't use makefiles.. Warm regards, Visham |
Hi,
I did look at the pgrep.c file...but i'm araid i don't know hw to include it in my code..i'm actualy modifying iptables kernel code, where I need to find the pid of another process. I found it easier to write just one line in the existing code rather than having to paste the source code of pgrep.c. i'm using RH9 by the way. Can you suggest me a way of how to use prep.c while causing minimal intrusion in the existing iptables kernel code? I can't use makefiles.. Also, can you tell me why you suggest the use of popen(). CAn you give me an example of how to use it? Warm regards, Visham |
Are you really wanting to call a shell script from kernel code ???
This is close to impossible, and reusing pgrep code in such a situation is at least challenging here too. Kernel code cannot make use of system calls or libc, although part of libc can be implemented in the kernel itself. |
open-heart surgery using a chainsaw
;) |
use getpidbyname()
Hi
i'm also new to linux but if you are using c and want to know pid of a process then you can use system call getpidbyname(). |
I doubt any operating system provides a "getpidbyname()" system call.
|
Hi to all,
I'm trying to call a .c program (Prog1.c) from another .c program (Prog2.c). Prog1.c is actually part of the iptables kernel code. I'm only modifying the file. I'll need to recompile the kernel. Prog2.c is a userspace program that i will write entirely. Prog1.c will send a signal to Prog2.c every time a packet passes through the code of Prog1.c. But in order to send the signal to Prog2.c, Prog1.c needs to know the pid of Prog2.c and then send it. Do you think it' the best way to do so... Warm regards, Visham |
Quote:
Hint: http://www.kernelnewbies.org/faq/index.php3#library |
All times are GMT -5. The time now is 09:51 PM. |