Lecture 18 - UCF Computer Science

Download Report

Transcript Lecture 18 - UCF Computer Science

CGS 3763 Operating Systems Concepts
Spring 2013
Dan C. Marinescu
Office: HEC 304
Office hours: M-Wd 11:30 - 12:30 AM
Lecture 18 – Monday, February 18, 2013

Last time:
Answers to student questions from last week’s lectures
 Threads


Today:


Next time


Threads
More on threads
Reading assignments

Chapter 4 of the textbook
 Chapters 4 textbook slides
Lecture 18
2
Threads

Thread
the smallest sequence of programmed instructions that can be
managed independently by an operating system scheduler
 a light-weight process
 multiple threads can share the same address space
On a single processor, multithreading generally occurs by timedivision multiplexing, the processor switches between different
threads. This context switching generally happens frequently
enough that the user perceives the threads or tasks as running at
the same time.
On a multiprocessor or a multi-core system the threads actually run
at the same time, with each processor or core running a particular
thread or task



Lecture 18
3
Single- and multi-threaded processes
Lecture 18
4
Reasons for multithreading




Reduce the execution time of a compute-intensive task.
Better resource utilization – recall the imbalance between the CPU,
memory, and I/O device bandwidth/speed.
Improve scalability
Example: multithreaded execution on a two core system
Lecture 18
5
Challenges





Identify activities that can run in parallel.
Divide the workload among the threads and balance the
load.
Minimize the communication among threads.
Avoid barrier-synchronization  instances when all
threads have to wait until all of them reach a certain
execution stage.
Debugging.
Lecture 18
6
Multithreading and client-server systems
Lecture 18
7
Common models for threaded programs



Manager/worker: a single thread, the manager assigns work to
other threads, the workers. Typically, the manager handles all input
and parcels out work to the other tasks. At least two forms of the
manager/worker model are common: static worker pool and dynamic
worker pool.
Pipeline: a task is broken into a series of suboperations, each of
which is handled in series, but concurrently, by a different thread. An
automobile assembly line best describes this model.
Peer: similar to the manager/worker model, but after the main thread
creates other threads, it participates in the work.
Lecture 18
8
Shared memory model for threads



All threads have access to the same global, shared memory
Threads also have their own private data
Programmers are responsible for synchronizing access (protecting)
globally shared data.
Lecture 18
9
Thread-safeness


The ability of an application to execute multiple threads
simultaneously without "clobbering" shared data or creating "race"
conditions.
For example, suppose that your application creates several threads,
each of which makes a call to the same library routine:
 This library routine accesses/modifies a global structure or
location in memory.
 As each thread calls this routine it is possible that they may try to
modify this global structure/memory location at the same time.
 If the routine does not employ some sort of synchronization
constructs to prevent data corruption, then it is not thread-safe.
Lecture 18
10
Multiple threads can share a module
Lecture 18
11
User and kernel threads

User threads- thread management done by user-level threads library
 POSIX Pthreads

Win32 threads
 Java threads

Kernel threads







Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
Recall that the kernel has to keep track of all processes on the
system and uses the PCB to do that.
The kernel has to keep track of al threads as well.
Lecture 18
12
Relations between user and kernel threads



Many-to-One  several user-level threads mapped to single kernel
thread:
 Solaris Green Threads
 GNU Portable Threads
One-to-One  each user-level thread maps to kernel thread:
 Windows NT/XP/2000
 Linux
 Solaris 9 and later
Many-to many many user level threads mapped to many kernel
threads
 Allows the operating system to create a sufficient number of
kernel threads
 Solaris prior to version 9
 Windows NT/2000 with the ThreadFiber package
Lecture 18
13
Lecture 18
14