简体   繁体   中英

c++ call linux commands forks childs

using linux commands in my c++ program. Trying to download a url from a list of arrays, once one download finishes, the next to start.... currently only downloads the first location in the array, then stops.

Does somebody see the error i'm doing?

// Begin the downloading process
pid_t child = 0;
child = fork();

if ( child < 0)
{
    cout << "Process Faileld to Fork<<endl;
    return 1;
}
if (child == 0)
    {
        wait(NULL);
    }
else
{
    for(int i = 0; i < numberOfDownloads; i++)
    {
    execl("/usr/bin/wget", "wget",locations[i], NULL);  
    }
}

trying to download something with the command wget, but i get an error

execl replaces the currently running process by the started process. So your loop body is only run once. You should put the fork() inside the loop, assuming you want to fork off numberOfDownloads instances of wget . (but be careful with such things)

Otherwise, use system() , which will not terminate your process but return to your loop after wget exits. Or popen() , if you need more control over the process and want to read wget output, without having to do all the I/O heavylifting yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM