A process is a program in execution, with it associated are process context and executable instruction.
A process has its own data, code, stack, register and memory space. Every process has its own virtual memory address range, I/O resources, opened files etc.
Creation of a process
Most widely used method to create a process is to use 'fork' and 'exec' system calls. As mentioned earlier, every process has parent, parent uses fork system call to create exactly same copy of itself. Once new process is scheduled, it can use exec system call to execute any program it wants to.
fork is a call where one process goes in and two come out. They both start there execution from the statement just after fork call (Remember new process is exact copy, hence its PC will be same).
How to distinguish between parent and child process? fork comes to rescue there. Call to 'fork' return child process's PID to parent process while zero to child process. By having check on return value of 'fork' system call we can figure out which process is parent and which is child.
Now, fork can be a very expensive call as OS has to duplicate whole lot of information, especially the virtually memory and pages currently used by the parent process. There is one concept which is called 'Copy on Write', so fork system call will not copy any of the pages till the time one of the process tries to modify the page. This arrangement makes fork system call fast.
Other system call is exec(). It is used to start a new program, it will replace contents of process with of program binary. There are many versions of the same system call used for varying purposes.
To wait for the return status, parent process uses wait() system call. It blocks the parent process till the time one of its child returns status. Usually return status of child process is used to check if the child process terminated normally or abnormally. Child process can inform their exit status using SIGCHILD signal.
There are variants of wait() like wait3() and wait4() which are non blocking call on parent process.
Read full article from Algorithms and Me: Anatomy of a Process
A process has its own data, code, stack, register and memory space. Every process has its own virtual memory address range, I/O resources, opened files etc.
Creation of a process
Most widely used method to create a process is to use 'fork' and 'exec' system calls. As mentioned earlier, every process has parent, parent uses fork system call to create exactly same copy of itself. Once new process is scheduled, it can use exec system call to execute any program it wants to.
fork is a call where one process goes in and two come out. They both start there execution from the statement just after fork call (Remember new process is exact copy, hence its PC will be same).
How to distinguish between parent and child process? fork comes to rescue there. Call to 'fork' return child process's PID to parent process while zero to child process. By having check on return value of 'fork' system call we can figure out which process is parent and which is child.
Now, fork can be a very expensive call as OS has to duplicate whole lot of information, especially the virtually memory and pages currently used by the parent process. There is one concept which is called 'Copy on Write', so fork system call will not copy any of the pages till the time one of the process tries to modify the page. This arrangement makes fork system call fast.
Other system call is exec(). It is used to start a new program, it will replace contents of process with of program binary. There are many versions of the same system call used for varying purposes.
- The calls with
vin the name take an array parameter to specify theargv[]array of the new program. - The calls with
lin the name take the arguments of the new program as a variable-length argument list to the function itself. - The calls with
ein the name take an extra argument to provide the environment of the new program; otherwise, the program inherits the current process's environment. - The calls with
pin the name search thePATHenvironment variable to find the program if it doesn't have a directory in it (i.e. it doesn't contain a/character). Otherwise, the program name is always treated as a path to the executable
To wait for the return status, parent process uses wait() system call. It blocks the parent process till the time one of its child returns status. Usually return status of child process is used to check if the child process terminated normally or abnormally. Child process can inform their exit status using SIGCHILD signal.
There are variants of wait() like wait3() and wait4() which are non blocking call on parent process.
Read full article from Algorithms and Me: Anatomy of a Process
No comments:
Post a Comment