Article title: Linux core-12. Linux kernel mechanism. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Original: David A Rusling
Translation: Banyan & fifa
Chapter 4 core mechanism
This chapter describes several common tasks and mechanisms provided by the Linux kernel to make other core components work effectively.
11.1 underlying processing mechanism
1.1 underlying part of the processing mechanism data structure in some special moments we are not willing to perform some operations in the core. For example, in the process of interrupt processing. When an interruption occurs, the processor stops the current operation and the operating system sends the interruption to the corresponding device driver. Because other programs in the system cannot run at this time, the interrupt handling process in the device driver should not be too long. Some tasks should be executed later. The underlying processing mechanism of Linux allows device drivers and other Linux core components to sort these tasks to delay execution. 1.1 provides a core data structure related to the underlying processing.
The system can have up to 32 different underlying processing processes. bh_base is an array of pointers pointing to these process entries. Bh_active and bh_mask indicate the processes that have been installed and those that are active. If bh_mask is at the nth position, it indicates that the nth element of bh_base contains the underlying processing routine. If bh_active is at the nth position, it indicates that the nth underlying processing routine can be called when the scheduler deems it appropriate. These indexes are defined as static. the timer underlying processing routines have the highest priority (the index value is 0), and the underlying processing routines of the console are followed by (the index value is 1 ). A typical underlying processing routine contains a linked list of tasks. For example, some underlying processing routines of immediate are executed through the immediate task queue (tq_immediate) of the tasks that need to be executed immediately.
Some core underlying processes are device-related, but some are more universal:
TIMER
This process is marked as active when a periodic clock interruption occurs, and is used to drive the core timer queue mechanism.
CONSOLE
This process is used to process messages in the process console.
TQUEUE
This process is used to process tty messages.
NET
This process is used for general network processing.
IMMEDIATE
This is a common process driven by several devices to queue tasks for later execution.
When the device driver or other parts of the core need to schedule some work delays to complete, they will add these tasks to the corresponding system queue, such as the timer queue, then, send a signal to the core to notify it of a certain underlying processing process. The specific method is to set some bits in bh_active. If the device driver adds a task to the immediate queue and wants the underlying processing process to run and process it, you can set the 8th position to 1. Check bh_active before returning to the calling process after each system call ends. If a bit is set to 1, the underlying processing process in the active state is called. The check sequence starts from 0 to 31st bits.
The corresponding bits in bh_active are cleared each time the underlying processing process is called. Bh_active is a transient variable that makes sense only when calling the scheduling manager. It can also avoid calling the underlying processing process when idle. 11.2 task queue
Figure 11.2: a task queue
Task queue is the main method for starting core delayed tasks. Linux provides a common mechanism for queuing and processing tasks in a queue. The task queue is usually used together with the underlying processing process. The underlying timer queue processes the timer queue during processing. The structure of the task queue is very simple, as shown in Figure 11.2. it consists of a tq_struct structure linked list. each node contains the address pointer for processing and the pointer to data.
These processes are used to process elements in the task queue, and pointers to the data are also used.
All parts of the core, such as device drivers, can create and use task queues. However, there are only three task queues created and managed by the core:
Timer
This queue is used to queue tasks that need to run as soon as possible when the next clock is ticking. Check whether the queue is empty when every clock tick occurs. if it is not empty, the underlying processing of the timer will activate this task. When the scheduling manager runs the next time, the underlying processing process of the timer queue processes the task queue together with other underlying processing processes. This queue cannot be confused with the system timer.
Immediate
The underlying processing of immediate has a lower priority than the underlying processing of the timer, so this type of task will be delayed.
Scheduler
This task queue is directly processed by the scheduling manager. It is used to support other task queues in the system. a task that can be run is a process for processing task queues, such as a device driver.
When processing a task queue, the element in the queue header is deleted from the queue and replaced with a null pointer. This delete operation is an uninterrupted atomic operation. The processing process of each element in the queue is called in sequence. Elements in this queue usually use static data allocation. However, there is no inherent mechanism to discard allocated memory. The task queue processing routine simply points to the next element in the linked list. This task truly clears any allocated core memory.
11.3 TIMER (TIMER)
1.3 system timer
The operating system should be able to schedule a task on time at a certain time point in the future. Therefore, a mechanism is required to ensure that tasks are scheduled and run on time. To support each operating system, the microprocessor must contain a programmable interval timer that can interrupt it cyclically. This periodic interrupt is called a system clock tick. it organizes system tasks like a timer.
The concept of clock in Linux is very simple: it indicates the time after the system starts to tick the number of times. All system clocks are based on this metric, and the name in the system is the same as a global variable-jiffies.
Linux contains two types of system timers, which can be used by queue routines at a certain system time, but their implementation is slightly different. The two mechanisms are illustrated in 1.3.
The first is the old timer mechanism, which contains a static array of 32-bit pointers pointing to the timer_struct structure and the screen code of the current active timer: time_active.
The positions in this timer table are statically defined (similar to the underlying processing table bh_base ). Its entry is added to the table during system initialization. The second is a relatively new timer, which uses a timer_list structure linked list with an expiration time in ascending order.
Both methods use jiffies as the end time, in this way, the timer running for 5 seconds will have to convert the 5 seconds to the unit of jiffies and add it to the current system time in the jiffies count to get the timer end time. When the clock of each system is ticking, the underlying processing process of the timer is marked as active so that the scheduling manager can process the timer queue the next time it runs. The underlying processing process of the timer includes two types of system timers. The old system timer checks whether the timer_active bit is set.
If the active timer has expired, its timer routine will be called and its active bit will be cleared. The entry of the new timer in the linked list of the timer_list structure will also be checked. Each expiration timer is cleared from the linked list, and its routine is called. One of the advantages of the new timer mechanism is that it can pass a parameter to the timer routine.
11.4 waiting queue
Processes often have to wait for system resources. For example, a process may need to describe the VFS inode of a directory in the file system, but this inode may not be in the buffer cache. In this case, the process can continue to run only after the inode is retrieved from the physical medium containing the file system.
Wait_queue
* Task
* Next
1.4 waiting queue
Linux core uses a very simple queue: waiting queue (see 1.4 ). It contains a pointer pointing to the task_struct structure of the process and a pointer waiting for the next element in the queue. The processes added to the waiting queue can be either disruptive or non-disruptive. The process that can be interrupted by time, such as timer expiration or signal. The status of the waiting process must be set to INTERRUPTIBLE or UNINTERRUPTIBLE. Because the process cannot continue running now, the scheduling manager takes control of the system and selects a new process to run and waits for the process to be suspended. Each process in the waiting queue is set to RUNNING. If the process has been deleted from the running queue, it will be re-placed into the running queue. The next time the scheduling manager runs, these processes will be candidates for running because they are no longer waiting. Waiting queues can be used to synchronize access to system resources. They are also used by Linux for signal lamp implementation.
11.5 Buzz lock
It is called spin lock more frequently. This is a primitive way to protect data structures or code snippets. At a certain time point, only one process is allowed to access the code in the critical section. Linux also uses an integer field as a lock to restrict access to certain fields in the data structure. Every process that wants to enter this region tries to change the initial value of this lock from 0 to 1. If the current value is 1, the process will try again. at this time, the process seems to spin in a tight loop code. The access to the memory area containing this lock must be atomic, that is, the process of checking whether the value is 0 and changing it to 1 cannot be interrupted by any process. Most CPU structures support this method through special commands, and we can implement this rumor lock in a non-buffered primary memory.
When the control process leaves the critical section, it will decrease the Buzz lock. Any process in the spin state can read it. the fastest of them will increase this value and enter the critical section.
11.6 signal lights
Traffic signals are used to protect code and data in the critical section. Remember that each time you access critical partition data, such as VFS inode in a directory, is performed by representing the core code of the process. It is very dangerous to allow a process to modify the critical zone data used by other processes without authorization. One way to prevent this problem is to use the buzz lock around the Accessed critical section, but this simple method will reduce system performance. Linux uses traffic signals to force a unique process to access critical code and data at a specific time point. Other processes must wait for the resources to be released for use. These wait processes will be suspended and other processes in the system can continue to run.
A Linux semaphore structure contains the following information:
Count
This field is used to save the number of files you want to access this resource. When it is a positive number, the resource is available. Negative and 0 indicate that the process must wait. When its initial value is 1, it indicates that only one process is allowed to access this resource at a time. When the process needs this resource, they must reduce this count value by 1 and add it to 1 after use.
Waking
This is the number of processes waiting for this resource, and the number of processes waiting to be awakened when the resource is available.
Wait queue
When the process waits for this resource, they are placed in this waiting queue.
Lock
The buzz lock used to access the waking domain.
Assume that the initial value of the traffic signal is 1. the first process that uses it sees this count as a positive number, and then it subtract 1 to get 0. Now this process
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service