LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2004, 08:35 PM   #1
laikos
Member
 
Registered: Nov 2002
Distribution: Slackware 8.1
Posts: 30

Rep: Reputation: 15
Question Getting PID of running process in C/C++


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.

I am using C, and I wish not to use ps or psof

Thanks
 
Old 03-07-2004, 08:38 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
you want getpid. Read man getpid for more info.
 
Old 03-07-2004, 08:41 PM   #3
laikos
Member
 
Registered: Nov 2002
Distribution: Slackware 8.1
Posts: 30

Original Poster
Rep: Reputation: 15
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
 
Old 03-07-2004, 08:51 PM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
Ah, sorry bout that.

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.
 
Old 03-07-2004, 08:53 PM   #5
laikos
Member
 
Registered: Nov 2002
Distribution: Slackware 8.1
Posts: 30

Original Poster
Rep: Reputation: 15
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.
 
Old 03-08-2004, 01:31 AM   #6
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276

Rep: Reputation: 31
Why don't you get the source code of ps and study this? I believe it's either
in binutils or shutils, google will tell you where to download it.
 
Old 03-08-2004, 02:57 AM   #7
linorg
Member
 
Registered: Nov 2003
Location: delhi
Distribution: redhat 8.0 ,fedora
Posts: 32

Rep: Reputation: 15
why don't u use
execv() function
 
Old 03-09-2004, 01:31 AM   #8
laikos
Member
 
Registered: Nov 2002
Distribution: Slackware 8.1
Posts: 30

Original Poster
Rep: Reputation: 15
Thanks everyone,

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.
 
Old 03-09-2004, 10:40 AM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
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:
Code:
int filter(const struct dirent *dir)
{
     return !fnmatch("[1-9]*", dir->d_name, 0);
}
</edit>

Last edited by Hko; 03-09-2004 at 03:32 PM.
 
Old 03-15-2004, 03:33 AM   #10
laikos
Member
 
Registered: Nov 2002
Distribution: Slackware 8.1
Posts: 30

Original Poster
Rep: Reputation: 15
Thanks HKO,
your code is cool!
 
Old 10-29-2015, 06:05 AM   #11
ash2006
LQ Newbie
 
Registered: Oct 2015
Posts: 4

Rep: Reputation: Disabled
Number of all active processes from /proc

program that will create a none zombie child process to display Number of all active processes.can some one help me with this?
 
Old 10-29-2015, 06:17 AM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 2,873

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
What is a 'none zombie child process'? What is the definition of 'active process'?
 
Old 10-29-2015, 08:39 AM   #13
genss
Member
 
Registered: Nov 2013
Posts: 738

Rep: Reputation: Disabled
man 5 proc
look at the section about /proc/pid/stat
more precisely the state variable in /proc/pid/stat(us)
 
Old 10-29-2015, 12:04 PM   #14
rtmistler
Senior Member
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 3,551
Blog Entries: 10

Rep: Reputation: 1338Reputation: 1338Reputation: 1338Reputation: 1338Reputation: 1338Reputation: 1338Reputation: 1338Reputation: 1338Reputation: 1338Reputation: 1338
Quote:
Originally Posted by ash2006 View Post
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.
 
Old 10-30-2015, 03:23 PM   #15
ash2006
LQ Newbie
 
Registered: Oct 2015
Posts: 4

Rep: Reputation: Disabled
Number of active processes

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.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

void main(){


int pid1,pid2,pid3;
int pfds1[2], pfds2[2];
char foo[4096];

pipe(pfds1); pipe(pfds2);

pid1 = fork();
if(pid1==0)
{
close(1); dup(pfds1[1]);
close(pfds1[0]);close(pfds1[1]);
close(pfds2[0]);close(pfds2[1]);
execlp("/bin/ps","ps","-A",NULL);
}
else
{
pid2 = fork();
if(pid2==0){

close(0); dup(pfds1[0]);
close(1); dup(pfds2[1]);

close(pfds1[0]);close(pfds1[1]);
close(pfds2[0]);close(pfds2[1]);

execlp("/usr/bin/wc","wc","-l",NULL);

}

}
close(pfds1[0]);close(pfds1[1]);
close(pfds2[0]);close(pfds2[1]);

//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);



}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting pid of a process!! vishamr2000 Programming 34 03-12-2015 07:12 AM
Start a Process with dedicated PID murder Linux - Newbie 5 08-15-2005 03:49 PM
how to get more process' info according to its PID? iclinux Programming 1 02-05-2005 05:30 AM
ps showing duplicate process same PID dooahhdoo Red Hat 0 07-09-2004 05:52 AM
Get Next PID of Process Tree zer0python Programming 7 11-26-2003 11:56 AM


All times are GMT -5. The time now is 07:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration