运行 C 程序中的 C 程序

Run a C program from within a C program

这个问题本身就很好解释了我有三个不同的 C 程序,为了尝试比较它们的效率,我试图通过让它们 运行 多次改变它们的参数来测试它们的 运行 时间(与花费的时间成正比)并写出每个程序花费多长时间 运行 某些参数(所以稍后我可以绘制结果)。

以下是我的代码

   # include <stdio.h> 
    # include <stdlib.h>
    # include <math.h>
    # include <time.h>  

   int main(void){

    int i;
    struct timeval bni, bmi, bfi, bnf, bmf, bff;
    FILE *in;
    char filename1[30] = "shuff.dat";
    int a1,a2,b1,b2,c1,c2;
    char command[100];

    in = fopen(filename1, "w");
    //for(i = 0; i<=100000; i +=100){
    for(i = 0; i<=10; i +=1){

        if (snprintf(command, sizeof(command), "./barajas_n.x %d", i) < sizeof(command)){
        a1 = gettimeofday(&bni , NULL);
        system(command);
        a2 = gettimeofday(&bnf , NULL);
        }

        if (snprintf(command, sizeof(command), "./barajas_m.x %d", i) < sizeof(command)){
        b1 = gettimeofday(&bmi , NULL);
        system(command);
        b2 = gettimeofday(&bmf , NULL);
        }

        if (snprintf(command, sizeof(command), "./barajas_fy.x %d", i) < sizeof(command)){
        c1 = gettimeofday(&bfi , NULL);
        system(command);
        c2 = gettimeofday(&bff , NULL);
        }
        fprintf(in, "%d %d %d %d \n", i, (a2-a1),(b2-b1),(c2-c1));
    }
    fclose(in);
}

我在终端 window 上收到以下消息:

这意味着该程序 运行正在执行其所有步骤,只是没有正确执行我想要计时的程序。

我已经像这样在终端中单独测试了所有三个程序

./barajas_*.x i
  1. 有人能告诉我为什么 system() 将其输入作为目录,
  2. 如何告诉 system() 它停止将其作为目录并执行它?

编辑:在聊天室中进行了长时间的讨论后,问题如 Jonathan Leffler 所说:“实际存在的命令名称与程序尝试使用的命令名称不匹配 运行。 “

实际问题由 iharob 的贡献回答,对于任何感兴趣的人来说,如果命令名称与程序 运行 的命令名称相匹配,他提供的代码片段应该有效。

system() 函数只接受一个 const char * 类型的参数,如果你需要构建命令,请尝试使用 snprintf(),像这样

char command[100];

if (snprintf(command, sizeof(command), "barajas_n.x %d", i) < sizeof(command))
{
    a1 = gettimeofday(&bni , NULL);
    system(command);
    a2 = gettimeofday(&bfi , NULL);
}

另外,gettimeofday()的return值对计算时差没有用。只是为了检查错误,你可以使用这个函数得到经过的时间

float elapsed_time(struct timeval *end, struct timeval *start)
{    
    struct timeval result;
    if (end->tv_usec - start->tv_usec > 1.0E6)
    {
        double adjust;

        adjust          = (end->tv_usec - start->tv_usec) / 1.0E6;
        start->tv_usec += 1.0E6 * adjust;
        start->tv_sec  -= adjust;
    }
    result.tv_sec  = end->tv_sec  - start->tv_sec;
    result.tv_usec = end->tv_usec - start->tv_usec;

    return result.tv_sec + result.tv_usec / 1.0E6;
}

然后打印经过的时间

printf("Elapsed time: %f\n", elapsed_time(&bni, &bfi);

正如另一个答案提到的,您需要添加一个斜线来执行程序,./programname 而不是 .programname,但这是另一个解决方案:

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>

#include <sys/time.h>
#include <time.h>

/* This function, just calculates the difference in seconds, between end and start */
float elapsed_time(struct timeval *end, struct timeval *start)
{
    struct timeval result;
    if (end->tv_usec - start->tv_usec > 1.0E6)
    {
        float adjust;

        adjust          = (end->tv_usec - start->tv_usec) / 1.0E6;
        start->tv_usec += 1.0E6 * adjust;
        start->tv_sec  -= adjust;
    }
    result.tv_sec  = end->tv_sec  - start->tv_sec;
    result.tv_usec = end->tv_usec - start->tv_usec;

    return result.tv_sec + result.tv_usec / 1.0E6;
}

/* this function will execute the command and wrap the system call
 * with 'gettimeofday()' so you can return the elapsed time while
 * the called program was running.
 *
 * It also builds the command string with the right parameter. 
 */
float run_command_and_return_time(const char *const program, int parameter)
{
    char           command[100];
    struct timeval start;
    struct timeval end;
    int            result;
    /* check that sprintf didn't need more characters */
    result = snprintf(command, sizeof(command), "%s %d", program, parameter);
    if ((result >= sizeof(command)) || (result < 0))
        return -1.0;
    gettimeofday(&start, NULL);
    system(command);
    gettimeofday(&end, NULL);

    return elapsed_time(&end, &start);
}

int
main(int argc, char **argv)
{
    char        cwd[PATH_MAX];
    const char *filename;
    FILE       *output;

    filename = "shuff.dat";
    output   = fopen(filename, "w");
    if (output == NULL)
        return -1;
    /* get the current working directory */
    getcwd(cwd, sizeof(cwd));
    /* add the cwd to the PATH variable, so your barajas_*.x programs are found,
     * this way you don't need the ./bara... anymore, just bara... will do it.
     */
    setenv("PATH", cwd, 1);
    /* from here it's pretty evident what the program does */
    for (int i = 0 ; i < 10 ; ++i)
    {
        float a, b, c;

        a = run_command_and_return_time("barajas_n.x", i);
        b = run_command_and_return_time("barajas_m.x", i);
        c = run_command_and_return_time("barajas_fy.x", i);

        fprintf(output, "%d %f %f %f \n", i, a, b, c);
    }
    /* don't forget to close the output file */
    fclose(output);

    return 0;
}

我注意到您的代码当前显示为:

if (snprintf(command, sizeof(command), ".barajas_n.x %d", i) < sizeof(command)){

在我看来你好像少了一个斜线:

if (snprintf(command, sizeof(command), "./barajas_n.x %d", i) < sizeof(command)){

如果这只是一个打字错误,那么剩下的一个问题是 gettimeofday() 总是 returns 0:

The gettimeofday() function shall return 0 and no value shall be reserved to indicate an error.

任何经过的时间都是通过对 struct timeval 结构的计算找到的,或多或少如 by iharob 中所示。

Post-聊天概要

我邀请卡洛斯和我一起 chat

在聊天中进行了一些讨论并查看了 运行 运行该程序的实际结果后,我们发现问题是拼写错误 — 实际存在的命令名称与实际存在的命令名称不匹配该程序试图 运行.