0% found this document useful (0 votes)
2 views

Threads

This lecture discusses threads in operating systems, highlighting the differences between processes and threads, and the benefits of using multithreading for resource sharing and responsiveness. It outlines the costs associated with process creation and inter-process communication, and presents user-level and kernel-level thread implementations, along with their advantages and disadvantages. The lecture emphasizes the efficiency of threads in managing multiple activities within a single process compared to traditional processes.

Uploaded by

alamad402
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)
2 views

Threads

This lecture discusses threads in operating systems, highlighting the differences between processes and threads, and the benefits of using multithreading for resource sharing and responsiveness. It outlines the costs associated with process creation and inter-process communication, and presents user-level and kernel-level thread implementations, along with their advantages and disadvantages. The lecture emphasizes the efficiency of threads in managing multiple activities within a single process compared to traditional processes.

Uploaded by

alamad402
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/ 18

Lecture 9: Threads

Operating Systems

Created by:
Dr. Salem Alqahtani
Dr. Aymen Trigui
Dr. Mudassir Rafi

1
Processes
● Recall that …
• A process consists of:
• An address space ( defining all the code and data pages )
• The code for the running program
• The data for the running program
• A set of OS resources
• Open files, network connections ..
• Execution state ( PC, SP, regs, etc)
• Processes are completely isolated from each other
Processes
Some Issues with the Processes
● Creating a new process is costly because of new address space and data
structures that must be allocated and initialized

● Communication between processes is costly because most communication


goes through the OS
• Inter Process Communication (IPC)
• Overhead of system calls and copying data
Example: A Multi-Activity Text Editor
● Process approach on data
• P1: read from keyboard
• P2: format document
• P3: write to disk

● The processes will actively access the same


set of data.
How do the processes exchange data?
● Context Switch for Processes- costly
● IPC and Synchronization are very costly
Context Switches of Processes: Expensive
● Context switch between processes
• Save processor registers for current process
• Load the new process’s registers

● Switch address spaces – expensive


• Hardware cache
• Memory pages (e.g., TLB content)
Ideal Solution for the Text Editor Threads
● Three activities within one process
• Single address space
• Same execution environment
• Data shared easily

● Switch between activities


• Only running context
• No change in address space (all threads
within a process share the same page
table)
Thread vs Process
● Thread
• Flow of control (activity) within a process

● Process
• Multiple threads
• For parallelism and efficient close cooperation

● States of threads in a process


• Running vs. waiting
Single and Multithreaded Processes

Single Threaded Process Multithreaded Process


Multithreaded Server Architecture
Benefits
● Responsiveness
• Interactive application ( a.g. web
browser )
● Resource sharing
• Address space and other resources
● Economy: Less overhead
• Solaris: process creation 30X
overhead than thread;
• Context switching threads within a
process 5X faster
● Scalability:
• Better utilization of multiprocessor /
multicore systems
Thread Implementations
● Process usually starts with a single thread
● Thread management: required operations
• Creation: procedure/method for the new thread to run
• Scheduling: runtime properties/attributes
• Destruction: release resources
● Thread Synchronization
• join, wait, etc.
● Who and where to manage threads
● User space: managed by applications
● Kernel space: managed by OS
• all modern OSes have kernel level support
User-Level Threads
● User threads: thread library at the user level
● Run-time system provides support for thread creation, scheduling and
management

Kernel has
NO
knowledge
of threads!
User-Level Threads
● Each process needs its own private thread table to keep track of the threads in
that process.

● The thread-table keeps track of the per-thread items (program counter, stack
pointer, register, state..)

● When a thread does something that may cause it to become blocked locally
(e.g. wait for another thread), it calls a run-time system procedure.

● If the thread must be put into blocked state, the procedure performs thread
switching
User-Level Threads
● Advantages
• Fast thread switching: no kernel activity involved
• Customized/flexible thread scheduling algorithm
• Application portability: different machines with library
● Problems
• OS kernel knows only process
• Blocking system calls: one thread blocks all activities
• All threads share one CPU → cannot use multi-proc./core
● Example thread libraries
• POSIX Pthreads
• Java threads
Kernel-Level Threads
● Supported directly by OS: kernel performs thread creation, scheduling &
management in kernel space
Kernel-Level Threads
● Scheduling entities: threads
• When a thread blocks, the kernel may choose another thread from the
same or different process

● Thread operations
• System calls → cost

● Some kernels recycle their threads


• New threads use existing data-structures
Kernel-Level Threads
• Advantages
• User activity/thread with blocking I/O does NOT block other activities/threads from
the same user
• Multi-activities in applications can use multi-proc/cores
• Problems
• Thread management could be relatively costly: all methods that might block a
thread are implemented as system calls
• Non-flexible scheduling policy
• Non-portability: app. can only run on same type of machine
• Examples:
• Linux, Solaris, Windows XP, Mac OS X

You might also like