Reputation: 45
I have a process forks a child one that executes an executable program using execve(2) system call
the parent process waits for the child one to finish using wait4(2) system call
what i need is to know the time and the memory used by the the application running by the child process
I used the (rusage) object returned by the wait4(2) system call to get the time value I wanted but after some researches I found that the memory used by the application cannot be achieved by the same (rusage) object since it's became not supported now days in this way
one way to go was to use the /proc pseudo file system and parse the status or statm files in the directory /proc/[pid]
assuming that I wanted to parse /proc/[pid]/status
what I need in this file is the field vmdata that specifies the memory used by data in KB
when I read the file before the child process is finished the field vmdata is not shown "it's missing from the file!!" obviously the process is not finished !! so I would not be able to determine exactly the memory used for the hole work
when I read the file after the child process is finished the directory /proc/[pid] would be removed !! I suggested that when the process is finished, the process data would be released
please !! any idea to get through this problem or any ideas for other ways ?
thanks ...
Upvotes: 0
Views: 373
Reputation: 8563
The VmData field appears to only exist for running processes. I don't know why you decided that it does not. Just run grep VmData /proc/$$/status
and you'll see it for your current shell process.
There are three interesting points in time in your question. The first is while your child process is running. The second is when it is no longer running, but exists as a zombie, and the third is after you have reaped it, and it no longer exists. You seem to have missed out on point #2.
If you want to catch your process while it is in zombie state (i.e. - it has already exited, but not yet reaped) you will need to find a way to get notified that it has finished without calling wait
, which will make it disappear. My recommended way is to register a SIGCHLD
handler. It will get called when your process exists, but will not remove it from the system. At that point in time, /proc/pid/status
will still exist.
Unfortunately for you, VmData
does not exist for zombie processes. Your options are either to periodically sample VmData
, or to stop the process when it is about to terminate, but have not yet done so in practice.
There is a race free method of doing this, but it, unfortunately, requires the use of the somewhat black magic ptrace
syscall. Full explanation of how to use ptrace
is beyond the scope of this answer. In a nutshell, however, your child process, before calling execve
, calls ptrace
with the PTRACE_TRACEME
argument. Your parent process then becomes your child process' debugger. It gets notified with wait
any time your child process is about to do something special.
This would take some playing with the call, but this would allow you to know when your child is about to exit, but has not yet actually done so.
Upvotes: 2