How to get how many processes running on a Linx machine?
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.
How to get how many processes running on a Linx machine?
Hi! All:
I know that /proc/ provides one dirctory to store the useful information for each process! But is there any way to get how many processes currently running on a machine, and get a list of pid? Thank you very much!
Thank you very much! But I just hope I can write a program to minitor the information for the processes such like "top" . I just wanna know there is any API wich can get how many processes running on a linux machine and get the pid list! Thanks!
I believe programs like ps and top simply view the contents of the /proc directory. The directories that are pure numbers are all the running process id.
Originally posted by fortezza What if you are just looking to see if a particular process is running ( via C/C++ ) by name, how could that been done?
Changed the code itsme86 posted to do that:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
int checkexe(char *pid, char *searchexe)
{
int len;
char *exefile;
char exepath[PATH_MAX];
char symlink[PATH_MAX];
/* Symlink /proc/<pid>/exe points to the executable file
* which is running in the process.
*
* Resolve the symlink, remove the directory part of the path,
* and compare that to the exe-name we are looking for.
*/
snprintf(symlink, PATH_MAX, "/proc/%s/exe", pid);
len = readlink(symlink, exepath, PATH_MAX);
/* Catch some readlink() errors: */
if (len >= PATH_MAX) {
fprintf(stderr, "Path for pid #%s too long. Truncated.\n", pid);
return 0;
}
if (len < 0) { /* Often harmless error, e.g. "permission denied". */
return 0; /* No error message, just returning false silently here. */
}
exepath[len] = '\0';
/* Skip directory part of the path to get to the filename */
exefile = strrchr(exepath, '/');
if (exefile == NULL)
exefile = exepath;
else if (++exefile == '\0')
exefile = exepath;
/* Compare the exe-name searching for with the file-name linked to
* by /proc/<pid>/exe.
*/
return !strcasecmp(exefile, searchexe);
}
int is_all_digits(char *str)
{
while (*str) {
if (!isdigit(*str))
break;
str++;
}
return *str ? 0 : 1;
}
int main(int argc, char *argv[])
{
DIR *dir;
struct dirent *dent;
struct stat stbuf;
char procpath[PATH_MAX];
if (argc != 2) {
fprintf(stderr, "Usage: %s <process executable name>\n\n", *argv);
exit(EXIT_FAILURE);
}
if (!(dir = opendir("/proc"))) {
perror("opendir()");
exit(EXIT_FAILURE);
}
while ((dent = readdir(dir))) {
if (!is_all_digits(dent->d_name))
continue;
snprintf(procpath, PATH_MAX, "/proc/%s", dent->d_name);
if ((stat(procpath, &stbuf)) == -1) {
perror("stat()");
continue;
}
if (S_ISDIR(stbuf.st_mode))
if (checkexe(dent->d_name, argv[1]))
printf("%s is running in PID #%s.\n", argv[1], dent->d_name);
}
closedir(dir);
return EXIT_SUCCESS;
}
It checks the /proc/<pid>/exe symlinks for the name. It works well, but now I started thinking it may be better to read the /proc/<pid>/cmdline files to check for the name, because non-privileged users more often have permissions to read /proc/<pid>/cmdline than to resolve the /proc/<pid>/exe symlink.
OTOH, I think /proc/<pid>/exe may be more accurate.
The overrall goal was to write a C++ program that could detect if a process was running and start it in the correct display if it wasn't.
Here is what just got working about 20 minutes ago. It needs some cleanup/commenting/error checking, but for now it gets the job done. The hard part was starting the process and then exiting, since every C++ function to start a process insists on waiting the the process to end. My workaround was to start the process under a thread, detach the thread, wait for the process to be in the process list, and then exit the main program. My compiler won't let me terminate the program until the child process is closed, but from the command line it works fine ( starts the external program and exits ).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.