1

I am working with the linux kernel. In my userspace (Simple C program file) I need to make an array that is equal in size to the number of processes running. Inside of my C program main function, how do I know the number of processes running?

I know that in kernel space, I would use the macro "for_each_process(task)" but this is strictly a kernel space macro. I need something similar for userspace. Advice?

  • 5
    I think this is already answered somewhere, but you could either count the entries in /proc or do a system call on 'ps -ef | wc -l' or some such – visibleman Mar 19 '19 at 00:24
  • You are welcome, note that the duplicate questions accepted answer actually lists a third method (sysinfo.h) that was not mentioned in my comment. – visibleman Mar 19 '19 at 01:12
  • 1
    Possible duplicate of [Is there a lightweight way to obtain the current number of Processes in Linux?](https://stackoverflow.com/questions/8141913/is-there-a-lightweight-way-to-obtain-the-current-number-of-processes-in-linux) – visibleman Mar 19 '19 at 01:13
  • 2
    Possible duplicate of [Get process list from kernel space in Linux](https://stackoverflow.com/q/13340672/608639), [Kernel module that iterates over all tasks using depth first tree](https://stackoverflow.com/q/19208487/608639), [Make a system call to get list of processes](https://stackoverflow.com/q/18408766/608639), [Linux API to list running processes?](https://stackoverflow.com/q/939778/608639), etc. – jww Mar 19 '19 at 01:20
  • @JohnDoe998, The barrage of duplicate comments/flags might seem a bit discouraging for a new user, but you should just view it as SO trying to keep itself tidy and lean. Consider removing the question, if you feel the duplicates answers your question. If not, then edit your post to state the reasons. – visibleman Mar 19 '19 at 01:40

0 Answers0