Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It might help to first understand why the word fork was used to name this function. Ever heard of a "fork on the road?" At a fork, the process has to split paths.</p> <p>First there is a single process executing normally until you reach the <code>fork</code> call. When fork is called, a new process is created, which is identical in virtually every way as the original process, <strong>except for the return value of the fork function.</strong> The newly created process is called the child process, and hence the process that spawned it is referred to as the parent process.</p> <p>Since you'd want to perform different tasks for each branch of the fork, it necessitates that you be able to distinguish the child process from the parent process. That's where the return value of <code>fork</code> comes in: <code>fork</code> returns the process id (pid) of the child (the newly created process) to the parent; it returns 0 to the child. Also, should the execution of <code>fork</code> go wrong, the return value is -1.</p> <p>In your code, you don't distinguish between the child and parent process, so both processes run the entire code that follows after the <code>fork</code> call.</p> <pre><code>//what the child process looks like after fork is called int main() { int a, b; b=fork(); // &lt;-- current line of execution: 0 is returned to b printf("\nmy name is manish\n"); printf("\n my name is anil\n"); printf("\n the value of b = %d",b); } // what the parent process looks like after fork is called int main() { int a, b; b=fork(); // &lt;-- current line: child process id is returned printf("\nmy name is manish\n"); printf("\n my name is anil\n"); printf("\n the value of b = %d",b); } </code></pre> <p>As you can see, both processes have the same code following the fork, hence the output is repeated. Perhaps if you want the parent process to output Manish and the child to output Anil, then you can do something like:</p> <pre><code>int main() { pid_t b; // note that the actual return type of fork is // pid_t, though it's probably just an int typedef'd or macro'd b = fork(); if (b == -1) perror("Fork failed"); else if (b &gt; 0) { printf("My name is Manish\n"); // parent process else printf("My name is Anil\n"); // child process printf("The value of b is %d\n", b); return 0; } </code></pre> <p>Finally, the last comment that must be made is that in your code, the output appears to have been executed first by one process in its entirety and then the other process in its entirety. That may not always be the case. For example, the operating system might allow the parent to execute the 'manish' output, then make this process wait, and handing the cpu over to the child process, which then executes 'manish'. However, the child process may continue and execute 'anil' and 'b' outputs, completing execution of the child process and thus returning execution back to the parent process. Now the parent finishes its execution by outputting 'anil' and 'b' itself. The final output of running this program may look something like:</p> <pre> my name is manish // executed by parent my name is anil // child the value of b = 0 // child my name is anil // parent the value of b = 2244 // parent manish.yadav@ws40-man-lin:~$ </pre> <p>Take a look at the man page for <a href="http://linux.die.net/man/2/fork" rel="noreferrer"><code>fork</code></a>. Also look at <a href="http://linux.die.net/man/2/waitpid" rel="noreferrer"><code>waitpid</code></a> for proper handling of child processes by parent processes so you don't create zombies.</p> <p>Edit: In response to your questions in the comments, I'll answer how you can simply run each process consecutively.</p> <pre><code>int main() { pid_t pid; int i; for (i=0; i&lt;NUM_PROCESSES; i++) { pid = fork(); if (pid == -1) { perror("Error forking"); return -1; } else if (pid &gt; 0) { // parent process waitpid(-1, NULL, 0); //might want to look at man page for this // it will wait until the child process is done } else { // do whatever each process needs to do; // then exit() doProcess(i); exit(0); } } // do anything else the parent process needs to do return 0; } </code></pre> <p>Of course, isn't the best code, but it's just to illustrate the point. The big idea here is the <strong><code>waitpid</code></strong> call, which causes the parent process to wait until the child process it just <code>fork</code>ed to terminate. After the child prcoess completes, the parent continues after the <code>waitpid</code> call, starting another iteration of the <code>for</code> loop and forking another (the next) process. This continues until all child process have executed sequentially and execution finally returns to the parent.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload