| Trivialnight 2005-02-22, 3:20 pm |
| I am doing a program that will calculate the 199th fibonacci number using pipelines and multiple processes for each calculation. I have figured everything out except why none of the processes before the last one never execute the findfib() function;
Our teacher gave us the pipeline code to redirect stdin and stdout, but even when i execute the example file he gave us with it. It still is just running the findfib function at the last process only, I would go ask him about it but today he doesn't have any office hours. All the right headers are there. I have written the code for calculate the numbers using strings and whatnot but this is the only thing that is holding me back from completing. It seems as though the child never equals the parent which makes it not go into the if parent parent.
code:
int
main(int argc, char *argv[])
{
int i,nproc;
if(chkargs(argc,argv) == 0) exit(1);
nproc = strtol(argv[1],0,0);
mkpipeln(&i,nproc);
findfib(i,nproc);
exit(0);
}
void
mkpipeln(int *id, int np)
{ int fd[2]; pid_t child;
for(*id = 1; *id < np; (*id)++) {
if(pipe(fd) < 0) die("pipe-1 failed");
if((child = fork()) < 0) die("fork failed");
if(child > 0){
if(dup2(fd[1],1) < 0) die("dup2-1 failed");
if((close(fd[0]) < 0) || (close(fd[1]) < 0)) die("close-1 failed");
break;
}
else{
if(dup2(fd[0],0) < 0) die("dup2-2 failed");
if((close(fd[0]) < 0) || (close(fd[1]) < 0)) die("close-2 failed");
}
}
printf("id: %d\n", *id);
}
void
findfib(int id, int np)
{ printf("id: %d\n", id);
}
Now shouldn't it print out each ID in each process?
-tn |