pipe help

i made a lot of processes. here is the code:

main()
{
printf("\nEnter K="); scanf("%d",&k);
printf("Enter L="); scanf("%d",&l);
printf("\nFather id=%d\n",getpid());
x=0;
makechild();
sleep(2);
return 1;
}
int makechild()
{
for(q=1;q<=k;q++)
{
if(f=fork())
{
}
else
{
printf("\nChild id=%d owner=%d\n",getpid(),getppid());
++x;
if(x<l) makechild();
sleep(1);
exit(0);
}
}
return;
}

it basically creates a tree of processes over L rows, each process having K sons.

now, my question is: how can i make these processes comunicate between them?
i figured i would need a lot of pipes but i can't seem to implement them and i'm getting sick with 'broken pipe' error.
can anyone give me some advice?

Depends what kind of communication you need, and what the purpose of the program is. It is hard to answer the question based on the information provided. Some methods that can be used to communicate between processes:

  • write results to file, parent reads file
  • shared memory
  • network sockets
  • unix sockets

You may be interested in this link:
http://www.ecst.csuchico.edu/~beej/guide/ipc/

HTH

Don't forget message queues and FIFO's :wink:

Another good link...
http://users.actcom.co.il/~choo/lupg/tutorials/multi-process/multi-process.html

Well, here is a quickie example of a pipe:

#include <stdio.h>   
#include <unistd.h>

void main()
{
        int pid, pipefd[2], parentpid, childpid;

        parentpid=getpid();
        pipe(pipefd);

        if (pid=fork()) {
                printf("I am the parent process and my pid is %d\n", getpid());
                printf("My child process has a pid of %d\n", pid);
                read(pipefd[0], (char *) &childpid, sizeof(childpid));
                printf("My child process sent me %d\n", childpid);
        } else {
                childpid=getpid();
                printf("I am the child and my pid is %d\n", childpid);
                write(pipefd[1], (char *) &childpid, sizeof(childpid));
                exit(0);
        }

        printf("I am still the parent process and my pid is %d\n", getpid());
        exit(0);
} 

A broken pipe means that you are writing onto a pipe that has been closed by the other end.

i was able to make a father and his son communicate through a pipe but the real problem is: in my example i have no control over when a process is created so i have no control over who is reading and/or writing to a pipe at a certain moment in time.
And i need to have some sort of control of that so i can send data from one process to it's sons and viceversa. How can i achieve that? I believe the most simple method would be using signals to tell the parent when a child is created so that the parent can start writing in the pipe and the child can read from it, but i'm totally lost here. :frowning:

Nothing that you're saying makes any sense at all.

A process has total control over when a child is created. When it invokes fork(), that is exactly when the process is created. The child exists when the very next statement is executed.

There is no need at all to synchonize the reading and writing on a pipe. The reader do can invoke read() hours before the data is written...it will simply wait unless it sets O_NDELAY in which case it returns immediately...so don't set that option. The reader can also read() hours after the write() on the other end. The data will wait. Neither case can cause a broken pipe, which is the error you complained about. That is caused by the reader exiting prior to the write.

Your real problem is that you'e trying to run a marathon when you can't yet walk.

If you attempt to use pipes in a full-duplex manner or use two pipe to achieve a two-way communication between processes, you will almost certainly cause deadlock. Get the data flowing in a single direction before you try to move on.