PowerPoint

advertisement
Threads, SMP, and
Microkernels
Chapter 4
Two Characteristics of
Processes (1)
Resource ownership
z A process is allocated a virtual address
space to hold the process image
z A process , from time to time, may be
assigned main memory plus control of
other resources, such as I/O channels, I/O
devices, and files.
Two Characteristics of
Processes (2)
Dispatching
z A process is an execution path (trace)
through one or more programs.
y execution may be interleaved with other
processes
(parent process and child process)
z A process has an execution state
(Running, Ready, Blocked, Blocked
Suspend, Read Suspend)
Dispatching and Thread
z These two characteristics are treated
independently by the operating system
z Resource of ownership is referred to as a
process or task
z Dispatching is referred to as a thread
Thread
z A thread is a piece of program code
executed in a serial fashion.
z A thread is a dispatchable of work. It
executes sequentially and is interruptable
so that the processor can turn to another
thread.
z A process is a collection of one or more
threads and associated system resources.
Multithreading
z Multithreading
Operating system supports multiple
threads of execution within a single
process
Threads and Processes
one process
one thread
multiple processes
one thread per process
one process
multiple threads
multiple processes
multiple threads per process
Single Threaded and
Multithreaded Process Models
Multithreaded
Process Model
Single-Threaded
Process Model
Process
Control
Block
User
Address
Space
Thread
Thread
Thread
Control
Block
Thread
Control
Block
Thread
Control
Block
Process
Control
Block
User
Stack
User
Stack
User
Stack
User
Address
Space
Kernel
Stack
Kernel
Stack
Kernel
Stack
User
Stack
Kernel
Stack
Thread
Relationship Between
Threads and Processes
Threads:Process
Description
Example Systems
1:1
Each thread of execution is a
unique process with its own
address space and resources.
Most UNIX implementations
M:1
A process defines an address
space and dynamic resource
ownership. Multiple threads
may be created and executed
within that process.
Windows NT, Solaris, OS/2,
OS/390, MACH
Relationship Between
Threads and Processes
Threads:Process
1:M
M:M
Description
Example Systems
A thread may migrate from one
process environment to
another. This allows a thread
to be easily moved among
distinct systems.
Ra (Clouds), Emerald
Combines attributes of M:1
and 1:M cases
TRIX
Examples of threading &
Multithreading
z MS-DOS supports a single process and a
single thread.
z Java support a single process with
multiple threads
z UNIX supports multiple user processes but
only supports one thread per process
z Solaris and Windows NT supports multiple
user processes , each of which supports
multiple threads
fork System Call
1. Syntax of fork System Call
Include File(s): <sys/types.h>
<unistd.h>
Summary: pid_t fork (void);
Return
Success : 0 in child, child PID in parent
Failure: -1
Set errno: yes
2. Function of fork System Call
When a fork system call is made, the operating system
generates a copy of the parent process which becomes
the child process.
Process
Dr. Ming Zhang
thr_create API (Application Program
Interface) (1)
1. The thr_create Function Prototype
Include File: <thread. .h>
int thr_create(
void* stackp, size_t
stack_size,
void*
(*funcp)(void*), void* argp,
long flags, thread_t* tid_p);
2. Function of thr_create( )
The function creates a new thread to execute a
function whose address is given in the funcp
Dr. Ming Zhang
thr_create API (2)
3. Return
Success : 1
Failure: 0
4. void* stackp
The address of a user-defined memory region.
This memory is used as the new thread runtime stack. If the stackp value is NULL, the
function allocates a stack region of stack_size
bytes.
5 size_t stack_size
The size of a user-defined memory region. This
memory is used as the new thread run-time
stack. If the stack_size value is 0, the function
uses a system default value that is one Dr. Ming Zhang
thr_create API
(3)
6. void* (*funcp) (void*)
The function specified in funcp should accept
one void*-typed input argument and return
void* data.
7. void* argp
The actual argument to be passed to the funcp
function, when the new thread starts executing,
is specified in the argp argument.
Dr. Ming Zhang
thr_create API
(4)
8. long flags
The flags argument value may be zero,
meaning that no special attributes are assigned
to the new thread.
The flags value may using one or more of the
following bit-flags:
THR_SUSPENDED: suspends the execution of
the new thread until another thread calls the
thr_continue function to enable it to execute
THR_NEW_LWP: Create a new LWP
Dr. Ming Zhang
(LightWeight Process) along with the new
thr_create API
(5)
9. thread_t* tid_p);
The new thread ID is return via the tid_p
argument.
If the actual value of the tid_p argument is
assigned NULL, no thread ID is returned.
The thread ID data type is thread_t.
Dr. Ming Zhang
thr_create API
(6 )
# include <thread.h>
........
thread_t tid;
//for return thread ID
void* send_msg(void* ptr);// function executed by
thread
MSGREC * pREC ;
// MSGRC: struct name
int main ( )
{ ..................
thr_create( 0, 0, send_msg, pRec, THR_SUSPENDD,
&tid);
Dr. Ming Zhang
................}
Threads differ from Chile
Processes (1)
z Threads may be managed by either userlevel library functions or the operating
system kernel.
z Child processes as created by the fork
system call are managed by the operating
system kernel
Threads differ from Chile
Processes (2)
z All threads in a process share the same
data and code segment.
z A child process has its own copy of virtual
address space that is separate from its
parent process.
Threads differ from Chile
Processes (3)
z If a thread calls the exit or exec function,
it terminates all the threads in the same
process.
z If a child process calls the exit or exec
function, its parent process is not
affected.
Threads differ from Chile
Processes (4)
z If a thread modifies a global variable in a
process, the changes are visible to other
threads in the same process.
z If a child process modifies a global
variable. the changes are NOT visible to
the parent process.
Benefits of Threads (1)
z Threads is more efficient, and require
much less kernel attention, to create and
manage than do child process, because
threads are managed by user-level library
function and kernel.
z Threads use much less system resources
than do child process, because thread can
share the data in same process.
Benefits of Threads (2)
z Takes less time to create a new thread
than a process
z Less time to terminate a thread than a
process
z Less time to switch between two threads
within the same process
z Since threads within the same process
share memory and files, they can
communicate with each other without
invoking the kernel
Thread Structure
z A thread ID
z A run-time stack
z A set of register (e.g., program counter
and stack pointer)
z A signal mask
z A schedule priority
z A thread-specific storage
Thread Features (1)
z Has an execution state (running, ready,
etc.)
z Saves thread context when not running
z Has an execution stack
z Has some per-thread static storage for
local variables
z Has access to the memory and resources
of its process
y all threads of a process share this
Thread Features (2)
z Suspending a process involves suspending
all threads of the process since all threads
share the same address space
z Termination of a process, terminates all
threads within the process
Synchronization of Using
Threads
z Some synchronization methods are
needed for threads accessing shared data,
because if a thread modifies a global
variable in a process, the changes are
visible to other threads in the same
process.
Benefits of Using Multithreaded
Programming
z It allows a process to make use of any
available multiprocessor hardware on any
system it is run on.
z It allows programmers to structure their
code into independently executable units
and maximize concurrency.
z Threads reduce the need to use fork to
create child processes, thus improving
each process performance (less context
switching).
User-Level Threads
z All thread management is done by the
application
z The kernel is not aware of the existence
of threads
z Thread switching does not require kernel
mode privileges
z Scheduling is application specific
Kernel-Level Threads
z There is no thread management code in
the application area, simply an application
programming interface (API) to the kernel
facility. (Windows NT)
z Kernel maintains context information for
the process and the threads
z Switching between threads requires the
kernel
Combined Approaches for
Threads
z Solaris operating systems provide a
combined ULT/KLT facility
z Thread creation is done completely in
the user space, as is the bulk of
scheduling and synchronization of threads
within an application.
z The multiple ULTs from a single
application are mapped onto smaller or
equal number KLTs.
Solaris Multithreaded Architecture
process 1
process 2
process 3
User
L
L
L
L
Kernel
Hardware
P
P
P
L: Lightweight Process; P: Processor;
:ULT;
: KLT
L
Symmetric
Multiprocessing
z MIMD
A set of processors simultaneously
execute different instruction sequences on
different data sets
z Shared memory multiprocessor
(Distributed Memory MIMD is clusters)
z Kernel can execute on any processor
z Typically each processor does selfscheduling form the pool of available
process or threads
Symmetric Multiprocessor
Organization
Processor
Cache
Processor
Cache
Main
Memory
. .
I/O
Subsystem
.
Processor
Cache
Categories of Computer
Systems (1)
z Single Instruction Single Data (SISD)
y single processor executes a single instruction
stream to operate on data stored in a single
memory
z Single Instruction Multiple Data (SIMD)
y one instruction is executed on a different set
of data by the different processors
Categories of Computer
Systems (2)
z Multiple Instruction Single Data (MISD)
y a sequence of data is transmitted to a set of
processors, each of which executes a
different instruction sequence.
z Multiple Instruction Multiple Data (MIMD)
y a set of processors simultaneously execute
different instruction sequences on different
data sets
Microkernel
z Small operating system core
z Contains only essential operating systems
functions
z Many services traditionally included in the
operating system are now external
subsystems
y
y
y
y
device drivers
file systems
virtual memory manager
windowing system and security services
Benefits of a Microkernel
Organization (1)
z Uniform interface on request made by a
process
y all services are provided by means of
message passing
z Extensibility
y allows the addition of new services
z Flexibility
y existing features can be subtracted
Benefits of a Microkernel
Organization (2)
z Portability
y changes needed to port the system to a new
processor is changed in the microkernel - not
in the other services
z Reliability
y modular design
y small microkernel can be rigorously tested
Benefits of Microkernel
Organization (3)
z Distributed system support
y message are sent without knowing what the
target machine is
z Object-oriented operating system
y components are objects with clearly defined
interfaces that can be interconnected to form
software
Solaris
z Process includes the user’s address space,
stack, and process control block
z User-level threads
z Lightweight processes
A lightweight process can be viewed as a
mapping between ULTs and KLTs.
z Kernel threads
Solaris User Level Threads
Stop
Runnable
Wakeup
Continue
Preempt
Stop
Stopped
Dispatch
Stop
Sleep
Active
Sleeping
Solaris Lightweight
Processes
Timeslice
or Preempt
Running
Wakeup
Dispatch
Stop
Stopped
Runnable
Wakeup
Blocking
System
Call
Continue
Blocked
Stop
Download