Agarrando o valor de retorno de execv ()
//code for foo (run executable as ./a.out)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/wait.h>
int main (int argc, char **argv) {
pid_t pid;
pid = fork();
int i = 1;
char *parms[] = {"test2", "5", NULL}; //test executable named test2
if(pid < 0) {
fprintf(stderr, "Fork failed");
return 1;
}
else if(pid == 0) {
printf("Child pid is %d\n", pid);
i = execv("test2", parms); //exec call to test with a param of 5
}
else {
wait(NULL);
}
printf("I is now %d\n", i); //i is still 1 here, why?
return 0;
}
Olá pessoal, estou tentando aprender um pouco sobre chamadas fork e execv (). Eu faço o meu programa foo.c acima fazer uma chamada para um arquivo que eu nomeei test.c. Eu bifurcar um filho e fazer com que ele faça uma chamada para execv, que adicionará apenas 10 ao parâmetro lido. Não sei por que a variável não muda, na parte inferior da minha função foo.c. A chamada precisa ser um ponteiro ou retornar um endereço? Qualquer ajuda seria muito apreciada. obrigado
Código para test.c (executável chamado test2)
#include <stdio.h>
int main(int argc, char ** argv[]) {
int i = atoi(argv[1]);
i = i +10;
printf("I in test is %d\n", i);
return i;
}