Mr. process life-Linux kernel social perspective (1) Scheduling

Source: Internet
Author: User
Document directory
  • Born
  • Start work
  • Dynamic title evaluation
  • O (1) Scheduling
  • Preferential policies for social spending
  • Super privileged class in the company-Overlord (Real-time process)
Reprinted:

Http://www.manio.org/cn/scheduling-of-linux-view-of-society.htmlWritten by manio (manioster {at) gmail.com)

Linux Kernel is a very complex system, and it is not easy to see the general context. In fact, we can think of running Linux as a human society in which processes are people in society. People are born, old, and dead, and processes are created, abnormal, and terminated. People have all kinds of property, and the process has the corresponding address space and equipment. People are restricted by various things, managed by people, and the process is also. The kernel is huge. Where should I start? I think it is a good way from the process perspective. In addition, while learning the Linux kernel, we will be more impressed and have a better understanding than our human society.

It should have been written since the creation of the process. However, I recently read the scheduling papers and read the Linux scheduling. Therefore, write scheduling-related items first.

First, we will introduce the human scenario corresponding to the process environment:

This is a company where many people (processes) have their corresponding titles (priorities ). This company is very strange, there is only one desk (CPU), at any time, only one person at work (task_running is running ). Others, either temporarily rest in the lounge (corresponding to the task_running ready state), sleep in the dormitory (corresponding to task_interruptable or task_uninterruptable), or dead, at the garbage station (task_zombie ). Yes, this is true. This company is a sweatshop. If it is exhausted, it will be directly thrown into the garbage station!

The company's floor plan is as follows:

Illustration: "Door push" is one-way: for example, the portal is connected to the studio and the garbage station. The staff can only go from the studio to the garbage station, rather than from the garbage station to the studio. The "Groove door" is two-way: for example, the door connecting the studio and lounge is a groove door, And the staff can move around.

Time slice(Time slice): The company's salary is particularly high, based on the number of hours of work. The workers get the money, but the workers do not have the money. Therefore, all people want to work at their desks, and they are scrambling to go. In order to give everyone the opportunity to go to work (to avoid some people having no money to get it, starve to death, I. e. starve to death), so the company stipulates that each person's work time cannot exceed the time slice assigned by the company ). The size of the time slice is determined by the title of the person.

Title(That is, the priority in the kernel): There are two types of titles: static priority and dynamic priority ). Static titles are inherent, and therefore are determined by the father (rich generation has inherent advantages !). Dynamic priorities are based on the performance of the company. The title is very important. The higher the title, the more chance you will make money at your desk.

Is the default time period good or short?

Naturally, it is not long, not short, or good (things in the world are like this ). If it is too short, for example, a staff member's time slice is 1 minute (the corresponding Linux time slice should be about 0.1ms, maybe less ), this person's ass in front of his desk is not hot and will leave, from getting up to return to the lounge, and then to the next person got up and sat down in front of his desk, this also takes a minute. In this case, the company will spend half of its time walking. This cannot be done. In a capitalist society, everyone must work hard to achieve high productivity.

Is it enough to have a long credits? No. For example, if the time slice is an hour, when two customers (two users in Linux) need to start business with the company at the same time (corresponding to the Startup Process in Linux ), two employees will process the two services separately. Clerk A occupies the office for an hour when he goes up. Clerk B cannot handle the customer's business this hour before the end of the hour, so Clerk B's customer has to wait for an hour in the cold wind! Companies in a capitalist society cannot make their God of Wealth unhappy. Therefore, it is better not to be long or short.

Back to the real Linux kernel: Many people think that too long time slice will lead to slow response of the program (such as the I/O response of the Word Processing Program), because it can only be processed after the time slice is used up. In fact, word processing programs and other highly interactive programs have a high priority and can seize the processes currently running for execution. In a company, for example, employee B has a high title and can kick employee a away from his desk. In fact, from the discussion of dynamic priorities, we can see that if a employee keeps occupying his or her desk and does not leave, his/her dynamic titles will become lower. The result is, the title of other employees in the lounge may naturally be higher than that of.

Process List): The company has a directory in which all employees of the Company are listed. This corresponds to the Process List in Linux, which contains information about all processes.

The following uses the mr. P experience as an example to describe

Personal history statement of mr. Process (Mr. P in short)

Born

This is the most weird company in the world. Employees are all single and all are male. And don't mate. You can call the system to call fork. All the children are male.

To simplify the problem, we will only discuss the closely related content with scheduling.

Figure: Detailed company plan

As shown in, there are actually two sofas in the lounge. A sofa is called an activity queue.Activity sofaA sofa is called an expiration Queue (we call it later ).Expired sofa). The staff sitting on the activity sofa still have time slice not used up. The time slice of the staff sitting on the expired sofa has just been used up (but they all hold the new time slice in their hands, this is the O (1) scheduling introduced by the 2.5 kernel, which will be discussed later ).

When mr. P's father was working at his desk, fork () suddenly gave birth to mr. P. At first birth, mr. P had the same static priority as his father ). He sat down on the couch when he was born and took half of his father's time. For example, before Mr. P was born, mr. P's father had 10 ms of time film. After mr. P was born, his father had 5 ms and mr. P had 5 ms. This mechanism aims to prevent someone from snatching time clips by having children: imagine that one person has 10 ms of time clips, and he has been born and has given birth to 100 children, then this program has 10 + 10 * Ms time slice. Then his child was born intermittently, so it is possible that most of the time films in the system are from his home, so his neighbors should not live. If there is no time film, he cannot do things, it is equal to no salary, no food, or starved to death.

In addition, unfortunately, even in a developed capitalist society, children may also get down (for various reasons, they will get down before they work at their desks for the first time ). In this case, the company will return the child's unused time sheets to his father. What a human company...

Start work

Like all other children, mr. P is bornYesAfter work, he sat on the activity couch waiting for work. Miss schedule (hereinafter referred to as Miss S), a member of the personnel department, checks whether the following two conditions have occurred at the end of each tick (kernel time unit, short time:

  1. Does the employee who works at his desk have used up his time slice.
  2. Is there a dynamic priority employee waiting on the activity couch.

One of these situations is true. The employee who works at the desk is about to be rushed out, and the employee with the highest dynamic title sitting on the activity SOFA goes to work.

It is assumed that after a period of time, no one has a higher dynamic title than mr. P. At the end of a time tick, Miss s goes to the lounge to check your current title. He can easily find the employees with the highest dynamic titles (sched_find_first), because they all sit on the dynamic titles (on the activity couch ), employees with the same dynamic titles sit together (refer to struct prio_array ). As long as Miss s can be searched in order, the first employee found on the sofa is the most professional title. When Miss s used the above method to find that mr. P currently has the highest dynamic title, he worked in front of his desk as clerk C. Miss s pulled C from his desk to the lounge. If the time slice of C is used up, it will be arranged to sit on the expired sofa (that is, the meaning of expired); if the time slice of C is not used up at this time, he will be arranged to sit on the sofa. When C is pulled to the lounge, his work tools, pens, paper, and everything should be taken away from his desk (this is about context switching of processes ). After leaving C, mr. P took his stuff to his desk and sat down, finishing the work supplies (process context switch ). Okay, mr. P can start working.

Dynamic title evaluation

Two types of companies:

Jihua(I/O consumable process): This type of people often deal with external customers, and the customer's response is always not fast. Therefore, social spending often waits for the customer to respond. In order not to allow the company to occupy the desk when the customer responds, the company will move to the dormitory to sleep when the customer waits for the response ).

Workaholic(Consumable Processor type): This kind of person is the legend of the hard-working king. If he seldom deals with the outside world, he knows to do things on the work desk. This kind of workaholic will cause some troubles to the company. If they occupy their desks, they will not be able to do anything. You know, a company still needs people to run its business.

The company has a set of mechanisms that can distinguish the two categories and give them titles. The criterion is their average sleep time ). Sleep time includes the time in the dormitory (task_interruptible and task_uninterruptible) and the waiting time in the lounge. However, the calculation of the average sleep time is not as simple as adding and calculating the average. In the task_int and task_unint statuses, the average sleep time increases differently. During running time, the average sleep time is decreasing. The specific average sleep time algorithm is in recalc_task_prio.

When an employee often goes to bed, the company knows that this person is a social expense, because he often goes to bed when the customer responds. When a staff member seldom sleeps, the company knows that the employee is a workaholic, because he seldom sleeps due to waiting for the response from the customer.

Now let's discuss mr. p. Assume that Mr. P waits for a customer to give data to him (for example, the keyboard data) at work, and adds his/her to a waiting queue and sets himself as task_int, then he enters the dormitory. Mr. P is waiting for the data he wants. Finally, the customer knocked on the keyboard and gave the data. Then the keyboard event manager calls the wake_up () function to wake up mr. P. If the dynamic title of mr. P is higher than the dynamic title of the employee currently working, mr. P will snatch the desk of the employee. If not, mr. p just sits in the lounge. We assume that mr. P has a high dynamic title, so now mr. P is at his desk.

O (1) Scheduling

After a while, the mr. P time slice is used up and it is time to leave. At this time, he should be taken to the expired sofa by Miss S. On the way from his desk to the expired sofa, Miss s will help him calculate the time slice (based on the dynamic title to determine the length of the time slice ). As a matter of fact, when every employee is taken to an expired couch, Miss s will help him calculate the time slice. In this way, all employees on the expired sofa are holding the time slice in their hands. The advantage of doing so is that when the activity sofa is empty, you only need to swap the "activity sofa" and "Expiration sofa" labels on the activity sofa and the expiration sofa, the original expired sofa is changed to an active sofa, and the original active sofa is changed to an expired sofa. In this way, Miss s can ask someone to work on the activity couch again. The time complexity of this "swap tag" action is O (1 ). This is the O (1) scheduling introduced by kernel 2.5.

Imagine what happened before debugging without this O (1): When all the time slices of all processes are used up, re-calculate the time slices of all the task_running processes. When there are only a few processes in a system, it is time-consuming for large systems with thousands of processes.

Preferential policies for social spending

In a company, social spending is very important. Their performance is directly related to customer satisfaction and the company's image. Therefore, Linux has some preferential policies dedicated to social spending.

Assume that Mr. P is an extremely social flower: he is dressed like a man at work (although a man). The key is that he has some clients who love to deal with him. When mr. P runs out of a time slice, as usual, he should be taken to the expired sofa by Miss s for the next job. However, this wait may take too long-wait until all the staff on the activity sofa have used up the time slice. Therefore, for customer satisfaction (if the customer is not satisfied after waiting for too long), Miss s left him on the activity couch After recalculating the time slice of mr. P. In this way, mr. P can process customer Io events more quickly.

This seems to be the privilege of social spending, but it is for the long-term interests of the company. If the customer's needs are not met in a timely manner and the customer is not satisfied, no one will use the company's products, even if the workaholic is doing well.

Super privileged class in the company-Overlord (Real-time process)

Social spending has some privileges, but its rights are even larger than that of Overlord. Companies sometimes have some time-consuming projects with deadline. In order to complete these important projects, the company has developed a privileged class, that is, the overlord. Their titles are higher than those of all other common employees. Other employees cannot go to work unless they finish their jobs.

These overlord can work in two ways:

FIFO (first in first out): When a overlord completes his work, other overlord can go to work. However, when a Overlord with a higher title arrives, the Overlord with a higher title can seize the desk of the current overlord.

Rr (route Robin.

Summary:

In this article, mr. P demonstrates some important mechanisms for Linux scheduling. I/O-consuming processes, CPU-consuming processes, and O (1) scheduling are introduced. These are big aspects. After understanding the big aspects, the world's top highly handwritten source code will be easier to understand.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.