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

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

Stress-ng:如何使用 execv 在 C 或 Cpp 中编写应用程序以调用 stress-ng 命令 CPU 和 MIPS 中的内存测试以及 return 如果成功则其状态还是失败? 给定一个可执行的 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);
}

如果你想解析 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] = '[=10=]';

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