Solved

dirent

Posted on 2001-08-02
13
197 Views
Last Modified: 2010-04-02
Hi

Can anyone explain me how to read the process ID from the  Unix struct dirent (found in man dirent).

Thank,

Liron
0
Comment
Question by:lirong
13 Comments
 
LVL 4

Expert Comment

by:IainHere
ID: 6344039
Do you want the PID of the calling proces?  If so, use the getsid() function (also in man pages).  I can't see how you'd get it from dirent - you could find everything in /proc, but why would that be useful?
0
 
LVL 2

Expert Comment

by:sateesh_babu
ID: 6344122
I have go thru the man page for dirent, and I find that there is no entry to get the process id. dirent is a structure that holds directory entries, and not related to processes. What does your question actually relate to?
Babu
0
 

Author Comment

by:lirong
ID: 6344209
Know. I'm want to know (from a C app)a process ID.

Do you have another way to do so ?

Liron
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6344218
Yes. getpid() will give you the current process's ID (rather than getsid(), sorry).  getppid() will get the parent's ID.
0
 

Author Comment

by:lirong
ID: 6344232
But I don't want to know my PID I wish to know another process PID. for example I'm running process X and with to know process Y pid.
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6344244
If you're trying to get the PID of a different process, there is no standard way of doing it.  Since you can have more than one process with a particular name, there can be no function that will return a PID when given an application's name.

You could (if your flavour of unix provides one) trawl through the /proc directory, which contains all of the PIDs as filenames, open each and query them.  This is generally not a good solution, since you should restrict /proc to superuser only (imagine rm -r /proc, which a colleague of mine did on a DEC Alphaserver, which at the time was the interface to a power station's control system)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:IainHere
ID: 6344250
from http://www.npl.washington.edu/faq/unix-FAQ-3-10.html


      However, some vendors provide functions for reading Kernel
      memory; for example, Sun provides the "kvm_" functions, and Data
      General provides the "dg_" functions.  It may be possible for any
      user to use these, or they may only be useable by the super-user
      (or a user in group "kmem") if read-access to kernel memory on
      your system is restricted.  Furthermore, these functions are
      often not documented or documented badly, and might change from
      release to release.

0
 

Author Comment

by:lirong
ID: 6344260
I know that I can see the process list from /proc. That is why I want to use opendir function and dirent structure. Do you know how can I know what is the process ID using dirent structure ?
0
 
LVL 4

Accepted Solution

by:
IainHere earned 150 total points
ID: 6344269
Are you creating process Y with fork()?  If so, the return value will be the PID.

The PID is the name of the file.  That is stored in dirent::d_name.
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6344285
Go to this page
http://docs.linux.cz/c-faq_comp.unix.programmer/faq.txt

and find the text "Reading the process table using popen and ps"

There are examples of how to do it the way you are describing, but popen is better.  Will this solve your problem?
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6362908
lirong,

Did you solve your problem?  If not, you should continue to ask here for help.  If you did, and any of the above was of use, please accept one of the comments as an answer.  If none of the comments were useful, you can delete the question.

Thanks.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6842554
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "IanHere" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

lirong
You have several open questions that need your attention. Please take some time, look into your profile/Question History and either close some of these or give some feedback to the experts.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 

Expert Comment

by:ComTech
ID: 6858037
After review, the proposed answer will be accepted by:

ComTech
Community Support Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now