Parameters of the exec function

0

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

Looking at the parameters of the exec function: exec function It does not talk about any parameters that can be passed to tell you how many times that Exec should run. Does anyone know if there is any kind of exec that does this?

    
asked by anonymous 28.02.2015 / 19:50

1 answer

2

Maybe the request is something recursive call type.

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

and the child program picks up the value, decreases 1 and calls itself until it is 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;
}
    
28.02.2015 / 21:05