These problems should be done on your own. You get credit if you turn in something, and we are not checking the correctness. So these exercises exist purely to reinforce the material, not to evaluate you.
Getting help from AI (besides being ruled out by the course policies) will actually hurt you, since the point of these questions is for you to gain the practice and experience of working through the problems.
You will need that general skill (of absorbing something by practicing it) in life, and in this semester you will need the specific problem-solving skills that are emphasized in these homeworks.
TLBs
Consider a TLB which can store 4 mappings (the TLB is fully associative, meaning that any entry can store any mapping; if this parenthetical confuses you, you can ignore it). Below you will write C code to compute the sum of all integers in an array a, which is 6 pages in length; you will do this in a way that maximizes the number of TLB misses (equivalently, minimizes the number of TLB hits).
A few things to note:
- The array is allocated to be page aligned, meaning that the first element in the array is at the beginning of a page.
- Your program can assume that the constant
PAGE_SIZEis the size of a page in bytes and thatsizeof(int)is the size of an integer. - You can ignore the effect on the TLB from fetching code; in other words, you can assume that the only memory references that affect the TLB are loads from array
a. (In real systems, there are separate TLBs for instructions and data; this question is focusing on the data TLB.) - You can further assume that the processor does nothing else while your code is running; that is, you don’t need to worry about TLB flushes from context switches.
uint64_t tlb_unfriendly() {
int *a = page_alloc(6 * PAGE_SIZE);
populate_array(a); // sets the integers in the array
uint64_t sum = 0;
/* YOUR CODE HERE: compute sum in the most TLB-unfriendly way possible */
return sum;
}
Uses of page faults
In this problem, you will describe how the implementation of malloc() can exploit paging so that the system (as a whole) can detect certain kinds of out of bound accesses; an out of bound access is when a process references memory that is outside an allocated range. In this problem we focus on overruns. Consider this code:
int *a = malloc(sizeof(int) * 100); /* allocates space for 100 ints */
a[0] = 5; /* This is a legal memory reference */
a[99] = 5; /* This is also a legal memory reference */
a[100] = 6; /* This is an overrun, and is an illegal memory reference. */
When the above executes, the process would ideally page fault as a result of an illegal memory reference, at which point the kernel would end the process.
Assume that malloc() is a system call, so its implementation is inside the operating system, and thus can manipulate the virtual address space of the process.
Describe how the implementation of malloc() can arrange for page faults when there are overruns like the one above.
Polling vs. interrupts
As discussed in class, two ways for an operating system to become aware of external events associated with a device are interrupts and polling. We observed that if a computer were receiving many interrupts, it might spend all of its time processing them and not get other work done; in that case, the operating system should switch to polling the device. Now consider the following:
A computer has an attached keyboard. The keyboard has a 1024-byte internal memory buffer to hold the codes of recently-pressed keys, each of which consumes 2 bytes of buffer space. (The buffer is a FIFO, which for our purposes means that the OS simply reads from it and doesn’t manage the memory; if this parenthetical confuses you, you can ignore it.)
This computer and its OS take 1 microsecond (10 − 6 seconds) to handle an interrupt from the keyboard. That duration includes everything: reading from the keyboard’s buffer, determining which key was pressed, and painting the appropriate letter to the screen.
Assume that polling requires a fixed cost of 1 microsecond per poll. Further assume that, per poll, the operating system can read an arbitrary amount of the keyboard’s internal memory buffer, up to the entire size of that buffer.
Assume that, if polling, the operating system checks the device in question every 200 milliseconds.
Assume that humans are sensitive to lags of 100 milliseconds or greater. Specifically, if a human types a letter, that letter must appear on the screen less than 100 milliseconds after the human types it, to avoid annoyance.
You type exceptionally quickly: 200 words per minute. Assume that the average word has 7 letters, including the space at the end of the word.
Each key code (each letter, in other words) generates a separate interrupt.
- How many interrupts per second would your typing generate on average? Show your work.
- Should the computer use polling or interrupts to handle your fast typing? Explain why your choice is acceptable and the other choice is not. Do not use more than three sentences.
Handing in the homework
Use Gradescope; you can enroll in our course with entry code J44YWJ.