1

In the book 'Operating System Concepts' - 9th Edition - Chapter 3 - Page 117 - Page 120 it says:

Both processes (the parent and the child) continue execution at the instruction after the fork(), with one difference: the return code for the fork() is zero for the new (child) process, whereas the (nonzero) process identifier of the child is returned to the parent.

The only difference is that the value of pid (the process identifier) for the child process is zero, while that for the parent is an integer value greater than zero (in fact, it is the actual pid of the child process).

Please can someone explain this concept to me.

Community
  • 1
  • 1
S. Salman
  • 590
  • 1
  • 6
  • 22

4 Answers4

3

Whenever a processor(CPU) runs a program , it stores the line number of the code it is executing (more formally address pointing to that instruction).The computer stores it in a register (kind of variable) called as stack pointer. So the stack pointer(SP) will store the current instruction processor has to execute (or run). This way computer track which instruction should be executed .

When a program runs , it is allocated some small memory in the computer's main memory.Here's where all our code along with important registers(including SP) which help processor to keep track of program when it's running.Too a process is uniquely identified by Process ID(PID).

Now let me come to your question. Whenever we call fork , a copy of the program from which you called fork is created. This copy is called as the "child process" and our original process is known as parent process.

When the copy created , all the memory that your program has been allocated is copied to some other place in memory (which is now child process's memory).So an identical running program(process) is created.

Now this copied memory contains the SP of the parent process , so whenever processor runs the program it directly runs the program from the same fork call line (since SP will store this line as current instruction when the process is created).Since our fork call was successful , it has to return a non-negative value (denoting success of fork system call)So it return 0 to the child process and child process ID to the parent(since the current Process ID was pointing here too).

Returning Child Process ID to parent makes a good deal since it's better that parent can keep a track of child process created from it.Too returning child a 0 make the deal even better as we have to return a non-negative number and other positive number may be some process's PID.

Run cd /proc in your linux system . All the directories having some numeral name are pid of some process(which may be active/inactive).Read more about it to clear the concept.

Hope that clears your doubt :).

Laschet Jain
  • 734
  • 1
  • 8
  • 24
0

when you call fork in your code it returns two values in case call is success one for parent (the code run by parent) and zero for child (the code run by child)

int childparent;
childparent = fork();
if (childparent >=0)
{
    if (childparent==0)
        //child code
    if (childparent>0)
    //parent code
}

the pid 0 mention in your sentence is not the process id shown by shell command ps

Edit: yes the code running in the parent (if childparent>0) case is running in context of that specific child which is just created. so return value to parent is the child actual Process ID (PID). if you fork in your simple code and sleep for long time in code enough to run ps you can match the PIDs shown in PS and printf in parent the return value of the fork() (printf("%d",childparent))

incompetent
  • 1,715
  • 18
  • 29
  • As I'm new to this topic can you please explain the creation of a new process (child process) in more detail. – S. Salman Sep 14 '15 at 06:10
  • i dont know what do you want to know more. when a process is created it runs in parent process space. what does it mean is that child is just duplicate of its parent with specific differences. all memory of parent is copied to child along with all fd opens. when you call one of exec function child is detached from parent – incompetent Sep 14 '15 at 06:25
  • i suggest you to read some other book before absorbing this book – incompetent Sep 14 '15 at 06:25
0

fork() makes a complete copy of the current process by creating a new process and then filling it with the current process. It chooses one to be the parent and the other to be the child. It indicates the difference only by adjusting the return value of fork(). If the process(es) ignore the return value, they behave identically. (There are some rarely significant other differences between the processes related to signal handling and delivery, file descriptors opened with special attributes, memory maps, etc.)

When you think you begin to understand it, look at this to see if you do.

Community
  • 1
  • 1
wallyk
  • 56,922
  • 16
  • 83
  • 148
0

I had this doubt when I was reading the book too. The answer is as follows:

When the main program (parent) executes fork(), a copy of its address space, including the program and all data, is created. System call fork() returns the child process ID to the parent and returns 0 to the child process. Both the parent and the child process can now start their execution from the immediate next line after the fork system call. Let me illustrate this with a simple example.

Consider this code:

main()
{
    pid=fork();
    if(pid == 0) // Condition to determine Parent/Child Process
        ChildProcess();
    else
        ParentProcess();
}

void ChildProcess()
{
    //Some Arbitrary Code
}

void ParentProcess()
{
    //Some Arbitrary Code
}

This snippet explains that based on a condition, both the processes (parent and child) can now execute in their own pre-defined way.

In the above example, say the process id of the child is 3456, then the parent would get that id as the return value from fork. However, the child will always get the process-id as 0 and then the execution continues.

The design of the fork() call is such because the complete administration of the child process(es) can now be handled by the parent and the parent can always keep a note of which of the child processes terminates, normally or abnormally, by implicitly/explicitly invoking exit() system call. A parent may also simply wait for the child by making a wait() system call which then return the pid of the child and in this way, the parent can keep a note of which child has terminated.

This is how child process creation and termination is handled.

There is one more thing I would like to add here which is not completely relevant to the question but I think would be helpful.

You would also have noticed the description of the exec() system call immediately after this discussion in the book. In short, both the discussions explain this:

Forking provides a way for an existing process to start a new one, but what about the case where the new process is not part of the same program as parent process? This is the case in the shell; when a user starts a command it needs to run in a new process, but it is unrelated to the shell.

This is where the exec system call comes into play. exec will replace the contents of the currently running process with the information from a program binary.

Thus the process the shell follows when launching a new program is to firstly fork, creating a new process, and then exec (i.e. load into memory and execute) the program binary it is supposed to run.

If you would like to know more about the fork() system call then you should also know about its internal implementation, especially how the clone() system call works.

Reference Sites:

The fork() system call article by MTU

How Fork & Exec work together