有没有办法从/proc目录中获取此信息?我希望能够获得每个进程在几秒钟内运行了多长时间。
编辑:我需要在C++中执行此操作。很抱歉给你造成了混乱。
发布于 2011-06-29 08:26:47
让我们来分析一下你要做的事情:
因此,为了获得当前时间,我们可以运行:
#include <cstdio>
#include <cstdlib>
char *command;
int process_number = 1; // init process.
SYSTEM ("mkfifo time_pipe");
sprintf (command, "stat /proc/%d -printf="%%X" > time_pipe", process_number); // get the command to run.
// since this directory is created once it starts, we know it is the start time (about)
// note the %%, which means to print a literal %
SYSTEM (command); // run the command.现在,下一步是将其解析为Unix时间--但我们不必这样做!X说明符实际上将其转换为Unix时间。因此,下一步将是(a)获得当前时间(b)减去时间:
timeval cur_time;
double current_time, time_passed;
char read_time[11]; // 32 bit overflows = only 11 digits.
FILE *ourpipe;
gettimeofday(&cur_time, NULL);
current_time = cur_time.tv_sec + (cur_time.tv_usec * 1000000.0);
// usec stands for mu second, i.e., a millionth of a second. I wasn't there when they named this stuff.
ourpipe = fopen ("time_pipe", "rb");
fread(read_time, sizeof (char), 10, ourpipe);
time_passed = current_time - atoi (read_time);
fclose (ourpipe);所以,是的,差不多就是这样。需要管道将输入从一个管道传递到另一个管道。
https://stackoverflow.com/questions/6514378
复制相似问题