CST 334 Week 5
WELCOME TO WEEK 5
Learning Journal
This week we learned about concurrency and its role in running programs. Concurrency is achieved through an abstraction called a thread. A thread is similar to a separate process but shares the same address space and has access to the same data. Each thread has its own stack, stack pointer (SP), and program counter (PC). The state of each thread in a process is stored in what is called a thread control block (TCB). Although threads share some data, each thread runs independently.
However, there is a problem. When we run code, even on a single processor, we don't necessarily get the desired result each time. This is due to uncontrolled scheduling; a timer interrupt can go off and create a context switch, which can lead to incorrect results. This is called a race condition, where the results depend on the timing of the code's execution, giving us an indeterminate result. This can also happen with multiple threads executing code in what is called a critical section. To prevent this, we need mutual exclusion. Mutual exclusion ensures that if one thread is executing within a critical section, others are prevented from doing so. Another requirement is to provide atomicity, ensuring that instructions either run to completion or do not run at all.
Mutual exclusion works via locks. Locks are synchronization primitives used to control access to shared resources. There are various types of locks, such as mutexes and spinlocks. Hardware support for mutual exclusion involves special CPU instructions that can atomically test and modify a memory location, ensuring no other thread can intervene. Examples include Test-and-Set and Compare-and-Swap. OS support for mutual exclusion is also crucial for managing concurrency. Conditional variables are synchronization primitives used to block a thread until a particular condition is met. They are typically used with mutexes to avoid race conditions. Conditional variables are also helpful for buffer problems (i.e. producer-consumer) where they allow threads to wait until a condition is met.
Comments
Post a Comment