0% found this document useful (0 votes)
29 views31 pages

CH 4 Threads

1) A thread is the basic unit of CPU utilization and processing. It is lighter weight than a process. 2) Using multiple threads allows an application to divide work into independent parts that can run simultaneously, improving responsiveness. Threads also allow for more efficient resource sharing than separate processes. 3) Modern multicore systems allow threads to literally run in parallel by distributing threads across multiple CPU cores. This enables true simultaneous execution that improves performance over concurrent threading on a single core. Proper multithreaded programming is necessary to take advantage of multicore architectures.

Uploaded by

meer hushyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views31 pages

CH 4 Threads

1) A thread is the basic unit of CPU utilization and processing. It is lighter weight than a process. 2) Using multiple threads allows an application to divide work into independent parts that can run simultaneously, improving responsiveness. Threads also allow for more efficient resource sharing than separate processes. 3) Modern multicore systems allow threads to literally run in parallel by distributing threads across multiple CPU cores. This enables true simultaneous execution that improves performance over concurrent threading on a single core. Proper multithreaded programming is necessary to take advantage of multicore architectures.

Uploaded by

meer hushyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Chapter 4: Threads

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 4: Threads

■ 1.1 Overview
■ 1.2 Multicore Programming

OBJECTIVES
■ To introduce the notion of a thread—a fundamental unit of
CPU utilization that forms the basis of multithreaded computer
systems

Operating System Concepts – 9th Edition 4.2 Silberschatz, Galvin and Gagne ©2013
Operating Systems

Thread
light weight is threds

• Thread: a basic unit of CPU utilization; heavy weight is proccess

• It composes of:
» Program counter
process can devide to number of threads
» Set of registers
local data saved in stack » Stack savind address temprory adres

• A thread has its own thread ID.


• Thread shares the following items with other threads that are
created in the same process:
» code section
Chapter 2

» data section
» Other operating system resources

3
4.1.1 Motivation

■ Most modern applications are multithreaded


■ Threads run within application
■ Multiple tasks with the application can be
implemented by separate threads
• Update display inside one program there are many threds

• Fetch data
• Spell checking
• Answer a network request
■ Process creation is heavy-weight while thread
creation is light-weight
■ Can simplify code, increase efficiency
■ Kernels are generally multithreaded
Operating System Concepts – 9th Edition 4.4 Silberschatz, Galvin and Gagne ©2013
Operating Systems

• Multiple actions executing simultaneously


a) Heavyweight process (traditional process) Processes with
• Owns the resources single proccess multiple
subprocesses
• Passive element
b) Lightweight process (Thread)
• Uses CPU and scheduled for execution Proces with
multiple threads
• Active element
c) Multithreaded applications programs
• Contain several threads running at one time
• Same or different priorities
• Examples:
Chapter 2

– Web browsers and time-sharing systems

5
Operating Systems

Components of Threads vs. Process


each threads has these items

• Items shared by all threads in a process


Chapter 2

• Items private to each thread

6
Single and Multithreaded Processes
stack and rigister can not be shared by threads

Figure 4.1

Operating System Concepts – 9th Edition 4.7 Silberschatz, Galvin and Gagne ©2013
Multithreaded Server Architecture

Figure 4.2 Refer to page164-165

Operating System Concepts – 9th Edition 4.8 Silberschatz, Galvin and Gagne ©2013
Operating Systems

Applications
• Use by many software packages that run on modern desktop
PCs.

Word Processor a thread for displaying graphic, another


thread for reading keystrokes
Web browser one thread displays image or text, while
another thread retrieves data from the
network
Web server when server receives a request, a thread
Chapter 2

will be created to service that request

9
4.1.2 Benefits
when on of the task stoped all the other tasks will be stop

■ Responsiveness – may allow continued execution if part of


process is blocked, especially important for user interfaces.

■ Resource Sharing – threads share resources of process,


easier than shared memory or message passing.

■ Economy – cheaper than process creation, thread


switching lower overhead than context switching.

■ Scalability – process can take advantage of multiprocessor


architectures.

Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
4.2 Multicore Programming

■ Multi-CPU systems. Multiple CPUs are placed in the


computer to provide more computing performance.
■ Multicore systems. Multiple computing cores are placed on
a single processing chip where each core appears as a
separate CPU to the operating system
■ Multithreaded programming provides a mechanism for more
efficient use of these multiple computing cores and improved
concurrency.
■ Consider an application with four threads.
• On a system with a single computing core, concurrency means
that the execution of the threads will be interleaved over time.
• On a system with multiple cores, however, concurrency means
that some threads can run in parallel, because the system can
assign a separate thread to each core

Operating System Concepts – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
Multicore Programming (Cont.)

■ There is a fine but clear distinction between concurrency


and parallelism.

■ A concurrent system supports more than one task by


allowing all the tasks to make progress.

■ In contrast, a system is parallel if it can perform more


than one task simultaneously.

■ Thus, it is possible to have concurrency without


parallelism

Operating System Concepts – 9th Edition 4.12 Silberschatz, Galvin and Gagne ©2013
Multicore Programming (Cont.)

■ Types of parallelism
• Data parallelism – distributes subsets of the
same data across multiple cores, same operation
on each
• Task parallelism – distributing threads across
cores, each thread performing unique operation

■ As number of threads grows, so does architectural


support for threading
• CPUs have cores as well as hardware threads
• Consider Oracle SPARC T4 with 8 cores, and 8
hardware threads per core

Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
Multicore Programming

■ Multicore or multiprocessor systems are placing pressure


on programmers. Challenges include:
• Dividing activities
• Balance
• Data splitting
• Data dependency
• Testing and debugging
■ Parallelism implies a system can perform more than one
task simultaneously
■ Concurrency supports more than one task making progress
• Single processor / core, scheduler providing concurrency

Operating System Concepts – 9th Edition 4.14 Silberschatz, Galvin and Gagne ©2013
Concurrency vs. Parallelism
run program in a single core

■ Concurrent execution on single-core system:

■ Parallelism on a multi-core system:


multy core process

Figure 4.4

Operating System Concepts – 9th Edition 4.15 Silberschatz, Galvin and Gagne ©2013
Operating Systems

Concurrency

• In a uniprocessor multiprogramming system:

– Execution of processes / threads are interleaved


in time to yield the appearance of simultaneous
execution;

– Known as concurrent processes / threads


execution;
Chapter 3

– Note that actual parallel processing does not


occur;

16
Operating Systems

Interleaving

P1
P2

P3

Time
Chapter 3

17
Operating Systems

• Multiprocessor system:

– Overlapping and interleaving of


processes execution;

– Parallel processing sometime occurs;

P1
P2
Chapter 3

P3
Time
18
Operating Systems

• Concurrency arises / occurs in three different contexts*:

Multiple • Multiprogramming was invented to


applications allow processing time to be
dynamically shared among a number
of active applications.
Structured • Application can be a set of concurrent
application processes.
Operating-system • Operating system is a set of
structure processes or threads.
• Example: Solaris and Linux (MS-
Chapter 3

DOS – no concurrency)
__________________________________
* Stalling, W. Operating Systems: Internals and Design Principles (5th Edition). Prentice Hall. (2005)
19
Operating Systems

• Interleaving and overlapping execution of processes


can be viewed as examples of concurrent
processing;
→ Need control synchronization and data access
synchronization (Chapter 6).

• Both present the same problems such as in sharing


global resources. Example:
– Global variables;
– Shared files or data bases;
Chapter 3

– Managing the allocation of resources optimally such


as the request use of a particular I/O device (may
lead to deadlock);
20
Operating Systems

Another Example of Concurrency

• Two or more threads need access to a shared


resource that must be used by only one thread at
a time
– Eg. When one thread is writing to a file, a second
thread must be prevented from writing to the file at
the same time.
Chapter 3

21
Operating Systems

Concurrent Programming

• Concurrent processing system

– One job uses several processors (multiprocessors)

– Executes sets of instructions in parallel

– Requires programming language and computer


system support
Chapter 3

22
Operating Systems

Applications of Concurrent
Programming
A = 3 * B * C + 4 / (D + E) ** (F – G)

Uniprocessor
(Sequential Computation)
Chapter 3

23
Operating Systems

A = 3 * B * C + 4 / (D + E) ** (F – G)

Steps Operation
1 A = 3 * B * C + 4 / (D + E) ** T1
2 A = 3 * B * C + 4 / T2 ** T1
3 A = 3 * B * C + 4 / T1
4 A = 3 * B * C + T2
5 A = T1 * C + T2
A = T1 + T2
Chapter 3

6
Uniprocessor
7 A (Sequential Computation)

24
Operating Systems

Multiprocessor
(Concurrent processing)

A = 3 * B * C + 4 / (D + E) ** (F – G)
Chapter 3

25
Operating Systems

1 2 3

A = 3 * B * C + 4 / (D + E) ** (F – G)

Steps Processor Operation


1 1 A = T1 * C + 4 / (D + E) ** (F-G)
2 A = T1 * C + 4 / T2 ** (F-G)
3 A = T1 * C + 4 / T2 ** T3
2 1 A = T4 + 4 / T2 ** T3
2 A = T4 + 4 / T5
Chapter 3

3 1 A = T4 + T1
Multiprocessor
4 1 A (Concurrent processing)

26
Operating Systems

EXAMPLE:

Screenshot from
MacBook Pro
Chapter 2

27
Chapter 2 Operating Systems

28
Chapter 2 Operating Systems

29
EXERCISE
From Silberchatz, Operating System Concepts
Chapter 4 Exercises

❑ Odd number Matric card – 4.3, 4.5


❑ Even number Matric card – 4.2, 4.4

❑ Due date: Submit to e-learning 5 days after the


end of the lecture.

Operating System Concepts – 9th Edition 3.30 Silberschatz, Galvin and Gagne ©2013
End of Chapter 4

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013

You might also like