• This is an open-book exam. • Notes and calculators are permitted, but not computers. • Write your answer legibly in the space provided. • You have 80 minutes for this exam. • We will drop your lowest score among questions 1–6.
Problem Max Score Symbols and Linking
1
15
15
Virtual Address Translation
2
15
15
Process Control
3
15
15
Signals
4
15
15
Garbage Collection
5
15
15
Cyclone
6
15
12
Total
75
1
1. Symbols and Linking (15 points) In this problem we consider issues of symbols and linking. Consider the following three files. File polygon.h: struct Node { float pos[2]; int marked; struct Node* next; struct Node* prev; }; typedef struct Node node;
/* only for GC */
node* alloc(); void init(); void gc();
/* allocated new node */ /* initialize store */ /* call garbage collector */
File main.c (with portions of the function elided) #include "polygon.h" node* root_ptr; int main () { node* p; init(); p = alloc(); root_ptr = p; ... gc(); ... return 0; }
/* root pointer, for GC */
/* GC root is first allocated pointer */
File gc.c (with function bodies elided) #include "polygon.h" #define N (1prev); }
3. (7 pts) Next, complete the function sweep. Maintain the free list as a singly linked list with next pointing to the next free element. There are many solutions–for calibration, our solutions adds 7 lines to the function body. void sweep () { int i; node* v; free_ptr = NULL; /* initialize free list */ for (i=0; imarked) v->marked = 0; else { v->next = free_ptr; free_ptr = v; } } }
10
6. Cyclone (15 points) In this problem we reconsider the garbage collector sketched above and port it from C to Cyclone. We have left out some type declarations for you to fill in. struct Node { float pos[2]; int marked; struct Node* next; struct Node* prev; }; typedef struct Node node; #define N (1next = NULL; /* init next pointer */ new_ptr->prev = NULL; /* init prev pointer */ new_ptr->marked = 0; /* unmarked */ /* do not initialize pos[2] */ return new_ptr; }
1. (6 pts) For each of the 6 missing types, fill in the most precise type among the following which makes the code type-check without any implicit casts (and hence without warnings). (i) node@ (most precise) (ii) node* (iii) node? (least precise) 1 node* 2 node* 3 node@ 4 node@ 5 node@ 6 node@ 2. (3 pts) Assume that Cyclone considers global variables as living on the heap (region ‘H). Each of the following expressions denotes a pointer. Indicate the region that this pointer points to, or write unknown if this cannot be determined. (i) p1 in function main.
‘H
(ii) &p1 in function main.
‘main
(iii) new_ptr in function alloc.
‘H
12
3. (3 pts) The pos[] fields are not initialized in the alloc function. Explain why this does not compromise safety of Cyclone. They are floating point numbers, so computation with them cannot lead to a memory error: the bit pattern in these fields can always be interpreted as a floating point number of some form.