Fork() Defination : In computing, the fork is an operation whereby a process creates a copy of itself. it is usually called a system call, implemented in the Kennel. Simply We can say that fork is the Primary method of process Creation It takes no arguments and returns a process ID
Overview In multitasking operating systems, processes (running programs) need a way to create new processes , to run other programs. In Unix systems equipped with virtual memory support (practically all modern variants), the fork operation creates a separate address space for the child. The child process has an exact copy of all the memory segments of the parent process.
Purpose of fork() The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call. Unix will make an exact copy of the parent's address space and give it to the child. Therefore, the parent and child processes have separate address spaces .
A=1 B=2 Example of Fork () Process A=1 B=2 Parent Child A=7 B=5 A=1 B=2 Exit Change the Child Process value
One Process Not effect the other Process If Child Process Return nothing
A Process executes the code Fork() Fork() Fork() The total number of the child process created is 3 (B ) 4 (C) 7 (D) 8 Example of Fork ()
Example of Fork () Main Process Child Process 1.Fork () 2.Fork () 3.Fork () 1 2 3 4 5 6 7 8 Answer is 7
System Call “exec” family
exec() In computing, exec is a functionality of an operating system that runs an executable file in the context of an already existing process, replacing the previous executable.
Why “exec” functionality ? Is fork enough to create a new process with different code and data segment in the same process id ?
Forking creates a process but it is not enough to run a new program, to do that the forked child needs to overwrite its own image with the code and data of the new program and mechanism is exec() creation.
W hen “exec()” call ? No new process create. Replace the data segment and code segment of current process. Process ID remain same. Calling process data and code is gone . Cannot return to the caller .
How exec work ? i nt main(){ doWorkOne (); call B.exe via exec doWorkTwo (); return 0; } int main(){ doWorkOne (); call B.exe via exec….. doWorkTwo (); ×× return 0; } A.cpp / A.exe int main(){ doWorkThree (); return 0 } B.cpp / B.exe int main(){ doWorkThree (); return 0; }
C language prototypes int execl (char const *path, char const *arg0, ...); int execle (char const *path, char const *arg0, ..., char const * envp []); int execlp (char const *file, char const *arg0, ...); int execv (char const *path, char const * argv []); int execve (char const *path, char const * argv [], char const * envp []); int execvp (char const *file, char const * argv []); “ l ” is specified as a list of arguments. “ v “ is specified as a vector (array of character pointers). “ e “ environment is specified as an array of character pointers. “ p “ user's PATH is searched for command, and command can be a shell program If exec return value<0 then Error occurred in “exec” call . Else successfully call the “exec”
Applications If any program need run a new process in the same context ( i.e : open a program from another program). Continuously run new process from child process ( i.e : Terminal ).