user7633138
user7633138

Reputation:

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 ?

Upvotes: 0

Views: 1274

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129504

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.

Upvotes: 1

Related Questions