【发布时间】:2015-07-27 14:34:10
【问题描述】:
我阅读了article 中关于 C 中内存使用计算的内容,但遇到了问题。
我编写了一个简单的测试程序,它可以运行超过一秒并使用超过 1 KB 的内存。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a;
int f[1000000];
sleep(1);
scanf("%d",&a);
printf("%d %d\n",a/10,a%10);
return 0;
}
然后我将它编译成一些main.exe 并检查文章中的程序操作
pid = fork();
if (pid == 0)
{
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = TIME_LIMIT;
setrlimit(RLIMIT_CPU, &rlim);
execv("./main.exe",NULL);
}
else
{
struct rusage resource_usage;
// set arbitrary lower limit value of memory used
int memory_used = 128;
pid_t pid2;
do {
memory_used = max(memory_used, get_memory_usage(pid));
if ((memory_used > memory_limit)
kill(pid, SIGKILL);
// wait for the child process to change state
pid2 = wait4(pid, &status, WUNTRACED | WCONTINUED, &resource_usage);
} while (pid2 == 0);
}
以及来自文章的函数get_memory_usage()
int get_memory_usage(pid_t pid) {
int fd, data, stack;
char buf[4096], status_child[NAME_MAX];
char *vm;
sprintf(status_child, "/proc/%d/status", pid);
if ((fd = open(status_child, O_RDONLY)) < 0)
return -1;
read(fd, buf, 4095);
buf[4095] = '\0';
close(fd);
data = stack = 0;
vm = strstr(buf, "VmData:");
if (vm) {
sscanf(vm, "%*s %d", &data);
}
vm = strstr(buf, "VmStk:");
if (vm) {
sscanf(vm, "%*s %d", &stack);
}
return data + stack;
}
但是字符串中的问题,其中pid2 = wait4(pid, &status, WUNTRACED | WCONTINUED, &resource_usage);。 while 只进行一次迭代并等待进程结束。但我需要计算内存,rusage resource_usage 不要给我这个信息。我怎样才能使while 在子进程运行并在他停止的地方停止时工作。
当他 ZOMBIE 时,子进程的状态有更多问题。它不给记忆。我也需要抓住它。因为如果对于我的main.exe 我使用这个测试程序:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a;
int f[1000000];
sleep(1);
scanf("%d",&a);
printf("%d %d\n",a/10,a%10);
return 0;
}
当我在无限while 中从get_memory_usage 执行一些输出时,它会显示所有/proc/[pid]/status 输出。我明白了,那个子进程是
Name: check.exe
State: R (running)
然后它会
Name: main.exe
State: Z (zombie)
这意味着proc 无法捕获 main.exe 正在运行的信息。
【问题讨论】:
-
wait4()
返回:-1 表示发生了一些故障,0 表示没有发生状态更改,否则返回子进程的 pid。建议检查 (-1 or pid of child)