Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

dirent

Posted on 2001-08-02
13
Medium Priority
?
205 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

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.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

886 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