CS3130 Spring 2023 quizzes



quiz for week 2

259 people have viewed this quiz

Answer the following question about the lecture material for this week.

Question 1 (5 pt; mean 4.47)

Suppose we have a program which is compiled from C code, but some of the C code is very repetitive. Rather than having the program's source code include a copy of the repetitive C code with the program, the program's authors want to include a Python script that generated that repetitive code, some hand-written C files, and a Makefile to build them all.

Suppose the Python script is called generate_lookup.py and when run it outputs the generated C code to its output. The Makefile writes the output of the Python script to lookup.c. The hand-written C files are called lookup.h and main.c.

Which of the following rules would be reasonable to include in the corresponding Makefile? ((tab) represents a tab character.) Select all that apply.

Regrade request

Suppose a single-core system is running three single-threaded processes A, B, and C.

The following events happen in the following order:

  1. Process A asks to read from the keyboard. No input is available initially.
  2. Process B does some computation.
  3. Process B sends a signal to process C.
  4. A key is pressed on the keyboard, causing process A to run.
  5. Process A accesses an invalid memory location and is terminated as a result.
  6. Process C's signal handler runs and prints a message to the screen.
Question 2 (4 pt; mean 3.08) (see above)

Which of the above steps are likely to involve making one or more system calls?

Write the numbers of the corresponding steps above; for example, if 1 and 5 involve system calls, write "15".

Answer:
Key: 136

didn't meant to ask about 3 or 6 since we did not cover signals yet; also accepted 16 or 1

Regrade request
Question 3 (4 pt; mean 3.35) (see above)

A non-system-call exception must occur during which of the steps above?

Write the numbers of the corresponding steps above; for example, if 1 and 5 must involve non-system-call exception, write "15".

Answer:
Key: 45

didn't meant to ask about 3 or 6 since we did not cover signals yet; also accepted 456 or 3456 or 345

Regrade request

Consider the following C code snippet running on a Linux system:

char buffer[1024];
int x, y;
fgets(buffer, sizeof buffer, stdin);
x = atoi(buffer);
fgets(buffer, sizeof buffer, stdin);
y = atoi(buffer);
printf("%d / %d = %d\n", x, y, x / y);
Question 4 (3 pt; mean 2.07) (see above)

Suppose x / y results in a division by 0. This will cause an exception to occur. While the handler for that exception is running, most likely ____. Select all that apply.

Regrade request
Question 5 (5 pt; mean 4.29) (see above)

Suppose the program above is run so that it takes input from the keyboard and shows output on a screen. When no division by zero occurs, which of the following operations occur in kernel mode when the code above runs? Select all that apply.

Regrade request


quiz for week 3

253 people have viewed this quiz

Answer the following question about the lecture material for this week.

Consider the following C code snippet running on a Linux system:

char buffer[1024];
int x, y;
fgets(buffer, sizeof buffer, stdin);
x = atoi(buffer);
fgets(buffer, sizeof buffer, stdin);
y = atoi(buffer);
printf("%d / %d = %d\n", x, y, x / y);
Question 1 (4 pt; mean 3.23) (see above)

On x86-64 Linux, dividing by 0 results in a process being sent the SIGFPE (Floating point exception — yes, even for integer division) exception. Suppose a signal handler for that signal was registered before the code above ran and divided by 0. Which of the following would be true about how the signal handler is run? Select all that apply.

Regrade request
Question 2 (3 pt; mean 2.58)

Consider the following a signal handler for SIGINT (triggered by Ctrl+C) that requires Ctrl+C to be pressed twice before actually exiting:

int sigint_counter = 0;
void handle_sigint(int signum) {
    if (sigint_counter == 0) {
        const char *message = "Press Ctrl+C again to exit!\n";
        write(1, message, strlen(message));
        sigint_counter = 1;
    } else {
        _exit(1);
    }
}

(The _exit function is like exit except that it does not run functions registered with atexit() or flush pending output to stdout and other stdio streams.) The signal handler is installed using the following code:

struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = &handle_sigint;
sigaction(SIGINT, &sa, NULL);

Which of the following is true about the above signal handler when running on x86-64 Linux? Select all that apply.

Regrade request
Question 3 (4 pt; mean 3.65)

POSIX access control lists like described in lecture would be suitable for enforcing which of the following policies on a system shared between students and an instructor? Select all that apply.

Regrade request
Question 4 (3 pt; mean 2.86)

In lecture, we mentioned the much more restricted permissions systems that Unix supports more commonly than ACLs. In this system, files have an owner user ID, a group ID, and 9 permission bits (read/write/execute for that user, that group, and others). This permission bit system is much less capable than ACLs. Which of the following policies can be achieved using these permission bits? (Ignore that the special administrator user root/superuser would have more access in all of these cases.) Select all that apply.

  1. if we assume it's okay to make a group containing just that user; otherwise no

  2. assuming we don't want to give others access to the file, this is not possible. Some solved this problem by giving all others (not just the other group) the ability to write, which is possible.

Regrade request

Consider the following page table:

virtual page #valid?physical page #
00---
110x2
210x3
310x9
40---
510x5
610x1
710x6

Suppose this page table is used on a system with 4096 byte pages. On this system, virtual addresses are divided into a 3-bit page number and a 12-bit page offset and physical addresses are divided into a 4-bit page number and a 12-bit page offset.

Question 5 (4 pt; mean 3.53) (see above)

If a program reads from virtual address 0x1234, what physical address will it read from?

Write your answer as a hexadecimal number. If an exception would occur instead, write "exception".

Answer:
Key: 0x2234
Regrade request


quiz for week 4

250 people have viewed this quiz

Answer the following questions about the lecture material for this week.

Consider a system with a virtual memory system with:

Question 1 (3 pt; mean 2.57) (see above)

A program tries to read a value from virtual address 0x12. The page table base register is set to physical byte address 0x100, so the processor looks up a page table entry from physical address ___. Write your answer as a hexadecimal number. Remember to take into account that page table entries are two bytes.

Answer:
Key: 0x102
Regrade request
Question 2 (3 pt; mean 2.77) (see above)

Suppose the value of the page table entry (from the previous question) is 0x4307 (when interpreted as an 2-byte integer). The program will end up accessing physical address _____. Write your answer as a hexadecimal number.

Answer:
Key: 0x432
Regrade request

Consider a system with a virtual memory system with:

Question 3 (4 pt; mean 2.95) (see above)

If a program can access 1000 distinct pages of memory (without exceptions occuring), then its page tables must take up at least ____ bytes. Remember to include both its first-level page tables and its second-level page tables.

Answer:
Key: 4096 * 3 = 12288
Regrade request
Question 4 (4 pt; mean 3.06) (see above)

When a program tries to access a value from memory, the processor:

  • reads its page table base register and gets the physical address 0x120000, then
  • reads a page table entry from physical address 0x120008 and that page table entry is valid and contains physical page number 0x123, then
  • reads a page table entry from physical address 0x123040 and that page table entry is valid and contains physical page number 0x6, then
  • reads the value from physical address 0x6010

What was the virtual address the program tried to access? Remember to take into account the page table entry size. Write your answer as a hexadecimal number. If not enough information is supplied, write "unknown" and explain in comments.

Answer:
Key: 0x208010

0x120008 has page offset 0x8 --> index 1 --> VPN part 1 is 1

0x123010 has page offset 0x040 --> index 0x40 / 8 = 8--> VPN part 2 is 0x8

0x6010 has page offset 0x10 --> page offset of VA is 0x10

combining these we get (0x1 << (12+9)) + (0x8 << 12) + 0x10

Regrade request

Suppose a system uses 4096-byte pages, so addresses have 12-bit page offsets.

Assume the system running this program is as lazy as possible in setting up page table entries -- it waits until a program accesses a virtual page to setup the page table entry for that page, following the allocate/load-on-demand strategy we discussed in lecture. (Perhaps a better strategy would be somewhere in between this lazy strategy and the strategy of loading everything in advance.)

The program starts, with all its pages initially unallocated (marked invalid in its page table). It then runs a function, whose machine code is loaded at addresses 0x2040-0x2072, which writes 3 8-byte values to the stack at addresses 0xFFF8, 0xFFF0, and 0xFFE8.

Question 5 (4 pt; mean 1.88) (see above)

As a result of the above operations, how many page faults will occur? (If not enough information is given write unknown and explain in the comments.)

Answer:
Key: 2

one for 0x2000-0x2FFF, one for 0xF000-0xFFFF

Regrade request


quiz for week 5

238 people have viewed this quiz

Answer the following questions about the lecture material for this week.

Suppose a system has:

Question 1 (4 pt; mean 3.58) (see above)

Suppose when looking up the virtual address 0x8001 on this system, the first-level page table entry the processor looks up has the value 0x1231. What is the (physical) address of the first byte of the second-level page table this processor will use?

Answer:
Key: 0x1230
Regrade request

Consider a network which provides the best-effort "mailbox" model gaurentees we discussed in lecture.

Suppose we try to send data from machine A to machine B that doesn't fit in a single message in parts using the following broken protocol:

Question 2 (4 pt; mean 3.94) (see above)

Suppose we send the data "ABCD" by splitting it into the four messages, "A", "B", "C", and "D". If the network does not corrupt, delay, or reorder messages, but does lose some messages, which of the following is it possible for machine B to think it was sent when it tries to reconstruct the data? Select all that apply.

My intention in writing this question was that "machine B receives all four parts" meant "machine B receives what it thinks are all four parts". Under this interpretation A and C are possible.

But some interpreted it as machine B having a way to verify it received all parts (despite not using this knowledge to reconstruct the message). Under this interpretation A and C are not possible.

Regrade request
Question 3 (4 pt; mean 3.71) (see above)

If the network loses every fourth message a machine sends, then how many messages will machine end up sending?

I meant to write "how many messages will machine A end up sending".

Assuming "received all four parts" just means that machine B gets four messages: Machine A will send A B C D and D will be lost. Then machine A will send A B C D again. Machine B will acknowledging receiving "ABCA". Machine A has sent 8 messages at this point and machine B has sent 1 (so total 9 messages sent).

If "received all four parts" includes machine B having a way to know whether it actually received four distinct messages, then it will never receive part D (it's lost on every resend), so it an arbitrarily large number of messages will be sent. (This scenario does not seem consistent with the incorrect message reconstruction from the previous question.)

Regrade request
Question 4 (4 pt; mean 3.56) (see above)

In the layers we discuss, a message sent on the link layer will have a destination address for the link layer in its frame. In Ethernet, this destination address is a MAC address. This destination address will identify where the message should go for the purpose of the link layer's functionality.

Suppose a program sends a frame containing message that is going to be sent to a faraway machine. In order to get to that machine, the data will first be received by a router on the local network, then that router will send it to another router, which will send it to another router, which will send it a final router. That final router will then send the data to the final machine.

When the message is initially sent (before it is forwarded by the first router), what best describes what the link-layer destination address that accompanies it will contain?

Regrade request
Question 5 (4 pt; mean 3.64) (see above)

Consider the scenario of the previous question.

In addition to the destination address for the link layer, the message will also contain a destination address for the network layer. The network layer also has an destination address in its packets. For example, in IP version 4, this would be an IP version 4 address (like 128.148.63.2).

When the message is initially sent (before it is forwarded by the first router), what best describes what the network-layer destination address that accompanies it will contain?

Regrade request


quiz for week 6

236 people have viewed this quiz

Answer the following questions about the lecture material for this week.

Question 1 (4 pt; mean 3.54)

Consider the following code where socket_fd is the file descriptor for a UDP socket (that has already been configured, including having its default destination address set with connect):

write(socket_fd, "FIRST", strlen("FIRST"));
write(socket_fd, "SECOND", strlen("SECOND"));
write(socket_fd, "THIRD", strlen("THIRD"));

Assuming the writes above do not fail, when code running on the other end runs:

char buffer1[100] = "initial value";
char buffer2[100] = "initial value";
read(other_socket_fd, buffer1, sizeof buffer1);
read(other_socket_fd, buffer2, sizeof buffer2);

Assuming messsage data is not corrupted on the network (despite the lack of end-to-end checksums), then, which of the following are possible outcomes in buffer1 and buffer2? Select all that apply.

  1. UDP has separate messages (not a stream), and each read reads only one message, so not possible

  2. dropped because of inconsistent phrasing with "will", I think the best interpretation of the question based on "possible outcomes" to select this option, but I understand the idea that we were implying that this might always happen; SECOND, THIRD message sent from machine successfully, but lost somewhere on network

  3. dropped because of inconsitent phrasing with "will"; FIRST message delayed by network

Regrade request

Suppose www.foo.com's DNS server when queried returns a record containing the IP address for www.foo.com specifying a time-to-live of 10000 seconds.

Question 2 (4 pt; mean 3.67) (see above)

We mentioned in lecture that an ISP's DNS server could obtain this record by first querying one of the "root" DNS servers. This root server would most likely ____.

Regrade request
Question 3 (4 pt; mean 3.49) (see above)

www.foo.com is accessed many times over the course of 5 hours by each of 1000 users. These 1000 users a split evenly across 10 different ISPs.

Each of these ISPs runs their own DNS server for their users that caches the returned records for as long as possible. About how many queries for the IP address of www.foo.com will www.foo.com's DNS server get?

Answer:
Key: 20

1 query per ISP near beginning of 5 hours, then another 10000 seconds later (= approx 2.8 hours) which less than 4 hours

Regrade request
Question 4 (4 pt; mean 3.43)

Suppose A and B each prepare to perform secure communications with each other as follows:

After this preparation is complete, we would expect A to have ____. Select all that apply.

  1. generated these, needs them to decrypt messages from B, sign messages for B

  2. if A has these, it can read things third-parties sent to B only

  3. needs to send things to B and verify signatures on things B sent

  4. could do this, but it's not useful

  5. could do this, but we were planning on using the keypairs generated for the communications, not something else. I don't think this option is consistent with the plan to use the keypairs for the communication, but I understand the idea of setting up symmetric encryption using the keypairs as being what was done when "A and B securely share[d] some information"

Regrade request
Question 5 (4 pt; mean 3.6)

Consider the following (broken) scheme for allowing users to run a command on a remote server:

The system administrator sets up MAC keys for every user of the system. These keys are distributed securely to those users when their account is setup and are used in addition to passwords.

When a user asks to login to the server remotely:

Which of the following are scenarios this protocol prevents? Select all that apply.

  1. user sending {MAC(secret, N + password), command}, can read command from message and ignore the rest

  2. user sending {MAC(secret, N + password), command}, can read that message and replace it with {MAC(N + password), other command}. It's true that we could avoid this by including the command in what is MAC'd, but someone implementing the protocol described above would think they complied with the protocol as written if they did not do that.

  3. I was aiming for the assumption here that the eavesdropper can send things on network, but doesn't get to interfere with our communciations like the more active attacker, but I did not make that clear enough. (But you'd still conclude that the eavesdropper couldn't forge commands if it was impossible for the eavesdroppper couldn't send things on the network.) Once original message is received by server, the N is no longer in use, so even though the eavesdropper has MAC(secret, N + password), they can't use that -- to run another command they'd need MAC(secret, N' + password) for some different number N'.(edited 24 March): Going through comments, I see that a bunch of students interpreted "login" in the question (which I should not have written) as meaning that the code was "one-time" per login, not "one-time" per command as I intended.

  4. MAC(key, foo) strongly resists recovery of information about foo unless you have the key, so even though attacker has MAC(key, foo), they won't be able to learn things about foo (which contains the password).

Regrade request


quiz for week 7

235 people have viewed this quiz

Answer the following questions about the lecture material for this week.

Question 1 (4 pt; mean 2.21)

In lecture, we discussed certificates used to verify website identities for TLS.

As typically used in TLS, the web server will have a certificate, which ___. Select all that apply.

  1. no, key in certificate typically used to establish other, short-term symmetric key which is used to encrypt this information

  2. web browser checks signature using already-obtained public key

Regrade request

In lecture, we mentioned key agreement protocols where two parties A and B would send each other key shares derived from secret values, then use the other's key share and their secret value to construct a shared secret value (known by A and B, but not anyone else).

Question 2 (4 pt; mean 3.73) (see above)

Suppose A and B use key agreement as follows:

  • first, they send each other key shares over an insecure network,
  • then, use their shared secret value to construct a key for symmetric encryption and a key for message authentication codes.

If other steps are not taken, this design would allow an attacker with sufficient access to the network to ___. Select all that apply.

  1. I think realistically the attacker would be able to prevent this by limiting A and B to a limited subset of message types/formats, but technically A and B could hide the key shares in other messages in a way which would be hard to detect.

  2. intended to write "the place of A"

  3. intended to write "the place of A for B" and "the place of B for A" instead of the other way around

Regrade request

Consider two strategies for eliminating duplicates from an array of values:

  1. constructing a hashtable from the list of values, discarding any values found that were already in the hashtable
  2. sorting the array of values, then removing consecutive items that have the same value
Question 3 (2 pt; mean 1.94) (see above)

Which of the two approaches would one expect to exhibit better spatial locality?

Regrade request
Question 4 (4 pt; mean 3.79) (see above)

In approach 1, at least when there are many duplicates to eliminate, the steps where the values are looked up in the hashtable would likely have significant ____. Select all that apply.

  1. dropped because of ambigiuity about whether these steps would involve accessing the array or not (my assumption is that they would in order to figure out what to look up, but some interpreted "steps where the values are looked up" as being a finer-grained step

Regrade request
Question 5 (3 pt; mean 2.48) (see above)

In approach 1, a hash function that better distributes values in the hashtable and better avoids hash collisions would likely ____. Select all that apply.

  1. For general values, a better hash function will usually spread values out better (closer to random) in the hashtable and thereby reduce "clumping" and spatial locality. But for more constrained values a better hash function might non-randomly distribute a known set of values to avoid collisions; or maybe a better hash function would allow for a smaller load factor.

Regrade request

Suppose a 8-set direct mapped cache with 4B blocks has the following contents:

set indextag (written in binary)validdata bytes (in hexadecimal; lowest address first)
00
1001101102 FF 41 02
2001101101 11 23 45
3011000133 55 77 9A
4000000100 01 02 03
50000011F0 80 90 01
60
70
Question 6 (4 pt; mean 3.79) (see above)

With the above cache accessing a two-byte integer from an address with tag 001101 (written in binary), set index 4, and offset 2 will result in a cache miss. As a result of the cache miss, what bytes currently stored in the cache will be replaced?

Regrade request


quiz for week 9

235 people have viewed this quiz

Answer the following quesitons about the lecture material for this week.

Consider the following C code:

unsigned char array[12];
...
a = array[0]; // (1)
b = array[0]; // (2)
sum += a * b;
a = array[4]; // (3)
b = array[1]; // (4)
sum += a * b;
a = array[8]; // (5)
b = array[2]; // (6)

For the following questions assume that:

  1. array starts at an address that is a multiple of 4096 (2 to the 12th power)
  2. only accesses to array use the data cache
  3. the compiler does not reorder or optimize the code to reduce the number of memory accesses for array
  4. the data cache is initially empty (all blocks invalid) when the code labeled (1) starts.
Question 1 (4 pt; mean 3.29) (see above)

With a direct-mapped 4-byte data cache with 2-byte blocks, the accesses labeled (1), (3), (4), (5) and (6) above will all be cache misses. The access labeled (1) is a compulsory miss also known as a cold miss. Which other misses are also compulsory or cold misses? Select all that apply.

Regrade request
Question 2 (4 pt; mean 3.87) (see above)

Consider 2-way (fully associative) 8-byte data cache with 4-byte blocks and an least-recently-used replacement policy. The array access labeled (1) above will be cache miss and the access labeled (2) will be a cache hit. Which other accesses will be cache hits? Select all that apply.

Regrade request
Question 3 (4 pt; mean 3.84) (see above)

Consider 2-way (fully associative) 8-byte cache with 4-byte blocks and first-in, first-out replacement policy The array access labeled (1) above will be cache miss and the access labeled (2) will be a cache hit. Which other accesses will be cache hits? Select all that apply.

(In a first-in, first-out replacement policy, the block replaced longest ago ("first-in") in a set is the next one replaced.)

Regrade request

Consider the following C snippet:

unsigned char array1[4096];
unsigned char array2[4096];
...
for (int i = 0; i < 2048; ++i) {
    array1[i] = array2[i + 2048] * array2[i];
}

For the following questions, assume:

  1. the snippet uses a 32768-byte 4-way set-associative cache with 64-byte blocks and an LRU replacement policy;
  2. the data cache is initially empty when the for loop starts;
  3. array1 and array2 are both located at addresses that are multiples of 32768.
Question 4 (4 pt; mean 3.19) (see above)

If the data cache has a write-through policy, during the execution of for loop, how many times will the cache read from or write to the next level of cache (or memory if there is no next level of cache) in order to read or write values in array1?

Answer:
Key: 2048 if write-no-allocate, 2080 (2048 writes + 32 reads) if write-allocate; if including accesses to array2 (particular interpretation of "in order to read or write") + 64 reads of array2

assuming that the compiler doesn't do anything that would reduce the number of writes to array1

Regrade request
Question 5 (4 pt; mean 2.08) (see above)

If the data cache has a write-back policy, during the execution of the for loop, how many times will the cache read from or write to the next level of cache (or memory if there is no next level of cache) in order to store values in array1?

Answer:
Key: 32 if write-back (read each block once to modify for first write), write-allocate; 2048 if write-back, write-no-allocate; if including accesses to array2 (particular interpretation of "in order to read or write") + 64 reads of array2

should have specified whether write-allocate or write-no-allocate since it dramatically affects the answer; we accepted both answers; also assuming the compiler doesn't do anything to reduce the number of writes to array1; gave half-credit for 0 (forgetting about reads, but knowing no writes) or 64 (reads for array2 only)

Regrade request


quiz for week 10

233 people have viewed this quiz

Consider a system with:

Question 1 (4 pt; mean 3.67) (see above)

Suppose this system runs an instruction like:

movq 0x12345, %rax

which loads 8 bytes from 0x12345 into %rax. Suppose while this instruction runs, all related TLB and cache accesses are hits. As part of running this instruction the system will ____. Select all that apply.

Regrade request
Question 2 (4 pt; mean 3.4) (see above)

Accessing the virtual address 0x12345 will use the same TLB set as accessing which of the following virtual addresses? Select all that apply.

Regrade request
Question 3 (4 pt; mean 3.68) (see above)

The amount of storage required to implement the data TLB is ____.

Regrade request
Question 4 (4 pt; mean 3.32)

Consider the following C code that uses the POSIX API:

printf("1"); fflush(stdout);
pid_t pid1 = fork();
pid_t pid2 = fork();
if (pid1 > 0 && pid2 > 0) {
    printf("2"); fflush(stdout);
    waitpid(pid1, NULL, 0);
    waitpid(pid2, NULL, 0);
    printf("3"); fflush(stdout);
}
printf("4"); fflush(stdout);
exit(0);

Which of the following are possible outputs of the above? Select all that apply.

Regrade request
Question 5 (4 pt; mean 2.6)

Consider the following C code that uses the POSIX API:

int pipe_fds[2];
pipe(pipe_fds);
int out_fd = open("foo.txt", O_WRONLY | O_CREAT);
dup2(pipe_fds[1], out_fd);
dup2(pipe_fds[0], STDIN_FILENO);

Which (if any) of the following are true after this code snippet runs, assuming pipe, open, and dup2 do not fail? Select all that apply.

  1. dup2 call will close it, because it replaces it with the pipe

Regrade request


quiz for week 11

232 people have viewed this quiz

Answer the following questions about the lecture material for this week.

Consider the following C code:

void *thread_func(void *arg) {
    int *p;
    p = (int*) arg;
    *p = 1;         /* <----------------------------------- LINE A */
    return (void*) (p + 2);
}

void foo() {
    int array[4] = {0, 0, 0, 0};
    pthread_t thread_ids[2];
    for (int i = 0; i < 2; i += 1) {
        int *q = &array[i];
        pthread_create(&thread_ids[i], NULL, thread_func, (void*) q);
    }
    for (int i = 0; i < 2; i += 1) {
        int *r = NULL;
        pthread_join(&thread_ids[i], (void**) &r);
        *r = 2;     /* <----------------------------------- LINE B */
    }
    printf("%d %d %d %d\n", array[0], array[1], array[2], array[3]);
}
Question 1 (3 pt; mean 1.67) (see above)

When the function foo() runs, the line labeled LINE A will most likely modify a value stored _____.

Regrade request
Question 2 (3 pt; mean 2.63) (see above)

When the function foo() runs, the line labeled LINE B will most likely modify values stored _____.

  1. intended answer, but requires pthread_join call to have correct first argument of thread_ids[i] instead of incorrect &thread_ids[i]

  2. based on pthread_join not working correctly due to incorrect first argument

Regrade request
Question 3 (6 pt; mean 5.26)

Consider the following C code:

struct info {
    int *array;
    int start;
    int end;
};

void *thread_func(void *arg) {       
    struct info *info;
    info = (struct info*) arg;
    for (int i = info->start; i < info->end; i += 1) {
        info->array[i] *= 2;
    }
    return NULL;
}

void double_all(int *array, int size) {
    pthread_t threads[2];
    for (int i = 0; i < 2; i += 1) {
        struct info cur_info;
        cur_info.array = array;
        cur_info.start = i * (size / 2):
        cur_info.end = (i + 1) * (size / 2);
        pthread_create(&threads[i], NULL, thread_func, &cur_info);
    }
    for (int i = 0; i < 2; i += 1) {
        pthread_join(&threads[i], NULL);
    }
}

double_all is intended to multiple every element of its input array by 2, where the array is specified using a pointer to its first element and its size. The implementation tries to use two threads to take advantage of multiple cores to do this, computing a range of the array (specified by start and end) for each thread to work on. But the above code is buggy. Which of the following are bugs in the code? Select all that apply.

  1. not intended answer, but pthread_join called with threads[i] instead of &threads[i], so pthread_join likely not to wait for threads to finish

Regrade request

Consider the following C code:

struct dynamic_array {
    int *data;
    size_t size;
    pthread_mutex_t lock;
};

void append_to_array(struct dynamic_array *dest, struct dynamic_array *to_add) {
    pthread_mutex_lock(&dest->lock);
    pthread_mutex_lock(&to_add->lock);
    int *new_data;
    size_t new_size = (dest->size + to_add->size) * sizeof(int);

    /* BEFORE REALLOC */
    new_data = realloc(dest->data, new_size);
        /* was incorrectly new_data = realloc(dest, new_size)  in quiz as released */

    if (!new_data) { abort(); /* out of memory */ }
    dest->data = new_data;
    /* AFTER REALLOC */

    memcpy(dest->data + dest->size, to_add->data, to_add->size * sizeof(int));
        /* was incorrectly dest->new_data + dest->size  in quiz as released */
    dest->size += to_add->size;
    pthread_mutex_unlock(&to_add->lock);
    pthread_mutex_unlock(&dest->lock);
}
Question 4 (4 pt; mean 3.46) (see above)

For this question, assume that all code follows the convention that dynamic_array structs are only read or modified while holding the lock in the struct. (That is, only after a locking the lock and without unlocking the lock until after the read/modification is done.)

In the append_to_array function, the lock for to_add is kept locked even while the realloc operation is run, even though that operation does not involve to_add. An idea one might have to take advantage of this is to temporarily release the lock on to_add by inserting a pthread_mutex_unlock(&to_add->lock) in place of the comment /* BEFORE REALLOC */ and inserting pthread_mutex_lock(&to_add->lock) in place of the coment /* AFTER REALLOC */. It might seem that this would allow more threads to work in parallel.

However, this change would allow for some incorrect behavior. Which of the following problems could we expect as a result of the attempt to temporarily release the to_add lock described above? Select all that apply.

  1. not in a way which is a problem like the question asks for: If the memcpy runs entirely before the reallocation happens, then yes it would copy from the old allocation, but there would be no new allocation at that time. I should've phrased this option more clearly as "attempt to copy from the old allocation despite their being a new allocation" to make it clear that I meant "out-of-date allocation", not just "old".

  2. each runs one at a time

Regrade request


quiz for week 12

233 people have viewed this quiz

Answer the following questions about the lecture material for this week.

Consider the following C code:

struct dynamic_array {
    int *data;
    size_t size;
    pthread_mutex_t lock;
};

void append_to_array(struct dynamic_array *dest, struct dynamic_array *to_add) {
    pthread_mutex_lock(&dest->lock);
    pthread_mutex_lock(&to_add->lock);
    int *new_data;
    size_t new_size = (dest->size + to_add->size) * sizeof(int);
    new_data = realloc(dest->data, new_size);
    if (!new_data) { abort(); /* out of memory */ }
    dest->data = new_data;
    memcpy(dest->new_data + dest->size, to_add->data, to_add->size * sizeof(int));
    dest->size += to_add->size;
    pthread_mutex_unlock(&to_add->lock);
    pthread_mutex_unlock(&dest->lock);
}
Question 1 (4 pt; mean 3.45) (see above)

It is possible for two simulatenous calls to append_to_array to hang indefinitely. Which, if any, of the following are examples of sets of simulatenous calls where this could happen? (Assume A, B, C, D, etc. represent distinct dynamic_array struct pointers.) Select all that apply.

Regrade request
Question 2 (4 pt; mean 3.91) (see above)

Which of the following changes to the above code would prevent the deadlock? Select all that apply.

  1. would not prevent append_to_array(A, A) deadlock

  2. would not prevent append_to_array(A, A) deadlock

  3. would not prevent append_to_array(A, A) deadlock

Regrade request

Suppose we want to implement a variant of the producer-consumer pattern where producing threads can produce two types of values (type A and type B), and consumer threads can either wait for a value of a particular type A OR they can wait for a value of both type A and type B. When the threads wait, they should wait without consuming a lot of processor time.

To implement this, we'll use a Queue implementation that provides queue_enqueue, queue_dequeue, and queue_size operations.

The following quesitons ask about the following incompete implementation. Blanks (formatted like _____________) represent omitted code:

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

Queue a_values;
pthread_cond_t a_available = PTHREAD_COND_INITIALIZER;
Queue b_values;
pthread_cond_t both_available = PTHREAD_COND_INITIALIZER;

void EnqueueA(Item item) {
    pthread_mutex_lock(&lock);
    queue_enqueue(&a_values, item);
    _________________________________ /* BLANK 1 */
    pthread_mutex_unlock(&lock);
}

void EnqueueB(Item item) {
    pthread_mutex_lock(&lock);
    queue_enqueue(&b_values, item);
    pthread_cond_signal(&both_available);
    pthread_mutex_unlock(&lock);
}

Item DequeueA() {
    pthread_mutex_lock(&lock);
    while (queue_size(a_values) == 0) {
        pthread_cond_wait(&a_available, &lock);
    }
    Item result = queue_dequeue(&a_values);      /* was b_values when quiz originally released */
    pthread_mutex_unlock(&lock);
}

void DequeueBoth(Item *a_item_ptr, Item *b_item_ptr) {
    pthread_mutex_lock(&lock);

    while (_______________________________) { /* BLANK 2 */

        pthread_cond_wait(_________________ /* BLANK 3 */, &lock);
    }
    *a_item_ptr = queue_dequeue(&a_values);
    *b_item_ptr = queue_dequeue(&b_values);
    pthread_mutex_unlock(&lock);
}
Question 3 (4 pt; mean 2.3) (see above)

Which of the following would be most appropriate to place in the blank labeled "BLANK 1"?

Regrade request
Question 4 (4 pt; mean 3.66) (see above)

Which of the following would be most appropriate to place in the blank labeled "BLANK 2"?

Regrade request

Suppose we have a system where three threads are running in parallel and we want a function to send a value from thread 0 to thread 1, and another value from thread 1 to thread 2, and another value from thread 2 to thread 0. To do this, each thread calls a function like:

other_threads_value = SendAndReceiveValue(thread_id, my_value);

where my_value is the current thread's value and thread_id is 0, 1, or 2, depending on which thread is making the call.

Functions can call SendAndReceiveValue multiple times; each time they call it they should receive values from the following call to SendAndReceiveValue.

Consider the following implementation of this function using semaphores:

int values[3];
sem_t value_ready[3];
sem_t value_empty[3];

int SendAndReceiveValue(int thread_id, int value) {
    int other_thread_id = (thread_id + 1) % 3;
    sem_wait(&value_empty[other_thread_id]);
    values[other_thread_id] = value;
    sem_post(&value_ready[other_thread_id]);
    sem_wait(&value_ready[thread_id]);
    int received_value = values[thread_id];
    sem_post(&value_empty[thread_id]);
    return received_value;
}
Question 5 (2 pt; mean 1.95) (see above)

What should the initial value of the semaphore value_ready[0] be? (By "value", we mean the non-negative integer stored by the semaphore.)

Answer:
Key: 0
Regrade request
Question 6 (2 pt; mean 1.89) (see above)

What should the initial value of the semaphore value_empty[0] be? (By "value", we mean the non-negative integer stored by the semaphore.)

Answer:
Key: 1
Regrade request


quiz for week 13

233 people have viewed this quiz

Answer the following questions about the lecture for this week.

Question 1 (4 pt; mean 2.67)

Suppose we build a pipelined processor with the five-stage design we discussed in lecture and, when run alone:

If the above processor is running an assembly program that includes the instruction addq %r8, %r9 (and there are no relevant data hazards or control hazards), then this instruction would start its writeback stage approximtely ___ ps after it starts its fetch stage.

Answer:
Key: 2000

500 * 4

Regrade request

For this question, consider a processor uses pipelining with 6 pipeline stages and a cycle time of 600 picoseconds (.000 000 6 milliseconds).

Question 2 (4 pt; mean 3.42) (see above)

Suppose we execute a program with 100000000 (100 million) instructions on this processor and the program experiences no data or control hazards. How long in microseconds, rounded to the nearest millisecond, would the program take to execute on the pipelined processor?

Answer:
Key: 60 (in milliseconds) or 60000 (in microseconds)

meant to always use milliseconds

Regrade request
Question 3 (4 pt; mean 3.75) (see above)

Suppose we modified this processor to have 5 pipeline stages by combining its third and fourth pipeline stages. Which of the following would likely be true about the modified processor? Select all that apply.

  1. assuming stalling is pretty rare, we'd have around 1 instruction per 600 ps versus 1 instruction per (something between 600 and 1200 ps).

  2. yes, if we assume the pipeline stages were approximately balanced beforehand --- in that case they won't be now, so we might end up with around 1000 ps (a bit less than twice original) cycle time and 5000 ps latency versus 600 ps cycle time and 3600 ps latency; no, if we assume cycles would be approximately balanced afterward (seems less likely to be me), in which case maybe we'd end up with a slightly lower latency

  3. I think this is most likely true, but there are scenarios one could conceive where it needs to stall exactly as often

Regrade request
Question 4 (4 pt; mean 3.74)

Suppose when running a benchmark program on a pipelined processor we discover that:

The pipelined processor has a 1000 ps cycle time.

On average, when running the benchmark program, the processor will complete an instruction approximately every ____ ps.

Answer:
Key: 1200

(1 + .05 * 2 + .10 * 1) * 1000

Regrade request

Consider a nine-stage pipelined processor with the following pipeline stages:

This processor uses forwarding combined with (when necessary) stalling to resolve data hazards.

Question 5 (4 pt; mean 3.24) (see above)

When running the assembly:

addq %r8, %r9
xorq %r8, %r11
subq %r9, %r10

on this nine-stage processor, the subq instruction will complete ____ cycles after the addq instruction completes.

(Assume that any instructions the processor is running not included in the snippet above:

  • do not use %r8, %r9, %r10, or %r11; and
  • do not require stalling

)

Answer:
Key: 3
Regrade request


quiz for week 14

231 people have viewed this quiz

Answer the following questions about the lecture material for this week.

For the following question, consider a six-stage pipelined processor with the following pipeline stages:

For the two execute stages:

Suppose the processor is running the following assembly snippet:

    movq (%r8), %r9  
    subq $128, %r9   
    jle foo          
    addq %r9, %r10   

and the jle is mispredicted.

Question 1 (4 pt; mean 3) (see above)

During the execution of the above assembly snippet, the addq instruction will finish ___ cycles after the movq instruction.

Answer:
Key: 7

the subq will stall for two cycles + two cycle delay for the jle + 2 instructions in between + 1 for addq to complete; also accepted 8 under the interpretation that the jle would need to stall (in decode or execute 1) to get the condition code values from the subq; also accepted 8 (jle stalling to get condition codes)

Regrade request

Consider the following assembly snippet:

    movq $4, %rax       // rax <- 4
start_loop:
    movq %r8, %r9       // r9 <- r8
    andq $3, %r9        // r9 <- r9 & 3
    cmpq $0, %r9        // set condition codes using r9 - 0
    jne skip            // if (r9 != 0) goto skip
    addq $1, %r8        // r8 <- r8 + 1     ***
skip:
    subq $1, %rax       // rax <- rax - 1
    cmp $0, %rax        // set condition codes using rax - 0
    jne start_loop      // if (rax != 0) goto start_loop
end_loop:

For the questions below, assume %r8 has an initial value of 4 every time the snippet above is run, in this case:

Question 2 (4 pt; mean 3.39) (see above)

If the processor running this snippet uses the forwards-not-taken, backwards-taken branch prediction strategy discussed in lecture, then the conditional jumps (jne skip and jne start_loop) will be predicted correctly ___% of the time. Round to the nearest percent.

Answer:
Key: 50

jne skip predicted as not taken; is not taken 1/4 times; jne start_loop predicted as taken, is taken 3/4 times

Regrade request
Question 3 (4 pt; mean 3.38) (see above)

Suppose the processor running this snippet predicts each conditonal jump as taken if it was taken the last time it was run, and predicts it as not taken if it was not taken the previous time it was run.

Assume the processor just ran the snippet and then runs the snippet a second time (that is, there is some loop surrounding the snippet). During the second execution, the conditional jumps in the snippet will be predicted correctly ____% of the time. Round to the nearest percent.

Answer:
Key: 50

since jne skip pattern is NTTT, predictions will be TNTT (first prediction from last iteration of previous execution); correct 50% of time; since jne start_loop pattern is TTTN, predictions will be NTTN; correct 50% of time

Regrade request
Question 4 (4 pt; mean 3.63)

Consider the following assembly snippet:

mov (%rax), %r9
add %r9, %r10
mov (%rbx), %r9
add %r9, %r11
mov (%rcx), %r9
add %r9, %r12
xor %r10, %r11

Consider an out-of-order processor design like we discussed in lecture. Assume that can perform multiple arithmetic operations at a time and can perform multiple loads at a time. (Its data cache is capable of doing multiple reads at a time, even if they are started in the same cycle.)

On that processor, which of the following is true about how this code could be executed? Select all that apply.

Regrade request
Question 5 (4 pt; mean 3.57)

Consider an out-of-order processor design similar to what we discussed in lecture, with execution units supporting running several arithmetic instructions at a time.

Suppose after register renaming, the end of the instruction queue for the processor contains:

add  %x10, %x19 -> %x20
sub  %x20, %x21 -> %x22
xor  %x18, %x22 -> %x24
imul %x18, %x18 -> %x25

where %x... represents a physical register name. Assume that there are also other instructions in the instruction queue, not shown above.

What of the following are true about these instructions and how they could be run on this processor? Select all that apply.

Regrade request


quiz for week 15

234 people have viewed this quiz

Answer the following questions about the lecture for this week.

Question 1 (5 pt; mean 4.65)

Consider the following assembly snippet:

addq %rcx, %rax
subq %rcx, %r9
xorq %rax, %r8
movq (%r8), %r10
imulq %r10, %r9
imulq %rax, %rax

On an out-of-order processor, the xorq instruction could do its computation at the same time as ____ is doing is computation. Select all that apply.

Regrade request
Question 2 (4 pt; mean 3.48)

Consider the following assembly snippet:

    cmpq %rax, %rbx
    jle other
    addq %r12, %rdx
    movq (%rdx), %r8
    subq %r8, %r9
    jmp after
other:
    addq %r13, %rcx
    movq (%rcx), %r8
    addq %r8, %r9
after:

Suppose this executes on an out-of-order processor, and the jle other is predicted as taken, but is actually not taken.

Then, ____. Select all that apply.

Regrade request
Question 3 (4 pt; mean 3.67)

Consider the following C code:

int IsPowerOfTwo(unsigned long x) {
    while (x > 1) {
        if ((x & 1) == 1) {
            return 0;
        }
        x = x >> 1;
    }
    return 1;
}

Assume this is translated to assembly in a manner that does omit any bitwise or arithmetic operations.

Suppose IsPowerOfTwo(a) takes substantially longer to execute than IsPowerOfTwo(b) for some non-zero a and non-zero b. (Note that IsPowerOfTwo's return value is not specified.)

Based on the time difference between IsPowerOfTwo(a) and IsPowerOfTwo(b), what is most likely true about a and b? Select all that apply.

  1. number of trailing zeroes determines number of iteations of loop; accepted either for this b/c of stating "does omit" instead of "does not omit" in question

meant to write "does not omit" any bitwise or arithmetic operations instead, and question does not make much sense with that statement; but I'm guessing it did not affect answers much

Regrade request
Question 4 (4 pt; mean 3.27)

Consider the following C snippet:

t = lookupTable[x];

where lookupTable is an array of 16384 chars located in memory at address 0x20CD00.

Suppose using the PRIME+PROBE strategy discused in lecture, we determine that the access to lookupTable modifies set 23 of a 65536-byte 4-way set-associative cache with 256-byte blocks.

What is a possible value of x?

To simplify this problem, assume all addresses are physical.

Answer:
Key: between 2560 and 2815 (inclusive)

0x20CD00 starts at cache index 13 offset 0, to get to index 23 offset 0 we need to advance by 10*256 bytes.

Regrade request

Consider the following C code:

if (x < 1024) {
    char y = array1[x];
    if (y < 64) {
        foo(array2[y*2048]);
    }
}

where array1 and array2 are arrays of chars.

If an attacker can control x, they can use the Spectre vulnerability to read from memory using this code.

Suppose array1 is located at address 0x1000000, array2 is located at address 0x2000000. To simplfiy the problem, assume all addresses are physical.

Question 5 (4 pt; mean 3.54) (see above)

The attacker will need to set x to a value related to the memory location that they want to read from. If they want to get information about the int at memory address 0x4000000, they should set x to ____. Write your answer as a hexadecimal.

Answer:
Key: 0x3000000
Regrade request