23
Lab 4 : Real-Time OS Team #7 P91922003 李李李 P90922016 李李李 R91921030 李李李

Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Embed Size (px)

DESCRIPTION

Common Pitfall Use a global flag to test whether a shared resource is currently available or not: int buffer_busy; … while (buffer_busy==0); buffer_busy = 1; // write data into buffer Busy Waiting

Citation preview

Page 1: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Lab 4 : Real-Time OSTeam #7

P91922003 李彥勳P90922016 謝嵩淮R91921030 侯凱文

Page 2: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Common PitfallUse a global flag to test whether a shared resource is currently available or not:

int buffer_busy;…while (buffer_busy==0);buffer_busy = 1;// write data into buffer

Page 3: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Common PitfallUse a global flag to test whether a shared resource is currently available or not:

int buffer_busy;…while (buffer_busy==0);buffer_busy = 1;// write data into buffer

Busy Waiting

Page 4: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Common PitfallUse a global flag to test whether a shared resource is currently available or not:

int buffer_busy;…while (buffer_busy==0);buffer_busy = 1;// write data into buffer

Interrupt

Page 5: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Common Pitfall ( continued )

Busy waiting : wasting CPU time. Even in the following situation, CPU load still heavy :while (buffer_busy==0) Sleep(0); Interrupt : if two tasks sharing the same buffer, two tasks may try to write this buffer simultaneously.

Page 6: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Common Pitfall ( continued )

This code section is frequently written by many engineers. Solution : using semaphore, mail box … IPC primitives so that the task can actually enter “Waiting” state without occupying CPU time-slice. ARM instruction “SWP” can be used to achieve “atomic operation” :

ADR r0, SEMAPHORESWPBr1,r1,[r0]

Page 7: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Critical Section Implementation of uC/OS-II Disable/Enable interrupt

ARMDisableIntMRS r12, CPSRORR r12, r12, #NoIntMSR CPSR_c, r12MOV pc, lr That’s why it is not a “HARD real time OS”.

Page 8: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Priority Inversion A long existing terminology, recently it becomes a buzzword due to “Pathfinder” on Mars (RTOS: VxWorks). It refers to a lot of situation, may even be used intentionally to prevent starvation. Unbounded priority inversion : Priority inversion occurs in unpredictable manners.

Page 9: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Priority Inversion (continued)

Page 10: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Priority Inversion (continued)

The problem is that τ1 and τ3 share a common data structure, locking a mutex to control access to it. Thus, if τ3 locks the mutex, and τ1 tries to lock it, τ1 must wait. Every now and then, when τ3 has the mutex locked, and τ1 is waiting for it, τ2 runs because it has a higher priority than τ3. Thus, τ1 must wait for both τ3 and τ1 to complete and fails to reset the timer before it expires.

Page 11: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Priority Inversion (continued)

Solution : priority inheritance protocol and priority ceiling protocol. Priority ceiling : raises the priority of any locking thread to the priority ceiling for each lock. Priority inheritance : raises the priority of a thread only when holding a lock causes it to block a higher priority thread.

Page 12: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Lab problems In 4.3.4 (eg3), if we did not define “SEMIHOSTED”, the stack will overflow and corrupt “personal_info” data structure. OSTimeDly() does not work, must reconfigure ARMulator according to 4.6.2. ( or just don’t use it )

Page 13: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文
Page 14: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Lab problems (continued) When AXD starts, it sometimes raise the following error, and AXD can’t continue :

Solution : remove this file (default-1-2-0-0.ses) and restarts AXD again.

Page 15: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Exercise Client task : Input ID Server task: Check the ID Simulate two tasks running in different machines. Server task controls all the important system parameters, and the checking rules. When checking rules or system parameters changed, only server side needs to be modified.

Page 16: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Exercise (continued)Client Task’s pseudo code : (input/output)

while (1) {Obtain system parameters from server;Allocate memory for input strings;Do basic checking to reject strings that are longer than the length limit specified in system parameters;Send strings to server side;//authentication completes, do jobs. Free allocated memory;

}

Page 17: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Exercise (continued)Server task’s pseudo code : (checking)

while (1) {Wait for messages from client side;Do checking on the ID strings, and perform actions according to the result (here we display the

results);Send system parameters to clients.

}

Page 18: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文
Page 19: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文
Page 20: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

DiscussionWhat are the advantages and disadvantages of using an RTOS? Adv : With RTOS, we can speed up the software development cycle, since it can do “resource allocation and managements” for us. Dis : Increase the total code sizes, some embedded OS may took a lot of system resources.

Page 21: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Discussion (continued) Dis: may results in un-wanted behavior due to the internal mechanism (such as scheduler), for example, priority inversion. Adv/Dis: Without RTOS, user can take full control of everything, in other words, user must take care of everything, this might lengthen the development cycle according to the complexity of the application.

Page 22: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

Conclusion If jobs can be done in a single task (simple application), no RTOS is needed. If jobs must be done in many different tasks which contest with each other for many shared resources – use RTOS. (eg: an embedded AP may require web service, telnet service, … )

Page 23: Lab 4 : Real-Time OS Team #7 P91922003 李彥勳 P90922016 謝嵩淮 R91921030 侯凱文

ReferenceModern Operating Systems, Andrew S. Tanenbaum.http://www.timesys.com/files/whitepapers/Priority_Inversion_1.0-TM.pdf