Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create two processes from a single Parent

I know I'm going to need to use fork(), but this just creates a single child process. Do i simply call fork again from within the child process? Also, I need them to communicate through a signal or pipe, which is easier to implement and what do i need to know for doing that (functions, etc..)

like image 855
fifamaniac04 Avatar asked Jul 01 '11 00:07

fifamaniac04


People also ask

How do you create multiple processes of a child?

Creating multiple process using fork() Explanation – Here, we had used fork() function to create four processes one Parent and three child processes. An existing process can create a new one by calling the fork( ) function. The new process created by fork() is called the child process.

How do you make a child process from the parent process?

A child process is created as its parent process's copy and inherits most of its attributes. If a child process has no parent process, it was created directly by the kernel. If a child process exits or is interrupted, then a SIGCHLD signal is send to the parent process.

How do you create a process for kids?

Fork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction following the fork() system call.

Can a process have more than one parent?

No. By definition of what we understand as parent process, it's the process that spawned the child process. One process has to do that. And that's the parent.

How many child processes can be created?

There are four different ways to create a child process in Node: spawn() , fork() , exec() , and execFile() . We're going to see the differences between these four functions and when to use each.


1 Answers

To create a second process, call fork() again - either within the parent or the child (but not both!). Which you choose depends on whether you want this process to be a child of the original parent or a child of the first child process (it is usual for it to be a child of the original parent).

Communicating through a pipe is much simpler and more reliable than using signals. pipe(), close(), read(), write() and select() are the key functions here.


For example, to have the parent create two child processes, you would do something like:

pid_t child_a, child_b;

child_a = fork();

if (child_a == 0) {
    /* Child A code */
} else {
    child_b = fork();

    if (child_b == 0) {
        /* Child B code */
    } else {
        /* Parent Code */
    }
}
like image 90
caf Avatar answered Sep 19 '22 05:09

caf