GUARINI NICOLAS — M. 918670 — [email protected]
Draw the control flow graph (CFG) of the method executePool(...)
given below.
class ProcessHandler {
public static final int NUM_PHYSICAL_UNITS = 8;
public static final int MAX_POSTPONE = 2;
private boolean isUsingVirtualUnits = false;
public void executePool(List<Process> primaryP, List<Process> secondaryP) {
for (Process p : primaryP) {
p.setPriority(Process.HIGH_PRIORITY);
}
List<Process> toExec = new ArrayList<>();
toExec.addAll(primaryP);
if (primaryP.size() > NUM_PHYSICAL_UNITS) {
switchToVirtualUnits();
toExec.addAll(secondaryP);
}
for (Process p : toExec) {
if (p.priority() >= Process.HIGH_PRIORITY) {
exec(p);
} else {
int ttl = p.updTimeToLive();
if (ttl > 0) {
handle_postponed(p);
}
}
}
}
private void switchToVirtualUnits() { isUsingVirtualUnits = true; /* ... */ }
private void exec(Process p) { /* ... */ }
private void handle_postponed(Process p) { assert(isUsingVirtualUnits); /* ... */ }
}
public class Process {
public static final int HIGH_PRIORITY = 10;
private int priority = HIGH_PRIORITY;
private int ttl = 1;
public Process(int ttl) { if (ttl > 1) this.ttl = ttl; }
public int priority() { return priority; }
public void setPriority(int p) { priority = p; }
public int updTimeToLive() { return --ttl; }
}
Use the CFG to answer the following questions:
Question 1) Show a CFG path that leads to executing method "handle_postponed
" without never executing method "switchToVirtualUnits
".
B1 — B2 — B3 — B6 — B8 — B9 — B10 — B13 — B14 — B11 — B9 — B15;
Question 2) Assume that a software analyzer reports an alarm for the program path of Question 1: The report warns that the assertion executed in method "handle_postponed" fails with that program path. Is that report a true alarm or a false alarm, and why?
The report is a real alarm because the assertion in handle_postponed()
asserts that the variable isUsingVirtualUnits
is true, but since the switchToVirtualUnits()
method (B7), which is the only one that sets this variable to true, has never been executed, then the assert fails, and therefore the alarm is true.