Iterative Fork() and Pipes problem in C

Hello all,

Hope you are all doing well.

I am very new to c programming and I have a task where I must make three (or more forks) communicate with the process that created them.

I used a for loop to create the forks, which did fine.
However, when trying to communicate, I used pipes (on the basis of FIFO), but I am not getting the output I need.

Let me explain:
The father must send to his sons their pids. (That is the son's pids).
He successfully sends it to the first son, but however, he sends the first son pid to the second son. And finally send the second son pid to the third one. Which is wrong.

What I want is that, the father sends to his sons their correct pids.

Here is the code and below is my output.
Can anyone help me please ?

Here is the code
[code]
#include
#include
#include
#include

#define reading 0
#define writing 1

main()
{
int num_son,n;
int *status;
char c;

//pipes declaration
int pipe_father_son[2],pipe_son_father[2];

printf("Father: I am the father and my pid is %d
", getpid());

//pipes initialisation
pipe(pipe_son_father);
pipe(pipe_father_son);

int i;
for(i=0; i<3; i++)
{
num_son=fork();
if(num_son==-1)
{
printf("creation error
");
exit(1);
}
if(num_son!=0) //father process
{
char message[15];
int n,num;

//the father send the son his number
num = num_son;

//close the pipe on read to write in it
close(pipe_father_son[reading]);
n=write(pipe_father_son[writing],&num,sizeof(num));

//the father receives a message from his son
////close the pipe on write to read in it
close(pipe_son_father[writing]);
n=read(pipe_son_father[reading],&message,sizeof(message));
//printf("Father: i received %s
",message);

//the father replies to his son
strcpy(message,"you are welcome");
n=write(pipe_father_son[writing],&message,sizeof(message));

//closes the pipes
close(pipe_father_son[writing]);
close(pipe_son_father[reading]);

sleep(1);

}
else //son process
{
int num_process,num_process_father,num;
char message[15];

num_process=getpid();
num_process_father=getppid();

//the son receives his number from his father
close(pipe_father_son[writing]);
n=read(pipe_father_son[reading],&num,sizeof(num));
printf("Son: %d my pid is %d and i received %d from my father %d
",i,num_process,num,getppid());

//the son thanks his father
close(pipe_son_father[reading]);
strcpy(message,"thanks");
n=write(pipe_son_father[writing],&message,sizeof(message));

//the son receives the you are welcome message from his father
n=read(pipe_father_son[reading],&message,sizeof(message));
//printf("Son: I received %s
",message);

//closes the pipes
close(pipe_father_son[reading]);
close(pipe_son_father[writing]);

sleep(1);
exit(num_son);

}


}

for(i=0; i<3; i++)
{
wait(&status);
printf("Father: my son %d is over
",i,(int) status/256);
sleep(1);
}
}
[/code]

Here is the output
[code]
Father: I am the father and my pid is 1804
Son: 0 my pid is 1805 and i received 1805 from my father 1804
Son: 1 my pid is 1806 and i received 1805 from my father 1804
Son: 2 my pid is 1807 and i received 1806 from my father 1804
Father: my son 0 is over
Father: my son 1 is over
Father: my son 2 is over[ATTACH]10963[/ATTACH]
[/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories