记录流程状态 [英] Record Process State

查看:61
本文介绍了记录流程状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C中实现一个程序来记录使用Linux环境(Cantos)从开始到终止的所有任务。



这个程序应该给我进程的PID以及此进程的所有相应状态,例如运行,休眠,等待,阻塞和终止,以及每个状态下的总内存和CPU使用情况。

解决方案

你可以使用top cammand获取进程状态:

假设要监视的pid是16452

  top   -  p   16452    -  b   |   grep   PID  -A1  
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME + COMMAND
16452 ashish 20 0 702608 189280 92164 S 0 0 2 3 2:55 74 chrome
-
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME + 命令
16452 ashish 20 0 702608 189280 < span class =code-leadattribute> 92164 S 0 3 2 3 2:55 75 chrome
-


根据建议您可以检索一些平均值(基于内核指标)但是,至少在我看来,你无法跟踪所有进程状态的变化,这个信息只对进程调度程序可用。


经过这么多建议后看起来更多或者不太合法,建议你获得* NIX命令'ps'的来源这里 [ ^ ]。

它似乎正是您要找的;)。 ..

I need to implement a program in C to record all tasks done by process from starting to termination using Linux Environment(Cantos).

This program should give me the PID of the process and all the corresponding states of this process as it run for e.g running, sleeping, waiting, block and termination along with the total memory and CPU usage in each state.

解决方案

You can use top cammand to get process status:
suppose pid to be monitered is 16452

top -p 16452 -b | grep PID -A1
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
16452 ashish    20   0  702608 189280  92164 S   0.0  2.3   2:55.74 chrome
--
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
16452 ashish    20   0  702608 189280  92164 S   0.3  2.3   2:55.75 chrome
--


As suggested you may retrieve some average values (based on kernel indicators) but, at least in my opinion, you cannot track all the process state changes, this info is only available to the process scheduler.


After so many suggestions it seems more or less legal to suggest you to get the sources of *NIX command 'ps' that you can find here[^].
It seems to be exactly what you are looking for ;) ...


这篇关于记录流程状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆