简体   繁体   中英

After fork(), how to keep running execve() in for() loop?

Here is an example, ignorning the error checking:

int main()
{
    pid_t pid = fork();
    if(0 == pid)
    {
        for(int i = 0; i < 5; ++i)
        {
            char* const args[] = { "/bin/ls", nullptr };
            execve("/bin/ls", args, nullptr);
        }
    }
    else if(pid > 0)
    {
        wait(nullptr);
    }
}

If exec() after fork(), as far as I know, the linux will not copy but cover original system.

If I want to keep running execve() in for() loop like this, what should I do ?

exec (all of the different forms) will replace your current executable with the one given to exec , so NOTHING you do within the forked code will matter. You need to either do a loop around fork , or convince the author of the other program to run the loop for you.

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