Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of process thread and scheduling in Linux

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "what is the concept of Linux process thread and scheduling", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the concept of Linux process thread and scheduling".

1 process concept 1.1 definition of processes and threads classic definition in the operating system: process: resource allocation unit. Thread: scheduling unit. PCB (Process Control Block, process control block) is used to describe the process in the operating system. The PCB in Linux is the task_struct structure.

1.2 process life cycle 1.2.1 process state R, TASK_RUNNING: ready or running, the process is ready to run, but not necessarily occupying CPU S, TASK_INTERRUPTIBLE: shallow sleep, waiting for resources, can respond to signals, generally process active sleep into state D, TASK_UNINTERRUPTIBLE: deep sleep, waiting for resources, not responding to signals Typical scenario is that the process acquires semaphore blocking Z, TASK_ZOMBIE: zombie, the process has exited or ended, but the parent process does not know, T, TASK_STOPED: stop, debug status, receive SIGSTOP signal process suspend

1.2.2 process creation and demise-related API1) system () starts a new process by calling shell

2) exec () starts a new process by replacing the current process image

3) fork () starts a new process by copying the current process image. In the child process, fork () returns 0, and the parent process fork () returns as the child process ID.

4) the wait () parent process hangs and waits for the child process to finish.

5) Orphan process and zombie process orphan process: if a parent process exits and one or more of its child processes are still running, those child processes will become orphan processes. The orphan process will be adopted by the init process (process number 1), and the init process will collect the status of them. The orphan process does not waste resources. Zombie process: a process uses fork to create a child process. If the child process exits and the parent process does not call wait or waitpid to get the status information of the child process, then the process descriptor of the child process is still saved in the system. This process is called a zombie process. Zombie processes waste system resources (the process descriptor task _ struct exists, the resources occupied by the process are recycled, there is no memory leak, and basically no system resources are wasted, see Song Baohua's course). Avoid zombie processes: causes of zombie processes: 1. After the child process ends, the parent process sends a SIGCHLD signal to the parent process, and the parent process ignores it by default; 2. The parent process does not call the wait () or waitpid () function to wait for the end of the child process. Avoid zombie process methods: 1, the parent process calls wait () or waitpid () to wait for the child process to finish, so that the parent process generally blocks at the wait and cannot handle anything else. 2. Capture the SIGCHLD signal and call the wait function in the signal processing function, which can avoid the problems described in 1. 3. Fork twice, the father process creates the son process, the son process creates another grandson process, then the son process commits suicide, and the grandson process becomes an orphan process is adopted by the init process.

1.3 Inter-process communication 1) signal signals refer to events. For example, pressing the CTRL-C key combination will send a SIGINT signal, which can be captured in the process and processed accordingly.

2) everything in the pipeline PIPE is a file, and the operation of the pipeline is also similar to the operation of the file. The popen () function is similar to the fopen () function and returns an object pointer. The pipe () function is similar to the open () function and returns an object descriptor. Pipes transfer data between kinship processes (related processes created by the same parent process).

3) named pipes FIFO named pipes can be used for unrelated interprocess communication. Mkfifo () / mknod () will create a file with a path and name in the file system. Just use this pipe file as an ordinary file, and you can achieve inter-process communication.

4) semaphores, message queues and shared memory are System V IPC mechanisms. Critical area: a code area where only one process can have exclusive access at any one time. Semaphores: most interprocess communication requires only binary semaphores, so only binary semaphores are discussed here. Before entering the critical area, perform the P operation (if the semaphore is greater than 1, minus 1 and enter the critical area, otherwise suspend the process); when exiting the critical area, perform the V operation (wake it up if any process is waiting to be suspended, otherwise the semaphore adds 1). Mutexes: mutexes are a subset of binary semaphores.

5) message queuing is similar to named pipes, but you don't have to consider the complex operation of opening / closing pipes. Message queuing exists independently of the process.

6) shared memory requires a piece of memory to be shared between communicating processes for data exchange.

2 the essence of the implementation of process threads is that the Linux scheduler actually recognizes the task_struct for scheduling. No matter the process thread, the underlying layer corresponds to a task_struct. The difference between the process and the thread is how much resources are shared. There is no resource sharing between the two processes, and all resources are shared between the two threads.

2.1After fork is executed by fork (), the task_struck of the parent process is copied to the child process. The parent and child processes initially have exactly the same resources, but they are two different copies, so any changes will split the two.

The parent and child processes use COW (Copy-On-Write, copy on write) technology to manage memory resources (mm):

Before fork, a piece of memory corresponds to a physical address and a virtual address, and the permission of the memory area is RW

After fork, the parent-child process sees the same virtual address and the same physical address of the memory area. The parent-child process actually uses the same piece of physical memory, and no memory copy occurs. The operating system will change the permission of this memory area to RO.

The PageFault is triggered by the parent or child process writing to the memory area, and the operating system makes a copy of the memory area. The virtual address seen by the parent and child process is still the same, but the physical address is different. The mapping of virtual address to physical address of each process is managed by MMU (Memory Management Unit, memory management unit).

Fork runs on CPU with MMU.

2.2 vfork ()

For CPU without MMU, COW cannot be applied and fork cannot be supported. CPU without MMU creates a process using vfork, and the parent process blocks all the way to the child process exit or exec. The essential difference between vfork and fork is that the parent-child processes in vfork share the same memory area.

2.3 pthread_create ()

Linux threads are essentially processes, but unlike inter-process resource sharing, all resources are shared between threads, as shown in the figure above. Each thread has its own task_struct, so each thread can be scheduled by CPU. The same process resources are shared among multiple threads. These two points just satisfy the definition of a thread. This is how Linux implements threads with processes, so threads are also called lightweight processes.

2.4 PID and TGID

POSIX requires that multiple threads of the same process get a unique ID value if they get the process ID.

Linux multithreading of the same process actually has a PID for each thread from a kernel perspective, but requires getpid to return a unique value in user space. Linux uses a trick to introduce the concept of TGID, the TGID value returned by getpid ().

Top command from a process perspective: the top command with no parameters (default) shows the process's utilization of single-core CPU. For example, if there are three threads in a process, the main thread creates thread 1 and thread 2, and thread 1 and thread 2 each call a while (1), then for dual-core CPU, thread 1 and thread 2 each have a core, and the occupancy rate is 100%, then the process CPU utilization seen by the top command is 200%. The process ID is the PID (that is, TGID) of the main thread.

Top command from a thread perspective: the top-H command shows CPU occupancy from a thread perspective. In the above example, thread 1 occupancy is 100% and thread 2 occupancy is 100%. The PID of a thread refers to the process ID in user space, and the value is TGID;. When it is specifically pointed out, the PID of a thread in the kernel space refers to the unique PID of the thread in the task_struct in the kernel.

3 process scheduling

Real-time process scheduling SCHED_FIFO: different priorities run to sleep first according to high priority, and then run to low priority; same priority first-in-first-out. SCHED_RR: different priorities according to the high priority first run to sleep, low priority then run; the same priority rotation. Kernel RT patch:

The following two parameters / proc/sys/kernel/sched_rt_period_us / proc/sys/kernel/sched_rt_runtime_us indicate the maximum time that RT can only run runtime during the period period.

3.2 ordinary process scheduling SCHED_OTHER:

3.2.1 dynamic priority (early 2.6) process has two measurement parameters: IO consumption and CPU consumption. High priority means: 1) get more time slices, 2) be able to preempt low priority ones when you wake up. The time slice turns. The kernel stores static priorities, and users can modify static priorities through nice. The dynamic priority of the process is calculated in real time according to the static priority. The scheduling algorithm rewards IO consumption (increasing priority to increase real-time performance) and punishes CPU consumption type (lowering priority to reduce real-time performance).

3.2.2 CFS: fully fair scheduling (new kernel) red-black tree, the value of the left node is smaller than that of the right node. So far, the smallest process in vruntime takes into account that CPU/IO and nice always find the smallest thread of vruntime to schedule. Vruntime = pruntime/weight × 1024; vruntime is the virtual run time, pruntime is the physical run time, and the weight of weight is determined by the nice value (the lower the nice, the higher the weight), then the thread with less run time and low nice value has less vruntime and will get priority scheduling. This is a process that changes dynamically with the operation.

Tools chrt and renice:

1 2 3 45 set SCHED_FIFO and 50 RT priority # chrt-f-a-p 50 10576 set nice # renice-n-5-g 9394 # nice-n 5. / a.out above is the content of this article about "what is the concept of process threading and scheduling in Linux". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it. Please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report