Father & children

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 :frowning:

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;
}

Rules violation
Posting of assignments on the forum is not allowed.

However, you seem to have taken some efforts on your assignment.
I have a piece of question here

you do a
close(tubo1[0]);
close(tubo1[1]);

and then you do a pipe(tubo1);

anyway can you explain what exactly is the issue, do you get some program errors, compile time, run time or the program is running but not as expected ?

I guess I'll leave this thread open for now. Maybe someone will close it though. Anyway please review the rules.

I can't guess what the program is supposed to do. But unix is byte oriented. Multiplying the third parameter of read() and write() by sizeof(char) is almost always wrong. This is very dangerous:
char frase[MAXNAME];
read(tubo2[0], frase, MAXNAME*sizeof(char))
although I don't see how it can get past a compiler with no semicolon. The other reads and writes are all for zero bytes. Reading and writing zero bytes is legal, but probably not useful.

I do a pipe and then another pipe because im creating a pipe, referred with "tubo2",for father process and another one in each iteratino for each child.

the program compile ok. but there is an execution error: "broken pipe". also i realize that father doesnt execute this code:

...
if (pid)
{
       /* father */
       printf("\nhello..."); /* this line is only an example. 
                                      the program executes it. Everything ok..*/
       for(i=0; i<2; i++)  /* it doest go into this loop. why?! */
       {
                /* modification of "frase" */
                ...
        }
}
else
{
         read(...); /* the function returns -1. Error */
         ...
}

Perderabo:
I didnt paste it well; yes, semicolon is needed. And im going to review rules again!

Mmm, sorry i dont have a high level as you can see... but why did you say that the other writes and reads are all zero?

Ok!

At last ive seen the problem!

at the top of my code, i did:

dup2(tubo1[0],tubo2[0]);
dup2(tubo1[1],tubo2[1]);

the problem is that dup does an assignment but dup2, firstly does a comparison between tubo1[0] and tubo2[0], though tubo2 isnt initiate! i hadnt this problem before because i always use dup

Thank you!

pd: sorry again (referred to rules)