waitpid not waiting long enough

For this assignment, I'm taking list of files, compiling each file as separate threads, then linking them.

The simplified code looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
while(!filelist.eof()) {
//read file line

switch(pid=fork()) {

case -1: exit / fork failed stuff

case 0:
      close(fd[0]);
      dup2(fd[1], STDOUT_FILENO);
      close(fd[1]);
	  execlp("gcc","gcc",(filenameList[child-1]).c_str(), "-c", "-I/usr/include","-DPROGVERSION=1.0",(char *) 0);
     _exit(0);

     default:
      close(fd[1]);
      dup2(fd[0], STDIN_FILENO);
      close(fd[0]);
   }//switch 
   
   }//while


   waitpid(-1,NULL,0);

   execvp(args[0],args);
}


All of the variables are defined elsewhere and the args array has args[0]="gcc" the rest are *.o filenames and the last is NULL.

what happens is when I run the program, I get a lot of messages like:

gcc: buf.o: No such file or directory
gcc: carg_parser.o: No such file or directory
gcc: main_loop.o: No such file or directory
gcc: main.o: No such file or directory
gcc: re.o: No such file or directory
gcc: signal.o: No such file or directory


where some, but not all the files are listed. However, when I look at the file directory all the files appear to be there. Does this mean, when it gets to linking the files, they are not finished compiling yet?

I just wanted to add a comment that just to see if the exec commands acted appropriately separately, I commented out the linking command the first run, and then switched and commented out the compile command on the second try. It ran perfectly separating it like that, so it's something about the waiting i think?
Last edited on
Would moving the waitpid command between the switch and while change it?
(Assuming everything else is correct.)
Hypothesis: waitpid() is waiting for any child process to finish, rather than all. The files that aren't listed in the error messages are the ones that compiled first, and one of them triggered waitpid() to return.
Topic archived. No new replies allowed.