Parameters of the exec function

Asked

Viewed 478 times

0

I was doing an exercise of a book and in it has the following order:

Looking at the function parameters exec: Function exec It does not talk about any parameter that can be passed to inform how many times that Exec must perform. Does anyone know if there’s any kind of exec that does this?

  • The text is a little weird but I think he’s talking about putting together a array with the number of processes. It’s just a kick. Another possibility is that you should make a function that receives this parameter and handle it to execute the exec().

1 answer

1

Maybe the request is something called recursive.

// programa 'pai'
    execl("filho.exe", "filho.exe", "4", 0);

and the 'son' program picks up the value, decreases 1 and calls itself to zero.

// programa 'filho' (falta validacao de erros)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv) {
    int n = atoi(argv[1]);
    printf("filho #%d\n", n);
    if (n > 1) {
        char tmp[10];
        sprintf(tmp, "%d", n - 1);
        execl("filho.exe", "filho.exe", tmp, 0); // chamada "recursiva"
    }
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.