









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Do not write in the boxes below.
I (xx/18) II (xx/15) III (xx/32) IV (xx/21) V (xx/14) Total (xx/100)
x = x + 1;
What does the above line of code do? Hint: remember the Therac-25. The question is asking about overflow, and is intended as a reminder that even simple operations can have complexity, when working close to the machine’s level of abstraction. Solution: It adds one to x, unless x starts as 255 = 28 − 1 in which case it sets x to 0. Or, call the value of x prior to this line x′. If 0 ≤ x′^ < 255 , then x is set to x′^ + 1 , whereas if x′^ = 255 , then x is set to 0. More concisely, x ← (x + 1 ) mod 2^8.
#include <stdio.h>
void func(int* p) {
int q = 2;
p = &q;
*p = 5; }
int main() {
int a = 9;
func(&a);
printf("%d\n", a);
return 0; }
What does this program print?
$ ./simplels
Your friend’s assignment specified that simplels must operate as follows:
Below is a sample run of your friend’s simplels implementation. The mkdir line creates a directory called foo, and the line after creates some files in that directory: abcd, mnopq, etc.
$ mkdir foo $ touch foo/abcd foo/mnopq foo/rstuv foo/wxyz $ ./simplels foo foo: $ echo $? 2
This run gives you some debugging hints. For example, notice that simplels runs and does so without dumping core. Thus, the bug is not a syntax or memory error. And notice that while simplels produces some output, it wrongly fails to print the files within foo. Your friend’s buggy implementation is on the next page, followed by some useful definitions.
1 int main(int argc, char* argv[]) { 2 struct stat sb; 3 struct dirent* direntp; 4 DIR* d; 5 6 if (argc != 2) { 7 printf("error\n"); 8 exit(2); 9 } 10 11 d = opendir(argv[1]); 12 if (d == NULL) 13 exit(2); 14 15 printf("%s:\n", argv[1]); 16 17 // assign result of readdir() to direntp, and check for NULL 18 while ( (direntp = readdir(d)) != NULL) { 19 20 if (direntp->d_name[0] == ’.’) 21 continue; 22 23 if (stat(direntp->d_name, &sb) < 0) 24 exit(2); 25 26 printf("%s", direntp->d_name); 27 28 if (S_ISDIR(sb.st_mode)) // is directory? 29 printf("/"); 30 printf("\n"); 31 } 32 return 0; 33 }
// Useful definitions: struct dirent { char d_name[256]; /* Null-terminated filename / ... / Other members; aren’t needed here */ };
// opendir() opens a directory stream corresponding to the directory name, // and returns a pointer to the directory stream. DIR *opendir(const char *name);
// readdir() returns a pointer to a dirent structure representing the next // directory entry in the directory stream pointed to by dirp. It returns NULL // on reaching the end of the directory stream or if an error occurred. struct dirent *readdir(DIR *dirp);
// stat() retrieves information about the file pointed to by // name, placing the information in the stat structure. int stat(const char *name, struct stat *statbuf);
acquire(&mutex); if (not_ready_to_proceed()) { wait(&mutex, &cv); } release(&mutex);
Under the above assumptions, when is this pattern correct? Follow the concurrency command- ments. Your answer should not be longer than one sentence. Never.
A A mutex B An atomic processor instruction C A queue of quiescent waiters D An implicit memory barrier E Deadlock detection F A ticketing algorithm G Busy-waiting H A monitor
B,D,G
North | | | | | | | | W ------- ------- E e <-B a s B-> s t ------- ------- t | ˆ | | B | | | | | South
The cross-roads can be in east-west mode: this means that east and west bicycles can go, and the north and south bicycles must wait. Or it can be in north-south mode, which is the other way around. The mode changes if and only if the cross-roads is empty and an orthogonal (to the current mode) bicycle wishes to enter. The cross-roads can hold 5 bicycles in each of the two current directions. For example, if the cross-roads is in east-west mode, it can accommodate 5 bicycles going east and 5 going west.
The cross-roads is a monitor; you will complete the implementation of this monitor. Each bicycle is a thread that calls into the monitor before entering the cross-roads; this call may cause the bicycle to wait. A bicycle also invokes the monitor after exiting the cross-roads. Pseudocode for a bicycle is on the next page. Some notes about your task:
Below, write down the conditions under which a bicycle can and cannot enter the cross-roads. Informal text is fine. This exercise will help you with the rest of the problem.
// Below, complete the implementation of // Xroads::Xroads() // and give the implementations of // void Xroads::Enter(dir_t d) // void Xroads::Exit(dir_t d) // Reminder: you need to add to the definition of Xroads on the previous page Xroads::Xroads() { memset(num, 0, sizeof(num)); mode = NORTHSOUTH;
// ADD SOME STUFF HERE
}
Additional data members in Xroads:
class Xroads {
.... private: ... Mutex m; Cond cv;
};
Methods:
Xroads::Xroads() { .... m.init(); cv.init(); }
void Xroads::Enter(dir_t d) { m.acquire();
while (!IsEmpty() && (dir2mode(d) != mode || num[d] >= 5)) cv.wait(&m);
if (IsEmpty()) mode = dir2mode(d);
++num[d];
m.release();
}
void Xroads::Exit(dir_t d) { m.acquire();
--num[d];
cv.broadcast(&m);
m.release(); }
As a reminder, the x86-64 imposes a multi-level page table structure: pages are 4KB, each page table entry is 8 bytes, and each individual page table (a node in the “tree”) occupies one page. Thus, each page table holds 4KB8B = 512 = 29 entries. Recall that the structure is four levels; each level is indexed by 9 bits of the virtual address.
What is the minimum number of physical pages consumed by a process that allocates 12KB (for example, 1 page each for code, stack, and data)?
7 pages: 3 for the memory it’s allocated, and 4 to implement the paging structure.
What is the minimum number of physical pages consumed by a process that makes 29 + 1 allo- cations of size 4KB each? You can leave your answer in terms of powers of 2, and sums thereof.
29 + 6 , or 518 pages. Each L4 page table holds 29 mappings. So we need two L4 page tables. The total is 1 + 1 + 1 + 2 for the page structures plus the 2^9 + 1 pages themselves.
What is the minimum number of physical pages consumed by a process that makes 218 + 1 allocations of size 4KB each? You can leave your answer in terms of powers of 2, and sums thereof.
218 + 29 + 6. Each L3 page table points indirectly to 218 last-level page entries (each L3 page table has 29 entries, each of which points to an L4 page table with 29 entries). Thus, the question requires two L3 page tables. The first L3 page table points to 29 L4 page tables; the second points to one L4 page table, for a total of 29 + 1 L4 page tables. Thus, the total is: 1 + 1 + 2 + 29 + 1 for the page structures plus 2^18 + 1 for the pages themselves.