Reputation: 668
Working on a simple downloader, that will download a file items in an array.
Currently it will download the first item in the array, however when the for loop goes to download the next item, it seems to stay at the same item it already downloaded.
meaning it's not incrementing to the next item, but it does run for the amount of times that it should.
i.e. 2 items to download, it will download the first item in teh array twice.
I belive I'm doing the forking process wrong, or the counter is getting reset in the for loop
// 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);
}
}
Upvotes: 0
Views: 154
Reputation: 39807
The problem is that your for
loop forks without considering child vs parent, and both child and parent perform the execl()
with i==0. You need to wrap your actions based on the return of fork()
the same way you did earlier in your code snippet.
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 */
}
Upvotes: 0