Final Study Guide

The final is closed book, closed note, no electronic devices allowed.

The final is comprehensive, so you should be familiar with the material in the exam1 study guide and the exam2 study guide, in addition to the material below.

You will be provided with a Reference Card with a list of RISC-V instructions and other useful information for the quiz.

On the course website there are blank copies of both exams and all the quizzes, along with their solutions. Questions on the final will be similar to those that you have seen in previous exams and quizzes. About 30% of the final is material since exam2, and about 70% is from previous material.

Concurrency

POSIX Threads (pthreads)

Creating Threads

pthread_t tid;
pthread_create(&tid, NULL, thread_func, (void*)arg);

Waiting for Threads to Finish

void* result;
pthread_join(tid, &result);

Passing Data to and from Threads

typedef struct {
    int id;
    char* msg;
} thread_data_t;

Locks

Basic Usage

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
// alternative init
pthread_mutex_init(&lock, NULL);

pthread_mutex_lock(&lock);
// critical section
pthread_mutex_unlock(&lock);

Use Cases


Condition Variables

Usage Pattern

#include <pthread.h>

pthread_cond_t cond;
pthread_mutex_t mutex;

// Initialize with default attributes
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&mutex, NULL);

// ... use the condition variable ...
pthread_mutex_lock(&lock);
while (!condition_met) {
    pthread_cond_wait(&cond, &lock);
}
// do work
pthread_mutex_unlock(&lock);

// signal another thread
pthread_cond_signal(&cond);

// Clean up when done
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);

Use Case


Semaphores

Initialization

sem_t sem;
sem_init(&sem, 0, initial_value);

Wait and Signal

sem_wait(&sem);   // decrement semaphore and wait if semaphore is negative
sem_post(&sem);   // increment semaphore and wake a single thread waiting on the semaphore

Use Cases


Producer-Consumer Problem

Key Conditions:


Readers-Writers Locks

Goals

Concurrent Data Structures

Examples:


Key Concepts to Know

Concept Know How To…
Race condition Explain what causes it and how to fix it
Deadlock Identify and prevent
Critical section Define and protect with locks
Bounded buffer Describe and implement
Signal vs broadcast Differentiate in condition variables
Mutex vs Semaphore Compare and choose correctly

Optimizing Program Performance

Why Optimize?

Measuring Performance

Optimization Blockers

How to maximize performance

Exploiting Modern Hardware (Microarchitecture)

Takeaways