Operating system Development Series -13.I. Process scheduling

Source: Internet
Author: User

The three processes above are all delayed for the same time, let's modify them and try to delay them for different times.

void TestA () {int i = 0;while (1) {disp_str ("A."); Milli_delay (300);}} void Testb () {int i = 0x1000;while (1) {disp_str ("B."); Milli_delay (900);}} void Testc () {int i = 0x2000;while (1) {disp_str ("C."); Milli_delay (1500);}}

After running, the number can be known that the output has a letter 140, B letter 51, C letter 32, So the ratio of the number of A and B is the ratio of the number of 2.745,a and C is 4.345, the two numbers and 3 (process B and a delay time ratio) and 5 (process C and a delay time ratio) is basically consistent.

To add a new member to the process table, proc.h:

typedef struct S_PROC {stack_frame regs;          /* Process registers saved in stack frame */u16 Ldt_sel;               /* GDT selector giving LDT base and limit */descriptor ldts[ldt_size]; /* Local descriptors for code and data */        int ticks;                 /* remained ticks */        int priority;u32 pid;                   /* Process ID passed in from MM */char p_name[16];           /* Name of the process */}process;

Two members were added to the process table: Ticks is decremented from an initial value to 0. In order to remember the initial value of ticks, we define a variable priority, which is constant. When all the process ticks are changed to 0, assign the ticks to priority and continue execution.

Ticks and priority values are initially assigned as follows, MAIN.C's Kernel_main ():

Proc_table[0].ticks = proc_table[0].priority = 150;proc_table[1].ticks = proc_table[1].priority =  50;proc_table[2 ].ticks = proc_table[2].priority =  30;

For process scheduling, we can write a single function, called Schedule (), in the PROC.C:

public void Schedule () {process* P;int greatest_ticks = 0;while (!greatest_ticks) {for (P = proc_table; p < proc_table+n R_tasks; p++) {if (P->ticks > greatest_ticks) {greatest_ticks = P->ticks;p_proc_ready = P;}} if (!greatest_ticks) {for (P = proc_table; p < proc_table+nr_tasks; p++) {p->ticks = P->priority;}}}

Also modify the clock interrupt handler function, CLOCK.C:

public void Clock_handler (int IRQ) {ticks++;p _proc_ready->ticks--;if (k_reenter! = 0) {return;} Schedule ();}

At the same time we change the delay time of all processes to the same value and change all milli_delay parameters to 200.

The result of the make run found that, although the time lag for each process was the same, the time to run was significantly different due to changing their priorities, which indicated that our priority policy was in effect!

However, the current ratio of the number of a, B, C three letters is 139:71:54, roughly equivalent to 2.57:1.31:1, and the process priority 5:1.67:1 (15:5:3) is relatively large. Why, first modify each process so that each of them prints a current ticks. Then modify the schedule (), add a few print statements and so on and then run again,

Modify CLOCK_HANDLER,CLOCK.C:

public void Clock_handler (int IRQ) {ticks++;p _proc_ready->ticks--;if (k_reenter! = 0) {return;} if (P_proc_ready->ticks > 0) {return;} Schedule ();}

Thus, no other process will have a chance to be executed until the ticks of one process has become 0.

As can be seen from the running result, process A is executed first, then B, then C, which is very different from the original. The reason is that the ticks of process A has been reduced from 150 to 0 before the control is given to B,b to run out of its ticks (50) and then to C, and then the respective ticks are reset to continue the next similar process. As you can see, process a performs 8 loops within 150ticks, B executes 3 loops within 50ticks, and C executes 2 cycles within 30ticks. So it's intuitive.

Let's change their priorities a little bit:

Proc_table[0].ticks = proc_table[0].priority = 15;proc_table[1].ticks = Proc_table[1].priority =  5;proc_table[2]. Ticks = Proc_table[2].priority =  3;

Then change the delay time of each process to 10ms:

void TestA () {int i = 0;while (1) {disp_str ("A."); Milli_delay (10);}}

The result is as follows, as you can see, the ratio of the number of characters that are printed is very close to 15:5:3:

" Source "

Operating system Development Series -13.I. Process scheduling

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.