从C程序中运行C程序

时间:2015-02-17 01:18:45

标签: c runtime system sh

这个问题几乎解释了我自己有三个不同的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);
}

我在终端窗口收到以下消息:

Error message

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

我已在终端中单独测试了所有这三个程序,如此

./barajas_*.x i
  1. 有人可以告诉我为什么system()将其作为目录输入,
  2. 如何告诉system()它停止将其作为目录并执行它?
  3. 编辑:在聊天室中经过长时间的讨论之后,Jonathan Leffler所说的问题是:“现有的实际命令名称与程序试图运行的命令名称不匹配”。

    iharob的贡献回答了实际的问题,因为如果命令名与程序运行的命令名匹配,那么任何对他提供的代码段感兴趣的人都应该有效。

2 个答案:

答案 0 :(得分:2)

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()的返回值对计算时差无用。它只是检查错误,你可以使用这个功能获得经过的时间

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

答案 1 :(得分:1)

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

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()总是返回0:

  

gettimeofday()函数应返回0,并且不保留任何值来表示错误。

通过struct timeval结构上的计算可以找到任何已用时间,answer iharob中显示的结果更多或更少。

聊天后简介

我邀请卡洛斯和我一起chat

在聊天中进行了一些讨论并看到了运行程序的实际结果之后,我们发现问题是一个错字 - 现有的实际命令名与程序试图运行的命令名不匹配。