Processes & Threads

buninnateSoftware and s/w Development

Nov 18, 2013 (3 years and 9 months ago)

95 views

Processes & Threads


Resource ownership


Process or Task


Scheduling/execution


Thread or lightweight process

One process,

one thread (MS
-
DOS)

One process,

multiple threads (Java Runtime)

Multiple processes,

multiple threads (W2K, Solaris, Linux)

Multiple processes,

one thread per process (Unix)

Processes & Threads (continue)


In a multithreaded environment, the followings are
associated with a process:


Address space to hold the process image


Protected access to processors, other processes (IPC), files, and I/O
resources (devices & channels)


Within a process, there may be one or more threads, each
with the following:


A thread execution state (Running, Ready, etc)


A saved context when not running


a separate program counter


An execution stack


Some static storage for local variables for this thread


Access to memory and resources of its process, shared with all
other threads in that process (global variables)


Single Threaded and Multithreaded Process Models

Key Benefits of Threads


It takes far less time to create a new thread in an existing
process than to create a brand
-
new process. (10X)


It takes less time to terminate a thread than a process.


It takes less time to switch between two threads within the
same process.


Threads enhance efficiency in communication between
different executing programs


since threads within the
same process share memory and files, they can
communicate with each other without invoking the kernel.

Key Benefits of Threads (continue)

Key Benefits of Threads (continue.)

Thread Functionality


Thread States


Running, Ready, and Blocked


Suspend is at process
-
level


Four Basic Thread Operations for a change in Thread State


Spawn
: threads can be spawned from a process or a thread within the same
process which provide an instruction pointer and its arguments. The new thread
will have its own register context and stack space and be out in the Ready Queue.


Block
: When a thread needs to wait for an event, it will block (saving its user
registers, program counter, and stack pointer). The processor may now turn to
execute another ready thread.


Unblock
: When the waited event occurs, the waiting thread is moved to the Ready
Queue.


Finish
: When the thread completes, its register context and stacks are deallocated.


Thread Synchronization


To be covered in Chapter 5 & 6.

User
-
Level and Kernel
-
Level Threads

User
-
Level Threads


Thread management is done by the application and the kernel is
not aware of the existence of threads


Advantages:


Thread switching does not require user/kernel mode
switching.


Thread scheduling can be application specific.


ULTs can run on any O.S through a thread library.


Disadvantages:


When a ULT executes a system call, not only the thread is
blocked, but all of the threads within the process are blocked.


Multithreaded application cannot take advantage of
multiprocessing since kernel assign one process to only one
processor at a time.

Kernel
-
Level Threads


Thread management is done by the kernel. No threaded
management code in the application


W2K, Linux.


Advantages:


The kernel can scheduling multiple threads from the same
process on multiple processors.


If one thread is blocked, the kernel can schedule another
thread on the same process.


Kernel routines themselves can be multithreaded.


Disadvantages:


Thread switching within the same process requires kernel
mode switching

User and Kernel
-
Level Threads Performance


Performance


Null fork
: the time to create, schedule, execute, and complete a process/thread that
invokes the null procedure.


Signal
-
Wait
: the time for a process/thread to signal a waiting process/thread and
then wait on a condition.


Procedure call: 7us

Kernel Trap:17us


Thread Operation Latencies

Operation


ULT

KLT

Process

Null fork


34

948

11,300

Signal Wait


37

441

1,840


Observation


While there is a significant speedup by using KLT multithreading compared to single
-
threaded
processes, there is an additional significant speedup by using ULTs.


However, whether or not the additional speedup is realized depends on the nature of the
applications involved.


If most of the thread switches require kernel mode access, then ULT may not perform much better
than KLT.