Hello, i should finished this program, if anyone could tell me whats wrong... This is an optional university work, though i cant leave this nearly finished. I need to see where is my error
What my program should do.
The user must type "num_proc" ( number of children). The program creates a ring of num_proc processes with num_proc pipes, although i only use one variable ("tubo2") to access pipes.father and children will change the content of "frase" twice.
int main(int argc, char* argv[])
{
int tubo1[2], tubo2[2], i,num_proc=0,numero=0,longitud=0,pid=0;
char frase[MAXNAME];
...
pipe(tubo1);
dup2(tubo1[0],tubo2[0]);
dup2(tubo1[1],tubo2[1]);
close(tubo1[0]);
close(tubo1[1]);
/* Creating num_proc number of processes */
for( i=0; i<num_proc; i++)
{
pipe(tubo1);
if ( (pid=fork()) == -1 )
printf("\nError. Fork\n\n");
else
{
if ( pid )
{
/* Father�s code */
dup2(tubo1[0],tubo2[0]);
close(tubo1[0]);
close(tubo1[1]);
}
else
{
dup2(tubo1[1],tubo2[1]);
close(tubo1[0]);
close(tubo1[1]);
break;
}
}
}
if ( pid )
{
/* Father */
for (i=0; i<2; i++)
{
/* "frase" modification. Father starts. */
write(tubo2[1], frase, longitud*sizeof(char));
read(tubo2[0], frase, longitud*sizeof(char));
}
}
else
{
/* Children code */
for (i=0; i<2; i++)
{
read(tubo2[0], frase, MAXNAME*sizeof(char))
/* "frase" modification */
write(tubo2[1], frase, longitud*sizeof(char));
}
}
return 0;
}