Lecture 4
Lecture 4
Lecture 4
Operating System Concepts – 9th Edition 5.1 Silberschatz, Galvin and Gagne ©2013
Lecture 5
Chapter 4: Threads
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 4: Threads
Overview
Multithreading Models
Thread Libraries
Threading Issues
Windows XP Threads
Linux Threads
Operating System Concepts – 9th Edition 5.3 Silberschatz, Galvin and Gagne ©2013
Objectives
To introduce the notion of a thread — a fundamental unit of CPU utilization
that forms the basis of multithreaded computer systems
To discuss the APIs for the Pthreads, Win32, and Java thread libraries
Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne ©2013
Multithreading
other thread
– boil 1 quart salty water
– serve
input data
Process
Program
Operating System Concepts – 9th Edition 5.5 Silberschatz, Galvin and Gagne ©2013
Process-Thread relationship
uniprogramming
ex: MS-DOS ex: Java VM
Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne ©2013
Process
Threads
• Process is a heavy weight • Threads are light weight
process. processes.
• Threads are bundled inside
the process.
• Process is a separate • Threads have a shared
program that has memory, data, resources,
separate memory, data, files etc.
resources, etc..
• Process are created using • Threads are created using
fork() method. clone() method.
• Context switch between • Context switch between
the processes is time the threads are not much
consuming. time consuming as
Process.
• Example:
Say, opening any browser • Example:
(Mozilla, Chrome, IE). At Opening multiple tabs in
this point new process the browser.
will start to execute.
Operating System Concepts – 9th Edition 5.7 Silberschatz, Galvin and Gagne ©2013
Benefits
• Continue even if part of an application
is blocked due to I/O
Responsiveness • Allow user interaction in one thread
while image is loading in another.
Multi-threads
Operating System Concepts – 9th Edition 5.9 Silberschatz, Galvin and Gagne ©2013
Multicore Programming
Multicore systems putting pressure on programmers,
challenges include:-.
Dividing activities:
Balance: Tasks should
Finding areas of code that
perform equal work of
can run in parallel as
equal value.
concurrent tasks.
Data dependency: Ensure
Data splitting: Data the execution of the tasks
manipulated by the tasks is synchronized to
must be divided to run on accommodate data
separate cores. dependencies between
tasks.
Testing and debugging:
Testing a program with
multiple execution paths
is inherently more
challenging
Operating System Concepts – 9th Edition 5.10 Silberschatz, Galvin and Gagne ©2013
Multithreaded Server Architecture
Operating System Concepts – 9th Edition 5.11 Silberschatz, Galvin and Gagne ©2013
Multicore Scalability
Operating System Concepts – 9th Edition 5.12 Silberschatz, Galvin and Gagne ©2013
Parallelism and Concurrency
Operating System Concepts – 9th Edition 5.14 Silberschatz, Galvin and Gagne ©2013
Thread Libraries
The first approach is to provide a library entirely in user
space with no kernel support.
All code and data structures for the library exist in
user space.
This means that invoking a function in the library
results in a local function call in user space and not
a system call.
The second approach is to implement a kernel-level
library supported directly by the operating system.
In this case, code and data structures for the library
exist in kernel space.
Invoking a function in the API for the library typically
results in a system call to the kernel.
Operating System Concepts – 9th Edition 5.15 Silberschatz, Galvin and Gagne ©2013
Thread Libraries
The second approach is
The first approach is to
to implement a kernel-
provide a library entirely in
level library supported
user space with no
directly by the operating
kernel support.
system.
All code and data In this case, code
structures for the and data structures
library exist in user for the library exist in
space. kernel space.
Examples
Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
Operating System Concepts – 9th Edition 5.17 Silberschatz, Galvin and Gagne ©2013
User Threads
Thread management done by user-level
threads library
There are three primary thread libraries:
Operating System Concepts – 9th Edition 5.18 Silberschatz, Galvin and Gagne ©2013
Pthreads
A POSIX (Portable Operating System Interface for Unix)
standard (IEEE 1003.1c) API for thread creation and
synchronization
This is a specification for thread behavior, not an
implementation. Operating-system designers may
implement the specification in any way they wish.
May be provided either as user-level or kernel-level
Operating System Concepts – 9th Edition 5.20 Silberschatz, Galvin and Gagne ©2013
Java Threads
Operating System Concepts – 9th Edition 5.21 Silberschatz, Galvin and Gagne ©2013
Threads Libraries Comparison
Operating System Concepts – 9th Edition 5.23 Silberschatz, Galvin and Gagne ©2013
Multithreading Models
Operating System Concepts – 9th Edition 5.24 Silberschatz, Galvin and Gagne ©2013
Many-to-One Model
Operating System Concepts – 9th Edition 5.25 Silberschatz, Galvin and Gagne ©2013
Many-to-One
Thread management is done by the thread library in user
space
Is efficient
But, entire process will block if a thread makes a
blocking system call.
Only one thread can access the kernel at a time,
Multiple threads can not run in parallel on
multiprocessors.
As an example,
Solaris Green Threads ( threading model used by the Java virtual
machine (JVM), at least on Solaris.
GNU Portable Threads (portable POSIX based library for Unix
platforms
Operating System Concepts – 9th Edition 5.26 Silberschatz, Galvin and Gagne ©2013
One-to-one Model
Operating System Concepts – 9th Edition 5.27 Silberschatz, Galvin and Gagne ©2013
One-to-One
Provides more concurrency.
No blocking due to another thread.
Threads may run in parallel on multiprocessors.
Examples
Windows NT/XP/2000
Linux
Solaris 9 and later
Operating System Concepts – 9th Edition 5.28 Silberschatz, Galvin and Gagne ©2013
Many-to-Many Model
Operating System Concepts – 9th Edition 5.29 Silberschatz, Galvin and Gagne ©2013
Many-to-Many Model
Examples
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
Operating System Concepts – 9th Edition 5.30 Silberschatz, Galvin and Gagne ©2013
Two-level Model
Operating System Concepts – 9th Edition 5.31 Silberschatz, Galvin and Gagne ©2013
Two-level Model
Examples
IRIX (developed by Silicon Graphics (SGI) to run
on the company's proprietary workstations and
servers. It is a variety of UNIX System)
Tru64 UNIX ( 64-bit UNIX operating
system currently owned by Hewlett-Packard (HP))
Solaris 8 and earlier (Sun microsystems)
Operating System Concepts – 9th Edition 5.32 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 5.33 Silberschatz, Galvin and Gagne ©2013
Semantics of fork() and exec()
fork()
The fork call basically makes a duplicate of the
current process, identical in almost every way (not
everything is copied over, for example, resource limits
in some implementations but the idea is to create as
close a copy as possible).
The new process (child) gets a different process ID
(PID) and has the the PID of the old process (parent)
as its parent PID (PPID). The two processes are now
running exactly the same code.
If the fork call does not work, no child is created and
the parent gets an error code.
Operating System Concepts – 9th Edition 5.34 Silberschatz, Galvin and Gagne ©2013
Semantics of fork() and exec()
Exec:
The exec call is a way to basically replace the entire
current process with a new program.
It loads the program into the current process space and
runs it from the entry point.
exec() replaces the current process with the executable
pointed by the function.
Control never returns to the original program unless
there is an exec() error.
Clone : Clone, as fork, creates a new process. Unlike fork,
these calls allow the child process to share parts of its
execution context with the calling process, such as the
memory space, the table of file descriptors, and the table of
signal handlers.
Operating System Concepts – 9th Edition 5.35 Silberschatz, Galvin and Gagne ©2013
Thread Cancellation
Operating System Concepts – 9th Edition 5.36 Silberschatz, Galvin and Gagne ©2013
Thread Pools
Operating System Concepts – 9th Edition 5.37 Silberschatz, Galvin and Gagne ©2013
End of Chapter 4
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013