1/18
Midterm 1 Review
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Threads of execution
an independent sequence of instructions inside a process. each thread has its own program counter, its own stack, and it’s own execution path, but shares the process’ text section, data section, heap, open files, and network ports
Traditional process
has one thread of control but modern OS allows a process to have multiple threads
Why create new threads instead of new processes?
threads are cheaper, faster, and share memory, making them ideal for tasks w/in the same program || perf for parallel tasks inside program
CON - creating a new process is resource intensive and time consuming
Why create new processes instead of new threads?
Processes are isolated and fault independent (if the process crashes, others will remain unharmed).
Processes cannot access each other’s memory which is good for privacy and security.
Processes can transform into a new program (exec())
Fault independent
If a process crashes, the others remain unharmed.
If a thread crashes, the entire process and other threads crash too
What do threads share?
All threads belonging to the same process share:
1) Code/text section
2) data section (global and static)
3) heap (dynamic memory
What do threads not share?
1) program counter
2) register set
3) stack
Thread local storage (TLS)
A mechanism that allows each thread to have its own private copy of a variable that it does not share w/ other threads. “global but private”; changes in one thread are not visible to others
Thread pool
A collection of threads created at a system startup that sleep until needed (waiting state)
If a thread is needed, it is awakened to service the request immediately; freezes and returns when done
Helps avoid overhead of constantly creating and deleting threads
What does the thread pool limit?
It limits the total # of threads active to avoid exhausting resources
How to crate a thread w/ std::thread
std::thread t(funcName, arg1, arg2, …)
Why do we need std::thread::join()?
Join() makes the calling thread wait until the created thread finishes. It is important bc it prevents the program from ending while threads are still running
avoids undefined behaviour
ensures proper cleanup of thread resources
without join(), the thread becomes detached and the program may terminate early
Join() vs wait()
Join() is for THREADS inside the same process || share memory
Wait() is for the child PROCESSES created w/ fork() || doesn’t share memory
Interrupt
a hardware generated signal that alerts the CPU to an event requiring attention.
Steps: generated → reaches CPU → CPU pauses → identifies type → runs ISR → resumes
What are system calls treated as?
Interrupts