In: Computer Science
Difference between a process and a program.
PROCESS: If a Program in Execution, then it is called Process. It is also the amount of work done to run a program.
PROGRAM: A sequence of instructions written in any Programming Language using an extension. It is also the subpart of process.
PROCESS |
PROGRAM |
Consists of Instructions are in Machine code at the time of Execution. |
Instructions are in any programming languages like c++, Java etc. |
Reside in Main Memory |
Reside in Secondary Storage Devices. |
Process is a Dynamic Object |
Program is Static Object |
Span of time is Limited |
Span of time is unlimited |
Entity is active and perform the intended functions of its related program |
Entity is passive such as the contents of a file stored on a Disk |
Basic process management is done with many system calls. System calls can be combined to solve a complex task. Fork () System call is used to create a duplicate process or new process is known as child process. The main process or calling process is known as Parent process. When the child process is created by using fork system call, both parent and children will have same relative address and different absolute address. Using the Process ID (pid) we can identify parent and child. After the fork () system call, the return value of parent pid is the child pid and the child pid is zero.
Exit () is a system call for the termination of process after it finishes the execution. There is no return value for exit () System call. Wait () is another system call that blocks the execution of parent process until one of its child process terminates. After child exits, parent process will resume its action.
Process whose parent process has terminated after calling the fork () system call, is called orphan process. The child remains running itself. The orphaned process will give to a special init system process or root process with PID 1 in the case of UNIX operating systems. Kernel, the main component in the OS for inter-process communication automatically sets the parent as init. This is known as re-parenting. After that, root process waits for the termination of orphan process. In the case of other operating systems, kernel suddenly terminates the orphan process. When this concept coming into modern Linux systems, init is replaced by sub reaper. Orphaned process will be given to the nearest still living ancestor subreaper.
Given below is the simple C language code for the Explanation of fork () System call.
int main()
{
// fork () Create a child process
int pid = fork();
if (pid > 0)
{
//getpid() returns process id
// while getppid() will return parent process id
printf("Parent process\n");
printf("ID : %d\n\n",getpid());
}
else if (pid == 0)
{
printf("Child process\n");
// getpid() will return process id of child process
printf("ID: %d\n",getpid());
// getppid() will return parent process id of child process
printf("Parent -ID %d\n\n",getppid());
sleep(10);
// At this time parent process has finished.
// So if u will check parent process id
// it will show different process id
printf("\nChild process \n");
printf("ID: %d\n",getpid());
printf("Parent -ID: %d\n",getppid());
}
else
{
printf("Failed to create child process");
}
return 0;
}
Output:
Child Process ID: 12591
Parent – ID 1