Server Architecture Design 4 ------ process scheduling

Source: Internet
Author: User
Today, let's take a look at the process scheduling of linuxcpu. for linux servers, we usually encounter two problems: 1. real-time performance, is there any way to ensure that a process runs preferentially and is not restricted by the time slice? other processes can run only when it is finished? 2. Multiple

Today, let's take a look at the process scheduling of linux cpu. for linux servers, we usually encounter two problems.

1. real-time performance: Is there any way to ensure that a process runs preferentially and is not limited by the time slice? other processes can run only when it is finished?

2. how can I customize a multi-core cpu and bind certain processes to some CPUs?

Before discussing these two issues, let's take a look at the basic knowledge of linux process scheduling.

 

There are two types of multitasking systems.

Non-preemptible multitasking: unless the process stops running on its own initiative, it will continue to execute;

Preemptible multitasking: there is a scheduler to determine when to stop a process, so that other processes can get the execution opportunity. Linux adopts this method.

Processes are also divided into two types for processing:

I/O consumption type: There are a large number of disk and network I/O operations. such processes are congested most of the time on io requests and their responses;

Processor consumable type: Most of these programs are complicated algorithms. an extreme example is while (1), an endless loop.

Process priority:

A process with a high priority runs first, and it takes a long time. Low-priority processes are vice versa.

Time slice:

Too large, long wait time, too small, frequent process switching. The default time slice is 20 ms.

Fairness of scheduling

In a system that supports multiple processes, each process should ideally occupy the CPU fairly based on its priority. There will be no uncontrollable situations like "Who has better luck and who has more Shares.

There are basically two ways to implement fair scheduling in linux:
1. assign a time slice (by priority) to the processes in the executable state. The processes that use the time slice are placed in the "expiration queue. Wait until all processes in the executable status expire, and re-allocate the time slice;
2. dynamically adjust the priority of a process. As the process runs on the CPU, its priority is continuously lowered so that other processes with lower priority can get a running opportunity;
The latter method has a smaller scheduling granularity and combines "fairness" with "dynamic priority adjustment" to greatly simplify the code of the kernel scheduler. Therefore, this method has become a new favorite of kernel scheduling programs.
Emphasize that the above two points are only for common processes. For real-time processes, the kernel cannot adjust the priority dynamically without any fairness.

An interesting example:

One system, two processes, one text editing, and one video encoding. The former is an I/O consumable type, and the latter is a processor consumable type. How are the processors assigning priority and time slice to these two processes.

First, text editing takes most of its time on the I/O wait and must respond to users' requests in a timely manner. Therefore, it has a high priority and takes a long time. When a user requests a video, video encoding is interrupted. When you need to wait for the I/O response, the time slice will be handed over in time for video encoding.

On the contrary, video encoding has a low priority and a short time slice.

========================================================== ==========================================

Well, I have finished the basic knowledge. next I will answer the two questions mentioned in the opening section.

Two real-time scheduling policies for linux:

SCHED_NORMAL: General scheduling policy. we usually use a preemptible scheduling policy based on time slices.

SCHED_FIFO: first-in-first-out scheduling without using time slices. Once a SCHED_FIFO-level process is in the executable state, it continues until it is blocked or explicitly released. FIFO is higher than NORMAL. only SCHED_FIFO or SCHED_RR tasks with higher priority can seize SCHED_FIFO tasks.

SCHED_RR: Similar to SCHED_FIFO, but with time slice, the priority is higher than SCHED_NORMAL. When SCHED_RR time slice is exhausted, other real-time processes with the same priority will be scheduled in turn,Note that real-time processes with the same priority. In other words, when the time slice is exhausted, only SCHED_FIFO and SCHED_RR with the same priority can be scheduled by the cpu, while SCHED_NORMAL with lower priority will not be rotated. Of course, high-priority real-time processes can be preemptible.

The real-time priority ranges from 0 ~ 99 and 99 are the highest priority. It can be obtained through the sched_get_priority_max function.

Summary: For real-time scheduling, SCHED_FIFO and SCHED_RR can be used. The difference is that the former has no time slice and knows that it is completed or blocked. The latter has a time slice and the time slice is exhausted, the cpu can be handed over to real-time tasks with the same priority.

 

After talking a lot, how do I set real-time scheduling policies? what are functions?

 

Nice () // Set the nice value of the process sched_setscheduler () // Set the scheduling policy sched_getscheduler () // Obtain the scheduling policy sched_setparam () of the process () // set the real-time priority of the process sched_getparam () // Obtain the real-time priority of the process sched_get_priority_max () // obtain the maximum value of real-time priority sched_get_priority_min () // obtain the minimum value of real () // Obtain the process time slice sched_setaffinity () // set the process processor's affinity sched_getaffinity () // Obtain the process processor's affinity sched_yield () // temporarily remove the processor

 

 

Example of setting a real-time scheduling policy SCHED_FIFO:

 

intrtsched_set_my_realtime_priority (int32_t prio) {    struct sched_param schp;    if (sched_getparam(0, &schp) < 0) {        return(-1);    }    schp.sched_priority = prio;    if (sched_setscheduler(0, SCHED_FIFO, &schp) < 0) {        return(-1);    }    return(1);}


Example of binding a specified cpu:

 

 

intrtsched_setaffinity_by_name(int32_t cpuid){    cpu_set_t mask;    CPU_ZERO(&mask);    CPU_SET(cpuid, &mask);    sched_setaffinity(0, sizeof(cpu_set_t), &mask);    return 1;}
This is an example of cpu binding. The system default process can run on any cpu, but to ensure the real-time performance of some processes, it is also necessary to bind it to a idle cpu to run.

 

Discard cpu

All right, binding cpu and setting priority ensure the real-time performance of a process. if we want to temporarily discard the real-time performance, let it out of the cpu, and let other processes run for a while, how can this problem be solved?

You can call the sched_yield () function to move the process from the active queue to the expired queue and hand over the cpu occupied by the process. Note that for real-time processes, instead of dumping the expired queue, it is placed at the end of the priority queue. Instead of in the expired queue.

 

Note:

1. the best Priority. do not set it as needed. once other processes are set, they will not be able to play. The highest priority is 99. setting a 98 is already very high. The author has tried it. once setting 99, why can't I connect to ssh ,? Sorry !!!!!! #? /Strong>

2. the cpu binding and priority setting can be thread-specific, O (worker _ priority) O ~;

3. high priority indicates that the cpu is not released. in this case, both pthread1 and pthread2 are bound to cpu1 and are real-time threads with the same priority. 1. get the spin_lock, and I/O is congested to hand over the cpu to 2. it happens that 2 and 1 share the same resource, and they also need to go to the same resource in the spin_lock. well, think about what the outcome will be, 2. it will always be spin_lock, occupying the cpu, and 1 won't be able to get the cpu. the two digits are stuck here, and no one can continue to execute .? Sorry !!!!!!!!? /Strong>

 

 

 

Related Article

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.