c++ - 压力-ng : Writing an application program using execv to invoke stress-ng commands and return if it is success or failure

标签 c++ c cross-compiling embedded-linux stress-testing

Stress-ng:如何使用 execv 在 C 或 Cpp 中编写应用程序来调用 stress-ng 命令以在 MIPS 中进行 CPU 和内存测试,并在成功或失败时返回其状态? 给定一个可执行的 stress-ng 文件,该文件已使用其工具链交叉编译为 MIPS32 版本。

示例 stress-ng 命令:

stress-ng --vm 8 --vm-bytes 80% -t 1h
stress-ng --cpu 8 --cpu-ops 800000

最佳答案

也许这就足够了:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void)
{
        pid_t pid;
        int ret;

        char *stress_ng = "/usr/bin/stress-ng";

        char *argv_new[] = { stress_ng,
                "--vm",  "8", "--vm-bytes",  "80%",
                "-t", "2s", "-v", NULL  };
        char *env_new[] = { NULL };

        pid = fork();
        if (pid < 0) {
                fprintf(stderr, "fork failed: %d (%s)\n",
                        errno, strerror(errno));
                exit(EXIT_FAILURE);
        } else if (pid == 0) {
                ret = execve(stress_ng, argv_new, env_new);
                if (ret < 0) {
                        fprintf(stderr, "execve failed: %d (%s)\n",
                                errno, strerror(errno));
                        exit(EXIT_FAILURE);
                }
                _exit(ret);
        } else {
                /* Parent */
                int status;

                ret = waitpid(pid, &status, 0);
                if (ret < 0) {
                        fprintf(stderr, "waitpid failed: %d (%s)\n",
                                errno, strerror(errno));
                        exit(EXIT_FAILURE);
                }
                ret = WEXITSTATUS(status);
                printf("stress-ng returned: %d\n", ret);
        }
        exit(0);
}

关于c++ - 压力-ng : Writing an application program using execv to invoke stress-ng commands and return if it is success or failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45278768/

相关文章:

c++ - 未定义对 wxGLCanvas 构造函数的引用?

c++ - 如何使用 strcpy 读取多个数据

makefile - 找不到 Ranlib

ubuntu - 通过 apt-get 在 X86 上安装 armhf lib

c++ - Mac OS X 中的 sem_getvalue() 功能障碍 - C++

c++ - `auto const*const` 可以通过类型定义为某种单字类型吗?

c - 将十六进制值存储在整数中

c - 如何防止按钮默认获得焦点

c++ - 编译时实际包含哪些文件

qt - QMake:如何选择要链接到的库版本