Basic Pipes

Trying to get basic pipes to work. I need a pipe for each child process that will take the STD_ERROR to be saved and read at the end by the parent process in sequential order.

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
//initializing the 10 multiple pipes
	int fd[20];

	for(int i=0;i<10;i++) {
		   if (pipe(fd+2*i) < 0)
		   {
			  write(STDERR_FILENO, "Pipe failed\n", 12);
			  exit(1);
		   }
   }


//child process within the fork
    case 0: 
      close(fd[2*child-2]);//close read end; of each pipe-fd[0] for child 1, fd[2] for child 2;
	  dup2(fd[2*child-1],2); //dup error stream to write end of pipe, fd[1] for child 1, fd[3] for child 2, etc.
	  close(fd[2*child-1]); //close write end*/
	  execlp("gcc","gcc",(filenameList[child]).c_str(), "-c", "-I/usr/include","-DPROGVERSION=1.0",(char *) 0);
     _exit(0);

//the main parent thread - outside of the switch for the fork
	  for(int i=1;i<child;i++) {
   	  	    close(fd[2*child-2]); //close write end
   	  	    read(fd[2*child-1],buf,100);
   	  	    cout<<buf;
   	  }


The child process is effectively cutting off the error stream and therefore I assume it's throwing it into the pipe. However, when I read the pipes, there appears to be nothing there (when I have purposely made sure an error stream would appear).

Is there a different way to read the pipe?
Topic archived. No new replies allowed.