-1

I am going to make program to get process informaition(pid, process name, virtual memory, physical memory ) using procfs. and i want print like this

PID Name Virtual physical shared
1    init 1234    123       22
33   firefox 33    33       2 

I think I can get the process information through proc/[pid]/stat I think I should know the pid if i want to get process information. but the problem is how can can I get the pid only given the process name.

because process pid is changed after reboot or after terminated and restart.

so my question is Is there a way to get the pid if I only know process name? not using shell script

is there file in proc folder that contains all of processse running now? suppose that I only know process name( ex) firefox), I want to get pid using proc and proc filesystem

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
jijijijiji
  • 1,038
  • 11
  • 17
  • See the program called `pgrep` – Gil Hamilton Jun 05 '15 at 13:12
  • I should make program without shell script – jijijijiji Jun 05 '15 at 13:23
  • `pgrep` isn't a shell script, nor do you need the shell to invoke it or retrieve its output. However, if you decide you do wish to reinvent that wheel, see @EricRenouf's answer below. – Gil Hamilton Jun 05 '15 at 13:36
  • I could understand not using `pgrep` due to portability constraints. But why not `ps`? It is POSIX-required making it MORE portable than doing it yourself with `/proc/`. Unless this is a learning exercise, you are wasting your time..... – Brian McFarland Jun 05 '15 at 14:02
  • this is assignment from school. I have to print 3 process information every seconds. and what I want to know is Is there a file in proc folder that contains informations of all process running now – jijijijiji Jun 05 '15 at 14:21
  • what I want to know is Is there a file in proc folder that contains informations(especially process name and pid matched with process name) of all process running now. If there is file like that, I could read file and by using pid matched with process name access proc/[pid] folder. then I think I could get the informaition of process( virtual memory, physical memory) – jijijijiji Jun 05 '15 at 14:29

1 Answers1

1

If you can't use a tool like pgrep you could look in all the /proc/<pid> directories and looking at the exe link in each to find the one that points to the executable you want. Or you could look at cmdline in each if that helps instead.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67