简体   繁体   English

如何在C中完成子进程后跟踪子进程使用的内存

[英]how to trace memory used by a child process after the process finished in C

I have a process forks a child one that executes an executable program using execve(2) system call 我有一个进程派生一个子进程,该子进程使用execve(2)系统调用执行可执行程序

the parent process waits for the child one to finish using wait4(2) system call 父进程使用wait4(2)系统调用等待子进程完成

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 我使用了wait4(2)系统调用返回的(rusage)对象来获取所需的时间值,但是经过一些研究,我发现应用程序使用的内存无法由同一个(rusage)对象实现,因为它并没有现在以这种方式支持几天

one way to go was to use the /proc pseudo file system and parse the status or statm files in the directory /proc/[pid] 一种方法是使用/ proc伪文件系统并解析目录/ proc / [pid]中的状态或statm文件
assuming that I wanted to parse /proc/[pid]/status 假设我想解析/ proc / [pid] / status
what I need in this file is the field vmdata that specifies the memory used by data in KB 我在此文件中需要的是vmdata字段,该字段指定KB数据所使用的内存

when I read the file before the child process is finished the field vmdata is not shown "it's missing from the file!!" 当我在子进程完成之前读取文件时,字段vmdata没有显示“文件中缺少它!” 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 !! 当子进程完成后我读取文件时,目录/ proc / [pid]将被删除! 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 ... 谢谢 ...

The VmData field appears to only exist for running processes. VmData字段似乎仅对于正在运行的进程存在。 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. 只需运行grep VmData /proc/$$/status ,您将在当前的shell进程中看到它。

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 (ie - 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. 如果要在僵尸状态下捕获进程(即-它已经退出但尚未收割),则需要找到一种方法来通知它已完成而无需调用wait ,这会使它消失。 My recommended way is to register a SIGCHLD handler. 我推荐的方法是注册SIGCHLD处理程序。 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. 在那个时间点, /proc/pid/status将仍然存在。

Unfortunately for you, VmData does not exist for zombie processes. 对于您来说不幸的是,僵尸进程不存在VmData 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. 您的选择是定期采样VmData ,或在即将终止时停止该过程,但实际上尚未这样做。

There is a race free method of doing this, but it, unfortunately, requires the use of the somewhat black magic ptrace syscall. 有一种不分种族的方法,但是不幸的是,它需要使用有些黑魔法的ptrace syscall。 Full explanation of how to use ptrace is beyond the scope of this answer. 有关如何使用ptrace完整说明超出了此答案的范围。 In a nutshell, however, your child process, before calling execve , calls ptrace with the PTRACE_TRACEME argument. 简而言之,子进程在调用execve之前,先使用PTRACE_TRACEME参数调用ptrace 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. 每当您的子进程将要执行某些特殊操作时,它都会得到wait通知。

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. 这需要花些时间玩这个电话,但这可以让您知道您的孩子何时将要退出,但实际上尚未退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM