0

I am doing a communication beetwen 3 processes (a father and 2 sons).

i'm using 5 pipes :
1- Father to the first son : father_son1

2 - Father to the second son : father_son2

3 - First son to second son : son1_son2

4 - Second son to first son : son2_son1

5 - 2 sons to the father (so they alternate who writes on this one, they don't do it at the same time) : son_father

At the start of the program, the father generates a random number beetwen 1 and 2, and sends to his 2 sons, using 2 pipes.

write(father_son1[1], &role, sizeof(int));
write(father_son2[1], &role, sizeof(int));

That goes well, both of them receives the variable role (random number). First son behaviour : If the "role==1", he does some calculations, sends the result to the second son,and reads it back (the second just reads it and send it back). Else, "role!=1", he reads the result of calculation made by second son, and writes it back (only to know what has been done).

Vice versa for the second son, if the "role==2", he does calculations, sends it to the first son and reads it back. Else "role!=2", reads and writes to the first.

When "role==2", second son does calculation, sends it to the first. the first son reads the result and sends it back. The second receives back the same result sent in the first place.

the problem is on the other case : When "role==1", the first does calculation, he sends the information to the second, the second always read 0, and sends back 0 (i don't read back the same result i sent).

        pid_son[0]=fork();
        if(pid_son[0]==0){
            read(father_son1[0], &role, sizeof(int));
            if(role==1){
                close(son2_son1[1]);
                close(son1_son2[0]);
                jeu1 = joue(cheval_joueur1); // The calculation made
                write(son1_son2[1], &jeu1, sizeof(int));
                read(son2_son1[0], &jeu2, sizeof(int)); // Not the same jeu1 sent abose
            }
            else {
                close(son1_son2[0]);
                close(son2_son1[1]);
                read(son2_son1[0], &jeu2, sizeof(int));
                write(son1_son2[1], &jeu2, sizeof(int));
            }           
            //Closed every pipe
            exit(0);
        }
        
        pid_son[1]=fork();
        if(pid_son[1]==0){
            read(father_son2[0], &role, sizeof(int));
            if(role==2){
                close(son2_son1[0]);
                close(son1_son2[1]);
                jeu1 = joue(cheval_joueur2);
                write(son2_son1[1], &jeu1, sizeof(int));
                read(son1_son2[0], &jeu2, sizeof(int)); // Same thing read back
            }
            else {
                close(son1_son2[1]);
                close(son2_son1[0]);
                read(son1_son2[0], &jeu2, sizeof(int));
                printf("value : %d\n", jeu2); // 0
                write(son2_son1[1], &jeu2, sizeof(int));
            }
            //Closed every pipe
            exit(0);
        }
ouss
  • 1
  • does the opposite work correctly? – Furkan Çetinkaya Oct 25 '20 at 02:42
  • As a guideline, in English, we refer to processes as parents and children (gender neutral) rather than using father or mother, son or daughter. – Jonathan Leffler Oct 25 '20 at 03:33
  • 1
    You've not created an MCVE ([Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve) — or MRE or whatever name SO now uses) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). That makes it hard to work out what's going wrong — we can't simply compile and run the code. You have two places where you comment `// Closed every pipe`; in neither case is that remotely true. You don't show any use of the child to parent pipe. – Jonathan Leffler Oct 25 '20 at 04:12

0 Answers0