Understanding Processes and Threads
Understanding Processes and Threads
Systems:
Internals
and Design
Principles Chapter 4
Threads
Ninth Edition
By William Stallings
the concept of a
one thread multiple threads
thread is not
recognized, is
referred to as a
single-threaded multiple processes multiple processes
approach
one thread per process multiple threads per process
n A Java run-time
environment is an
example of a multiple processes
one thread per process
multiple processes
multiple threads per process
process with
multiple threads
Figure 4.1 Threads and Processes
RPC RPC
Request Request
Process 1
Server Server
RPC Server
Request
Thread A (Process 1)
Thread B (Process 1)
RPC
Request Server
Thread A (Process 1)
Thread B (Process 1)
User Level
Thread (ULT)
Kernel level
Thread (KLT)
Process B Process B
Ready Running Ready Running
Blocked Blocked
(c) (d)
Thread 1 Thread 2 Thread 1 Thread 2
Ready Running Ready Running Ready Running Ready Running
Process B Process B
Ready Running Ready Running
Blocked Blocked
Colored state
is current state
Figure 4.6 Examples of the Relationships Between User-Level Thread States and Process States
©
2017
Pearson
Educa2on,
Inc.,
Hoboken,
NJ.
All
rights
reserved.
ULTs
can run
Scheduling can be
application specific on any
OS
Writing an application as
multiple processes rather than
multiple threads
• However, this approach eliminates
the main advantage of threads
Kernel-Level
Operation User-Level Threads Threads Processes
Null Fork 34 948 11,300
Signal Wait 37 441 1,840
Table 4.1
Thread and Process Operation Latencies (µs)
Table 4.2
Relationship between Threads and Processes
relative speedup
6
5%
10%
4
0%
8
2
2%
relative speedup
6
5%
0
1 2 3 4 5 6 7 8
10% number of processors
4 (a) Speedup with 0%, 2%, 5%, and 10% sequential portions
2 2.5
2.0 5%
0 10%
1 2 3 4 5 6 7 8 15%
20%
relative speedup
number of processors 1.5
(a) Speedup with 0%, 2%, 5%, and 10% sequential portions
1.0
2.5 0.5
5% 0
2.0 1 2 3 4 5 6 7 8
10%
15% number of processors
20%
relative speedup
1.0
Figure 4.7 Performance Effect of Multiple Cores
©
2017
Pearson
Educa2on,
Inc.,
Hoboken,
NJ.
All
rights
reserved.
0.5
64
Oracle DSS 4-way join
TMC data mining
DB2 DSS scan & aggs
Oracle ad hoc insurance OLTP
48
g
lin
ca
ts
ec
scaling
rf
pe
32
16
0
0 16 32 48 64
number of CPUs
§ Multiprocess applications
§ Characterized by the presence of many single-threaded
processes
§ Java applications
§ All applications that use a Java 2 Platform, Enterprise
Edition application server can immediately benefit from
multicore technology
§ Multi-instance applications
§ Multiple instances of the application in parallel
©
2017
Pearson
Educa2on,
Inc.,
Hoboken,
NJ.
All
rights
reserved.
Valve Game Software
Render
Scene List
Particles
Character
Bone Setup
Draw
Etc.
Available
Handle Table objects
Handle1 Thread x
Handle2 File y
Handle3 Section z
Processes Threads
• An entity • A dispatchable
corresponding unit of work
to a user job or that executes
application that sequentially and
owns resources is interruptible
©
2017
Pearson
Educa2on,
Inc.,
Hoboken,
NJ.
All
rights
reserved.
Process ID A unique value that identifies the process to the operating system.
Security descriptor Describes who created an object, who can gain access to or use the
object, and who is denied access to the object.
Base priority A baseline execution priority for the process's threads. Table 4.3
Default processor affinity The default set of processors on which the process's threads can
run.
Windows
Quota limits The maximum amount of paged and nonpaged system memory,
paging file space, and processor time a user's processes can use. Process
Execution time The total amount of time all threads in the process have executed.
I/O counters Variables that record the number and type of I/O operations that
Object
the process's threads have performed.
VM operation counters Variables that record the number and types of virtual memory
Attributes
operations that the process's threads have performed.
Thread context The set of register values and other volatile data that defines the
execution state of a thread.
Thread processor affinity The set of processors on which the thread can run, which is a Windows
subset or all of the processor affinity of the thread's process.
Thread execution time The cumulative amount of time a thread has executed in user mode
and in kernel mode. Thread
Alert status A flag that indicates whether a waiting thread may execute an
asynchronous procedure call. Object
Suspension count The number of times the thread's execution has been suspended
without being resumed.
Attributes
Impersonation token A temporary access token allowing a thread to perform operations
on behalf of another process (used by subsystems).
Achieves concurrency
without the overhead of
using multiple processes
Preempted
Ready Running
Transition Unblock
Waiting Terminated
Resource Not Available
Not Runnable
user user
thread thread
Lightweight Lightweight
process (LWP) process (LWP)
syscall() syscall()
Kernel Kernel
thread thread
System calls
Kernel
Hardware
LWP 2 LWP 1
LWP ID LWP ID
Priority Priority
Signal Mask Signal Mask
Registers Registers
STACK STACK
n The priority of this LWP and hence the kernel thread that supports it
n A signal mask that tells the kernel which signals will be accepted
n The kernel stack for this LWP, which includes system call arguments,
results, and error codes for each call level
thread_create() intr()
swtch()
syscall()
RUN ONPROC SLEEP
preempt()
wakeup()
signal signal
Running
State
termination
creation
Ready scheduling Executing Zombie
event
signal
or
event
Uninterruptible
Interruptible
Broadcast Content
Receiver Provider
Application
Activity Service
Dedicated
Virtual Machine
n The window typically fills the screen, but may be smaller than the screen and
float on top of other windows
n The activities are arranged in a last-in-first-out stack in the order in which each
activity is opened
n If the user switches to some other activity within the application, the new
activity is created and pushed on to the top of the back stack, while the
preceding foreground activity becomes the second item on the stack for this
application
©
2017
Pearson
Educa2on,
Inc.,
Hoboken,
NJ.
All
rights
reserved.
Activity
launched
onCreate()
Entire
Lifetime
onStart() onRestart()
User navigates
to the activity
Visible
Lifetime
onResume()
App process
killed Foreground
User returns
Lifetime to the activity
Resumed
Apps with higher
priority need memory
onPause()
Paused
User navigates
to the activity
onStop()
Stopped
onDestroy()
Activity
shut down