Podcast
Questions and Answers
Which of the following is NOT a characteristic of a thread?
Which of the following is NOT a characteristic of a thread?
- Stack
- Process Control Block (correct)
- Program counter
- Register set
Why is thread creation considered 'lightweight' compared to process creation?
Why is thread creation considered 'lightweight' compared to process creation?
- Threads do not require a program counter.
- Threads have their own memory space.
- Threads share code, data, and OS resources with other threads in the same process. (correct)
- Threads are managed by the kernel, which streamlines operations.
In the context of multithreading models, what is a major drawback of the Many-to-One model?
In the context of multithreading models, what is a major drawback of the Many-to-One model?
- It avoids restrictions on the number of threads in a process.
- It creates a one-to-one mapping between user-level threads and kernel threads.
- It allows multiple threads to run in parallel on multicore systems.
- If one thread blocks, the entire process blocks. (correct)
Which multithreading model provides the most concurrency by mapping each user-level thread to a kernel thread?
Which multithreading model provides the most concurrency by mapping each user-level thread to a kernel thread?
Which of the following is an advantage of the One-to-One multithreading model?
Which of the following is an advantage of the One-to-One multithreading model?
In the context of multithreading models, what is the primary benefit of the Many-to-Many model?
In the context of multithreading models, what is the primary benefit of the Many-to-Many model?
Which multithreading model is a combination of the Many-to-Many and One-to-One models, allowing a user-level thread to be bound to a kernel thread?
Which multithreading model is a combination of the Many-to-Many and One-to-One models, allowing a user-level thread to be bound to a kernel thread?
Which of the following is NOT a typical benefit of multithreading?
Which of the following is NOT a typical benefit of multithreading?
What does it mean for a multithreaded application to be 'responsive'?
What does it mean for a multithreaded application to be 'responsive'?
What is a primary advantage of multithreading related to resource sharing?
What is a primary advantage of multithreading related to resource sharing?
Why is allocating memory and resources to a process more 'expensive' than creating threads?
Why is allocating memory and resources to a process more 'expensive' than creating threads?
In the context of multithreading, what does 'scalability' refer to?
In the context of multithreading, what does 'scalability' refer to?
When dealing with multicore systems, which of the following is NOT a challenge faced by programmers?
When dealing with multicore systems, which of the following is NOT a challenge faced by programmers?
In parallel programming, what does 'data parallelism' primarily involve?
In parallel programming, what does 'data parallelism' primarily involve?
Which of the following BEST describes 'task parallelism'?
Which of the following BEST describes 'task parallelism'?
What is the key difference between concurrency and parallelism?
What is the key difference between concurrency and parallelism?
Which of the following provides concurrency on a single-core system?
Which of the following provides concurrency on a single-core system?
What is the main purpose of thread libraries?
What is the main purpose of thread libraries?
What is the key difference between implementing a thread library in user space versus kernel space?
What is the key difference between implementing a thread library in user space versus kernel space?
Which of the following is true about Pthreads?
Which of the following is true about Pthreads?
Which operating systems commonly implement the Pthreads standard?
Which operating systems commonly implement the Pthreads standard?
Which of the following is NOT a characteristic of Windows Threads?
Which of the following is NOT a characteristic of Windows Threads?
What are the two primary ways Java threads can be created?
What are the two primary ways Java threads can be created?
In the context of Windows threads, what is the purpose of the Executive Thread Block (EThread)?
In the context of Windows threads, what is the purpose of the Executive Thread Block (EThread)?
What type of information is primarily stored in the Kernel Thread Block (KThread) in Windows?
What type of information is primarily stored in the Kernel Thread Block (KThread) in Windows?
What does the Thread Environment Block (TEB) contain in Windows?
What does the Thread Environment Block (TEB) contain in Windows?
How does Linux refer to threads?
How does Linux refer to threads?
Which system call is used in Linux to create a new thread?
Which system call is used in Linux to create a new thread?
In Linux, what flag would you use with the clone()
system call to ensure that the child task shares the same memory space as the parent task?
In Linux, what flag would you use with the clone()
system call to ensure that the child task shares the same memory space as the parent task?
Flashcards
What is a Thread?
What is a Thread?
Threads provide multiple paths of execution within a process, enabling concurrent task execution. They are the basic unit of CPU utilization.
Thread Characteristics
Thread Characteristics
A lighter-weight form of a process which shares code, data, and OS resources with other threads in the process, but has its own ID, program counter, registers, and stack.
Threads vs. Processes
Threads vs. Processes
Threads within a process run in a shared memory space. Processes run in separate memory spaces. Threads share code, data, and OS resources.
User Threads
User Threads
Signup and view all the flashcards
Kernel Threads
Kernel Threads
Signup and view all the flashcards
Many-to-One Model
Many-to-One Model
Signup and view all the flashcards
One-to-One Model
One-to-One Model
Signup and view all the flashcards
Many-to-Many Model
Many-to-Many Model
Signup and view all the flashcards
Two-Level Model
Two-Level Model
Signup and view all the flashcards
Why Multithreading?
Why Multithreading?
Signup and view all the flashcards
Responsiveness (Multithreading benefit)
Responsiveness (Multithreading benefit)
Signup and view all the flashcards
Resource Sharing (Multithreading benefit)
Resource Sharing (Multithreading benefit)
Signup and view all the flashcards
Economy (Multithreading benefit)
Economy (Multithreading benefit)
Signup and view all the flashcards
Scalability (Multithreading benefit)
Scalability (Multithreading benefit)
Signup and view all the flashcards
Multicore Programming Challenges
Multicore Programming Challenges
Signup and view all the flashcards
Parallelism
Parallelism
Signup and view all the flashcards
Data Parallelism
Data Parallelism
Signup and view all the flashcards
Task Parallelism
Task Parallelism
Signup and view all the flashcards
Concurrency
Concurrency
Signup and view all the flashcards
Thread Libraries
Thread Libraries
Signup and view all the flashcards
Pthreads
Pthreads
Signup and view all the flashcards
Windows Threads
Windows Threads
Signup and view all the flashcards
Java Threads
Java Threads
Signup and view all the flashcards
Linux Threads
Linux Threads
Signup and view all the flashcards
Study Notes
- Modern applications are multithreaded and perform multiple tasks within the same application by using separate threads.
- Kernel is also multithreaded.
- A thread runs within a process.
- Thread creation is lightweight whereas process creation is heavyweight.
- Threads simply code and increase efficiency.
- A thread is a path of execution within a process; a process can contain multiple threads.
- Threads are the basic unit of CPU utilization or execution.
- A thread comprises: a thread ID, a Program counter, a Register set, and a Stack
- A thread within a process can share its code, data section, and OS resources with other threads within the process.
Threads vs. Process
- Threads within a process run in a shared memory space; processes run in separate memory spaces.
- Threads share code and data sections, and OS resources, with other threads within the process.
- A process does not share its parts.
- Threads have their own ID, Program counter, register set, and stack.
User Threads vs. Kernel Threads
- User threads: Management is done by user-level threads library.
- Main user thread libraries include: POSIX Pthreads (Linux & Unix), Windows threads, and Java threads.
- Kernel threads: Supported directly by the kernel (OS).
- Examples of OSs with kernel threads include: Windows, Linux, Solaris, and macOS.
- For user threads and kernel threads to interact, they must have a relationship or a model between them.
- Three possible types of models: Many-to-One, One-to-One, and Many-to-Many.
Multithreading Models
- Many-to-One: Many user-level threads connect to a single kernel thread.
- Disadvantages: If one thread is blocked, all will be blocked; multithreads cannot run in parallel on a multicore processor.
- Example systems include: Solaris Green threads and GNU Portable threads.
- One-to-One: Each user-level thread has a corresponding individual kernel thread.
- Creating a user-level thread creates a kernel thread.
- Advantages: More concurrency than the Many-to-One model.
- Disadvantage: There can be restrictions on the number of threads per process due to overhead.
- Example systems include: Windows, Linux, and Solaris 9 and later.
- Many-to-Many: Allows many user-level threads to map to many kernel threads.
- Example systems include: Solaris 9 and prior, and Windows thread fiber packages.
- The OS allows creating a sufficient amount of kernel threads.
- Two-Level Model: Allows user-level threads to be bound to kernel threads.
- The user-level thread is assigned to a kernel thread, and this relationship is maintained throughout execution.
- Example systems include: Solaris 8 and earlier, IRIX, and HP-UX.
Why Multithreading?
- A thread is sometimes called a light weight process.
- A process with multiple threads can perform multiple tasks at the same time.
- Multiple threads allows browsers to perform multiple tasks simultaneously.
- Can achieve parallelism by dividing process into multiple threads, each thread executing its part of code at a time.
- Multithreaded processes have multiple threads, and each executes a part of the code at a time in parallel compared to single threaded.
Benefits of Multithreading
- Responsiveness: Continues execution even if a process is blocked or performing a lengthy operation, which is useful for user interfaces.
- Resource Sharing: Threads share the memory and resources of a process by default, which is easier than message passing and shared memory.
- Economy: Allocating memory and resources to a process is more expensive than creating threads, which have low overhead. Switching threads is faster than context switching in processes
- Scalability: Threads can run in parallel on different processors, which makes jobs faster and scalable.
Thread Libraries
- Thread libraries provide programmers with APIs to create and manage threads.
- Two ways to implement libraries: Entirely in user space, or kernel-level library supported by the OS.
- User-space implementation is faster but may lack access to low-level functionalities and require kernel support.
Types of Threads
- Pthreads: A POSIX standard API used for creation and synchronization of threads
- It's a specification, not an implementation e.g. the API specifies thread library behaviour, but implementation is library developer's responsibility
- Java Threads: Managed by the Java Virtual Machine (JVM), implemented using the underlying OS's thread model.
- Java threads can be created by extending the Thread class or implementing the Runnable interface.
Multicore Programming
- Multicore or multiprocessor systems put pressure on programmers, leading to challenges like: Dividing activities, balance, data splitting, data dependency and testing & debugging
- Parallelism is a system that can perform more than one task simultaneously, whereas concurrency is a system that supports more than one task making progress.
Types of Parallelism
- Data parallelism distributes subsets of the same data across multiple cores, performing the same operation on each.
- Task parallelism distributes threads across cores, with each thread performing a unique operation.
- The number of threads grows as the architectural support for threading increases.
- CPUs have cores and hardware threads ex. Oracle has 8 cores and 8 hardware threads per core.
Concurrency
- Concurrency on a single-core system can be provided in user-level and kernel-level.
- Single core system supports more than one task in progress, but takes more time.
OS Thread Examples
- Windows Threads: Implemented using the Windows API (primary API) and implements one-to-one mapping and in Kernel-level.
- Java Threads: Java threads can be created in 2 ways: Extending the {@code Thread} class; implementing the {@code Runnable} interface
- The Windows thread contains: Thread ID, Register set to represent the state of processor, A separate stack for user and kernel modes for when it runs in user or kernel mode, Private data storage area used by runtime libraries.
- Primary data structures of Windows threads are: ETHREAD (Executive thread block), KTHREAD (Kernel thread block), and TEB (Thread Environment Block).
- The ETHREAD has a pointer that points to the process the thread belongs to and the KTHREAD in kernel space.
- The KTHREAD has scheduling and synchronization information, kernel mode, and a pointer to TEB in user space.
- The TEB contains the thread ID, user mode stack, thread local storage and is in user space.
- Clone() System call allows child task to share address space with parent task (process).
- The clone flags includes that clone FS (file system info is shared), clone VM (same memory space is shared), clone-SIGHAN (signal handlers is shared) and clone-FILES (open files is shared)
- Struct Task structs points to process data structures (shared or unique).
- The KTHREAD has scheduling and synchronization information, kernel mode, and a pointer to TEB in kernel space.
- Linux Thread refers to threads as tasks and uses the
clone()
system call to create threads - Java threads can be created in 2 ways: Extending the thread class, Implementing the runnable interface.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.