IT215: Systems Software, Winter 2014-15 Second In-Sem Exam (2 Hours)
IT215: Systems Software, Winter 2014-15 Second In-Sem Exam (2 Hours)
Roll Number:
Instructions:
• Make sure your exam is not missing any sheets, then write your name and roll number on the top of
this page.
• Clearly write your answer in the space indicated. None of the questions need long answers.
• For rough work, do not use any additional sheets. Rough work will not be graded.
• The exam has a maximum score of 50 points. It is CLOSED BOOK. Notes are NOT allowed.
1 (12):
2 (6):
3 (4):
4 (12):
5 (6):
6 (4):
7 (6):
TOTAL (50):
Page 1 of 10
Problem 1. (12 points):
Circle the single best answer to each of the following questions. If you circle more than one answer, you
will lose the mark for the corresponding question.
1. Imagine a process (called “process A”) that spawns three child processes. If all three child processes
terminate before process A is picked by the kernel to be run again, how many times could process A
received SIGCHLD?
(a) 0
(b) 1
(c) 3
(d) 1 or 3
2. Consider the following piece of code. Note that the file name is the same for both calls to open, and
assume the file one.txt exists.
Page 2 of 10
The following program is a failed attempt by a student to get a parent and child process to communicate —
the child is intended to run the /bin/ls program (which produces a directory listing to stdout) and the
parent is to run the /usr/bin/wc program (which counts the characters, words, and lines in its stdin and
prints the counts to stdout).
main()
{
int fds[2];
pipe(fds);
if (fork() == 0) {
/* child */
dup2(fds[1], 1);
execl(PROG1, PROG1, NULL);
} else {
/* parent */
dup2(fds[0], 0);
execl(PROG2, PROG2, NULL);
}
}
3. Upon running the program, the student observes that there is no output whatsoever. Why?
Page 3 of 10
Problem 2. (6 points):
Consider the C program below. (For space reasons, we are not checking error return codes, so assume that
all functions return normally.) Note that newlines may be present in the output but are disregarded for this
problem. Additionally, assume that all printf statements are flushed immediately.
int main() {
fork();
printf("0");
if (fork() == 0) {
/* this next exec prints "1" */
execl("/bin/echo", "echo", "1", NULL);
fork();
printf("2");
}
printf("3");
return 0;
}
For each of the following strings, circle whether (Y) or not (N) this string is a possible output of the program.
You will be graded on each sub-problem as follows:
a. 00112233223333 Y N
b. 0132310323 Y N
c. 013013 Y N
d. 001133 Y N
e. 013310 Y N
f. 030311 Y N
Page 4 of 10
Problem 3. (4 points):
Your answers to the following questions should be 50 words or less. Responses longer than 50 words will
receive 0 points.
A. The implementation of tsh–your tiny shell assignment–relied on a SIGCHLD handler to update the
job list whenever a job terminated or stopped. The installed SIGTSTP handler, on the other hand,
simply intercepted and forwarded SIGTSTP on to the foreground job. Why not update the job list to
reflect the job state change inside the implementation of the SIGTSTP handler?
B. Signals set to be caught by custom signal handlers will have default signal handling behavior in the
child process after exec is called. Why can’t the child process inherit the parent’s installed signal
handlers instead?
Page 5 of 10
Problem 4. (12=8+4 points):
Consider an allocator that uses an implicit free list, where the layout of each memory block is as follows:
31 2 1 0
_____________________________________
Header | Block Size (bytes) | |
|______________________________|______|
| |
| |
|_____________________________________|
Footer | Block Size (bytes) | |
|______________________________|______|
Each memory block, either allocated or free, has a size that is a multiple of 8 bytes. Thus, only the 29 higher
order bits in the header and footer are needed to record block size, which includes the header and footer.
The usage of the remaining 3 lower order bits is as follows:
• bit 0 indicates the use of the current block: 1 for allocated, 0 for free.
Note: The header and footer will always be present regardless of whether the block is allocated or not.
Implement the code as indicated by the comments below. Make use of previous results and the function
size() when possible. Careful: pointer arithmetic is valid only on pointers to actual types, not void *.
Part 1
void *p = malloc(20);
return __________________________________;
}
Page 6 of 10
Part 2
Given the contents of the heap shown on the left, fill in the header and footer values in hex (on the right
side) after block 3 is freed, and coalescing is done.
Page 7 of 10
Problem 5. (6 points):
For each code snippet, circle the value(s) of i that could possibly be printed by the printf command at
the end of each program. Assume that all functions and procedures return correctly and that all variables
are declared and initialized properly. Also, assume that an arbitrary number of SIGINT signals, and only
SIGINT signals, can be sent to the code snippets below randomly from some external source. Careful:
There may be more than one correct answer for each question. Circle all the answers that could be correct.
Penalty for circling wrong answer = -2 marks
Code snippet 1: Code snippet 2:
int i = 0; int i = 0;
(a) 0 (a) 0
(b) 1 (b) 1
(c) 50 (c) 50
(d) 100 (d) 100
(e) 101 (e) 101
(f) Terminates with no output. (f) Terminates with no output.
Page 8 of 10
Problem 6. (4 points):
The following program performs a shell-like file copying. It forks out a child process to copy the file. The
parent process waits for the child process to finish and, at the same time, captures the SIGINT signal. The
signal handler asks the user whether to kill the copying or not. If the user answers ’y’, the child process will
be terminated. Otherwise, the signal handler just silently returns from the signal handler.
Fill in the blank lines to make the code work. Please fill in “(empty)” if no code is needed.
pid_t child_pid = 0;
printf("Stop copying?\n");
scanf("%c", &answer);
if (answer == ’y’ && child_pid != 0)
;
}
void copy() {
if ((child_pid = fork()) == 0) {
copy_file();
exit(0);
}
else {
if (wait(NULL) == child_pid) {
printf("Child process %d ended.\n", child_pid);
}
}
}
Page 9 of 10
Problem 7. (6 points):
You are asked to show what each of the following two programs prints as output.
• Assume that file infile.txt contains the ASCII text characters “15213”;
• When a process with no children invokes waitpid(-1, NULL, 0), this call returns immediately.
int main()
{
int fd;
char c;
fork();
waitpid(-1, NULL, 0);
return 0;
}
OUTPUT:
Program B
int main()
{
int fd;
char c;
fork();
waitpid(-1, NULL, 0);
return 0;
}
OUTPUT:
Page 10 of 10