I've been puzzling over this for a while, and now I could use some help. I'm trying to create a loop which will fork off a child process and call "echo hello" through execve().
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, char *argv[],char *envp[]){
int i = 0;
while(i<10){
pid_t pid;
pid = fork();
if(pid != 0){
int status;
waitpid(-1, &status, 0);
}
if(pid == 0) {
char *arg_array[2];
arg_array[0]="echo";
arg_array[1]="hello";
char filename[] = "/bin/echo";
if (execve(filename,arg_array,envp) == (-1)) {
printf("ERROR!\n");
exit(1);
}
}
i++;
}
}
At first the code failed on its first run through, while succeeding on every subsequent run. Now, after cleaning it up for presenting here, it won't succeed at all- all I get is ERROR! x 10. I must have broken something further, and I'm not able to tell what.
This is only my second question on this site, so if you've got any suggestions for improving my questions/ constructive criticism, please share! Thank you.