Linux System Programming Learning Note (vi) process scheduling

Source: Internet
Author: User

1. Process scheduling

The process Scheduler is the component of a kernel this selects which process to run next. The progress scheduler needs to maximize processor utilization and provide multiple processes concurrently Rows of virtual deciding which processes run, when, and for how long is the process scheduler ' s fundamental responsibility. Time Slice: The scheduler allocates the process a "slice" of the processor ' s times Linux process scheduling algorithmCFS (Completely Fair Scheduler): Enforce fair access to a resource among contending consumers 2. Time slice if the time slice is too large, then the suspend process begins execution before thewait Too long, which reduces the granularity of concurrent execution, even if the user perceives a delay if the time slice is too small, the system is between processesswitching time will cost a lot., the advantage of time locality loses CPU-intensive: Processes is hungry for CPU time, such as scientific computation, mathematical computation, image processing I/O intensive: Spend more time blocked waiting for some Reso Urce than Executing,often issuing and waiting for file or network I/O, blocking on keyboard input 3. CFS scheduling traditional UNIX process scheduling, there are two most basic variables: priority and time slice CFS introduced fair scheduling algorithm, CFS gives each process a certain proportion of processor time, rather than time slice start, the CFS gives N process equivalent 1/n processor Time, Then weigh each process proportionally to dynamically adjust processes with the default nice value of zero has a weight of one, so their proportion is unchangedproces SES with asmaller nice value (higher)Receive a larger weight, increasing their fraction of the processorprocess with a lArger Nice value (lower)Receive a smaller weight, decreasing their fraction of the processor in order to determine the actual time per process run, CFS needs to divide the proportions into a fixed period for example: 20 milliseconds, 2 processes of the same priority, each process is given the same weight and processor time ratio, that is, each process 10 milliseconds if it is 5 processes, each process is assigned 4 milliseconds, if 200 processes? It should be common sense to allocate 100 microseconds per process, but due to the large cost of context switching and the loss of local benefits, this will severely affect the throughput rate of the system when CFS introduces another variable: minimum granularity (minimum granularity)minimum granularity is the lower limit of the length of time any process is running, this will guarantee that the context switching overhead will not be too large by the total system time overhead by assigning proportions of the processor and not fixed timeslices,cfs are able to enforce F Airness:each process gets its fair share of the processor 4. Nice Valprocesses is assigned priorities that affect how long they run,unix have historically called these priorities nice Valueslegal Nice values range from?Inclusive, with a default value of 0,The higher The nice value, the lower the priority, the smaller the nice value, the higher the priority
/**/<unistd.h>int Nice (int Inc);
passing 0For Inc. is a easy-to-obtain theCurrent Nice valueThe nice function returns 1 when it is wrong, but the new friendly value may also be-1, in order to differentiate the function error and return a nice new value of-1, use as follows:
intRet;errno=0; RET= Nice (Ten);/*increase Our Nice by ten*/if(ret = =?)1&& errno! =0) perror (" Nice");Elseprintf ("Nice value are now %d\n", ret);

Get/Set Priority:

#include <sys/time.h><sys/resource.h>int getpriority (intint  WHO); int setpriority (intint int prio);

 /*   returns the current Process ' s priority  */ int   Ret;ret  = getpriority (prio_process, 0  );p rintf (  , ret); 
/**/int0); if (ret = =?) 1 )        perror ("setpriority");
5. Processor Association The Process scheduler must decide which processes run on each CPU if a process is scheduled on a CPU core, the process scheduler should AI M to schedule it on the same CPU in the future because of the processthe cost of migrating from one CPU core to another CPU core is huge (primarily cache impact)If a process moves to a new CPU and writes new data into memory, the data of the old CPU ' s cache can become stale so that the process scheduler Must be inprocess Migration CPU costsAndmultiple CPU Load BalancingStrike a balance between the Linux Scheduler attempts to schedule the same processes on the same processors for as long as possible, Migrati Ng a process from one cputo another only in situations of extreme load imbalance. This allows the scheduler to minimize the cache effects of migration but still ensure thatall processors in a system is Evenly loaded 6. Real-time system hard real-time and soft real-time a real-time system requires absolute adherence to operational DEADLINESA soft real-time system does not co NSider over‐running a deadline to be a critical failure 7. Linux real-time scheduling policy FIFO scheduling: A fifo-classed process would continue running so long as no higher-priority process becomes runnable time slice rotation : When a rr-classed process exhausts its timeslice, the scheduler moves it to the end of the list of processes at its PRI Ority Note: in these two real-time scheduling strategies, if there is a higher priority process, the low-priority process will not run
intpolicy;/*get our scheduling policy*/Policy= Sched_getscheduler (0);Switch(policy) { Casesched_other:printf ("Policy is normal\n");  Break; Casesched_rr:printf ("Policy is round-robin\n");  Break; Casesched_fifo:printf ("Policy is first-in, first-out\n");  Break; Case-1: Perror ("Sched_getscheduler");  Break;default: fprintf (stderr,"Unknown policy!\n");}

Linux implements a range of 1 to inclusive for the real-time scheduling policieslinux provides, system calls for Retrieving the range of valid priority values:
intmin, max;min=sched_get_priority_min (SCHED_RR);if(min = =?)1) {perror ("Sched_get_priority_min"); return 1;} Max=Sched_get_priority_max (SCHED_RR);if(max = =?)1) {perror ("Sched_get_priority_max"); return 1;} printf ("SCHED_RR Priority range is%d-%d\n", Min, max);
Real-Time Scheduling considerations: Design real-time programs must be extra careful, can easily bring down the entire system (1) If there are no higher priority real-time processes in the system, the CPU-intensive loop program will run until the end (2) Take care not To starve the rest of the system's Processor Time (3) If a real-time process busy-waits for a resource held by a LOWER-PR Iority process, the real-time process would busy-wait forever. (becauselow-priority processes do not run to free resources)

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.