FORK() function   - Overview

# Fork() function 
<!-- 発端や概要を記載 -->
This is the summary of what the fork function does and what activities we'd expect to see in the operating system after calling this function from a program. 
I wrote this in English, but please use google-translater if you prefer reading this in Japanese;)

# 目次
<!-- タイトルとアンカー名を編集 -->
1. [Fork Definition](#fork-definition)
1. [Impact on Storage](#impact-on-storage)
1. [States](#states)
1. [Behavior](#behavior)
1. [参考文献](#参考文献)

<!-- 各チャプター -->
<a id="#fork-definition"></a>
# Fork Definition
The fork function is a facility in operating systems, mainly Linux, to duplicate the caller process. The newly created process is identified as a child process that has a pointer to the caller and inherits all properties of its parent—including all file registers, environment strings, etc. Although the child looks inferior, **it is a different process so that it has its own process image stored in a new address space**. It is clear that any process created from a fork will have the same properties in common.

<a id="#impact-on-storage"></a>
# Impact on Storage
Regarding the memory locations of the two processes, if the fork is successful, UNIX will make one identical but a **separate copy of the virtual address spaces** for the child. Thus, any modifications to one of the processes would be independent of the other. Even though the parent and child processes will have identical variable names, changes in variables to either process will **not** affect the other process because it happens in its own address space. 

<a id="#states"></a>
# States
Since the parent process invokes the fork function, then it is in a running state. As soon as the parent process calls the fork function, a new child process is created and is in the new state. Then, the child is immediately admitted to the ready state. Now the child and the parent are both ready to run simultaneously and will compete for the CPU. That is, **either process could be in the running state or the blocked state**. Hence the consequence is that there is no guarantee that for which process will reach the exit state first. Sometimes we want to make sure that the execution of the parent process finishes after all its children’s processes. In order to guarantee the situation, the parent process must wait until all the child processes are executed, the “wait” call can be used. During the wait time, the parent process will be in the blocked state. Hence, there is no competition here so that the child process is the only running process.

<a id="#behavior"></a>
# Behavior
After a fork, the child process will have a **unique process ID (PID)**. The return values to the fork() call in the parent process and child process are different and important. Since the two processes will go on to execute their own set of instructions, it is necessary for the operating system to distinguish the child process from the parent process, and this could be done by examining the **returned value of the call**. If a fork is successfully completed, **the fork command will return 0 to the child process, and return the PID of the child process to the parent process (a positive value)**. Otherwise, a negative number is returned to the parent process to indicate an error and no child process will be created. Thus, based on the returned value, the system can identify which one is the parent and which one is the child, or that an error case of unsuccessful child creation has occurred. There is a unique behavior of duplication in the fork function. Supposed that there is a process called the fork function, it will create a new child process. If the parent process calls another fork function, then it will create another child process; and so does the existing child. In other words, the number of fork invocations elicits processes in 2^(n), where n is the number of invoked forks.



<a id="#参考文献"></a>
# 参考文献
- [書籍、ウェブサイト](URL)
- [リンクテキスト](URL)