ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.