Sie sind auf Seite 1von 4

# HOMEWOK #1

## (Due Tuesday 1-March-2016 by 11:00 am)

1. Consider the following program in Listing 1.1. The function lSecondsSinceMidnight returns
the number of seconds since midnight. A hardware timer asserts an interrupt signal every
second, which causes the microprocessor to run the interrupt routine vUpdateTime to
update the static variables that keep track of the time.
static int iSeconds, iMinutes, iHours;
void interrupt vUpdateTime (void){
++iSeconds;
if (iSeconds >- 60){
iSeconds = 0;
++iMinutes;
if (iMinutes >= 60){
iMinutes = 0;
++iHours;
if (iHours >= 24)
iHours = 0;
}
}
long lSecondsSinceMidnight (void)
{
return ( (((iHours * 60) + iMinutes) *60) + iSeconds);
}

## (A) This program contains a shared data bug.

(i) Explain how this bug may occur in the code of Listing 1.1
(ii) Propose how this bug may be solved.
(B) Now consider the program in Listing 1.2. The program tries to solve the bug in Listing 1.1
static long int lSecondsToday;
void interrupt vUpdateTime (void){
++lSecondsToday;
if (lSecondsToday == 60 * 60 * 24){
lSecondsToday =0L;
}
}
long lSecondsSinceMidnight (void){
return (lSecondsToday);
}

## Listing 1.2 Proposed Solution to the bug in Listing 1.2

This program also may contain a shared data bug when the registers in the
microprocessor are not as large as the data space needed to store a long integer.
(i)
Suppose that longintegers are 32 bits long and that your microprocessor
has 16-bit registers. How far off can the result of lSecondsSinceMidnight be?
What if your microprocessor has 8-bit registers?
(ii)
Even if your microprocessor has 32-bit registers, Listing 1.1 has another
potential suble bug. This bug will show itself if your system has an interrupt
that is higher priority than the timer interrupt that corresponds to
updateTime and if the interrupt routine for that higher-priority interrupt uses
lSecondsSinceMidnight. What is this bug, and how might you fix it?
2. Consider the following function. Is the following function reentrant?
int errors;
void countErrors (int e)
{
errors += e;
}

University of Rwanda

Homework #1

Page | 1

## 3. Consider the code in Listing 3.1.

static int iValue;
int iFixValue (int iParm)
{
int iTemp;
iTemp =iValue;
iTemp += iParm * 17;
if (iTemp > 4922)
{
iTemp =iParm;
}
iValue =iTemp;
iParm =iTemp + 179;
if (iParm < 2000)
{
return 1;
}
else return 0;
}

Listing 3.1.
Where do you need to take and release the semaphores in the code Listing 3.1 to make the
function reentrant? Rewrite your program to reflect the changes. You can call the necessary
semaphore functions through pseudocodes or just use plain English comments.
4. A car engine controller, which is designed based on a processor with a capability similar to that of
an Atmega2560, must have the following functions:
(1) Generate pulses for the spark plugs. A 4-cylinder engine has 4 spark plugs and each one
fires every two revolutions of the engine. At the max rate of 6,000 RPM, we need 200
pulses per second sent out to four digital ports (one bit for each spark plug). Each spark
plug is pulsing 50 times per second. Each pulse is 1ms. These pulses must be timed to
within 10sec. 6000 RPM is the maximum but the speed is always changing. The engine
can go as low as 200RPM. Assume that it takes 5sec of CPU time to send out a pulse to
the digital port.
(2) Compute fuel injection settings. Adjust fuel rate 10 times per second.
(3) Pollution sensors must be read every 1 second 100ms. Compute the moving average of
each sensor reading over the last 600 seconds. This takes 500sec of CPU time.
(4) Evaluate the health of the sensors by checking the moving averages against a table. Do
every minute. If a sensor fails, start a new task which makes the Check Engine light blink
(until reset by technician).
(5) Read accelerator (driver input) 10 times per second.
(6) Read fuel consumption and odometer every second and compute miles per gallon.
Apply a digital filter to this measurement to smooth the result over 24 hours.
(7) Update speedometer, RPM display, MPG display Fuel Gauge, Temp Gauge, every 500 ms.
(A) What type of architecture would you use for this application? Explain how your answer meets
(B) List the tasks you would design as parts of the real-time software system. Specify whether a task
should be an interrupt service routine instead. Also, for each task, list its inputs and outputs.

University of Rwanda

## CIT3423 Real-Time Systems

Homework #1

Page | 2

5. Consider a RTOS that has a preemptive priority based scheduler. There are five tasks, one data
buffer, one 8-bit digital I/O port, and two serial ports. The five tasks are: (each tasks pseudo code is
inside a while infinite loop which is not shown). The working principle of the tasks is summarized in
the following pseudocode:
1. Initialize digital I/O port for input
3. Count rising edges on each input
4. Sleep for 5 milliseconds
1. Read Data from serial port 1
2. Check CRC byte for errors in serial data
3. if (data buffer is empty) {write data payload to buffer}
1. Initialize digital I/O port to output
2. write data to digital outputs
3. toggle bit 0 of digital I/O port on and off (i.e. make a brief pulse on bit 0).
4. Sleep for 10ms
1. if (data buffer is full) {take data from buffer}
2. Write data to serial port 2
1. Count number of bytes in data buffer
2. display byte count on LCD
3. Sleep for 250ms
(A) Which resources are shared by at least two tasks? Fill in the table with a group of tasks on the
left and one or more resources on the right in the following table

(B) Identify the specific lines number of the above pseudocode which are critical sections.

6. In the code in Listing 6.1, assume that messages in your RTOS consist of void pointers, that
the function SendMessage() places the void pointer passed to it on a queue, and that
while (1){

## // Other part of the program omitted for clarity

if (/*A key has been pressed on the keyboard*/ ){

vGetKey ();
}

## // Other part of the program omitted for clarity

}
}
void vGetKey (void){
char ch= // Get Key from keyboard */

## SendMessage (KEY_MB0X, &ch, PRIORITY_NORMAL);

}
char *p_chLine; /* Pointer to key character pressed */
char ch; /* The character that was pressed. */
while (1){
/* Wait for another key to be received. */

## p_chLine = ReceiveMessage (KEY_MB0X, WAIT_FOREVER);

ch = *p_chLine;
// Do what is needed with ch

}
}

Listing 6.1

(A) What is wrong with the code in the code of listing 6.1?
(B) Propose a way to solve this problem.

University of Rwanda

## CIT3423 Real-Time Systems

Homework #1

Page | 3

7. The following pseudo code shows two processes trying to enter into critical section

PROCESS P0

PROCESS P1

## while flag [1]

{
// do nothing
}
flag[0]=true;
<critical section block>
flag[0]=false

## while flag [0]

{
// do nothing
}
flag[1]=true;
<critical section block>
flag[1]=false

In a nutshell, the process P0 checks the flag of the process P1. If the flag is set 'on' (i.e.,
flag[1]=true), the process P1 is in the critical section. If the flag is 'off (i.e., flag[1]='false'), the
process P0 sets its own flag to 'true' and enters the critical section. The process P1 behaves
similarly. Does the above code guarantee mutual exclusion? If yes, explain in details why. If
no, give an example.
8. Consider two tasks P1 and P2 which use two shared resources R1 and R2. The two resources
are protected by semaphores S1 and S2 as shown in the following pseudo-code:

## The following scenario is given:

P2 is executing (running)
P1 become ready for execution when P2 has done its Wait operation on S2
Assuming that task P1 has higher priority than P2 and that the semaphores S1 and S2 are
both initialized to one.
(A)Show that the above described scenario leads to a deadlock, i.e. that the
execution of P1 and P2 will block both P1 and P2.
(B) Suppose that a priority ceiling protocol is used. This means that for each shared
resource, a priority is associated with the resource. This priority, denoted PR is chosen
to be higher than the highest priority of all tasks using the resources. In the situation
described above, this means that PR1 and PR2 are both chosen as a priority which is
higher than the priority of P1. The priority ceiling protocol then has the effect that
when a task tries to reserve a resource R, its priority is changed, from its original
value to PR, and when a task releases a resource, its priority is set back to its original
priority. In this conditions, show that the use of priority ceiling protocol leads to a