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.
Hi All,
I would like to obtain the process id of runing processes in the system
The shell equivalent of what I'm trying to do is "ps -ef". If there is sample code I would really appreciate it.
Hi jtshaw ,
Man getpid, getppid tells me that it is for the current process. However, what I really want is list of running processes not only the current process
thanks
What I believe the ps function does is reads all the information from /proc. You'll notice a bunch of directories that are numbers. Those numbers are your pids. In each directory you have information about the pids.
Heh,
No problem, well that is what I'm trying to do now but it get so troublesome plus we've to look into each of it to find what argument are passed to the process. I wish there's a smart and fast way of doing it.
I use the method used in ps source. It is a little tedious but it works. execv() is not my option because I'm trying to get the pid of all the process in the system, not only ones started by me.
Maybe you can use this?
I posted it here before some time ago.
It scans /proc for all PID's and prints them. Instead of printing the PID you can
put any code to get more info from the /proc/<PID> directory by changing the processdir() function.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <fnmatch.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
void processdir(const struct dirent *dir)
{
puts(dir->d_name);
}
int filter(const struct dirent *dir)
{
uid_t user;
struct stat dirinfo;
int len = strlen(dir->d_name) + 7;
char path[len];
strcpy(path, "/proc/");
strcat(path, dir->d_name);
user = getuid();
if (stat(path, &dirinfo) < 0) {
perror("processdir() ==> stat()");
exit(EXIT_FAILURE);
}
return !fnmatch("[1-9]*", dir->d_name, 0) && user == dirinfo.st_uid;
}
int main()
{
/* Based on example in "man scandir" */
struct dirent **namelist;
int n;
n = scandir("/proc", &namelist, filter, 0);
if (n < 0)
perror("Not enough memory.");
else {
while(n--) {
processdir(namelist[n]);
free(namelist[n]);
}
free(namelist);
}
return 0;
}
<edit>
This only prints processes owned by the user running this program. If you prefer all processes, you can reduce filter() to just:
program that will create a none zombie child process to display Number of all active processes.can some one help me with this?
When you fork() a child, wait for a termination signal from that child and then the child will not become a zombie. Review the earlier answers regarding how to get your list of all active processes. Note that you've resurrected an 11 year old thread to ask this question, not shown any effort on your part, and should try some programming or scripting efforts first before asking your question.
I have put some effort. And i had this program before posting the question here. But i was not getting the expected output. And i want my question to be short. So i didnt post my program.
//Parent waits for all of its 3 children to finish execution
wait(NULL);wait(NULL);wait(NULL);
int nbytes = read(pfds1[0], foo, sizeof(foo));
printf("Output: (%.*s)\n", nbytes, foo);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.