2016-11-15 51 views
-3

我有2個名爲file1.c和file2.c中 我想從我的主文件運行file1.c中的多個副本file2.c中如何運行另一個程序的多個副本 - Visual C

+4

您是否知道C文件和程序是不同的東西? –

+0

你是在討論線程,多進程,還是在runsum中連續多次在testsim中調用函數?沒有這些信息,這將很快關閉。 –

+0

@DietrichEpp是的,我知道。 – Sheri

回答

1

檢查系統調用fork()exec()。叉允許複製當前進程及其所有內存。執行調用允許替換爲當前進程執行的代碼。

基本上,從你的主要過程中,你會叉多次。如果fork的結果爲0,則使用子進程的命令行調用exec。

int i; 
for (i = 0; i < 10; i++) { 
    // fork() returns 0 for the child process, 
    // and the actual pid of the new process for the parent process. 
    pid_t pid = fork(); 

    if (!pid) { 
     // This if will be executed only by the child process. 

     // execvp() first argument is the executable file, 
     // the second argument is a varargs for each arguments of the command line. 
     execvp("testsim"); 
    } 
} 

但是,爲了達到這個目的,兩個c文件都必須編譯成可執行文件。

+0

如果給定的c文件被傳遞到exevp,它將不會工作得很好:)我們不太確定OP與什麼級別的問題... –

+0

@MichaelDorgan你可以告訴我你的電子郵件。讓我詳細分享問題。 – Sheri

+0

@ sturcotte06也需要你的電子郵件。 – Sheri

0

使用system()從另一個可執行文件執行可執行文件。

根據你想要多少次運行testsim加入這一行你runsim.c

system("path_to_testsim_executable/testsim_executable_name"); 

撥打以上電話線。

現在編譯.c文件並運行runsim可執行文件。

+0

謝謝你能分享你的電子郵件。我想與你分享這個問題。 – Sheri

+0

無法在我的情況下運行 可執行文件名** hello **但是當我運行系統(「hello」); 它輸出 你好:沒有找到 – Sheri

+0

@Sheri你確定'testsim.c'的可執行文件名是'hello'而不是'a.out'就像你在unix中得到的一樣嗎?並且您必須提供可執行文件的路徑。你不能只在那裏寫'你好'。我剛剛看到你接受了一個答案。這意味着你已經有了解決方案。在獲得解決方案後,我需要另外解釋一下嗎? – SkrewEverything

相關問題