2012-09-18 49 views
0

使用簡單的下載程序,它將下載數組中的文件項。分叉子進程

目前它會下載數組中的第一個項目,但是當for循環去下載下一個項目時,它似乎停留在它已經下載的相同項目中。

這意味着它不會遞增到下一個項目,但它運行的次數應該是。

即要下載的2個項目,它將下載第一個項目的數組兩次。

我相信我做了分流處理錯誤,或計數器復位獲得在for循環

// Begin the downloading process 
pid_t child = 0; 
child = fork(); 
wait(); 
if (child < 0) 
{ 
    cout << "Process Failed to Fork" <<endl; 
    return 1; 
} 
if (child == 0) 
    { 
     wait(); 
    } 
else 
{ 

    for(int i = 0; i < numberOfDownloads; i++) 
    { 
    child = fork(); 
    wait(); 
    execl("/usr/bin/wget", "wget",locations[i], NULL); 
    } 
} 
+0

而你的程序並不是真正的下載器,它只是'wget'的一個包裝器。您應該使用libcurl(用於HTTP客戶端處理的庫)進行調查。 –

+0

你可以在C++ 11 – balki

回答

0

的問題是,你的for環叉不考慮孩子家長VS,並且兩個孩子,父母使用i == 0執行execl()。您需要按照您之前在代碼段中所做的相同方式,基於fork()的返回來包裝您的操作。

else 
{ 
    for(int i = 0; i < numberOfDownloads; i++) 
    { 
     child = fork(); 
     if (child > 0) execl("/usr/bin/wget", "wget",locations[i], NULL); 
    } 

    /* call wait() for each of your children here, if you wish to wait */ 
} 
+0

中使用std :: async謝謝,那是我的問題 – user1050632