LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 08-29-2003, 11:53 AM   #1
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
Find out wat current processes are ??


How do I get to know all the processes that are running at the current instance....I know that the command "ps" does that thing.
But , I am trying to write a program that would mimic "ps" and so, I would be needing some function .
I thought that accessing files at the directory "/proc" n checking the files for their group n user i.ds would do . But I haven't been able to figure out how to find it ... Maybe u could help me with this or if their is a better way by which I can get the pids it would be fine.
Mind u I am doing this using C/C++ n so no shell programming plz..[ like use "ls -l " n then cut fields 3 and 4 to get it...]
Please help me with this.

Last edited by SaTaN; 08-30-2003 at 02:11 AM.
 
Old 08-29-2003, 12:35 PM   #2
LinuxTiro
Member
 
Registered: Aug 2003
Posts: 59

Rep: Reputation: 15
How abt accessing the directories in /proc using scandir .You can use stat to find if it is a directory...Then u can descend into each directory n then check for a file named environ which would start
with USER=<user name> . U'll not be able to access other users environ files unless u r the root....Then compare the <user name>
with ur current user name n the directories in which the environ file has a match are the required processes
 
Old 08-29-2003, 12:46 PM   #3
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Original Poster
Rep: Reputation: 32
Thatz Ok but that a bit tedious to check all the directories in the /proc directory . If there were to be a single function which would return the pids it would be much more easier.

Anyway thanx hitler
 
Old 08-29-2003, 06:08 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by SaTaN
Thatz Ok but that a bit tedious to check all the directories in the /proc directory . If there were to be a single function which would return the pids it would be much more easier.
I think there's no other straight-forward way to do it. "ps" itself works this way. Anyways, you don't need to read the subdir's in /proc one-by-one yourself if you use the scandir() function.

I hope this does more or less what you're looking for:
Code:
/* Based on example in "man scandir" */

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <fnmatch.h>

void processdir(const struct dirent *dir)
{
     /* Just print the name in this case */
     /* You can open and parse the files */
     /* in dir to access all available   */
     /* info about the process.          */
     puts(dir->d_name);     
}

int filter(const struct dirent *dir)
{
     return !fnmatch("[1-9]*", dir->d_name, 0);
}

int main() {
     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;
}

Last edited by Hko; 08-29-2003 at 06:14 PM.
 
Old 08-29-2003, 10:19 PM   #5
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Original Poster
Rep: Reputation: 32
Hey but this would be giving all the files in /proc . How do I check whether they belong to me or belong to a different user.....As u know, ps would give only the processes which are run by the current user
 
Old 08-29-2003, 10:22 PM   #6
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Original Poster
Rep: Reputation: 32
Now all I need is given a file how do we find the group and user i.d of that file ? [ The 3rd n 4th fields of "ls"]
 
Old 08-29-2003, 11:13 PM   #7
LinuxTiro
Member
 
Registered: Aug 2003
Posts: 59

Rep: Reputation: 15
i think u can open the environ file and then use the contents given it that file for getting the user id.
 
Old 08-29-2003, 11:24 PM   #8
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Original Poster
Rep: Reputation: 32
Hitler thatz not the problem....
The problem is that the files in the /proc are the pids of all the users... I need to find only those processes which belong to me and so, I would need to find the user information of that particular file
 
Old 08-29-2003, 11:32 PM   #9
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Original Poster
Rep: Reputation: 32
In the post Hko had sent there was a function processdir() which I changed this way....

void processdir(const struct dirent *dir)
{
FILE *fptr;
char string[100];
puts(dir->d_name);
if(chdir(dir->d_name)==0)
printf("Couldn't change dir\n");
if((fptr=fopen("environ","r"))!=NULL)
{
fscanf(fptr,"%s",string);
printf("%s \n",string);
fclose(fptr);
}
else
printf("Couldn't open file\n");
}

It always prints Couldn't open files...
Mind I amn't programming as the "root" but as a normal user.

Can u plz show me wat the error is ???
 
Old 08-29-2003, 11:38 PM   #10
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
Quote:
Originally posted by SaTaN
Hitler thatz not the problem....
The problem is that the files in the /proc are the pids of all the users... I need to find only those processes which belong to me and so, I would need to find the user information of that particular file
check the permissions of the directories...3 and 4 columns of ls -l
 
Old 08-29-2003, 11:42 PM   #11
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Original Poster
Rep: Reputation: 32
Thatz exactly what I didn't want .......Plz check my first post. I am doing it in C
So, I will not be using shell programming anywhere . All I need is a function to find it out.
 
Old 08-29-2003, 11:42 PM   #12
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Original Poster
Rep: Reputation: 32
Thatz exactly what I didn't want .......Plz check my first post. I am doing it in C
So, I will not be using shell programming anywhere . All I need is a function or something else to find it out.
 
Old 08-29-2003, 11:43 PM   #13
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
Quote:
Originally posted by SaTaN
Thatz exactly what I didn't want .......Plz check my first post. I am doing it in C
So, I will not be using shell programming anywhere . All I need is a function or something else to find it out.
Advanced programming in UNIX environment by Richard Stevens. check out the chapter on files and directories I think it's the 4th chapter.
 
Old 08-29-2003, 11:52 PM   #14
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Original Poster
Rep: Reputation: 32
Quote:
Originally posted by vanquisher
Advanced programming in UNIX environment by Richard Stevens. check out the chapter on files and directories I think it's the 4th chapter.
Maybe u could tell me the function so that I could use it by checking the man pages....I don't have a programming in UNIX enivornment nearby right now.

Anyway thanx

Last edited by SaTaN; 08-30-2003 at 01:28 AM.
 
Old 08-30-2003, 01:21 AM   #15
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
Quote:
Originally posted by SaTaN
Anyway thanx punk
Looks like you feel a lot lucky today...well, do ya? edit that post and no personal matters here...stick to the subject and answer the posts. I'm not in a habit of starting or getting into flame wars. Behave properly.

sorry everybody...hope this never happens again.

Last edited by vanquisher; 08-30-2003 at 01:24 AM.
 
  


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
Any way to view the current processes? alyks Suse/Novell 7 10-18-2004 10:04 AM
monitoring active processes and identifying the required processes. gajaykrishnan Programming 2 08-13-2004 01:58 AM
Too many processes? mooreted Linux - Software 2 04-05-2004 03:27 PM
current Slack-Current giving troubles? r_jensen11 Slackware 5 02-02-2004 05:08 PM
Staying Current (with current) hjles Slackware 1 01-21-2003 07:03 PM


All times are GMT -5. The time now is 11:48 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration