The Story Behind Processes: Mastering the Heart of Linux OS!

Linux | Red Hat Certified | IT Technology | Operations Engineer

👇 Join the technical exchange QQ group with 1000 members. Note 【Official Account】 for quicker approval.

The Story Behind Processes: Mastering the Heart of Linux OS!

1. In-Depth Understanding of Basic Process Concepts
Textbook Concept: An execution instance of a program, a program that is currently executing, etc.
Kernel Perspective: An entity responsible for allocating system resources (CPU time, memory).
Since the concept of processes is very important, we will explain it again here:
When we have written a program, compiling the source code generates the corresponding executable program. When we run this program with ./, the operating system does two things:
1) Load the executable program into memory:

The Story Behind Processes: Mastering the Heart of Linux OS!

However, even though the executable program has been loaded into memory, the operating system cannot manage these programs at this point because there is no data structure in memory that describes the relevant attributes. Therefore, in the operating system, to describe the relevant attributes of the program loaded into memory, a concept called PCB is derived. (In Linux, it is called task_struct) The Linux operating system is written in C, and essentially task_struct is a structure. Each task_struct stores the attribute information of the corresponding process.
2) Load the code and data into the PCB (task_struct):

The Story Behind Processes: Mastering the Heart of Linux OS!

This operation ultimately achieves one goal: transforming the operating system’s management of processes into the management of specific data structures.
2. Managing Processes Effectively
1. Methods for Effective Process Management
To manage processes effectively, we first need to describe the processes well, and the management of processes still satisfies our previously drawn conclusion:
First describe, then organize.
2. Describing Processes – PCB
Process information is stored in a data structure called the Process Control Block, which can be understood as a collection of process attributes. In textbooks, it is referred to as PCB (process control block), and in the Linux operating system, the PCB is: task_struct (which is also the node of the linked list organizing processes shown above)
task_struct – A Type of PCB
In Linux, the structure used to describe processes is called task_struct.
task_struct is a data structure in the Linux kernel that is loaded into RAM (memory) and contains information about the process.
// task_struct structure comments://  ==========================long state // Task running state (-1 not runnable, 0 runnable (ready), >0 stopped).long counter // Task running time count (decrementing) (ticks), running time slice.long priority // Running priority. When the task starts running, counter = priority, the larger it is, the longer it runs.long signal // Signal. It is a bitmap, each bit represents a signal, signal value = bit offset + 1.  struct sigaction sigaction[32] // Signal execution attribute structure, corresponding to the operation and flag information to be executed for the signal.  long blocked // Process signal mask (corresponding to signal bitmap).//  --------------------------  int exit_code // Exit code when the task execution stops, which will be taken by its parent process.  unsigned long start_code // Code segment address.  unsigned long end_code // Code length (in bytes).  unsigned long end_data // Code length + data length (in bytes).  unsigned long brk // Total length (in bytes).  unsigned long start_stack // Stack segment address.  long pid // Process identifier (process number).  long father // Parent process number.  long pgrp // Parent process group number.  long session // Session number.  long leader // Session leader.  unsigned short uid // User identifier (user id).  unsigned short euid // Effective user id.  unsigned short suid // Saved user id.  unsigned short gid // Group identifier (group id).  unsigned short egid // Effective group id.  unsigned short sgid // Saved group id.  long alarm // Alarm timer value (ticks).  long utime // User mode running time (ticks).  long stime // System mode running time (ticks).  long cutime // Child process user mode running time.  long cstime // Child process system mode running time.  long start_time // The moment the process starts running.  unsigned short used_math // Flag: whether a coprocessor has been used.//  --------------------------  int tty // The sub-device number of the tty used by the process. -1 means not used.  unsigned short umask // File creation attribute mask bit.  struct m_inode * pwd // Current working directory inode structure.  struct m_inode * root // Root directory inode structure.  struct m_inode * executable // Executable file inode structure.  unsigned long close_on_exec // Bitmap flag for closing file handles on execution. (See include/fcntl.h)  struct file * filp[NR_OPEN] // File table structure used by the process.//  --------------------------struct desc_struct ldt[3] // Local descriptor table for this task. 0-empty, 1-code segment cs, 2-data and stack segment ds&ss.
task_struct contents (internal variables) classification:
    Identifier: Unique identifier describing this process, used to distinguish it from other processes.        State: Task state, exit code, exit signal, etc.        Priority: Priority relative to other processes.        Program counter: Address of the next instruction to be executed in the program.        Memory pointer: Includes pointers to program code and process-related data, as well as pointers to shared memory blocks with other processes.Context data: Data in the processor registers during process execution [Example of rest study, add image CPU, registers].I/O status information: Includes displayed I/O requests, I/O devices allocated to the process, and the list of files used by the process.Accounting information: May include total processor time, total clock counts used, time limits, accounting numbers, and other information.
3. Organizing Processes
It can be found in the kernel source code. All processes running in the system exist in the kernel in the form of a task_struct linked list. (The example below uses a linked list to organize processes)

The Story Behind Processes: Mastering the Heart of Linux OS!

The Story Behind Processes: Mastering the Heart of Linux OS!

For course inquiries, add: HCIE666CCIE

↑ Or scan the QR code above ↑

What technical points and content do you want to see?

You can leave a message below to let Xiao Meng know!

Leave a Comment