0% found this document useful (0 votes)
39 views38 pages

CS 3220: Operating Systems: Instructor

This document provides an overview of threads in operating systems. It discusses that a process can have multiple threads of execution that share the same address space, in a concept known as multithreading. Threads allow for pseudo-parallelism within a single process and are lighter weight than processes. Examples are given where threads are useful, such as in word processors and web servers. The classical thread model and POSIX threads standard are introduced. Implementation of threads in user space and kernel space are compared.

Uploaded by

Zeeshan Wasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views38 pages

CS 3220: Operating Systems: Instructor

This document provides an overview of threads in operating systems. It discusses that a process can have multiple threads of execution that share the same address space, in a concept known as multithreading. Threads allow for pseudo-parallelism within a single process and are lighter weight than processes. Examples are given where threads are useful, such as in word processors and web servers. The classical thread model and POSIX threads standard are introduced. Implementation of threads in user space and kernel space are compared.

Uploaded by

Zeeshan Wasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

CS 3220:

OPERATING SYSTEMS

LECTURE 2

INSTRUCTOR:
OSAMA AHMED KHAN
osamaahmedkhan MAR 18TH, 2020
THREADS

• A Process:
• has a unique Address Space, and
• has a Single Thread of Control/Execution
• might have Multiple Threads of Control sharing the same Address
Space (MiniProcesses), and the concept is known as
MultiThreading
THREAD USAGE

• Why need Threads?


1. An Application might have multiple activities going on at once
• Multiple Sequential Threads run in PseudoParallelism sharing the
Address Space as well as Data among themselves
2. Threads (Lightweight Processes) are lighter in weight than
Processes
• Easier (10-100 times faster) to create and destroy than Processes
• Threads need changes done dynamically and rapidly
3. Threads decrease total computing time of a Process
• No performance gain when all Threads are in Ready State
• Substantial computing and substantial I/O improve performance of the
Process
• Replication of many Processes running in parallel inside a single
system
THREAD USAGE
THREAD USAGE

• Examples where Threads are required:


1. Word Processor
• Writing a 800-Page Book (Chapters, Sections and SubSections)
• Deleting one sentence from Page 1 and making edition on Page 600
• Three Threads required
i. User Interaction
ii. Reformatting
iii. Backup
• With a single Thread
• Each function would have to wait for its turn
• What if there were three Processes assigned to Word Processor?
THREAD USAGE
THREAD USAGE

2. Spreadsheet
• Three Threads required
i. User Interaction
ii. Recomputing
iii. Backup

3. Server for a World Wide Web site


• Two Threads required
i. Dispatcher
ii. Worker
• What if a single Thread was assigned to the Server?
• What if a single Thread was assigned to the Server with non-blocking
I/O?
THREAD USAGE
THREAD USAGE
THREAD USAGE
THREAD USAGE

4. Applications processing large amount of data


• Three Threads required
i. Input
ii. Processing
iii. Output
THREAD USAGE

•  Examples where Threads are not required:


1. Playing Chess (either with another User or with Computer)
2. Computing “first” Prime Numbers
THE CLASSICAL THREAD MODEL

• Process Model
1. Resource Grouping
• Groups related resources to be managed efficiently
2. Execution
• Thread of Execution OR Thread possesses the following
components:
i. Program Counter: Keeps track of which instruction to execute next
ii. Registers: Holds current working variables
iii. Stack: Contains execution history, with one frame for each current
working procedure
iv. State: Running, Ready or Blocked
• All the components are stored in a Thread Table
THE CLASSICAL THREAD MODEL
THE CLASSICAL THREAD MODEL
THE CLASSICAL THREAD MODEL

Characteristic Process Thread


Requirement Multiple programs Multiple functions
Divisible Components Threads None
Hierarchical Relationships Yes No
Weight Heavy Light
Creation/Destruction Slow Fast
Address Space Unique Shared
Protection Yes No
Data Sharing Infrequent Frequent
Changes Required Infrequent Frequent
Competition Hostile Cooperative
Developer Unique Same
THE CLASSICAL THREAD MODEL

• In MultiThreading, a Process always starts with a single


Thread
• Common Procedures used by Threads:
• thread_create (procedure’s name): Creates a new Thread
• thread_exit: Exits upon finishing work, and vanishes (all
components are destructed)
• thread_join (other thread’s id): Waits/Blocks until a specific
Thread exits
• thread_yield (other thread’s id): Voluntarily gives up the CPU
to let another Thread run
• Usually done for Threads that are expected to finish their work in
short durations, and eventually return the control to the calling
Thread
THE CLASSICAL THREAD MODEL

• Complications introduced by MultiThreading:


1. Upon a new process creation, should the Child Process have the
same number and type of Threads as the Parent Process?
i. Microsoft Word creating MathType
ii. Microsoft Word creating another Microsoft Word
2. During a new process creation, if a Thread in the Parent Process
was blocked, should the same Thread in the Child Process also
remain blocked?
3. Upon generation of an input signal (e.g. via Keyboard), which
Thread (Parent’s or Child’s or Both) should handle it?
• Signals are sent to Processes and not to Threads
• What happens if two or more Threads register to receive the same
signal?
THE CLASSICAL THREAD MODEL

4. What happens if one Thread closes a file while another Thread is


still reading from it?
5. What happens if one Thread requests more Memory, and upon
Context Switching, the other Thread also requests more Memory?
6. Local vs. Global vs. Private Global Variables
• e.g. “errno” is a Global variable for all Threads running inside a Process
• Solution:
• Private Global Variables allow each Thread to have a private copy of the
Global Variable
THE CLASSICAL THREAD MODEL

7. What happens if multiple Threads access the same Buffer?


• Solution:
• Private Buffer for each Thread
• Disadvantage:
• Resource-intensive
8. Is the newly developed Multi-threaded application backward
compatible, and thus can run on single-threaded systems?
THE CLASSICAL THREAD MODEL

Process:
{
Global Variables
Thread 1: Thread 2: …
{ {
Private Global Variables Private Global Variables
… …
Function 1: Function 1:
{ {
Local Variables Local Variables
… …
} }
Function 2: Function 2:
{ {
Local Variables Local Variables
… …
} }
}
}
THE CLASSICAL THREAD MODEL
POSIX THREADS

• Pthreads: IEEE standard package for Portable Threaded


Programs
• Over 60 function calls
IMPLEMENTING THREADS IN
USER SPACE
• Threads package is implemented entirely in User Space
• Kernel does not know anything about the Threads
• It manages Processes only through Process Table residing in
Kernel Space
• Run-time System: Collection of procedures (thread_create,
etc.) that manages Threads
• Each Process has its own Run-time System and a private
Thread Table
IMPLEMENTING THREADS IN
USER SPACE
IMPLEMENTING THREADS IN
USER SPACE
• Complications introduced by User Space Implementation:
1. While a Thread is running, no other Thread in the same Process
will run until the former Thread executes thread_yield
• No Context Switching among Threads inside a Process takes place
• Not much use of Scheduler (no need of Threads)
• Solution:
i. Allow Run-time System to send an Interrupt frequently and
regularly (overhead will increase)
IMPLEMENTING THREADS IN
USER SPACE
2. What happens if a Thread reads from a Keyboard before any
keys have been pressed?
• The blocking Thread will also block the rest of the Threads in the
Process, and in result the entire Process
• Solutions:
i. Return 0 bytes to the calling Thread, instead of getting blocked (will
result in non-blocking system calls)
ii. Check in advance if a call will block (e.g. select system call in UNIX
tells in advance whether a read system call will block) and act
accordingly (retry after a little while)
• Inefficient solution
• Precautionary code written to do the checking is known as Jacket or Wrapper
IMPLEMENTING THREADS IN
KERNEL SPACE
• Threads package is implemented entirely in Kernel Space
• Kernel knows everything about the Threads
• It manages Processes as well as Threads through a global Process
Table and a global Thread Table respectively
• Both the tables reside in Kernel Space
• No Run-time System required
IMPLEMENTING THREADS IN
KERNEL SPACE
• Thread Recycling:
• Performed to avoid Overhead associated with Kernel Space
implementation
• Upon destroying a Thread,
• it is marked as Not Runnable
• its Thread Table is kept intact
• Upon creating a new Thread,
• Thread Table of the destroyed Thread is re-used

• Can also be performed to avoid Overhead associated with User


Space implementation
• Not performed usually due to already low Overhead
IMPLEMENTING THREADS IN
KERNEL SPACE
Characteristic/ Implementation User Space Kernel Space
Entire Implementation User Space Kernel Space
Thread Management Run-time System Kernel
Run-time System Per Process (Private) None
Process Table One (Global) One (Global)
Process Table Residence Kernel Space Kernel Space
Thread Table Per Process(Private) One (Global)
Thread Table Residence User Space Kernel Space
Context Switching among No Yes
Threads
Implementation on Single- Possible Not Possible
Threaded System
Overhead Less More
Space Requirement High Low
HYBRID IMPLEMENTATION

• Multiplexing User-level Threads onto Kernel-level Threads


• Developer decides about:
i. Total number of Kernel-level Threads required
ii. Mapping of relevant User-level Threads onto specific Kernel-
level Threads
• Kernel knows only about the Kernel-level Threads and
manages them
• User-level Threads associated with a specific Kernel-level
Thread take turns using it
HYBRID IMPLEMENTATION
HYBRID IMPLEMENTATION
Characteristic/ User Space Kernel Space Hybrid
Implementation
Entire Implementation User Space Kernel Space User/Kernel Space
Thread Management Run-time System Kernel None/Kernel
Run-time System Per Process (Private) None None
Process Table One (Global) One (Global) None
Process Table Residence Kernel Space Kernel Space N/A
Thread Table Per Process (Private) One (Global) One (Global)
Thread Table Residence User Space Kernel Space Kernel Space
Context Switching among No Yes No/Yes
Threads
Implementation on Single- Possible Not Possible Possible
Threaded System
Overhead Less More Less
Space Requirement High Low Low
SCHEDULER ACTIVATIONS

• Hybrid Implementation
• Kernel assigns a number of Virtual Processors (starting from
one) to each Process
• Run-time System in User Space allocates Threads to Virtual
Processors
• A Process can ask for more Virtual Processors and can also
return some if it no longer needs
• Kernel can also take back Virtual Processors assigned to a
Process, in order to assign them to more needy Processes
SCHEDULER ACTIVATIONS

• When a running Thread is blocked:


1. Kernel activates the Run-time System via an Upcall
2. Run-time System schedules a new Thread
3. Kernel activates the Run-time System again via an Upcall when
data for the blocked Thread is available
SCHEDULER ACTIVATIONS

• When a Hardware Interrupt occurs while a Thread is running:


1. Running Thread is blocked
2. CPU switches to Kernel mode
3. Upon finishing handling the Interrupt:
A. If Hardware Interrupt is not of interest to the blocked Thread:
• Interrupt Handler puts the blocked Thread back into Running state
B. If Hardware Interrupt is of interest to the blocked Thread:
• Run-time System decides which Thread to schedule:
i. Blocked Thread
ii. A Thread from the Ready Queue
iii. Any other choice
POP-UP THREADS

• On a World Wide Web Server:


• Traditional approach:
• Thread or Process waits (is blocked) for an incoming message
• Alternate approach:
• Arrival of a message causes the system to create a new Thread,
called as a Pop-up Thread
• Advantages:
1. Loading of Thread Table is avoided
2. Latency between Message arrival and start of Processing is reduced
3. All Pop-up Threads are identical in nature
POP-UP THREADS

You might also like