Due: Tuesday, April 15 at 11:59pm
Each question is worth an equal number of points. When a question asks you to describe or explain, your answers can be relatively brief. The [name] tags give attribution to authors of other OS textbooks who wrote the question (you do not need the textbook to be able to answer the question, though). Please submit your answers as a PDF file to Gradescope.
a) Set value of timer
b) Read the clock
c) Clear memory
d) Turn off interrupts
e) Switch from user to kernel mode
Below is a small C program that prints the passage of time in seconds using the simple Unix alarm interface (see man 2 alarm on ieng6):
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
volatile int seconds;
void handler (int signum) {
seconds++;
alarm (1); // schedule next SIGALRM
}
int main (int argc, char *argv[]) {
int last = seconds = 0;
signal (SIGALRM, handler); // SIGALRM is the timer signal
alarm (1); // schedule SIGALRM handler callback, arg is # of seconds
while (1) { // normally we would do useful work instead of spinning
if (last != seconds) {
printf ("%d sec\n", seconds);
last = seconds;
}
if (last == 5) return 0;
}
}
a) What is the output of this program? To verify your answer, you can compile and execute the program on ieng6:
$ cc file.c [assuming you put the program contents into file.c] $ ./a.out
b) Generally it is not possible to predict how long a program will take to execute by inspecting its code. However, for this program we can. Approximately how long does it take to execute? (You can run it on ieng6 with time ./a.out.)
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
volatile struct data { long int one, two; } value;
void handler (int signum) {
if (value.one != value.two)
printf ("%ld != %ld\n", value.one, value.two);
alarm (1); // schedule next SIGALRM
}
int main (int argc, char *argv[]) {
static struct data zeros = { 0, 0 }, ones = { 1, 1 };
signal (SIGALRM, handler); // SIGALRM is the timer signal
alarm (1); // schedule SIGALRM handler callback, arg is # of seconds
while (1) {
value = ones;
value = zeros;
}
}
The program is a tight loop assigning ones and zeroes to value. Is it possible for the program to ever print out the != message in handler? Briefly explain why or why not. (Again you can compile the program and experiment with it.)
#include <stdlib.h>
int main (int argc, char *arg[])
{
fork ();
if (fork ()) {
fork ();
} else {
char *argv[2] = {"/bin/ls", NULL};
execv (argv[0], argv);
fork ();
}
}
a. How many total processes are created (including the first process running the program)? (Note that execv is just one of multiple ways of invoking exec, see man 3 exec for all possibilities.) You do not need to provide an explanation.
b. How many times does the /bin/ls program execute?
[Hint: You can always add debugging code, compile it, and run the program to experiment with what happens.]