CS241

Review

C Fundamentals

  1. Consider the code below:

    void main() { 
     int x = 0; 
     x++; 
     if (fork()) x++; 
     x++; 
     printf("%d\n");
     } 
  1. In terms of processes, what is an orphan? Write a piece of code that creates an orphaned process.
  1. In terms of processes, what is a zombie? Write a piece of code that creates a zombie process.

Memory & Allocators

None! I Couldn’t think of any

Intro to Pthreads

  1. Consider the code below:
void * thread(void *ptr) { printf("Hello!"); } 
void main() { 
     pthread_t pid1, pid2; 
     pthread_create(&pid1, NULL, thread, NULL); 
     pthread_create(&pid2, NULL, thread, NULL); 
     pthread_join(pid1, NULL); 
}

How many times is “Hello!” printed?

  1. Name two things that are shared between multiple threads in a single process Additionally, name two things that are not shared.
  1. What is one specific reason that a context switch between two threads that are in a single process faster than a context switch between two processes?
  1. In terms of the system resources needed to create them, is it cheaper to create a thread or a process? Why?
  1. Explain how a semaphore can be used as a mutex to protect a critical section that should only be accessed by a single thread at a time.
  1. Consider the code below:

    void main() { 
     pthread_mutex_t mutex; 
     pthread_mutex_init(&mutex, NULL); 
     pthread_cond_t cond; 
     pthread_cond_init(&cond, NULL); 
     pthread_cond_broadcast(&cond); 
     pthread_cond_wait(&cond, &mutex);
     }

Describe the result of running this program.

Synchronization

  1. What does it mean if the Readers-Writers problem has a solution that has a “writers-preference”? What does this specifically mean for the readers?

Deadlock

  1. What are three different solutions to the Dinning Philosophers problem? Identify if each solution requires any global state (eg: a global lock).
  1. What are the four conditions of deadlock?
  1. Choose two of the conditions of deadlock you identified in (1). For each method chosen, provide a set or rules for the philosophers in the Dinning Philosophers Problem that removes the deadlock condition you picked. Explain how the rule you gave eliminated deadlock.
  1. Identify the following definition “There exists a set of process P1,P2,… such that there is a process dependency cycle in the Resource Allocation Graph”
  1. No Pre-emption
  2. Hold and Wait
  3. Circular Wait
  4. Livelock
  5. Mutual Exclusion

IPC

  1. Consider a 64-bit system that has 16 KB pages. If a single-level page table is used and each PTE is 4 bytes, how large is the page table?
  1. Consider the same 64-bit system with 16 KB pages and 4 byte PTEs. Instead of a single-level page table, a multi level page table is used where each page table fits exactly in one page. How many levels of page tables exist?
  1. Consider a system with 40-bit addresses and a two-level page table. Both of the first- and second-level page table indexes are 16-bits. What is the offset of the address 0x584ace42?
  1. When executing a program, accessing the memory address 0x80500ac3 caused a segmentation fault. Explain the process that the computer used to look up that memory address and when the segmentation fault occurred.
  1. Two processes independently access the address 0x400241. When the value of that address is printed out, it was different in each process. Explain how this could happen.

Networking

  1. Why does HTTP run on TCP? What could happen if HTTP ran on UDP?
  1. For the entire Internet, there are less than 20 root name servers. Each of these servers can handle less than 10,000 simultaneous requests, meaning that less than 200,000 simultaneous DNS lookups to the root name server can be made at one time. However, there are tens of millions of simultaneous DNS requests made on the Internet. How is this possible?

File Systems

  1. Consider the code below: int main() { int *ptr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); *ptr = 0; fork(); (*ptr)++; printf("The value is: %d\n", *ptr); } What all is printed out after the program is executed?
  1. Synchronization (such as mutexes, semaphores, or conditional variables) are often not used with pipes. Why?
  1. When is synchronization required when using a mmap?
  1. What is the maximum size of a file that can be stored in an i-node based file system where the size of each disk block is 8 KB, the size of a disk pointer is 4 B, and each i-node contains 5 direct entries, 3 single-indirect entries, 2 double-indirect entries, and 1 triple-indirect entry?
  1. If the size of a disk pointer doubles, how does this affect the maximum size of the file on a system where each i-node contains 10 direct entires, 1 single-, 1 double-, and 1 triple-indirect entry?
    1. Does the maximum size grow by a factor of 2x (eg: doubles)?
    2. Does the maximum file size shrink by a factor of 0.5x (eg: the new maximum file size is half of the old size)?
    3. What is the new maximum file size as a factor to the old maximum file size?
  1. Name three things that are included in an i-node entry and one thing that is not contained in an inode entry.
  1. A file has the permissions “755”. Explain in English terms what “755” signifies.

Signals

  1. Consider the Code Below void handler(int sig) { printf("foo"); while (1) { } } int main() { signal(SIGINT, handler); while (1) { } return 0; }

If Ctrl+C is pressed 5 times, how many times is “foo” printed out to the screen?

  1. Consider the code below:

    void handler(int sig) { 
    printf("foo\n"); 
    } 
    int main() { 
    signal(SIGKILL, handler); 
    while (1) { } 
    return 0;
     }
  1. If you send three SIGKILLs to the process in a short period of time, what happens?
  1. Consider the code below:

    void handler(int sig) { 
    printf("foo\n"); 
    } 
    int main() { 
    signal(SIGINT, handler); 
    while (1) { } 
    return 0;
     }

Questions?

Authors: Steve & Bhuvan