Stress-ng:使用execv编写应用程序来调用stress-ng命令,如果成功或失败则返回

时间:2017-07-24 10:55:08

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

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

示例stress-ng命令:

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

2 个答案:

答案 0 :(得分:1)

也许这就足够了:

#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);
}

答案 1 :(得分:1)

如果要解析stress-ng的输出,则需要在父级和子级之间创建管道,父级需要通过管道读取和解析输出,如下所示:

#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;
    int fds[2];

    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 };

    if (pipe(fds) < 0) {
        fprintf(stderr, "pipe failed: %d (%s)\n",
            errno, strerror(errno));
        exit(EXIT_FAILURE);
    }

    pid = fork();
    if (pid < 0) {
        fprintf(stderr, "fork failed: %d (%s)\n",
            errno, strerror(errno));
        exit(EXIT_FAILURE);
    } else if (pid == 0) {
        //close(STDERR_FILENO);
        close(STDIN_FILENO);
        close(fds[0]);
        dup2(fds[1], STDOUT_FILENO);
        dup2(fds[1], STDERR_FILENO);
        ret = execve(stress_ng, argv_new, env_new);
        if (ret < 0) {
            fprintf(stderr, "execve failed: %d (%s)\n",
                errno, strerror(errno));
            exit(EXIT_FAILURE);
        }
        close(fds[1]);
        _exit(ret);
    } else {
        /* Parent */
        int status;
        FILE *fp;
        char buffer[1024];

        close(fds[1]);
        fp = fdopen(fds[0], "r");
        if (!fp) {
            fprintf(stderr, "fdopen failed: %d (%s)\n",
                errno, strerror(errno));
            exit(EXIT_FAILURE);
        }

        while (fgets(buffer, sizeof(buffer), fp)) {
            size_t len = strlen(buffer);
            if (len > 0)
                buffer[len - 1] = '\0';

            if (strstr(buffer, "completed"))
                printf("GOT: <%s>\n", buffer);
        }
        fclose(fp);
        close(fds[0]);

        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);
}