tags:

views:

1011

answers:

7

Can you help to develop ps command of Linux in C language?

Or is there any reference site which can help me?

+1  A: 

Investigate the linux /proc filesystem. There is s good article on the topic here.

anon
+1  A: 

Could it be that you're looking for this?

soulmerge
+2  A: 

Have you not looked at the source to see how its implemented? I can not hope, in a SO question to explain the format of everything in /proc .. so its highly advisable to study what exists.

This is looking alarmingly like homework. Find the code here, study it .. then feel free to ask questions :)

If you're going to re-do it, please make the arguments make sense:

********* simple selection *********  ********* selection by list *********
-A all processes                      -C by command name
-N negate selection                   -G by real group ID (supports names)
-a all w/ tty except session leaders  -U by real user ID (supports names)
-d all except session leaders         -g by session OR by effective group name
-e all processes                      -p by process ID T  all processes on this terminal     -s processes in the sessions given a  all w/ tty, including other users
-t by tty g  OBSOLETE -- DO NOT USE    -u by effective user ID (supports names) r  only running processes
 U  processes for specified users x  processes w/o controlling ttys     t  by tty
*********** output format **********  *********** long options ***********
-o,o user-defined  -f full            --Group --User --pid --cols --ppid
-j,j job control   s  signal          --group --user --sid --rows --info
-O,O preloaded -o  v  virtual memory  --cumulative --format --deselect
-l,l long          u  user-oriented   --sort --tty --forest --version
-F   extra full    X  registers       --heading --no-heading --context
                    ********* misc options *********
-V,V  show version      L  list format codes  f  ASCII art forest
-m,m,-L,-T,H  threads   S  children in sum    -y change -l format
-M,Z  security data     c  true command name  -c scheduling class
-w,w  wide output       n  numeric WCHAN,UID  -H process hierarchy

Yeah, we need those options .. without confronting a textual angry fruit salad :)

Tim Post
+5  A: 

http://procps.sourceforge.net/ has the source code for the version of ps used on Linux systems. Examining it may be useful for you.

Lars Wirzenius
+2  A: 

You could parse the /proc file system to find out the list of running processes.

Check out the code for procps. That should give you some idea of how to do this.

trex279
+2  A: 

It helps to look into the sequence of syscalls done by those programs. Use strace to do that. I think what ps does is crawling through /proc, gathering all the processes and their information.

Have a look at the site of procps, download its code and look how they did it.

Johannes Schaub - litb
+2  A: 

Since the way how ps is retrieving information follows no standard (actually it does so in a different kind of way for every operating system family) and as this way is subject to change with every new release in every new OS... if you need the information ps provides, you should consider to actually just call ps and parse its output. Yes, I know working in such a way seems ugly and not professional, but it works quite lovely for many applications, it's actually pretty cross platform, and it's more unlikely that the output format of ps changes in the future (what would break your parser) than it is that the way how ps retrieves its information might change.

Mecki