Fall16exam Final KAISTans
Fall16exam Final KAISTans
KAIST EE209
Programming Structures for Electrical Engineering
Final Exam
Name:
Student ID:
This exam is open book and notes. Read the questions carefully and focus your answers on
what has been asked. You are allowed to ask the instructor/TAs for help only in understanding
the questions, in case you find them not completely clear. Be concise and precise in your
answers and state clearly any assumption you may have made. You have 140 minutes (9:00
AM – 11:20 AM) to complete your exam. Be wise in managing your time. Good luck.
Question 1 / 25
Question 2 / 20
Question 3 / 10
Question 4 / 15
Question 5 / 15
Question 6 / 15
Total / 100
Name: Student ID:
(b) Many programs exhibit some sort of locality of reference in memory access
such as temporal locality and spatial locality. Briefly explain why the locality of
reference matters in performance. (5 points)
(c) When multiple processes run on a machine, they share the CPU by taking
turns. When does one process switch to another? Give two examples where a
process context switches to another process. (5 points)
1
Name: Student ID:
(e) In the following code, (1) what kind of exceptions do you see? (2) How can
you make the program _not_ crash? (You cannot modify func() nor avoid
calling func() in main(), but you can add some code before calling func()). No
need to write any real code, but be specific about your approach. (5 points)
#include <stdio.h>
void func(void) {
char *str = “hello World\n”;
*str = ‘H’;
printf(“%s”, str);
}
int main() {
func();
return 0;
}
(memory) fault. func() will generate memory access violation and make the
program crash. One way to make the program _not_ crash is to set up a
signal handler to catch SIGSEGV to jump to instructions that do not incur
memory access violation. For grading, you will get a full point if you
mention signal handling of SIGSEGV.
But in practice, simply installing a signal handler for SIGSEGV isn’t
enough since after handling the signal, the same instruction that triggers
memory access violation would be executed again, which generates
SIGSEGV. It would make the program get stuck in an infinite loop.
To get out of the infinite loop, the signal handler must change the
instruction point (e.g., EIP) to a safe place. One thing you can do to
achieve this is to use setjmp()/longjmp().
In short, it is not a good idea to catch/ignore SIGSEGV. The best thing to
do is to fix the bug.
2
Name: Student ID:
4
Name: Student ID:
(b) How do you improve the performance of TableSum() above as we insert and
delete many (key, value) items over time? Briefly explain your approach (5 points)
c) You want to limit the total number of items stored at the table. When you insert a
new item, you first check if the total number of items is below X. If so, you can insert
the new item. If the total number is X, you need to remove the least recently used
(LRU) item from the table and insert the new item into the table. What would you do
implement this LRU-based item replacement? Briefly explain your approach (explain
extra data structures and algorithms for keeping track of the LRU property).
Have a separate list (called k) that keeps track of the LRU items. At
insertion of a new item, add the item to the end of k. At deletion of an item,
remove the item from k. At looking up for an item in the table, move the
item (if it’s found) at k to the end of k. Whenever an LRU item is looked up
(for replacement), retrieve the front item at k.
5
Name: Student ID:
(a) How many ‘A’ do you see when you call test1()? (Assume every fork()
succeeds. ) (5 points)
void test1(void)
{
int i;
for (i = 0; i < 5; i++){
fork();
}
printf(“A”);
}
32 times
At the end of i = 0, two processes (parent, child)\
At the end of i = 1, four processes in total since two processes call fork()
independently. For each iteration, the number of processes doubles.
At the end of i =2, eight processes exist.
At the end of i =3, 16 processes exist.
At the end of i =4, 32 processes exist.
(b) How many ‘A’ do you see when you call test2()? (Assume every fork()
succeeds. ) (5 points)
void test2(void)
{
if (fork() != 0) {
if (fork() == 0)
fork();
} else {
fork();
}
printf(“A”);
}
5 times
6
Name: Student ID:
In order to complete the program, you need to fill out these two functions.
int main(void)
{
char *args[] = {“grep”, “KyoungSoo”, NULL};
if (RedirectStdin(“score”) < 0) {
fprintf(stderr, “could not read from score\n”);
exit(-1);
}
if (RedirectStdout(“result”) < 0) {
fprintf(stderr, “could not open result\n”);
exit(-1);
}
execvp(“grep”, args);
7
return 0;
}
Name: Student ID:
8
Name: Student ID:
C code:
int CountCapitalLetters(char *p)
{
int count = 0;
while (*p != 0) {
if (isupper((int)*p)) count++;
p++;
}
return count;
}
Assembly language code: Please fill in the code after “pushl %ebp”.
CountCapitalLetters:
pushl %ebp
movl %esp, %ebp
pushl %ebx
pushl %esi
pushl %edi
sub $4, $esp # make space for count
movl $0, -16($ebp) # count = 0
WLoop:
movl $0, $eax # init $eax to 0
movb 8($ebp), $al # $al = *p
cmpb $0, $al # *p != 0
je LDone
pushl $eax # parameter for isupper()
call isupper
9
addl $4, $esp # clean up the param
cmpl $0, $eax
Name: Student ID:
je LNext
addl $1, -12($ebp) # count++
LNext:
addl $1, 8($ebp) # p++
jmp WLoop
LDone:
popl %edi
popl %esi
popl %ebx
movl $ebp, $esp
popl %ebp
ret
10
Name: Student ID:
$ ls
a
b
c
$ ls | tee x
a
b
c
x
$ cat x
a
b
c
x
11
Name: Student ID:
On success, it returns the number of bytes read (zero indicates end of file).
It is not an error if this number is smaller than the number of bytes
requested. On error, -1 is returned.
ssize_t write(int fd, void *buf, size_t count);
On success, it returns the number of bytes written. The return value could
be smaller than count. On error, it returns -1.
FILE* fopen(const char *path, const char *mode);
FILE* fclose(FILE *stream);
size_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream);
fwrite() writes nmemb elements of data, each size bytes long, to the
stream pointed to by stream, obtaining them from the location given by ptr.
On success, it returns the number of items written. On error, it returns a
short item count (or zero).
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> /* no points will be deducted for
not including these header files */
12
Name: Student ID:
if (fp != NULL) {
if (fwrite(buf, 1, len, fp) != len) {
fprintf(stderr,
"write to file %s failed\n", argv[1]);
exit(-1);
}
}