LU8 Thread Programming Flashcards
What is a thread?
The smallest unit of execution within a process.
How do threads improve program performance?
perform multithreading
What are multiple strands of execution in a single program called?
Threads
When you invoke a program with threads, what does Linux create first?
A new process.
Initially, how many threads does a Linux process have?
One
True or False: A single process can have multiple threads executing different parts of the program at the same time.
True
Compared to processes, what is one key advantage of using threads for concurrent execution?
- lightweight
- less overhead for creation/termination/context switching.
What part of thread process do threads not share?
Stack Pointer and registers
What part of process do threads share?
Code, Heap, Static Data
What does stack pointer points to?
Points to the current stack where local variables, function parameters, and return addresses are stored.
What are some of the disadvantages of processes vs threads
Processes:
* do not share resources very well
* context switching cost is very high
If a crash happens on a thread, what can potentially happen?
Crash the entire process!
What are three benefits of using threads?
- lightweight
- Takes less time to create/terminate/context switching
- inter-thread communication without invoking the kernel
Under what criteria is a task suitable for threading?
- Has multiple parallel sub-tasks, blocking and long waits,
- use many CPU cycles
- needs to respond to asynchronous event
Describe a scenario of when a thread can be use.
- Window manager
- multhreaded server
- spreadsheet
- etc.
What library do you need to use if you want to code with threads?
libpthread for POSIX threads
POSIX: portable operating system interface
What are the four major groups functions to handle the race condition in multithreading?
- Thread management (Creating, Detaching, Joining, Setting Attributes)
- Mutexes (Creating, Destroying, Locking, Unlocking)
- Condition variables (Creating, Destroying, Waiting, Signaling)
- Synchronization (Read/Write Locks, Barriers)
What is the purpose of Thread management in threads?
Routines that work directly on threads - creating, detaching, joining, etc.
What is the purpose of Mutexes in threads?
synchronization
ensure that only one thread can access a shared resource at any given time. This prevents race conditions and data corruption by enforcing mutual exclusion.
think of public restroom
Name a few functions you can do with Mutex.
creating, destroying, locking and unlocking mutexes
What is the purpose of Condition variables?
Routines that address communications between threads that share a mutex.
This group includes functions to create, destroy, wait and signal based upon specified variable values
Is a routine to manage read/write lock a part of Synchronization thread?
Yes
What API standard is POSIX threads a part of?
Standardized portable thread API
What header file is needed for POSIX threads?
<pthread.h>
</pthread.h>