fw Flashcards
Race Condition
2 threads try to access a shared resource in a critical section of code. This leads to a sequence of behaviors where data can be accessed in a way unintended by the programmer and the behavior depends on the thread timing.
Mutex
A type of lock where only one thread can hold it
Synchronization
Co-ordination of threads to ensure that they don’t access a shared resource at the same time, can be done through methods like mutex, sempahore, etc.
Semaphore
A lock with a counter on it. Each time a thread grabs the semaphore, the counter decreases, and once the counter hits 0, then no other thread can grab it (and thus access the shared data)
Dead lock
When two or more threads are unable to run because one is waiting for another to release a lock, resulting in a stand still of the program.
How to increase/decrease semaphore values?
Semaphore is initialized at a certain value., Signal command increases the semaphore value by 1, and “wait” decreases semaphore value by 1. If the value of the semaphore is already 0, then the thread waits until the value is 1.
Live lock
Two threads respond to each other continuously, while not getting any work done.
Thread wait
The thread that called wait goes into the wait queue associated with that condition variable. Then when it is the thread’s turn to run again, and the condition variable was signaled, then the thread can go.
Thread signal
Thread signal wakes up a thread waiting on a condition variable. (doesn’t immediately stop the current thread, still need the thread to unlock).
Thread broadcast
wakes up all threads waiting on the condition variable. Every thread that is currently waiting on the condition variable is awakened and will attempt to re-acquire the mutex and proceed (note that in a single threaded system signal only activates the next thread, but broadcast would be able to allow all subsequent threads to be able to grab?)
Garbage Collection
Cleaning up memory no longer used by the program. Higher level languages have automatic garbage collection.
Memory Leak
When we allocate heap memory but forget to de-allocate it, allowing others to access it when it may not be intended to be accessed.
Dangling pointer
When we free some memory, but there is still a pointer pointed to it. If we attempt to de-reference that pointer, then we can get some undefined behavior, such as seg fault, or the program may continue and experience silent failure.
Pass by value vs pass by reference
Pass by value is where a copy of the value is passed into the function. Pass by reference is where a reference to the actual variable in memory is passed into the function, meaning altering it in the function prototype will change the contents of the memory.
You can change contents of memory of an input argument by passing in a pointer for pass by value (a copy of the address is created which still allows you to manipulate the memory there).
The stack in C
Every time a function is called, a stack (stack allocation frame) is created which contains arguments, return address, and local variables. Before the program is called the next assembly instruction after the jump is also saved to the link register so that after the function ends, we can return to that next instruction.