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

Lecture 4

The document discusses threads and multithreading. It covers thread libraries, multithreading models, threading issues and examples in Windows and Linux. It introduces threads as fundamental units of CPU utilization and discusses APIs for thread libraries in Pthreads, Win32 and Java.

Uploaded by

Khaled Nady
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)
14 views

Lecture 4

The document discusses threads and multithreading. It covers thread libraries, multithreading models, threading issues and examples in Windows and Linux. It introduces threads as fundamental units of CPU utilization and discusses APIs for thread libraries in Pthreads, Win32 and Java.

Uploaded by

Khaled Nady
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

Operating System

Lecture 4

Dr. Abdalla Mostafa Abdalla


[email protected]

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

 Operating System Examples

 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

 To examine issues related to multithreaded programming

Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne ©2013
Multithreading

 The execution part is a “thread” that can be multiplied


same CPU working
Pasta for six on two things

other thread
– boil 1 quart salty water

– stir in the pasta CPU


thread of execution

– cook on medium until “al


dente”

– 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

multiprogramming ex: early UNIX ex: Solaris, Mach, Windows

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.

• Memory / resource sharing is by default


with threads.
Resource
• Several different threads of activity within
Sharing
the same address space.

• It is more economical to create and


context-switch threads.
• In general, creating a process is 30
Economy times slower,
• context-switching a process is 5 times
slower.
Operating System Concepts – 9th Edition 5.8 Silberschatz, Galvin and Gagne ©2013
Single and Multithreaded Processes

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

Concurrent Execution on a Single-core System

Parallel Execution on a Multicore System

Operating System Concepts – 9th Edition 5.12 Silberschatz, Galvin and Gagne ©2013
Parallelism and Concurrency

 To distinct between parallelism and concurrency


 A system is parallel if it can perform more than one
task simultaneously.
 In contrast, a concurrent system supports more than
one task by allowing all the tasks to make progress.

 Thus, it is possible to have concurrency without


parallelism.
 Before the multicore architectures, most computers
systems had only a single processor.
CPU schedulers were designed to provide the
illusion of parallelism by rapidly switching between
processes.
Operating System Concepts – 9th Edition 5.13 Silberschatz, Galvin and Gagne ©2013
Thread Libraries
 Thread library provides programmer with API
(Application Programming Interface) for creating and
managing threads

 Two primary ways of implementing User


 Library entirely in user space level
 Kernel-level library supported Kernel
by the OS level

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.

Invoking a function Invoking a function


in the library results in in the API for the
a local function call library typically results
in user space and not in a system call to
a system call. the kernel.
Operating System Concepts – 9th Edition 5.16 Silberschatz, Galvin and Gagne ©2013
Kernel Threads

 Supported by the Kernel

 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:

POSIX Win32 Java


Pthreads threads threads

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

 API specifies behavior of the thread library,


implementation is up to development of the library

 Common in UNIX operating systems (Solaris, Linux,


Mac OS )
Operating System Concepts – 9th Edition 5.19 Silberschatz, Galvin and Gagne ©2013
Multithreading with Win32
 The Microsoft C/C++ compiler provides support for
creating multithread applications.
 With MS, there are several ways to program with
multiple threads:
 You can use C++ and the Windows Runtime library
 the Microsoft Foundation Class (MFC) library
 the .NET runtime
 the C run-time library
 the Win32 API.

Operating System Concepts – 9th Edition 5.20 Silberschatz, Galvin and Gagne ©2013
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

Operating System Concepts – 9th Edition 5.21 Silberschatz, Galvin and Gagne ©2013
Threads Libraries Comparison

Win32 thread Pthreads Java thread

• The Windows • Pthreads, the • The Java thread


thread library is a threads extension API allows
kernel-level library of the POSIX threads to be
available on standard, may be
created and
Windows systems. provided as either
a user-level or a managed
kernel-level library. directly in Java
programs.

System However, because in most instances the JVM is running on


top of a host operating system, the Java thread API is generally
implemented using a thread library available on the host
This means that on Windows systems, Java threads are typically
implemented using the Windows API; UNIX and Linux systems
often use Pthreads Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 5.22
Kernel And User Thread Relationship

 User threads are supported above the kernel and are


managed without kernel support,
 Whereas kernel threads are supported and managed
directly by the operating system.
 Virtually all operating systems (including Windows,
Linux, Mac OS X, and Solaris) support kernel threads.
 Ultimately, a relationship must exist between user
threads and kernel threads.

Operating System Concepts – 9th Edition 5.23 Silberschatz, Galvin and Gagne ©2013
Multithreading Models

Many-to- One-to- Many-to-


One One Many
• Two-level

Operating System Concepts – 9th Edition 5.24 Silberschatz, Galvin and Gagne ©2013
Many-to-One Model

Many user-level threads


mapped to single kernel
thread.

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

Each user-level thread maps to


kernel thread.

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.

 Creating a user thread requires creating the


corresponding kernel thread.
 Hence, OSes limit the number of threads.

 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

Many user level


threads are
mapped to
many kernel
threads.

Operating System Concepts – 9th Edition 5.29 Silberschatz, Galvin and Gagne ©2013
Many-to-Many Model

 Allows the operating system to create a sufficient


number of kernel threads.

 Suffers from neither of shortcomings of many-to-


one or one-to-one.

 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

 Similar to many-to-many, except that it allows a user


thread to be bound to kernel thread.

 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

 Terminating a thread before it has finished

 Two general approaches:


Asynchronous cancellation terminates the
target thread immediately
Deferred cancellation allows the target thread to
periodically check if it should be cancelled

Operating System Concepts – 9th Edition 5.36 Silberschatz, Galvin and Gagne ©2013
Thread Pools

 Create a number of threads in a pool where they


await work

 Usually slightly faster to service a request with an


existing thread than create a new thread

 Allows the number of threads in the application(s) to


be bound to the size of the pool

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

You might also like