LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-16-2013, 02:22 PM   #1
plainenough
LQ Newbie
 
Registered: Nov 2013
Posts: 4

Rep: Reputation: Disabled
Question Breakdown of the ps command, more than a working example.


As my first post on this forum I wanted to open up with a clear question. I am curious how ps uses the /proc directory so efficently to gather the relevent information to display processes.

I have seen a lot of posts that seem to ask the same question and are merely answered with, "This is how you use ps.", "This is what it is for.", and my favorite, "It gathers infromation from the /proc directory. Use man proc for more information."

(added) I'm thankful for the answer Posted below by syg00. It has clarified a little more about the reason that there is so little information about how ps is so fast. Looking at the alternatives, I might dive into the source to gain a little more insight.

Now I agree that man proc has a ton of useful information, and per individual process I am able to slightly determine how to gather some of the information I am looking to recreate. The parts that I am missing are:

1) Does ps just look through every pid directory for the information? If so, how is this efficient?

2) Is there a simple breakdown of how it does this task that you can link me to or provide me with? (Conceptually it is all there, I would strace ps but I'm not too clear on how to interpret it.)

3) Ultimately my goal is to recreate ps inside of another script. I would like a fundamental idea of how ps works to obtain the information. better understanding of how I can gather some experience farming information out of proc, does anyone have any ideas of how I could put the man pages to use while looking for information? I just really need some idea like, " Check out this system task and tell me X about it. "

Last edited by plainenough; 11-16-2013 at 03:45 PM. Reason: Asking the correct questions
 
Old 11-16-2013, 02:54 PM   #2
jamison20000e
Senior Member
 
Registered: Nov 2005
Location: ...uncanny valley... infinity\1975; (randomly born:) Milwaukee, WI, US( + travel,) Earth( I wish,) END BORDER$!◣◢┌∩┐ Fe26-E,e...
Distribution: any GPL that works well on my cheapest; has been KDE or CLI but open... http://goo.gl/NqgqJx &c ;-)
Posts: 3,708
Blog Entries: 2

Rep: Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163
Hi sounds interesting, I'm only curious and am on to that man page now. A search for ps source code may help you tho?

Last edited by jamison20000e; 11-16-2013 at 02:55 PM. Reason: spelling
 
Old 11-16-2013, 03:20 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 17,108

Rep: Reputation: 2615Reputation: 2615Reputation: 2615Reputation: 2615Reputation: 2615Reputation: 2615Reputation: 2615Reputation: 2615Reputation: 2615Reputation: 2615Reputation: 2615
/proc is a pseudo filesystem - it exists in kernel cache , and is only populated when it is read. There is no I/O involved - this is presumably where the "efficiently" reference comes from.
strace is indeed the answer to your other queries.
Note that if you plan to do this (iterate over /proc) from a script, it will be much slower. Be judicious if planning to loop over /proc on a timer - processes often disappear before you get around to handling them.

Last edited by syg00; 11-16-2013 at 03:21 PM. Reason: clarification
 
2 members found this post helpful.
Old 11-16-2013, 03:30 PM   #4
jamison20000e
Senior Member
 
Registered: Nov 2005
Location: ...uncanny valley... infinity\1975; (randomly born:) Milwaukee, WI, US( + travel,) Earth( I wish,) END BORDER$!◣◢┌∩┐ Fe26-E,e...
Distribution: any GPL that works well on my cheapest; has been KDE or CLI but open... http://goo.gl/NqgqJx &c ;-)
Posts: 3,708
Blog Entries: 2

Rep: Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163
Cool, another good man page Thx.

Last edited by jamison20000e; 11-16-2013 at 03:55 PM.
 
Old 11-16-2013, 03:30 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392
I have to agree with jamison20000e's mentioning the ps source code as a reference. The details asked for by the OP can only be found there. strace might be a good option if source-code-reading isn't your thing.

Quote:
1) Does ps just look through every pid directory for the information?
Yes it does, I know that much. And it doesn't matter if you execute, for example ps, ps -ef or ps -C process_name. Same principle seems to be at work: Look through all the processes and fetch and display what is wanted based on options used.

EDIT: I hope the OP does realize how many options the ps command has.....

Last edited by druuna; 11-16-2013 at 03:31 PM.
 
1 members found this post helpful.
Old 11-16-2013, 03:49 PM   #6
plainenough
LQ Newbie
 
Registered: Nov 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by druuna View Post
I have to agree with jamison20000e's mentioning the ps source code as a reference. The details asked for by the OP can only be found there. strace might be a good option if source-code-reading isn't your thing.

Yes it does, I know that much. And it doesn't matter if you execute, for example ps, ps -ef or ps -C process_name. Same principle seems to be at work: Look through all the processes and fetch and display what is wanted based on options used.

EDIT: I hope the OP does realize how many options the ps command has.....
Yeah I have been diving into some man pages and articles breaking down ps and its many options. I am just gathering more of a high level view. Quickly learning that maybe the reason I have trouble with this idea is I'm not asking myself the right questions.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Power BreakDown abakhiet Linux - Server 2 05-21-2010 01:16 AM
LAN Breakdown azuiden Linux - Networking 2 11-16-2005 09:27 AM
aMSN breakdown akasantos Fedora 2 04-06-2004 07:38 AM
need a distro breakdown spiderworm Linux - Distributions 5 10-29-2003 01:48 PM
Better breakdown of forums Infinity LQ Suggestions & Feedback 4 10-18-2001 06:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration