top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the best way to get PID of all processes in C?

+2 votes
719 views

I know, we can get the PIDs of all the running processes using,

ps ax | awk '{print $1}'

I want to know 2 things,
1. Is it a best way? (If not then what is the best way to get it?)
2. Will it work on all the distribution of Linux? (Ex. Ubuntu, RHEL, SLES, etc etc)

posted Aug 11, 2016 by Chirag Gangdev

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
I minor correction in your question. print $2 will show the process id. print $1 will show list of UID (user id) not the PID (process id).

1 Answer

0 votes

Not sure about your requirement but in linux we have two commands pidof and pidstat. pidof is used to get the details of a specific process and pidstat can list the statistics of all process including pid.

Let me know if looking something different..

answer Aug 11, 2016 by Salil Agrawal
I want PID of all processes and then i have to perform some task using it,
Yes thats true, pidstat will give you 4-5 parameters and you need to pick only 1 that is PID. Not sure if there is some option to do that in the pidstat (check man page)
Ok Thank you Sir,
1 more things,
Output of PIDSTAT will be same for all distribution of linux?
I think so, but not tested...
Ok Thank you sir,
One more question i wanted to ask,
In Linux, Under '/proc/' all PIDs will be of all Kernel Processes and User Processes, Right?
Is there any way to Distinguish them?
Ask a fresh query so that it would be helpful to other people in future.
Sure Sir,
Thank you
Similar Questions
+1 vote

Want to know how to represent a Graph (V,E) in C/C++.

+3 votes

Is there any way to get the original source code from binary ?

+1 vote

I know,
Proc file system will have all the PIDs of all processes, I want to distinguish kernel processes and user processes?
Can anyone help?

...