number of processes, how to get notified on a change
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I was wondering whether there is a variable for storing current number of processes on the system.
There is no such a variable. You may learn how many processes in your system are working, by this command: ps axh | wc -l. Of course, you can assign its output to a variable. :-)
Quote:
Also, how can I get notified when a new process enters the system.
Hmm, may I ask why do you want this? Working system is not somethig static, there are a number of processes (even on a home desktop, not only server) which may terminate, spawn child processes, run external commands/utilities etc. Monitoring of these activities can be a liittle bit annoying. ;-)
P. S. Oops, didn't notice that this is "Non-*NIX Forums > Programming". Sorry, mea culpa. :-) This post can be deleted.
Last edited by allez; 07-31-2008 at 10:11 AM.
Reason: An erroneous post
allez makes a good pt, if this is *nix, or (I believe) almost any OS. The num of processes is usually highly dynamic. Embedded systems would be a different kettle of fish
Hi, I am not able to get notified when a new process enters the system. Could any body guide me on how can this be done. Inotify does not work on /proc. I don't want to run a daemon which goes through the list in /proc every few milliseconds to know that new process has come.
Even if I try to see when the fork system call is made, its quite heavy as every time a system call is made, we have to check whether the system call is fork..
It does not matter for me if the number of processes is huge. I wont do much processing when I get notified about the entry of the process. So number of processes is not an issue.
Could anybody please guide me how I could do this. This is required for my term project and I am almost stuck here from 10 days... I know that there are people here who can guide me..
Could anybody please guide me how I could do this. This is required for my term project and I am almost stuck here from 10 days... I know that there are people here who can guide me..
Thanks for any input...
Two things:
1. You did not answer the question which I asked you after your original post. When someone responds with a request for more information, it is good manners to respond to their request.
2. We are not here to do your homework. See the LQ rules. If you can show that you have taken some steps toward finding a solution, and need guidance about what direction to take, most people (including me) are happy to help, but don't expect us to provide you with solutions to your homework questions.
I have not been able to do much as there is nothing else I can do except getting notified about the entry of a process.
I thought going through the entries in /proc very often would result in huge overhead as this has to be done very frequently.( may be 100 millisecods ?...)
So I have been thinking that getting notified about process entry is the only solution.
I am not asking anybody to do my homework. I just wanted to say that I have been helpless is solving the problem for a long time.
There is no such a variable. You may learn how many processes in your system are working, by this command: ps axh | wc -l. Of course, you can assign its output to a variable. :-)
Running this command will generate two processes.
Processes may come and go at the rate of several per second. Being notified about each of these seems like a good way to suck the CPU and bury the I/O channels.
If you really need to do this, you need to create a process that doesn't end, and you probably should do it in C or C++. Easiest way would be to make it sleep for a set time then execute a loop. When it checks for new processes, it should do this without spawning any other processes. To do this, you need to learn about the task structure and how to walk it, because this is where the system stores its task information. Basically, each time through the loop, you walk the entire task list via the links in the task structure, looking for whatever information interests you. You also would presumably count the entries in the list.
Can you explain what purpose there is to being notified about process creation? This seems a rather peculiar quest.
The correct and most efficient way to do this would be to modify the kernel source, adding the necessary code to the code that implements execve(2). Any other polling method in userspace will be unreliable, and very expensive.
For a standard user-space implementation, with notification via signals, there is no guaranty that you will get "notified" on every single execve(2) call. You may get notified only once for more than one new process exec'd. You'd need to implement a user-space kernel daemon.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.