In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about the principle analysis of the timer based on linux 0.11 operating system. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope everyone can gain something according to this article.
The timer principle of the operating system is that the operating system maintains a linked list of timer nodes. When a timer node is added, a jiffies value is set, which is the frequency of triggering timed interrupts. Linux 0.11 triggers 100 times per second, or every 10 milliseconds. Add a timer with a jiffies value of 2, which will be executed after two timed interrupts. The jiffies value is incremented by one for each timed interrupt.
_timer_interrupt: push %ds # save ds,es and put kernel data space push %es # into them. %fs is used by _system_call push %fs pushl %edx # we save %eax,%ecx,%edx as gcc doesn't pushl %ecx # save those across function calls. %ebx pushl %ebx # is saved as we use that in ret_sys_call pushl %eax movl $0x10,%eax mov %ax,%ds mov %ax,%es movl $0x17,%eax mov %ax,%fs incl _jiffies ...
The following is the timer structure diagram
#define TIME_REQUESTS 64
static struct timer_list { long jiffies; void (*fn)(); struct timer_list * next;} timer_list[TIME_REQUESTS], * next_timer = NULL;
void add_timer(long jiffies, void (*fn)(void)){ struct timer_list * p;
if (! fn) return; //Turn off interrupt to prevent multiple processes from "simultaneously" operating cli(); //expire directly and execute callback if (jiffies fn) directly break; //There are no empty items if (p >= timer_list + TIME_REQUESTS) panic("No more time requests free"); //assign values to empty items p->fn = fn; p->jiffies = jiffies; //Create a list in the array p->next = next_timer; // next_timer points to the first node, i.e. the earliest due node next_timer = p; /* Modify the linked list to ensure that timeouts are in descending order Principle: Each node is coordinated by the arrival time of the previous node, and the jiffies in the node are timeout times. is the number of jiffies after the previous node expires. */ while (p->next && p->next->jiffies
< p->jiffies) { //If the previous node is larger than the subsequent node, subtract the value of the subsequent node from the previous node to calculate the offset value. Prepare the replacement position below. p->jiffies -= p->next->jiffies; //save it first fn = p->fn; //replace callback of two nodes p->fn = p->next->fn; p->next->fn = fn; jiffies = p->jiffies; //Replace two nodes is timeout p->jiffies = p->next->jiffies; p->next->jiffies = jiffies; /* At this point, the first node is the fastest to expire, and you need to update the values of subsequent nodes, which is actually to find a suitable location. Insert, because the kernel is a timer queue implemented with arrays, insert is implemented by replacing positions. If it is a linked list, then directly find the appropriate position, insert it, the so-called appropriate position, Find the first node larger than the current node and insert it in front of it. */ p = p->next; } /* Kernel implementation here has a bug, when the current node is the smallest, need to update the value of the first node in the original list, Otherwise, the expiration time of the first node in the original list will be extended. The repair code is as follows: if (p->next && p->next->jiffies > p->jiffies) { p->next->jiffies = p->next->jiffies - p->jiffies; } That is, update the offset of the first node in the original list relative to the new first node, and the remaining nodes do not need to be updated because they are relative to the new node. The offset of the node in front of him is unchanged, but there is no node before the first node in the original list, so the offset is his own value. And now a node is inserted in front of it, so its offset is relative to the offset of the previous node */ } sti();}//timed interrupt handler void do_timer(long cpl){ extern int beepcount; extern void sysbeepstop(void);
if (beepcount) if (!-- beepcount) sysbeepstop(); //currently in user mode, increase the execution time of user mode, otherwise increase the system execution time of the process if (cpl) current->utime++; else current->stime++; // next_timer is empty, indicating that there is no timing node if (next_timer) { //Subtract one jiffy from the first node, because the other nodes are offset from the first node, so the values of the other nodes do not need to change. next_timer->jiffies--; //The current node expires. If there are multiple nodes with the same timeout time, i.e., the offset from the first node is 0, the while loop will be entered multiple times. while (next_timer && next_timer->jiffies fn; next_timer->fn = NULL; //next node next_timer = next_timer->next; //execute timed callback function (fn)(); } } if (current_DOR & 0xf0) do_floppy_timer(); //The available time of the current process is reduced by one. If it is not 0, continue to execute. Otherwise, you may need to reschedule if (--current->counter)>0) return; current->counter=0; //If it is a system process, continue to execute if (! cpl) return; //process scheduling ();} After reading the above, do you have any further understanding of the principle analysis of linux 0.11 based operating system timers? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.