Week 29
CST-334 Week 5
This is our fifth week in CST-334, also known as Operating Systems.
Operating Systems: Three Easy Pieces
This week we read chapters 26-29 in our textbook.
Chapter 26: Concurrency and Threads
This chapter discusses concurrency and the basics of threads.
Key Takeaways:
- A multi-threaded program has multiple points of execution, called threads.
- Threads share the same code and heap, but have different stacks.
- Threads are used to utilize parallelism or keep a program running during I/O requests.
- Multi-threaded programs can be indeterminate.
- Race conditions, also called a data races, are outputs or variables that change based on the timing of the code's execution.
- Critical sections are pieces of code that access shared variables.
- Atomic instructions are executed completely or not at all.
Chapter 27: Thread API
This chapter discusses functions for creating and using threads.
Key Takeaways:
- The function pthread_create(thread, attr, start_routine, arg) is used to create threads. Thread is a pointer to a pthread_t variable. Attr specifies attributes the thread might have. Start_routine is a pointer to a function. Arg is a pointer to arguments for the function pointed to by start_routine.
- The function pthread_join(thread, value_ptr) returns when a thread completes. Thread is the thread to wait for. Value_ptr is the expected return value.
- The function pthread_mutex_lock(mutex) acquires a lock, or waits for a lock to become available and then acquires it. Mutex is the lock to be acquired.
- The function pthread_mutex_unlock(mutex) frees a lock. Mutex is the lock to be freed.
- A lock can be initialized by:
- Setting the lock to PTHREAD_MUTEX_INITIALIZER.
- Calling pthread_mutex_init(mutex, NULL). Mutex is the lock to be initialized. NULL is any attributes the lock may have.
- Condition variables are used to signal values between threads.
- The function pthread_cond_wait(cond, mutex) puts the calling thread to sleep and waits for another thread to signal the condition. Cond is the condition to wait for.
- The function pthread_cond_signal(cond) signals a condition. Cond is the condition to be signaled.
- To use threads you have to include the pthread.h header and add -pthread when compiling.
Chapter 28: Locks
This chapter discusses locks.
Key Takeaways:
- Mutex is a common name for locks.
- Mutual exclusion, fairness, and performance are used to judge the efficacy of a lock.
Chapter 29: Locked Data Structures
This chapter discusses how to make data structures thread safe.
Key Takeaways:
- An approximate counter is sometimes used to improve multi-threaded performance. In an approximate counter, each core has a local version of a variable, and the local variables are occasionally synced with the global version of the variable.
- An approximate counter becomes slower as it becomes more accurate, and vice-versa.
- Hand-over-hand locking, or lock coupling, uses multiple locks for different parts of a linked list.
Comments
Post a Comment