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

Preview:

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

Lab 4 : Real-Time OSTeam #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

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

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

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.

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]

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”.

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.

Priority Inversion (continued)

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.

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.

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 )

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.

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.

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;

}

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.

}

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.

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.

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, … )

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

Recommended