A simple UNIX-Linux CFS annotation, unix-linuxcfs

Source: Internet
Author: User

A simple UNIX-Linux CFS annotation, unix-linuxcfs
This series is named simple UNIX, but it also includes the details of various UNIX-like systems. This series of articles are based on my study notes over the past few years, focusing on the analysis of various UNIX, UNIX-like Implementation ideas and insights.
This article is short. It only analyzes the details of the Linux CFS scheduler.
When the process looks for the next process to be run, is it just a process in the lower left corner of the Red-black tree? In fact, this is the most simple implementation, but considering optimization, it is not that simple. The CPU cache utilization should be considered. That is, if process A preempys process B, in the context of process, does pick next select process B or the lower-left process of the Red-black tree? In addition, if process A is just awakened and tries to seize process B, but fails, will process A be selected in the lower-left corner of the Red-black tree? Yes, these are all problems.
Let's take a look at the implementation of pick next:

static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq){    struct sched_entity *se = __pick_next_entity(cfs_rq);    struct sched_entity *left = se;    if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)        se = cfs_rq->next;    /*     * Prefer last buddy, try to return the CPU to a preempted task.     */    if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)        se = cfs_rq->last;    clear_buddies(cfs_rq, se);    return se;}
Obviously, we need to compare the process at the bottom left of the red/black tree with the other two processes, namely, next and last, and next, the process that fails to be preemptible, last is the process that is preemptible after successful preemption. Which of the three processes is the best next process? The conditions for implementing Linux CFS are:
1. Try to satisfy the needs of the newly awakened process to seize other processes;
2. minimize the impact of cache refreshing caused by the above preemption.
How does Linux CFS implement it? In Linux, two variables are saved, namely, the process that fails to be preemptible and the process that is preemptible after being preemptible. The priority sequence is as follows:
Processes that are successfully preemptible> failed processes> processes at the bottom left of the red/black tree

Whether or not the first two processes can be selected is determined by the wakeup_preempt_entity function. If there are too many codes, you can see the following illustration:




The CFS details were not implemented when CFS was initially implemented in Linux 2.6.23. Therefore, starting from the simplest age, you can gradually understand the ins and outs of details. It is advantageous to see Changelog. I have to say that reading history is wise.


Programming in unix/linux c

1. The atoi (argv [1]) string is converted to an integer, and program parameters are passed into the program as strings. Therefore, the integer class must be converted to an integer by atoi.

2. The first parameter of the argv [1] Program

For example: cat a.txt
Then argv [0] = cat
Argv [1] = a.txt

How to change Linux CFS scheduling to random Scheduling

1. Real-time scheduling priority in CFS.
2. CFS allocates a ratio based on the time when processing the static priority. Each process has the opportunity to run the process with the highest priority in a cycle. Scheduling runs in real time, the running time and opportunity are unfair.
 

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.