top of page

Creating and deleting processes in the Linux

Jun 15, 2024

3 min read

0

99

Creating and deleting processes in the Linux kernel involves several key steps, each contributing to the management and execution of processes within the operating system. Here’s an overview of these steps:


Process Creation Steps

1. Fork System Call:

- Purpose: Initiate process creation.

- Description: The `fork()` system call is used to create a new process that is a copy of the calling process (parent process). Upon successful creation, two processes exist: the parent and the child.


2. Memory Allocation:

- Purpose: Allocate memory space for the new process.

- Description: The Linux kernel allocates a new memory space for the child process, copying the address space of the parent process using a copy-on-write mechanism. This ensures that initially, both processes share the same memory pages until one modifies them.


3. Process Initialization:

- Purpose: Initialize process attributes.

- Description: The child process needs initialization to differentiate it from the parent. This includes setting up the process ID (`pid`), process group (`pgid`), session (`sid`), file descriptors, signal handling, and other attributes.


4. Exec System Call:

- Purpose: Replace the process memory image with a new program.

- Description: After `fork()`, a child process typically uses the `exec()` family of system calls (e.g., `execve()`) to replace its memory space with a new program. This step is crucial for executing different applications or commands within the same process context.


5. Task Struct Creation:

- Purpose: Create a task structure for the new process.

- Description: In the Linux kernel, each process is represented by a `task_struct` structure. During process creation, this structure is allocated and initialized to store various attributes and state information of the process.


6. Task Scheduler Addition:

- Purpose: Add the new process to the task scheduler's run queue.

- Description: Once initialized, the new process is added to the appropriate run queue by the Linux scheduler. The scheduler determines when and how long the process will execute on the CPU based on scheduling policies (e.g., fair scheduling, real-time scheduling).


Process Deletion Steps


1. Exit System Call:

- Purpose: Initiate process termination.

- Description: When a process completes its execution or encounters an error, it invokes the `exit()` system call. This call triggers the cleanup and termination process for the process.


2. Resource Reclamation:

- Purpose: Reclaim resources allocated to the process.

- Description: Upon process termination, the kernel releases resources associated with the process, including memory pages, open file descriptors, allocated semaphores, and other kernel resources.


3. Parent Notification:

- Purpose: Notify the parent process of the child's termination.

- Description: If a child process terminates, the parent process is notified via the `SIGCHLD` signal. The parent can then collect exit status information of the child using the `wait()` or `waitpid()` system calls.


4. Task Struct Cleanup:

- Purpose: Free the `task_struct` of the terminated process.

- Description: The `task_struct` of the terminated process is deallocated, freeing up kernel memory.


5. Process Group and Session Cleanup:

- Purpose: Clean up process group and session information.

- Description: If the terminating process was a group leader or session leader, associated process group and session information are cleaned up.


6. Finalization:

- Purpose: Perform final cleanup tasks.

- Description: Any final cleanup tasks specific to the terminated process are performed, ensuring that all resources are properly released and the system state remains consistent.


Understanding these steps provides insights into how the Linux kernel manages process creation and deletion, ensuring efficient resource utilization and robust process management in multi-tasking environments.


#linuxdevicedrivers #ldd #linuxlovers



Jun 15, 2024

3 min read

0

99

bottom of page