LinuxQuestions.org
Help answer threads with 0 replies.
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
 
Search this Thread
Old 11-24-2007, 12:47 PM   #1
mcnkevin
LQ Newbie
 
Registered: Nov 2007
Posts: 3

Rep: Reputation: 0
need help to iterate through PID's


Hi, I need to make a process that implements a simpler version of PS.

Basically, I need it so that it runs through all the PID's in the /proc file system and gets the CMD names like it does in ps.

How would i do this in C. (I have a file that is created in the /proc file system using functions from proc_fs.h. On every read of that file I want it to show all the PID's and their CMD Names).

Ive been searching around and it seems

find_task_by_id(pid)

Will get me the task_struct of a particular PID, which i can then get the CMD name from.

However how can I iterate through the PID's in the proc filesystem.

Thanks for any help.
 
Old 11-24-2007, 01:46 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I would imagine that it would be better to make direct syscalls rather than go through the proc filesystem, since the proc filesystem is not available on all systems. It will also likely be faster.

Incidentally, what could be simpler than using ps?
Code:
ps ax -o pid,cmd
If you want to know how ps does it, simply look at the source for that program.
 
Old 11-24-2007, 02:35 PM   #3
mcnkevin
LQ Newbie
 
Registered: Nov 2007
Posts: 3

Original Poster
Rep: Reputation: 0
This is in kernel code tho, and thus i need to go through the proc filesystem.

How do i do basic directory and file searching in kernel code?
What files do i need to include to implement functions such as

opendir/readdir and use the DIR filetype.

Why wont it allow me include stdio.h / dirent.h /stdlib.h?

Last edited by mcnkevin; 11-24-2007 at 02:36 PM.
 
Old 11-24-2007, 02:56 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I was curious about how ps is running, so I ran strace with it, and it looks as though it is using the proc filesystem itself. I did not expect it.
 
Old 11-24-2007, 04:07 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by mcnkevin View Post
Why wont it allow me include stdio.h / dirent.h /stdlib.h?
Those are headers associated with libc and to use their functions, programs need to link to libc. Kernel code can't link to user-space libraries: it must do everything using system calls and the kernel interface. The system calls involved in using /proc would probably be a lot more work than figuring out how to do it straight through the kernel interface. Look at /usr/include/asm/unistd.h for a list of system calls on your system.
ta0kira
 
Old 11-24-2007, 05:12 PM   #6
mcnkevin
LQ Newbie
 
Registered: Nov 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Ive been looking at proc_fs.h and I think i can do most of what i need to in there:

Although im not sure how to implement it..

but:

Would using proc_readdir(struct file *, void *, filldir_t); in a loop allow me to go through all the proc_entries in the proc filesystem?

Although it seems this returns an int. If i could somehow loop through the entries and get a handle for each proc_dentry struct and then I could get the proc_inode from that and then get the pid from that.

Just wondering if proc_readdir is the right thing to use here and if so how do I implement it. If not are their any other methods in proc_fs.h that do the job? Thanks.

Btw quicklink to proc_fs.h here : http://lxr.linux.no/source/include/linux/proc_fs.h
 
Old 11-24-2007, 05:57 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I think proc_fs.h is the right way to go (as opposed to the actual /proc directory.) I don't know much about it, but I do know it's a kernel interface, so you won't have to link to a library. You'll just have to make sure procfs is enabled in the kernel.
ta0kira
 
Old 11-24-2007, 06:08 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,487

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
Huh .... ???
Kernel-space iterating over /proc ???. WTF ???
That's user-space thinking.

task_struct is a linked list. current points to the current task (maybe init, but who cares) - just walk the list.

*Then* use your /proc/whatever to expose the results to user-land.
 
  


Reply

Tags
process, ps


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache - httpd service has to many PID's eXtremer Linux - Server 5 11-16-2007 08:18 AM
Bots without PID's, how to kill em? genderbender Linux - General 3 05-23-2007 10:35 PM
named running as 6 pid's ?!?! emersony Linux - Networking 2 11-18-2006 02:47 PM
c: iterate an array of structs kpachopoulos Programming 2 08-15-2006 12:06 PM
Forking, Spawning, and those pesky PID's k1mgy Programming 4 12-13-2004 07:23 AM


All times are GMT -5. The time now is 11:52 PM.

Main Menu
Advertisement
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