LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-04-2005, 12:47 PM   #1
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Rep: Reputation: 31
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
 
Old 09-04-2005, 12:53 PM   #2
UnderDark
Member
 
Registered: Aug 2005
Location: USA.Michigan
Distribution: Slackware 10.1
Posts: 34

Rep: Reputation: 15
Code:
ps ax | grep [process name]
will search running running programs for the [process name].

the first number on the left is the pid
 
Old 09-04-2005, 04:09 PM   #3
Slackwise
LQ Newbie
 
Registered: Sep 2005
Location: Chicago, IL
Distribution: Slackware
Posts: 1

Rep: Reputation: 0
On Slackware, there is a /sbin/pidof which returns the pid of a program.
 
Old 09-05-2005, 01:01 AM   #4
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Original Poster
Rep: Reputation: 31
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
 
Old 09-05-2005, 01:38 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
Here's one way, a rewriting of Solaris pgrep/pkill commands to Linux:
http://cvs.sourceforge.net/viewcvs.p...19&view=markup
 
Old 09-05-2005, 08:18 AM   #6
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Original Poster
Rep: Reputation: 31
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
 
Old 09-05-2005, 09:10 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
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.
 
Old 09-06-2005, 05:20 AM   #8
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Original Poster
Rep: Reputation: 31
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
 
Old 09-06-2005, 05:25 AM   #9
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Original Poster
Rep: Reputation: 31
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
 
Old 09-06-2005, 09:21 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
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.
 
Old 09-06-2005, 09:47 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
open-heart surgery using a chainsaw
 
Old 09-06-2005, 12:17 PM   #12
Pratik H Pandya
LQ Newbie
 
Registered: Jun 2005
Location: India
Distribution: RedHat 9.0,Fedora core 3
Posts: 22

Rep: Reputation: 15
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().
 
Old 09-06-2005, 02:53 PM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
I doubt any operating system provides a "getpidbyname()" system call.
 
Old 09-07-2005, 04:22 AM   #14
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Original Poster
Rep: Reputation: 31
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
 
Old 09-07-2005, 06:11 AM   #15
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
Quote:
Prog1.c will send a signal to Prog2.c every time a packet passes through the code of Prog1.c.
How do you plan to send a signal to Prog2 from kernel code ?

Hint: http://www.kernelnewbies.org/faq/index.php3#library
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Start a Process with dedicated PID murder Linux - Newbie 5 08-15-2005 03:49 PM
how to get more process' info according to its PID? iclinux Programming 1 02-05-2005 05:30 AM
Getting PID of running process in C/C++ laikos Programming 9 03-15-2004 03:33 AM
Get Next PID of Process Tree zer0python Programming 7 11-26-2003 11:56 AM
How to find the pid to stop a process.... vous Linux - Networking 6 08-14-2003 04:18 AM


All times are GMT -5. The time now is 08:51 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration