![]() |
VOOZH | about |
A process is an instance of a program running, and its lifecycle includes various stages such as creation, execution, and deletion.
Operating systems like Windows and Linux maintain a parentâchild hierarchy of processes. Every new process is created by an already running process using system calls, making the creator the parent and the newly formed one the child.
Steps involved in creating a new process:
PID Assignment:
The OS gives the new process a unique PID and adds an entry for it in the process table.
Memory Allocation:
PCB Initialisation:
Other Structures:
Process creation in many operating systemsâespecially Unix-like systemsâis done using the fork() system call. ll. When a running process calls fork(), the operating system creates a new process. The original process becomes the parent, and the newly created one is the child.
fork() system call creates a copy of the current process, including all its resources, but with just one thread.exec() system call replaces the current process's memory with the code and data from a specified executable file. It doesnât return; instead, it "transfers" the process to the new program.waitpid() function makes the parent process wait until a specific child process finishes executing.Example:
int pid = fork();
if (pid == 0)
{
/* Child process */
exec("foo");
}
else
{
/* Parent process */
waitpid(pid, &status, options);
}
In Windows, the system call used for process creation is CreateProcess(). This function is responsible for creating a new process, initializing its memory, and loading the specified program into the process's address space.
CreateProcess() in Windows combines the functionality of both UNIX's fork() and exec(). It creates a new process with its own memory space rather than duplicating the parent process like fork() does. It also allows specifying which program to run, similar to how exec() works in UNIX.CreateProcess(), you need to provide some extra details to handle any changes between the parent and child processes. These details control things like the processâs environment, security settings, and how the child process works with the parent or other processes. It gives you more control and flexibility compared to the UNIX system.Processes terminate themselves when they finish executing their last statement, after which the operating system uses the exit() system call to delete their context. Then all the resources held by that process like physical and virtual memory, 10 buffers, open files, etc., are taken back by the operating system. A process P can be terminated either by the operating system or by the parent process of P.
A parent may terminate a process due to one of the following reasons:
A process can be terminated/deleted in many ways. Some of the ways are:
abort() system call.SIGSTOP to pause the child or SIGKILL to immediately terminate it.