Lecture 6 Flashcards
How are tasks carried out in shared memory vs distributed memory in terms of processes and threads?
Shared memory: starts a single process and forks threads. Threads carry out tasks.
Distributed memory: starts multiple processes since each process operates in its own memory space. Processes carry out tasks.
Critical section?
Section of code where resources are accessed and manipulated.
It is a part of the code that must be executed atomically.
Mutex?
Mutual exclusion lock.
Used to protect critical sections.
Two operations: Locking (acquiring) and Unlocking (releasing).
my_val = Compute_val(my_rank);
Lock(&add_my_val_lock);
x += my_val;
Unlock(&add_my_val_lock);
Busy-waiting?
A technique in concurrent programming where a thread or process repeatedly checks for a certain condition to become true. Like being stuck in a while loop until its condition is satisfied.
Waste of computer resources.
Semaphores?
A counter that can be incremented or decremented atomically.
Signal (release or “V”): increments the semaphore value by a specific amount. If other threads or processes were blocked, waiting for the semaphore value to become positive, signal can unblock one or more of them.
Wait (acquire or “P”): decrements the semaphore value by one. If this becomes negative, the thread or process attempting to acquire the semaphore is blocked and put into a waiting state until the semaphore value becomes non-negative.
These are atomic. So “wait” can’t be called simultaneously.
Mutex locks and binary semaphores.
POSIX Threads?
Pthreads.
Standard for Unix-like operating systems.
Specifies an API for multi-threaded programming.
How do threads work in a C program?
Call pthread_create() to create a new thread.
Call pthread_join() once for each thread. A single call to pthread_join() will wait for the thread associated with the pthread_t() object to complete.