CMPSC 311 Exam 2 2015
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CMPSC 311 Exam 2
March 27, 2015
1 Short Questions (33pts total, 3pts each, be brief)
1. What is the term for a bug in which memory is allocated but never freed?
2. What does the following code print?
char x[] = "hello";
printf("%d\n", sizeof(x));
3. When debugging a segfault, what type of variable is most likely to be the cause of the problem?
4. After the following code runs, what is the difference (as a number of bytes) between the address that a points to and the address that b points to?
int32_t x;
int32_t *a, *b;
a = &x;
b = a + 5;
5. If you are running a program in GDB and it crashes, what is the first command you should use to investigate where the crash occurred?
6. Write a single shell command which uses the contents of the file unordered .txt as input to the sort command, and writes the output to the file ordered .txt.
(Bonus) Write a second command which does the same thing but uses a different mechanism to provide the input to sort.
7. Why is octal (base 8) a useful number base for expressing a file’s mode?
8. What is the name of the kernel data structure that stores the mapping between a process’s virtual memory addresses and physical memory addresses?
9. What is the term for I/O which can collect several write requests and perform them all as one larger chunk?
10. What standard C function checks the value of errno and prints a nicely formatted error message to the standard error stream?
11. A 32-bit integer is represented by the following four bytes in memory:
0x4a |
0x3b |
0x2c |
0x1d |
where 0x4a is the first byte (i.e. has the lowest address).
Would these four bytes represent a larger integer on a little-endian architecture or a big-endian architecture?
2 Medium Questions (37pts total)
12. (5pts) What is the output from the following shell script?
#! /bin/bash
true | | echo one
false && echo two
false | | echo three
echo four && false
true && echo five
13. (5pts) Explain the difference between the GDB commands next, step, and finish.
14. (5pts) Explain why this function does not work correctly, and specify what the resulting array will actually contain. (Assume that is_prime is defined and implemented correctly.)
// Allocates and returns an array of length "len" which contains 1 in // prime-numbered elements and 0 in non-prime-numbered elements int *make_prime_array(int len)
{
int *arr = malloc(len * sizeof(int));
for (int i = 0; i < len; i++)
if (is_prime(i))
arr[i] = 1;
return arr;
}
15. (5pts) What is the value of each of the following C expressions? (Please give your answers in binary.)
(a) 0b1110 | 0b0101 =
(b) 0b1110 | | 0b0101 =
(c) 0b1110 & 0b0101 =
(d) 0b1110 && 0b0101 =
(e) 0b1110 ^ 0b0101 =
16. (5pts) Fill in your implementation of tiptap_getpos. You may use your own names for fields and variables.
void tiptap_getpos(struct tiptap *tt, uint16_t *x, uint16_t *y, uint16_t *z) {
}
17. (6pts) Write an implementation of the strlen function below:
int strlen(char *str)
{
}
18. (6pts) When the operating system executes a program, it sets up each segment in the process’s virtual address space using the mmap system call. For each segment listed below, specify the value that should be used for the prot and flags fields. (Remember, these are bit fields, so there may be 0, 1 or multiple flags.)
Options for the prot field: PROT_READ, PROT_WRITE, PROT_EXEC.
Options for the flags field: MAP_SHARED, MAP_ANONYMOUS, MAP_PRIVATE.
(a) Code segment, prot =
(b) Code segment, flags =
(c) Data segment, prot =
(d) Data segment, flags =
(e) Stack segment, prot =
(f) Stack segment, flags =
3 Long Questions (30pts total, 10pts each)
19. Write a function that takes a 32-bit unsigned integer and a 16-bit unsigned integer, uses bitwise operations to overwrite the middle 16 bits of the 32-bit integer with the 16-bit integer, and returns the result.
For example, given the inputs 0x12345678 and 0xf00d, it should return 0x12f00d78.
uint32_t oreo(uint32_t cookie, uint16_t creme)
{
}
20. Write a function that opens the file /tmp/foo .dat using high-level I/O, reads five decimal integers from the file, prints them to standard error, and closes the file. (You can use the "r" mode when opening the file.)
void print_yo_ints(void)
{
}
21. We discussed three specific types of allocation that take place in C. For each of these three types, give (1) the name (e.g., “static allocation”), (2) the segment in a process’s virtual address space where the allocation typically happens, and (3) the lifetime of variables/memory allocated in that way.
2023-03-31