[英]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);
}
我在終端窗口上收到以下消息:
這意味着該程序正在運行其所有步驟,只是沒有正確執行我想定時的程序。
我已經像這樣在終端中分別測試了所有三個程序
./barajas_*.x i
編輯:在聊天室中進行了長時間討論之后,該問題如喬納森·萊夫勒(Jonathan Leffler)所說:“存在的實際命令名與程序試圖運行的命令名之間不匹配。”
實際的問題由iharob的貢獻來回答,對於感興趣的任何人,只要命令名與程序正在運行的命令名匹配,他提供的代碼段都應該起作用。
system()
函數僅接受const char *
類型的一個參數,如果您需要使用snprintf()
構建命令try,例如這樣
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;
}
我注意到您的代碼當前顯示為:
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
結構,可以找到任何經過的時間,或多或少,如iharob的答案所示。
我邀請卡洛斯和我聊天 。
在聊天中進行了一些討論並查看了運行該程序的實際結果后,我們發現問題是一個錯字-存在的實際命令名與該程序試圖運行的命令名之間不匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.