⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cs427 the thread mechanism in zebra.htm

📁 zebra免费软件
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0059)http://wiki.cs.uiuc.edu/cs427/The+Thread+Mechanism+in+Zebra -->
<HTML><HEAD><TITLE>cs427: The Thread Mechanism in Zebra</TITLE>
<META http-equiv=Content-Type content="text/html; charset=big5">
<META content="MSHTML 6.00.2800.1458" name=GENERATOR></HEAD>
<BODY vLink=#000000 link=#0000ff bgColor=#ffffff><FONT color=#000000>
<TABLE cellSpacing=2 cellPadding=0 width="100%" bgColor=#ffffc0>
  <TBODY>
  <TR>
    <TD><FONT face=Arial><A 
      href="http://wiki.cs.uiuc.edu/cs427/EDIT/The+Thread+Mechanism+in+Zebra">Edit</A></FONT></TD>
    <TD><FONT face=Arial><A 
      href="http://wiki.cs.uiuc.edu/cs427/RENAME/The+Thread+Mechanism+in+Zebra">Rename</A></FONT></TD>
    <TD><FONT face=Arial><A 
      href="http://wiki.cs.uiuc.edu/cs427/DIFF/10/9/The+Thread+Mechanism+in+Zebra">Changes</A></FONT></TD>
    <TD><FONT face=Arial><A 
      href="http://wiki.cs.uiuc.edu/cs427/HISTORY/The+Thread+Mechanism+in+Zebra">History</A></FONT></TD>
    <TD><FONT face=Arial><A 
      href="http://wiki.cs.uiuc.edu/cs427/UPLOAD">Upload</A></FONT></TD>
    <TD><FONT face=Arial><A 
      href="http://wiki.cs.uiuc.edu/cs427/DOWNLOAD/">Download</A></FONT></TD>
    <TD><FONT face=Arial><A href="http://wiki.cs.uiuc.edu/cs427">Back to 
      Top</A></FONT></TD></TR></TBODY></TABLE>
<H2><FONT face=Arial><A 
href="http://wiki.cs.uiuc.edu/cs427/SEARCH/The+Thread+Mechanism+in+Zebra">The 
Thread Mechanism in Zebra</A></FONT></H2><!-- unformatted page contents --


--------

[NEXT > http://wiki.cs.uiuc.edu/cs427/Patterns+in+Zebra] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [HOME > http://wiki.cs.uiuc.edu/cs427/Software+Architecture+of+Zebra]

---------

<br>


After the daemons are started and have performed their initial initializations like installation of command nodes and command elements, they do most of their work by executing the functions using the thread mechanism. The thread is not what we understand of normal operating system supported threads. Zebra defines thread as a structure and performs its own scheduling of threads. 

There is one master thread that maintains the list of threads to be executed. The information maintained by master thread includes:

* Read list
* Write list
* Timer list
* Event list
* Ready list
* Unuse list

The threads structure includes the following information

* id;
* type of the thread
* pointer to the next thread in the list
* pointer to the previous thread in the list
* pointer to the master thread
* pointer to the event function
* event arguments
* file descriptor in case of read/write
* time value


Thread functions

* thread_make_master ();
* thread_add_read() 
* thread_add_write() 
* thread_add_timer() 
* thread_add_event() 
* thread_cancel_event() 
* thread_fetch() 
* thread_call() 
* thread_execute() 

Zebra and all other daemons uses this thread structure to perform its operations. These thread structures store the functions to be called. 

After zebra has been started, it performs certain initializations and after daemonizing itself it uses thread_fetch() to get the thread at the head of the event queue and executes the function stored in it.  

Before thread_fetch is called inside the daemon, the event queue, read queue, write queue, and timer queue contain threads that are waiting to be called. During the call of thread_fetch, thread_fetch dequeues a thread at the head of the event queue and this thread will be the one that gets executed. If the event queue is empty, all the threads in the read queue and write queue are transferred to the event queue. [[[FENG > Zebra References]]


<b> Thread Life Cycle </b>[[[FENG > Zebra References]]

The life cycle of a thread is shown in the following figure and a brief description is outlined below.

<br>
<br>
<center>
[http://wiki.cs.uiuc.edu/cs427/DOWNLOAD/ZebraThreadLifeCycle.gif]
</center>
<br>
<br>

* A new thread can be created easily by a daemon using thread_add_read, thread_add_write, or thread_add_timer function calls, depending on the type of the thread it wants to create. 

** thread_add_read - adds a thread to the read queue that is responsible for accepting and reading data from the outside through a socket.

** thread_add_write adds a thread to the write queue that is responsible for flushing and writing data to the outside through a socket.

** thread_add_timer adds a thread to the timer queue that is responsible for timing an event such as updating and redistributing table.

* In all of the calls made above, the function thread_new is used to allocate memory space for a new thread. It first checks if there is any unuse thread in the unuse queue. If there is one, it will use it as the new thread. Otherwise, it will call malloc to allocate memory space for the new thread. The parameters of the thread structure are set to default. Finally, thread_new invokes thread_all_list to add the new thread to the desired thread queue.

* There is a point at which the Zebra daemon continuously fetches threads from the event queue and executes them. Once the thread get executed, the thread?s type is set to unuse and transferred to the unuse queue.

* If the event queue is empty, the daemon executes select function that monitors 搒ets?of file descriptors; in particular readfds, writefds, and exceptfds. When select returns, readfds, writefds, and exceptfds would have modified to reflect which of the file descriptors selected is ready for reading, writing, and performing exception. Then, the program can test if the file descriptor of a thread is ready, by using FD_ISSET.

* After performing select, all the threads inside the read queue that have their readfds flag ready, are moved to event queue. All the threads inside write queue that has its writefds flag ready, are moved to the event queue. Other threads that are not ready for I/O remain in their own queues. Once the threads in the read and write queue are moved to the event queue, their readfds and writefds flags are cleared. exceptfds are not used in Zebra.

* Only timer threads that have their timers expired relative to current clock time are moved to the event queue.

* Once this is done, the thread at the head of the event queue is fetched and gets executed if there is any. If the head of the event queue is empty, repeat step 3 to 5 indefinitely. 



---------------

[NEXT > http://wiki.cs.uiuc.edu/cs427/Patterns+in+Zebra] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [HOME > http://wiki.cs.uiuc.edu/cs427/Software+Architecture+of+Zebra]
//-- unformatted page contents -->
<P>
<P>
<HR>

<P><A href="http://wiki.cs.uiuc.edu/cs427/Patterns+in+Zebra">NEXT</A> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A 
href="http://wiki.cs.uiuc.edu/cs427/Software+Architecture+of+Zebra">HOME</A> 
<P>
<HR>

<P><BR>
<P>
<P>After the daemons are started and have performed their initial 
initializations like installation of command nodes and command elements, they do 
most of their work by executing the functions using the thread mechanism. The 
thread is not what we understand of normal operating system supported threads. 
Zebra defines thread as a structure and performs its own scheduling of threads. 
<P>There is one master thread that maintains the list of threads to be executed. 
The information maintained by master thread includes: 
<P>
<UL>
  <LI>Read list 
  <LI>Write list 
  <LI>Timer list 
  <LI>Event list 
  <LI>Ready list 
  <LI>Unuse list </LI></UL>The threads structure includes the following 
information 
<P>
<UL>
  <LI>id; 
  <LI>type of the thread 
  <LI>pointer to the next thread in the list 
  <LI>pointer to the previous thread in the list 
  <LI>pointer to the master thread 
  <LI>pointer to the event function 
  <LI>event arguments 
  <LI>file descriptor in case of read/write 
  <LI>time value </LI></UL>
<P>Thread functions 
<P>
<UL>
  <LI>thread_make_master (); 
  <LI>thread_add_read() 
  <LI>thread_add_write() 
  <LI>thread_add_timer() 
  <LI>thread_add_event() 
  <LI>thread_cancel_event() 
  <LI>thread_fetch() 
  <LI>thread_call() 
  <LI>thread_execute() </LI></UL>Zebra and all other daemons uses this thread 
structure to perform its operations. These thread structures store the functions 
to be called. 
<P>After zebra has been started, it performs certain initializations and after 
daemonizing itself it uses thread_fetch() to get the thread at the head of the 
event queue and executes the function stored in it. 
<P>Before thread_fetch is called inside the daemon, the event queue, read queue, 
write queue, and timer queue contain threads that are waiting to be called. 
During the call of thread_fetch, thread_fetch dequeues a thread at the head of 
the event queue and this thread will be the one that gets executed. If the event 
queue is empty, all the threads in the read queue and write queue are 
transferred to the event queue. [<A 
href="http://wiki.cs.uiuc.edu/cs427/Zebra+References">FENG</A>] 
<P>
<P><B>Thread Life Cycle </B>[<A 
href="http://wiki.cs.uiuc.edu/cs427/Zebra+References">FENG</A>] 
<P>The life cycle of a thread is shown in the following figure and a brief 
description is outlined below. 
<P><BR><BR>
<CENTER><IMG 
src="cs427 The Thread Mechanism in Zebra_files/ZebraThreadLifeCycle.gif" 
border=0> </CENTER><BR><BR>
<P>
<UL>
  <LI>A new thread can be created easily by a daemon using thread_add_read, 
  thread_add_write, or thread_add_timer function calls, depending on the type of 
  the thread it wants to create. </LI></UL>
<UL>
  <UL>
    <LI>thread_add_read - adds a thread to the read queue that is responsible 
    for accepting and reading data from the outside through a socket. 
</LI></UL></UL>
<UL>
  <UL>
    <LI>thread_add_write adds a thread to the write queue that is responsible 
    for flushing and writing data to the outside through a socket. </LI></UL></UL>
<UL>
  <UL>
    <LI>thread_add_timer adds a thread to the timer queue that is responsible 
    for timing an event such as updating and redistributing table. </LI></UL></UL>
<UL>
  <LI>In all of the calls made above, the function thread_new is used to 
  allocate memory space for a new thread. It first checks if there is any unuse 
  thread in the unuse queue. If there is one, it will use it as the new thread. 
  Otherwise, it will call malloc to allocate memory space for the new thread. 
  The parameters of the thread structure are set to default. Finally, thread_new 
  invokes thread_all_list to add the new thread to the desired thread queue. 
  </LI></UL>
<UL>
  <LI>There is a point at which the Zebra daemon continuously fetches threads 
  from the event queue and executes them. Once the thread get executed, the 
  thread?s type is set to unuse and transferred to the unuse queue. </LI></UL>
<UL>
  <LI>If the event queue is empty, the daemon executes select function that 
  monitors 搒ets?of file descriptors; in particular readfds, writefds, and 
  exceptfds. When select returns, readfds, writefds, and exceptfds would have 
  modified to reflect which of the file descriptors selected is ready for 
  reading, writing, and performing exception. Then, the program can test if the 
  file descriptor of a thread is ready, by using FD_ISSET. </LI></UL>
<UL>
  <LI>After performing select, all the threads inside the read queue that have 
  their readfds flag ready, are moved to event queue. All the threads inside 
  write queue that has its writefds flag ready, are moved to the event queue. 
  Other threads that are not ready for I/O remain in their own queues. Once the 
  threads in the read and write queue are moved to the event queue, their 
  readfds and writefds flags are cleared. exceptfds are not used in Zebra. 
</LI></UL>
<UL>
  <LI>Only timer threads that have their timers expired relative to current 
  clock time are moved to the event queue. </LI></UL>
<UL>
  <LI>Once this is done, the thread at the head of the event queue is fetched 
  and gets executed if there is any. If the head of the event queue is empty, 
  repeat step 3 to 5 indefinitely. </LI></UL>
<P>
<P>
<HR>

<P><A href="http://wiki.cs.uiuc.edu/cs427/Patterns+in+Zebra">NEXT</A> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A 
href="http://wiki.cs.uiuc.edu/cs427/Software+Architecture+of+Zebra">HOME</A>
<P>
<HR>

<FORM action=/cs427/SEARCH method=post><INPUT type=hidden value=SEARCH 
name=COMMAND> <INPUT type=submit value=Find...> <INPUT size=40 
name=SEARCHPATTERN> </FORM>
<TABLE cellSpacing=2 cellPadding=0 width="100%" bgColor=#ffffc0>
  <TBODY>
  <TR>
    <TD><FONT face=Arial><A 
      href="http://wiki.cs.uiuc.edu/cs427/EDIT/The+Thread+Mechanism+in+Zebra">Edit</A></FONT></TD>
    <TD><FONT face=Arial><A 
      href="http://wiki.cs.uiuc.edu/cs427/RENAME/The+Thread+Mechanism+in+Zebra">Rename</A></FONT></TD>
    <TD><FONT face=Arial><A 
      href="http://wiki.cs.uiuc.edu/cs427/DIFF/10/9/The+Thread+Mechanism+in+Zebra">Changes</A></FONT></TD>
    <TD><FONT face=Arial><A 
      href="http://wiki.cs.uiuc.edu/cs427/HISTORY/The+Thread+Mechanism+in+Zebra">History</A></FONT></TD>
    <TD><FONT face=Arial><A 
      href="http://wiki.cs.uiuc.edu/cs427/UPLOAD">Upload</A></FONT></TD>
    <TD><FONT face=Arial><A 
      href="http://wiki.cs.uiuc.edu/cs427/DOWNLOAD/">Download</A></FONT></TD>
    <TD><FONT face=Arial><A href="http://wiki.cs.uiuc.edu/cs427">Back to 
      Top</A></FONT></TD></TR></TBODY></TABLE></FONT></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -