Spring16exam Final KAISTans
Spring16exam 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 (1:00
PM – 3:20 PM) 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) For caching in a memory hierarchy, what is the motivation for a larger cache
block size? Check the answer. (5 points)
(c) Assume you can choose either 1% of page faults or 10% of cache miss for
running your program instructions. Which one would you choose for better
performance and why? (5 points)
10% cache miss rate is better since a page fault requires disk activity,
which is about 10,000 to 100,000 times slower than a cache miss on
memory access.
1
Name: Student ID:
(d) On Linux, you can copy fileA to fileB like “cp fileA fileB”. During
execution, what kind of exceptions does cp possibly experience and why?
Assume cp does not have any bugs, and fileA is a large file on a disk. (5
points)
2
Name: Student ID:
t->array[h] = p;
return 0;
}
Name: Student ID:
4
Name: Student ID:
5
Name: Student ID:
(a) How many ‘A’ do you see when you call printA()? (Assume every fork()
succeeds. ) (5 points)
void printA(void)
{
printf(“A”);
fflush(stdout);
if (fork() == 0) {
fork();
printf(“A”);
}
printf(“A”);
}
Common: A
first fork – parent: A (fork != 0)
first fork – child: second fork –parent AA
first fork – child: second fork –child AA
In total, we see 6 As
(b) How many ‘A’ do you see when you comment out the line, fflush(stdout);? (5
points)
In total, we see 8 As
6
Name: Student ID:
void printEE209(void)
{
fprintf(stdout, “Final exam is easy\n”);
fprintf(stderr, “Final exam is hard\n”);
}
void main(void)
{
/* error processing is omitted for brevity*/
int fd = dup(1);
dup2(2, 1);
dup2(fd, 2);
printEE209();
}
7
Name: Student ID:
C code:
int add2int(char *a, int b[], int n)
{
int sum = atoi(a);
int i;
Assembly language code: Please fill in the code after “push %ebp”.
There could be many solutions. The following is one of them.
add2int:
pushl %ebp
movl %esp, %ebp
pushl %ebx # -4($ebp)
pushl %esi # -8(%ebp)
pushl %edi # -12(%ebp)
subl $4, %esp # for local var, sum
movl 8(%ebp), %eax # for parameter, a
pushl %eax # parameter for atoi
call atoi
addl $4, %esp
movl %eax, -16(%ebp) # store the return val to sum
movl $0, %ebx # use %ebx as i, i = 0
jmp LCHECK
8
LOOP:
movl 12(%ebp), %eax # $eax has parameter, b
leal (,%ebx,4), %edx # $edx = 4*i
Name: Student ID:
9
Name: Student ID:
void infiloop()
{
while (1) sleep(1);
}
int main(void)
{
if (fork() == 0) {
printf(“I am a child\n”);
infiloop();
} else {
printf(“I am a parent\n”);
}
return 0;
}
10
Name: Student ID:
void infiloop()
{
while (1) sleep(1);
}
static int isChildReady = 0;
void SetChildReadyFlag(int signum)
{
isChildReady = 1;
}
int main(void)
{
/* any reasonable signal number is fine */
signal(SIGINT, SetChildReadyFlag);
if (fork() == 0) {
printf(“I am a child\n”);
/* send the signal to parent process */
kill(getppid(), SIGINT);
infiloop();
} else {
/* any reasonable code that waits for the flag
change is fine */
while (!isChildReady) sleep(0);
printf(“I am a parent\n”);
}
return 0;
} 11