0% found this document useful (0 votes)
60 views24 pages

OS-CO2-Session 11 Multithreading Models

The document discusses different multithreading models including many-to-one, one-to-one, and many-to-many. It also covers thread libraries like Pthreads, Windows threads, and Java threads. Specific examples are provided of implementing threads in Windows and Java. Common thread management functions like joining threads are demonstrated for Pthreads and Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views24 pages

OS-CO2-Session 11 Multithreading Models

The document discusses different multithreading models including many-to-one, one-to-one, and many-to-many. It also covers thread libraries like Pthreads, Windows threads, and Java threads. Specific examples are provided of implementing threads in Windows and Java. Common thread management functions like joining threads are demonstrated for Pthreads and Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Operating Systems

Multithreading Models

Dr. Elangovan G
Associate Professor
Department of Artificial Intelligence and Data Science
Koneru Lakshmaiah Education Foundation
(Deemed to be University)
Green fields, Vaddeswaram, AP-522502, India

Session : 11
Multithreading Models
• Multithreading Models
• Many-to-One
• One-to-One
• Many-to-Many
• Two-level Model
• Thread Libraries
• Pthreads
• Windows Multithreaded C Program
• Java Threads
8/24/2022 Operating Systems - CO2 2
User Threads and Kernel Threads
• User threads - management done by user-level threads library
• Three primary thread libraries:
• POSIX Pthreads
• Windows threads
• Java threads
• Kernel threads - Supported by the Kernel
• Examples – virtually all general-purpose operating systems, including:
– Windows
– Linux
– Mac OS X
– iOS
– Android
8/24/2022 Operating Systems - CO2 3
User and Kernel Threads

8/24/2022 Operating Systems - CO2 4


Multithreading Models
• Many-to-One
• One-to-One
• Many-to-Many

8/24/2022 Operating Systems - CO2 5


Many-to-One
• Many user-level threads mapped to single kernel thread
• One thread blocking causes all to block
• Multiple threads may not run in parallel on multicore system
because only one may be in kernel at a time
• Few systems currently use this model
• Examples:
– Solaris Green Threads
– GNU Portable Threads

8/24/2022 Operating Systems - CO2 6


One-to-One
• Each user-level thread maps to kernel thread
• Creating a user-level thread creates a kernel thread
• More concurrency than many-to-one
• Number of threads per process sometimes restricted due to
overhead
• Examples
– Windows
– Linux

8/24/2022 Operating Systems - CO2 7


Many-to-Many Model
• Allows many user level threads to be mapped to many
kernel threads
• Allows the operating system to create a sufficient number
of kernel threads
• Windows with the ThreadFiber package
• Otherwise not very common

8/24/2022 Operating Systems - CO2 8


Two-level Model
• Similar to M:M, except that it allows a user thread to be bound
to kernel thread

8/24/2022 Operating Systems - CO2 9


Thread Libraries
• Thread library provides programmer with API for creating and
managing threads
• Two primary ways of implementing
– Library entirely in user space
– Kernel-level library supported by the OS
• POSIX Pthreads,
• Windows, and
• Java.
8/24/2022 Operating Systems - CO2 10
Pthreads
• May be provided either as user-level or kernel-level
• A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization
• Specification, not implementation
• API specifies behavior of the thread library, implementation is
up to development of the library
• Common in UNIX operating systems (Linux & Mac OS X)

8/24/2022 Operating Systems - CO2 11


Pthreads Example

8/24/2022 Operating Systems - CO2 12


Pthreads Example (Cont.)

8/24/2022 Operating Systems - CO2 13


Pthreads Code for Joining 10 Threads

8/24/2022 Operating Systems - CO2 14


Windows Multithreaded C Program

8/24/2022 Operating Systems - CO2 15


Windows Multithreaded C Program (Cont.)

8/24/2022 Operating Systems - CO2 16


Java Threads
• Java threads are managed by the JVM
• Typically implemented using the threads model provided by
underlying OS
• Java threads may be created by:
– Extending Thread class
– Implementing the Runnable interface

– Standard practice is to implement Runnable interface

8/24/2022 Operating Systems - CO2 17


Java Threads
Implementing Runnable interface:

Creating a thread:

Waiting on a thread:

8/24/2022 Operating Systems - CO2 18


Java Executor Framework
• Rather than explicitly creating threads, Java also allows
thread creation around the Executor interface:

• The Executor is used as follows:

8/24/2022 Operating Systems - CO2 19


Java Executor Framework

8/24/2022 Operating Systems - CO2 20


Java Executor Framework (Cont.)

8/24/2022 Operating Systems - CO2 21


Quiz
• 1. The model in which one kernel thread is mapped to many user-level threads is called
___________
A. Many to One model
B. One to Many model
C. Many to Many model
D. One to One model

Answer: A
• 2. In the Many to One model, if a thread makes a blocking system call ___________
A. the entire process will be blocked
B. a part of the process will stay blocked, with the rest running
C. the entire process will run
D. none of the mentioned

Answer: A

8/24/2022 Operating Systems - CO2 22


Quiz
• 3. In the One to One model when a thread makes a blocking system call ___________
A. other threads are strictly prohibited from running
B. other threads are allowed to run
C. other threads only from other processes are allowed to run
D. none of the mentioned

Answer: B
• 4. Which of the following is the drawback of the One to One Model?
A. increased concurrency provided by this model
B. decreased concurrency provided by this model
C. creating so many threads at once can crash the system
D. creating a user thread requires creating the corresponding kernel thread

Answer: D

8/24/2022 Operating Systems - CO2 23


Multithreading Models
✓Multithreading Models
✓Many-to-One
✓One-to-One
✓Many-to-Many
✓Two-level Model
✓Thread Libraries
✓Pthreads
✓Windows Multithreaded C Program
✓Java Threads
8/24/2022 Operating Systems - CO2 24

You might also like