Review

Question

What is systems programming?

Low-level, or hardware-aware, programming

Question

What is the execution stack?

The stack of all currently executing functions, with the currently running function at the top.

Question

What is a stack frame?

The portion of the stack in which a function runs and stores local variables.

Question

What is the heap?

The portion of the process reserved for dynamic memory allocation requests.

Question

What is the name of the compiler with use on goldengate and our lab machines?

gcc

Question

How are boolean values handled in C?

All non-zero values are true. 0 is false.

Question

How is C different from Java?

Many answers: Java has garbage collection whereas C
requires manual memory management. C compiles directly to
binary code whereas Java uses a virtual machine.

Question

What is git?

Source control system.

Question

How does git differ from Github?

Git is a source control system. Github is a
service for storing and managing git repositories.

Question

What does the git commit command do?

Saves a snapshot of you code.

Question

What does the git push command do?

Uploads your current git repository history to github.

Question

What does the git pull command do?

Downloads your current git repository history to github.

Question

What is the terminal?

Text-based window that runs a shell.

Question

What is a shell?

A shell is a program that runs commands.

Question

What shell do we use on goldengate?

bash

Question

What is the difference between static and dynamic allocation of memory?

Question

How do we dynamically allocate memory in C?

Question

Think of an example where we would need to allocate memory dynamically.

Question

What is a pointer?

Question

What are common uses of pointers in C?

Question

What are the differences between pass-by-value and pass-by-pointer?

Question

Think of an example where we would need to pass a function argument by pointer.

Question

What is a segmentation fault? Give three (or more) coding mistakes that cause segmentation faults.

Question

What types of errors does valgrind detect?

Memory leaks, invalid reads, invalid writes

Question

What is gdb?

The GCC debugger

Question

What is a memory leak?

Question

What is a void pointer?

A generic pointer that can point to any data type

Question

Why do we need to recompile C code to run on different hardware and operating systems?

Question

How are strings represented in binary in C?

A string is a null-terminated array of characters.
A character is an ASCII value

Question

How are floating-point numbers represented in binary?

Question

What is a bit?

Container for either 0 or 1

Question

What is a byte? e.g. How many bits are in a byte?

A byte is the smallest addressable amount of memory
8 bits

Question

Suppose we have 4 bytes of data in memory. How do we know whether these bytes represent string, floating-point number, color, or integer?

From the data type

Question

What is overflow?

Question

When does overflow occur for signed integers?

Question

When does overflow occur for unsigned integers?

Question

How do we negate a binary number?

Question

What is signed extension?

Question

What is the difference between text and binary files?

Question

What is the different between little endian and big endian byte ordering?

Question

Why (and when) is padding added to structs?

Question

What is the difference between logical and arithmetic shifts?

Question

What is the CPU?

Question

What is RAM?

Question

What is ISA?

Question

What are the five main components of any Von Neuman Architecture?

Question

What is the ALU?

Question

What are registers? Name several important registers that are contained in the CPU.

Question

What is a hardware bus?

Question

What four steps are performed by the computing architecture every time it executes an instruction.

Question

What is assembly language?

Question

What types of operands are there in assembly?

Question

How does assembly handle data types?

Question

What does the lea instruction do? How does it differ from the mov command?

Question

How are parameters passed to functions?

Question

How is machine code generated from source code? What is the relationship between machine code and assembly?

Question

What do the registers %rsp and %rbp represent? How do they relate to functions?

Question

What the do the registers %rax and %eax typically represent? When do we use one versus the other?

Question

What two steps occur when the CPU executes the instruction push %rbp?

Question

What two steps occur when the CPU executes the instruction pop %rbp?

Question

What does the instruction -0x4(%rbp) mean?

Question

How is the function stack implemented in assembler?

Question

Why do buffer overflows present security risks?

Question

What does it mean to reverse engineer a program?

Question

What are the two main jobs of the operating system?

Question

What happens when the operating system runs a program?

Question

Name three operating systems.

Question

What does it mean to say that processes have a "lone view" of the operating system?

Question

What does the OS store in a process?

Question

What must the OS do to context switch between processes?

Question

What is virtual address space (VAS)?

Question

List two advantages of virtual address spaces?

Question

When does the OS send the SIGSEGV signal?

Question

How do we send a kill signal to a process?

Question

What is the difference between an interrupt and a trap?

Question

Why do we need interrupts and traps?

Question

What is a process?

Question

Why do we need special mechanisms, such as pipes or sockets, to communicate between processes?

Question

How can you kill a process using ps ?

Question

What is an orphan process?

Question

What is a zombie process?

Question

What are the different states that a process can be in?

Question

Why might we want to register a signal handler for the SIGINT signal?

Question

For processes to run concurrently, what is needed in terms of hardware?

Question

What is a thread?

Question

What is a mutex?

Question

What is a race condition?

Question

What does it mean for a task to be atomic?

Question

What is deadlock?

Question

What does it mean for a function to be thread safe?

Question

Why and when might we want to use multiple threads?

Question

When might we want to use multiple processes versus multiple threads?

Question

For threads to run concurrently, what is needed in terms of hardware?

Question

When designing a multi-threaded application, what factors should we consider?

Question

What mechanisms can we use to synchronize between threads?

Question

What is a critical section of code?

Question

What is the memory hierarchy?

Question

What are some examples of primary storage?

Question

What are some examples of secondary storage?

Question

What is locality and what impact does it have on performance?

Question

How can a command like malloc keep track of the blocks of memory that the user requests?

Question

In the reading on malloc, what was the purpose of the free list?

Question

What are three characteristics of different types of memory?

Question

What are different metrics we might consider for optimizing a program?

Question

What types of optimizations can the compiler do?

Question

What types of optimizations can the compiler NOT do?

Question

What is function inlining?

Question

What is loop unrolling?

Question

What is the minimum size of the following structures and variables? If the variable is a pointer, give the size of the memory that the pointer points to.

struct Snack {
  char name[32];
  int quantity;
  float cost;
};
struct Snack snacks[2];

Question

Consider navigating directories from the command prompt in UNIX. Suppose you are in your home directory. How would you create a subdirectory with name foo?

Question

When Willow runs her program from the command line, she gets the following error? What is happening and how can she fix it?

$ ls
Makefile  fortune  fortune.c  hello  hello.c
$ hello
Command 'hello' not found

Question

The following program crashes. What is the problem and how can we fix it?

#include <stdio.h>

int main() {
  int* value = NULL;
  int a = 4;

  printf("value is %d\n", *value);
}

Question

The following program crashes. What is the problem and how can we fix it?

#include <stdio.h>
#include <string.h>

void initialize(char text[]) {
  strcpy(text, "pina collada");
}

int main() {
  char str1[5];
  initialize(str1);
  printf("%s\n", str1);
  return 0;
}

Question

Draw the directory hierarchy that corresponds to the following commands

$ pwd
/home/qwerty
$ mkdir A
$ cd A
$ mkdir Z
$ touch talk.c

Question

When Bella runs valgrind on her program, she receives an error. What is wrong with her program?

// ==2515== Conditional jump or move depends on uninitialised value(s)
// ==2515==    at 0x484ED28: strlen
int main() {
  char name[16];
  for (int i = 0; i < strlen(name)-1; i++) {
    name[i] = i+'a';
  }
  name[15] = '\0';
  printf("Name is %s\n", name);
  return 0;
}

Question

Draw the stack diagram for the following program.

struct node { struct node* next; };

struct node* insert_front(struct node* head) {
  struct node* n = malloc(sizeof(struct node));
  n->next = head;
  return n;
}

int main() {
  struct node* list = insert_front(NULL);
  // draw function stack here
}

Question

Convert the following binary numbers to hexadecimal.

0110 1010

Question

Convert the following binary numbers to hexadecimal.

1110 0011

Question

Convert the following binary numbers to hexadecimal.

0110 1110

Question

Convert the following hexadecimal numbers to binary.

0xF5

Question

Convert the following hexadecimal numbers to binary.

0x03

Question

Convert the following hexadecimal numbers to binary.

0x2D

Question

Convert the following decimal numbers to hexadecimal.

72

0x42

Question

Convert the following decimal numbers to hexadecimal.

54

0x36

Question

Convert the following decimal numbers to hexadecimal.

25

0x17

Question

Compute the following using 4-bit unsigned integers. Express your final answer in hexadecimal.

3 << 8

Question

Compute the following using 4-bit unsigned integers. Express your final answer in hexadecimal.

2 & 7

Question

Compute the following using 4-bit signed integers. Express your final answer in hexadecimal.

-6 >> 3

Question

Compute the following using 4-bit signed integers. Express your final answer in hexadecimal.

2 ^ 7

Question

What is the size of this struct? Assume standard data type sizes for x86_64 machines such as goldengate.

struct foo {
    float x;
    unsigned char c[3];
    struct foo* v;
};

Question

Ava implemented the following program to keep track of her finances. However, it’s not working properly. What is going on and how can she fix it?

int main() {
  unsigned int balance = 1000;
  for (unsigned int i = 10; i >= 0; i--)
  {
    balance -= 3;
  }
  return 0;
}

Question

What is the value of flipped?

unsigned char a = 0x7E;
unsigned char leftMask = 0xC0;
unsigned char rightMask = 0x03;
unsigned char left = (leftMask & a) ;
unsigned char right = (rightMask & a) ;
unsigned char leftShift = left >> 4;
unsigned char rightShift = right << 4;
unsigned flipped = leftShift | rightShift;

0x24

Question

Consider the following file permissions.

-rw-r--r-- 1 alinen alinen   191 Sep 10 12:07 greeting.c
-rwxr-xr-x 1 alinen alinen 15960 Sep  3 12:33 hello
-rw-r--r-- 1 alinen alinen    73 Sep  3 12:26 hello.c

What octal number corresponds to the file permissions for hello.c?

0644, or 110 100 100

Question

Suppose we have a 4-bit bit field that stores whether an animal can fly, swim, or walk, defined with the following masks

#define NONE 0x0
#define FLY 0x1
#define SWIM 0x2
#define WALK 0x4

Suppose an animal’s bitfield contains the value 0x3. What capabilities does it have?

Question

Does the following code produce an orphan process? Why or why not?

if (fork() == 0)
{
  printf("Child, PID = %d\n", getpid());
  exit(0);
}
else {
  printf("Parent, PID = %d\n", getpid());
  while(1) {}
}

Question

Does the following code produce a zombie process? Why or why not?

if (fork() == 0)
{
  printf("Child, PID = %d\n", getpid());
  exit(0);
}
else {
  printf("Parent, PID = %d\n", getpid());
  while(1) {}
}

Question

What possible outputs are there for this program?

int main() {
  pid_t ret;
  printf("A\n");
  ret = fork();
  if(ret == 0) {
    printf("B\n");
    ret = fork();
    if(ret == 0) {
      printf("C\n");
    }
    printf("D\n");
  }
  return 0;
}

ABCDD
ABDCD

Question

How many times is Z printed?

int main() {
  pid_t ret;
  printf("A\n");
  for (int i = 0; i < 3; i++) {
     ret = fork();
     if (ret == 0) {
        printf("B\n");
     }
  }
  printf("Z\n");
  return 0;
}

8

Question

How many times is B printed in the following example?

int main() {
  pid_t ret;
  printf("A\n");
  for (int i = 0; i < 3; i++) {
     ret = fork();
     if (ret == 0) {
        printf("B\n");
     }
  }
  printf("Z\n");
  return 0;
}

7 times

Question

What is the output of the following program?

#include <stdio.h>
typedef void (*functionType)(int a, int b);

void example(int a, int b) {
  printf("This is a function stored as data! %d %d\n", a, b);
}

int main() {
  functionType myFunction = example;
  (*myFunction)(10, 3);
}

Question

Which of the following dereferences are safe?

void* generic = NULL;
int a = 3;
double b = 4.5;
generic = &b;

printf("test %f\n", *generic); // 1
printf("test %f\n", *((double *) generic)); // 2
printf("test %d\n", *((int *) generic));  // 3

1) Doesn't compile
2) Safe
3) Unsafe

Question

Suppose a serial program takes 10 seconds to run but only 2 seconds with 4 cores. What is the efficiency?

speedup = Ts/Tp = 10/2 = 5
efficiency = speedup/ncores = 5/4

Question

Suppose a program is 90% parallelizable and the serial version of the program (e.g. the one with one thread) takes 10 seconds to run. What is the speedup if we have 5 cores?

speedup = Ts/Tp = Ts/((0.9/5) * Ts + 0.1 * Ts) ~ 3.57

== Question Suppose a program is 90% parallelizable and the serial version of the program (e.g. the one with one thread) takes 10 seconds to run. What is the theoretical best case speedup if we have infinitely many cores?

speedup = Ts/Tp = Ts/((0.9/inf) * Ts + 0.1 * Ts) = 10

== Question Draw a stack diagram for the following program. Show all intermediate values.

#include <stdio.h>

int main() {
  int vals[4] = {1,2,3,4};

  for (int* ptr = vals; ptr < vals+4; ptr++) {
    printf("%p %d\n", ptr, *ptr);
  }

  return 0;
}

Question

The following program segmentation faults. What is the error?

#include <stdio.h>

int main() {
  int* value = NULL;
  printf("value is %d\n", *value);

  int a = 4;
  value = &a;
  printf("value is %d\n", *value);
}

Question

In the following code, give a variable whose usage has temporal locality. Give a variable with spatial locality.

#include <stdio.h>

int main() {
    int i, size = 0;
    int my_arr[10];
    for (i = 0; i < 10; i++) {
        my_arr[i] = i;
        size++;
    }
    return 0;
}

Question

How the following code would change after constant folding?

#define N 5
int test1 = N - 4;
int test2 = N + 4;

Question

Can the compiler optimize the following code? If no, how can we make the following code more efficient?

char text[64];
scanf(" %s", text);
int sum = 0;
for (int i = 0; i < strlen(text); i++) {
  sum += text[i];
}

Question

How would the following code change after constant propagation and dead code removed?

int debug = 0;

//sums up all the elements in an array
int doubleSum(int *array, int length){
    int i, total = 0;
    for (i = 0; i < length; i++){
        total += array[i];
        if (debug) {
            printf("array[%d] is: %d\n", i, array[i]);
        }
    }
    return 2 * total;
}

Question

How would the following code change after unrolling the loop?

void loopDeLoo(float* pos, float scale){
    int i;
    for (i = 0; i < 3; i++){
      pos[i] = scale * pos[i];
    }
}

Question

Why can’t the following code be optimized by the compiler?

void shiftAdd(int *a, int *b){
  *a = *a * 10; //multiply by 10
  *a += *b; //add b
}

Memory aliasing: the two pointers might refer to the same data or different
data. The compiler can't be sure and so does not risk altering the program
behavior by changing the code.

Question

What in the following code can’t be optimized by the compiler?

int main() {
  char test[32];
  strcpy(test, "hello");
  for (int i = 0; i < strlen(test); i++) {
    printf("%c\n", test[i]);
    if (i == 0) {
      strcpy(test, "hello!");
    }
  }
}

Moving the call to strlen could change the behavior of the program, so the
compiler will not move it out of the loop. The programmer should avoid calling
functions repeatedly in a loop unless necessary.