Solved

dirent

Posted on 2001-08-02
13
196 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:IainHere
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
After review, the proposed answer will be accepted by:

ComTech
Community Support Admin
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

12 Experts available now in Live!

Get 1:1 Help Now