Process Concept – II

Continuing from the previous post, we discuss operations on processes. In this post, we discuss process creation in detail. Process creation using fork() system call, process tree, example of process creation using UNIX system calls [fork(), exec(), wait() ] have been discussed here. 

OPERATIONS ON PROCESSES

Process Creation
  • Each process usually identified using Process identifier (pid) : unique integer value for each process in the system
  • During execution, a process may create several new processes.
  • Creating process → Parent Process
  • Created process → Child process
  • These new processes may further create new processes → forms Tree of processes
  • This tree can be created by recursive tracing of parent processes upto the init process
Process tree on a typical Linux system
Process tree on a typical Linux system
           
Example of Process tree in Linux OS
  • init process :  pid = 1, serves as parent of all processes
  • after system boot, init creates multiple user processes
  • sshd : responsible for managing ssh connected clients
  • kthreadd : responsible for creating additional processes that perform tasks on behalf of the kernel
  • login : responsible for managing clients that log on directly on the system
Allocating resources to child processes
  • child created → resources like CPU time, memory, files I/O devices needed to accomplish its task
  • 2 ways of obtaining resources :
    • get resources directly from the OS
    • get a subset of parent’s resources [to prevent any process from overloading the system by creating too many child processes]
  • Parents can give resources to children in 2 ways :
    • partition resources among children
    • share resources (such as memory) with the children.
  • Parents may also need to pass along initialization data to child process [other than the logical and physical resources]
    • Example : Consider a process which has been created to display contents of a file on the screen.
    • To do that, when the child process is created, the parent passes the name of the file to the child. This is used by the child to open the file and write the contents out.
Execution of Process alongside Child process
  • On creation of child process :
    • Parent can continue to execute concurrently with its children
    • Parent can wait until some or all of the children have been terminated.
Address space of the child process
  • There are 2 possibilities for address space of new process
    • Child process → duplicate of parent process → same program and data as parent
    • Child process has new program loaded into it

Let us discuss an example for Process Creation to understand it more clearly. First we will discuss process creation in UNIX operating system and then take the discussion forward with the Windows Operating system. UNIX OS
  • In UNIX, as discussed already, each process is identified by a unique integer known as the Process id (pid).
  • Creation of new process via → fork() system call
 → Understanding the fork() system call
  • fork() takes no arguments, but it returns the process id
  • When a process calls fork(), it creates a new process which becomes the child of the calling process
  • The child process consists of a copy of the address space of the parent process → this helps in communication between the parent and the child
  • The child also inherits privileges and scheduling attributes from the parent, as well as certain resources, such as open files, from the parent.
  • Both the parent and the child continue their execution at the instruction after the fork() system call.
Before fork()  
After fork() executed 
  • Since both the child contains the same exact copy of parent’s address space (i.e. both have separate address spaces), therefore we use the return value of fork() to distinguish parent from child.
  • Both processes have identical but separate address spaces → variables initialized before fork() system call have same value in both address spaces.
    • However, From this point onwards, due to separate address spaces, modifications, if any, will be independent of each other.
    • Therefore, if the child changes value of any variable, it will affect only the variable in child’s address space. All the other address spaces, even if they contain variables with identical names, will not be affected.
  • Let the value returned by fork() be ret, i.e.
ret = fork();
  • if ret < 0 → child creation was unsuccessful
  • if ret == 0 → child process
  • if ret > 0 → return pid of child process inside parent process
What after fork() ?
  • Sometimes, the new process created via fork() system call is not a part of the same program as its parent.
  • In these cases, the process which has to go separate ways uses the exec() system call to replace the process’s memory space with a new information from a program binary.
  • Thus, the exec() system call loads a binary file into memory (destroying the memory image of the program containing the exec() system call) ans start its execution.
  • In this manner, two processes are able to communicate and then go separate ways.
This is the general process of running a new program, i.e.
  • first fork() → creating a new process
  • then exec() → load into memory and execute the program binary which is supposed to run.
What after exec() ?
  • It all depends on the requirements.
    • The parent may create more children.
    • If the parent has nothing else to do, it can issue a wait() system call to move itself off the ready queue until the termination of the child.


Process Creation using fork() system call
Process Creation using fork() system call
Process Creation in Windows is done in a similar manner using the Windows API.
  • CreateProcess() similar to fork(), used to create new process
    • fork() has child process inheriting the address space
    • CreateProcess() requires loading a specified program into the address space of the child process during process creation.
    • requires parameters, unlike fork() which requires no parameters.

In the next post, we will take this discussion forward by understanding the concepts of Process Termination.

Until then, keep reading and sharing 🙂