Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fork parent child communication

Tags:

c

fork

ipc

I need some way for the parent process to communicate with each child separately.

I have some children that need to communicate with the parent separately from the other children.

Is there any way for a parent to have a private communication channel with each child?

Also can a child for example, send to the parent a struct variable?

I'm new to these kind of things so any help is appreciated. Thank you

like image 500
Emil Grigore Avatar asked Jan 05 '13 09:01

Emil Grigore


People also ask

Which pipe allows communication between parent and child?

Two-way Communication Using Pipes Step 1 − Create two pipes. First one is for the parent to write and child to read, say as pipe1. Second one is for the child to write and parent to read, say as pipe2. Step 2 − Create a child process.

What is parent/child communication?

Parent–child communication is the verbal and nonverbal interaction between a parent and child within a family system. Parents are biological or nonbiological caregivers (e.g., adoptive parents or stepparents) and parent–child communication takes place throughout the child's ages and developmental stages.

What happens when fork () is called?

When a process calls fork, it is deemed the parent process and the newly created process is its child. After the fork, both processes not only run the same program, but they resume execution as though both had called the system call.


1 Answers

(I'll just assume we're talking linux here)

As you probably found out, fork() itself will just duplicate the calling process, it does not handle IPC.

From fork manual:

fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent.

The most common way to handle IPC once you forked() is to use pipes, especially if you want "a private comunication chanel with each child". Here's a typical and easy example of use, similar to the one you can find in the pipe manual (return values are not checked):

   #include <sys/wait.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <unistd.h>
   #include <string.h>

   int
   main(int argc, char * argv[])
   {
       int pipefd[2];
       pid_t cpid;
       char buf;

       pipe(pipefd); // create the pipe
       cpid = fork(); // duplicate the current process
       if (cpid == 0) // if I am the child then
       {
           close(pipefd[1]); // close the write-end of the pipe, I'm not going to use it
           while (read(pipefd[0], &buf, 1) > 0) // read while EOF
               write(1, &buf, 1);
           write(1, "\n", 1);
           close(pipefd[0]); // close the read-end of the pipe
           exit(EXIT_SUCCESS);
       }
       else // if I am the parent then
       {
           close(pipefd[0]); // close the read-end of the pipe, I'm not going to use it
           write(pipefd[1], argv[1], strlen(argv[1])); // send the content of argv[1] to the reader
           close(pipefd[1]); // close the write-end of the pipe, thus sending EOF to the reader
           wait(NULL); // wait for the child process to exit before I do the same
           exit(EXIT_SUCCESS);
       }
       return 0;
   }

The code is pretty self-explanatory:

  1. Parent forks()
  2. Child reads() from the pipe until EOF
  3. Parent writes() to the pipe then closes() it
  4. Datas have been shared, hooray!

From there you can do anything you want; just remember to check your return values and to read dup, pipe, fork, wait... manuals, they will come in handy.

There are also a bunch of other ways to share datas between processes, they migh interest you although they do not meet your "private" requirement:

  • shared memory "SHM", the name says it all...
  • sockets, they obviously work as good if used locally
  • FIFO files which are basically pipes with a name

or even a simple file... (I've even used SIGUSR1/2 signals to send binary datas between processes once... But I wouldn't recommend that haha.) And probably some more that I'm not thinking about right now.

Good luck.

like image 115
cmc Avatar answered Sep 19 '22 08:09

cmc