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

Lect 4

The document discusses processes and threads in operating systems. It defines processes as the unit of resource allocation and protection, while threads are the unit of scheduling and execution. The document compares single-threaded and multithreaded process models and describes the benefits of using threads.

Uploaded by

라치
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Lect 4

The document discusses processes and threads in operating systems. It defines processes as the unit of resource allocation and protection, while threads are the unit of scheduling and execution. The document compares single-threaded and multithreaded process models and describes the benefits of using threads.

Uploaded by

라치
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

1

Threads CHAPTER 4
2
Processes and Threads

 A process
 Unit of Scheduling/Execution
 Unit of Resource Ownership

 These two characteristics are treated


independently by the operating system
 Unit of Dispatching is referred to as a Thread (or
Lightweight Process)
 Unit of Resource of Ownership is referred to as a
Process (or Task)
3
Multithreading

 Operating system supports multiple threads of execution within a


single process

 MS-DOS supports a single thread

 Some variants of UNIX supports multiple user processes but only


supports one thread per process

 Windows, Solaris and many modern version of UNIX supports multiple


threads
4
Threads and Processes

one process one process


one thread multiple threads

multiple processes multiple processes


one thread per process multiple threads per process

= instruction trace

Figure 4.1 Threads and Processes


5
Processes

 A process in a multithreaded environment

 defined as the unit of resource allocation and a unit of


protection

 a virtual address space which holds the process image

 Protected access to processors, other processes, files,


and I/O resources
6
Threads

► Have an execution state (running, ready, etc.)


► Saves thread context when not running
► Have an execution stack
► Have some per-thread static storage for local
variables
► Have access to the memory and resources of its
process
► all threads of a process share this
7
Single Threaded and
Multithreaded Process Models
Single-Threaded Multithreaded
Process Model Process Model
Thread Thread Thread
Thread Thread Thread
Process User Control Control Control
Control Stack Block Block Block
Block

Process User User User


User Kernel Stack Stack Stack
Control
Address Stack
Block
Space

User Kernel Kernel Kernel


Address Stack Stack Stack
Space

Figure 4.2 Single Threaded and Multithreaded Process Models


8
Remote Procedure Call Using
Threads
Time

RPC RPC
Request Request

Process 1

Server Server

(a) RPC Using Single Thread

RPC Server
Request

Thread A (Process 1)

Thread B (Process 1)

RPC
Request Server

(b) RPC Using One Thread per Server (on a uniprocessor)

Blocked, waiting for response to RPC


Blocked, waiting for processor, which is in use by Thread B
Running

Figure 4.3 Remote Procedure Call (RPC) Using Threads


9
Benefits of Threads

► Takes less time to create a new thread than a process

► Less time to terminate a thread than a process

► Less time to switch between two threads within the


same process

► Since threads within the same process share memory


and files, they can communicate with each other
without invoking the kernel
10
Thread States

 Spawn : Spawn another thread

 Ready

 Running

 Blocked

 Finish : Deallocate register context and stacks


11
Thread State vs. Process State

 Some states are process-level concepts

 Suspending a process involves suspending all threads of


the process since all threads share the same address
space

 Termination of a process, terminates all threads within


the process
12
User-Level Threads

 Thread library contains code for


creating and destroying threads,
for passing messages and data
between threads, for scheduling
User
thread execution, and for saving Threads
Space
Library
and restoring thread contexts
Kernel
 Any application can be Space
programmed to be multithreaded
by using a thread library P
 The kernel is not aware of the
existence of threads
(a) Pure user-level

User-level thread Kernel-level thread


13
Relationships between User-Level
Thread States and Process States

(a) (b)
Thread 1 Thread 2 Thread 1 Thread 2
Ready Running Ready Running Ready Running Ready Running

Blocked Blocked Blocked Blocked

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

Blocked Blocked Blocked Blocked

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
14
Kernel-Level Threads

 All of the work of thread


management is done by the kernel
 The kernel can simultaneously
Threadsthreads from User
schedule multiple the User
Library Space Space
same process on multiple
processors Kernel Kernel
Space Space
 If one thread in a process is
blocked, the kernel can schedule
P
another thread of the same
process P
 Switching between threads
requires the kernel
(a) Pure user-level (b) Pure kernel-level

User-level thread Kernel-level thread P Process


15
User-Level Threads vs. Kernel-
Level Threads

User-level threads Kernel-level threads


 Thread switching does not  Simultaneously schedule
require kernel mode multiple threads on multiple
privileges processors
 Scheduling can be  If one thread is blocked,
application specific another thread can be
 ULTs can run on any OS scheduled
 Kernel routines themselves
can be multithreaded
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)
16
Combined Approaches for
Threads

 The multiple ULTs from a single


application are mapped onto some
(smaller or equal) number of KTLs
ds User User Threads User
ry Space Space Library Space
 Significant speedup by using KLT
(running
Kernel in parallel on multipleKernel Kernel
processors)
Space and additional Space Space
significant speedup by using ULT
P (reducing unnecessary mode
switching)
P P P

) Pure user-level (b) Pure kernel-level (c) Combined

vel thread Kernel-level thread P Process


17
Relationship Between Threads and
Processes

Threads:Processes Description Example Systems


1:1 Each thread of execution is a Traditional UNIX
unique process with its own implementations
address space and resources.
M:1 A process defines an address Windows NT, Solaris, Linux,
space and dynamic resource OS/2, OS/390, MACH
ownership. Multiple threads
may be created and executed
within that process.
1:M A thread may migrate from Ra (Clouds), Emerald
one process environment to
another. This allows a thread
to be easily moved among
distinct systems.
M:N Combines attributes of M:1 TRIX
and 1:M cases.
18
Windows Process and Thread

 Implemented as objects
 A process can be created as a new process or as a copy of
an existing process
 An executable process may contain one or more threads
 A Windows process must contain at least one thread to execute
 That thread may then create other threads
 A thread is a dispatchable unit of work
 Both process and thread objects have built-in synchronization
capabilities
19
Windows Process Object Attributes

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.

Default processor affinity The default set of processors on which the process's threads can
run.

Quota limits The maximum amount of paged and nonpaged system memory,
paging file space, and processor time a user's processes can use.

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
the process's threads have performed.

VM operation counters Variables that record the number and types of virtual memory
operations that the process's threads have performed.

Exception/debugging ports Interprocess communication channels to which the process


manager sends a message when one of the process's threads causes
an exception. Normally, these are connected to environment
subsystem and debugger processes, respectively.

Exit status The reason for a process's termination.


20
Windows Thread Object Attributes

Thread ID A unique value that identifies a thread when it calls a server.

Thread context The set of register values and other volatile data that defines the
execution state of a thread.

Dynamic priority The thread's execution priority at any given moment.

Base priority The lower limit of the thread's dynamic priority.

Thread processor affinity The set of processors on which the thread can run, which is a
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.

Alert status A flag that indicates whether a waiting thread may execute an
asynchronous procedure call.

Suspension count The number of times the thread's execution has been suspended
without being resumed.

Impersonation token A temporary access token allowing a thread to perform operations


on behalf of another process (used by subsystems).

Termination port An interprocess communication channel to which the process


manager sends a message when the thread terminates (used by
subsystems).

Thread exit status The reason for a thread's termination.


21
Windows Thread States

Runnable
Pick to Standby
Run Switch

Preempted
Ready Running

Resource Unblock/Resume Terminate


Resource Available Block/
Available
Suspend

Transition Unblock
Waiting Terminated
Resource Not Available
Not Runnable

Figure 4.11 Windows Thread States


22
Solaris Processes and Threads

Process

user user
thread thread
Lightweight Lightweight
process (LWP) process (LWP)
syscall() syscall()

Kernel Kernel
thread thread

System calls
Kernel
Hardware

Figure 4.12 Processes and Threads in Solaris


23
Solaris Thread States

IDLE PINNED

thread_create() intr()

swtch()
syscall()
RUN ONPROC SLEEP
preempt()

wakeup()

STOP ZOMBIE FREE


prun() pstop() exit() reap()

Figure 4.14 Solaris Thread States


24
Linux Tasks

 A process, or task, in Linux is represented by a


task_struct data structure

 State
 Scheduling information
 Identifiers
 Interprocess communication
 And others
25
Linux Threads

Linux does A new The clone()


not process is call creates
recognize a created by separate
distinction copying the stack
between attributes of spaces for
threads and the current each
processes process process

User-level The new


threads are process can
mapped be cloned
into kernel- so that it
level shares
processes resources
26
Linux Process/Thread Model

Stopped

signal signal

Running
State
termination
creation
Ready scheduling Executing Zombie

event
signal
or
event
Uninterruptible

Interruptible

Figure 4.15 Linux Process/Thread Model

You might also like