3304
Solutions to Final Exam
August 9, 1997
Consider carefully the following C program:
#include <stdio.h> /* LINE 1 */
/* LINE 2 */
/* number of times strange called */ /* LINE 3 */
static int called = 0; /* LINE 4 */
/* LINE 5 */
int strange (int n) { /* LINE 6 */
static int called = 0; /* LINE 7 */
int x; /* LINE 8 */
/* LINE 9 */
called++; /* LINE 10 */
x = n-2; /* LINE 11 */
if (x <= 1) /* LINE 12 */
return 1; /* LINE 13 */
else /* LINE 14 */
return x*strange(x-1); /* LINE 15 */
} /* LINE 16 */
/* LINE 17 */
void main () { /* LINE 18 */
int x; /* LINE 19 */
/* LINE 20 */
for (x=1;x<7;x++) /* LINE 21 */
printf("strange(%d)=%d\n",x,strange(x)); /* LINE 22 */
printf("strange called %d times\n",called); /* LINE 23 */
} /* LINE 24 */
Assume normal compile-link-execute processing of this program.
Do the following for this program.
strange(1)=1 strange(2)=1 strange(3)=1 strange(4)=2 strange(5)=3 strange(6)=4 strange called 0 times
You are to add Dijkstra's guarded commands to C, Pascal, or ALGOL (your choice). You are to make the syntax compatible with the existing language and ``in the same style.'' Do the following:
and
are defined elsewhere.
do q1 > q2 -> temp := q1; q1 := q2; q2 := temp;
[] q2 > q3 -> temp := q2; q2 := q3; q3 := temp;
[] q3 > q4 -> temp := q3; q3 := q4; q4 := temp;
od
goto
gotoany label1, label2, ..., labeln
that nondeterministically transfers control to one of the labels
in the list.
Here is a solution for C.
if and do constructs
of section 7.6 in C-like syntax.
We add new keywords guardedif and guardeddo
to distinguish these constructs from other C control structures.
Here is the EBNF for these two constructs:
The punctuation in quotes (") must appear literally as given.
guardeddo construct.
guardeddo {
(q1 > q2) {
temp = q1; q1 = q2; q2 = temp;
}
(q2 > q3) {
temp = q2; q2 = q3; q3 = temp;
}
(q3 > q4) {
temp = q3; q3 = q4; q4 = temp;
}
}
loop_top: if q1>q2 goto first_true if q2>q3 goto false_true if q3>q4 goto third_block goto loop_exit first_true: if q2>q3 goto true_true if q3>q4 gotoany first_block third_block goto first_block false_true: if q3>q4 gotoany second_block third_block goto second_block true_true: if q3>q4 gotoany first_block second_block third_block goto first_block second_block first_block: temp := q1 q1 := q2 q2 := temp goto loop_top second_block: temp := q2 q2 := q3 q3 := temp goto loop_top third_block: temp := q3 q3 := q4 q4 := temp goto loop_top loop_exit:
This problem is about Ada tasks and the possibility of deadlocks.
Assume that each of the tasks in question
is in an infinite loop
in which it will periodically accept any of its entries.
Do the following.
Can these tasks deadlock?
Explain.
Can these tasks deadlock?
Explain.
task task1 is
entry get;
entry put;
end task1;
task task2 is
entry new;
entry old;
end task2;
task task3 is
end task3;
task task4 is
entry win;
entry lose;
end task4;
Assume that task1 invokes only task2.new
and task4.lose.
Assume that task2 invokes no entries in the other tasks.
Assume that task3 invokes only task1.get, task1.put,
task2.old, and task4.win.
Assume that task4 invokes only task2.new.
task monday is
entry january;
entry february;
end monday;
task tuesday is
entry june;
entry july;
end tuesday;
task wednesday is
entry october;
entry november;
end wednesday;
Assume that monday invokes only tuesday.june
and tuesday.july.
Assume that tuesday invokes only monday.january,
wednesday.october, and wednesday.november.
Assume that wednesday invokes only monday.february.
As there are no directed cycles in this graph, there can be no deadlock.
As there are directed cycles in this graph,
deadlock is possible.
For example,
it could happen that monday
is waiting for a rendezvous with tuesday.june,
while tuesday is waiting for a rendezvous with monday.january.
This is clearly a deadlock.
A Prolog binary tree is a binary tree
such that every internal node has exactly two children
and every node is labeled with a Prolog atom.
Here is an example:
Do the following:
prologtree(X)
that is true when X is a Prolog binary tree.
You may assume that there exists a predicate atom(Y)
that is true when Y is a Prolog atom.
bob,
is represented by that atom, in this case, bob.
If X and Y are Prolog binary trees
and Z is an atom,
then the Prolog binary tree with root labeled Z,
left child of the root X,
and
right child of the root Y
is represented by the Prolog list [Z,X,Y].
The example tree is represented
[bob,julie,[alice,sue,bill]]
prologtree(X) :- atom(X). prologtree([X,Y,Z]) :- atom(X), prologtree(Y), prologtree(Z).
[bob,julie,[alice,sue,bill]]The table in Figure 2 gives the successful steps that Prolog follows. The steps without instantiations are the resolution of
atom terms (or goals),
where no rule is involved.
At the end of the table,
the empty goal indicates success.
This document was generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
The command line arguments were:
latex2html -split 0 -t CS 3304 Solutions to Final Exam solvefinal.
The translation was initiated by cs3304sm class account on Sat Aug 9 13:16:56 EDT 1997