Solved

dirent

Posted on 2001-08-02
13
199 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

820 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