Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 2: Note: To show that a routine is not reentrant, you must provide a sc

ID: 653995 • Letter: E

Question

Exercise 2:

Note: To show that a routine is not reentrant, you must provide a scenario and show how this scenario produces wrong results (i.e., results different from the sequential execution of two calls). Without a scenario, your answers will be worthless.

Consider a CPU with an ARM architecture. Such a CPU saves on the stack a subset S of registers whenever an interrupt occurs, and restores the same set of registers when back from any interrupt service handler.

The following function swaps two variables. In order to make it faster, we use a register as a temporary variable.

Register temp;

byte swap(int *x, int *y){

temp = *x;

*x     = *y;

*y     = temp

}

Case 1: suppose that the register used for temp belongs to subset S. Is this function rentrant? Why if yes? If not, provide a scenario that shows it is not reentrant?

Case 2: suppose that the register used for temp does not belong to subset S. Is this function rentrant? Why if yes? If not, provide a scenario that shows it is not reentrant?

Case 3: Suppose that swap function is not reentrant, propose two methods that would allow a safe use of the swap function.

Explanation / Answer

Case 1: suppose that the register used for temp belongs to subset S. Is this function rentrant? Why if yes? If not, provide a scenario that shows it is not reentrant?
Ans1. Yes it is reentrant.In ARM architecture exception handling uses stacks extensively because each exception has a specific mode of operation, so switching between modes occurs and saving the previous mode data is required before switching so that the core can switch back to its old state successfully. So here the value can be retrieved back from the temp register.


Case 2: suppose that the register used for temp does not belong to subset S. Is this function rentrant? Why if yes? If not, provide a scenario that shows it is not reentrant?
Ans2.It is not reentrant, the scenario is as follows:
Register temp;

byte swap(int *x, int *y){

temp = *x;

*x = *y;
// hardware interrupt might invoke isr() here!
*y = temp;
}

void isr()
{
int x = 1, y = 2;
swap(&x, &y);
}

This swap function leaves the global data in a non-consistent state at the time it exits.That's why it should not be used in interrupt service routine isr().

Ans3. In most cases, non-reentrant functions must be replaced by functions with a modified interface to be reentrant.

example: the caller can provide the temp variable itself
byte swap(int *x, int *y,int temp){
temp = *x;
*x = *y;
*y = temp
}
It can be made reentrant by removing static (or global) non-constant data.
Example:
Register temp;
void swap(int *x, int *y)
{
int s;

s = temp; // save global variable
temp = *x;
*x = *y;

// hardware interrupt might invoke isr() here!
*y = temp;
temp = s; // restore global variable
}

void isr()
{
int x = 1, y = 2;
swap(&x, &y);
}