Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views32 pages

Threads

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 32

CENG 302

OPERATING SYSTEMS

Dr. Mansur Alp TOÇOĞLU


In this chapter,
• Thread
• Benefits of Threads
• Multi-Core Programming
• Amdahl law
• Parallel Working Types
• Thread Libraries
• Pthreads
topics will be discussed.

2
THREAD
• A thread,
• Thread ID
• Program counter,
• A group of register and
• Includes a stack structure.
• Threads commonly use the program code, data part,
operating system resources such as files.
• Classic processes have a single thread.
• If a process has more than one thread, it can
simultaneously do more than one task.
• Most of the software applications that work on modern
computers are working multithread on today.
3
THREAD (CONTINUES…)

• Applications are developed in the form of a single


process with numerous threads.
• A web browser,
• can transfer data with a thread,
• can display data on the screen with another thread.
• A word processing application (Microsoft Word),
• can fetch data from the keyboard with a thread,
• can perform spell check with a thread,
• can edit the screen with another thread.
• Most operating systems are Kernel Multi-Threaded.
4
THREAD (CONTINUES…)

• Each thread has its own components without sharing.


Single and Multi-Threaded Process:

code data files code data files

registers stack registers registers registers

stack stack stack

thread thread

single-threaded process multithreaded process


5
MULTI-THREAD SERVER
ARCHITECTURE
• If a web server process works multithreaded, a
separate thread is created for each incoming request
and the process continues to listen to the port.
• It takes time and costly to create a separate process
for each request.
(2) create new
(1) request thread to service
the request
client server thread

(3) resume listening


for additional
client requests 6
BENEFITS OF THREADS
• We can collect the benefits of the use of thread in 4
main headings:
1. Responsiveness: In interactive applications, the user
continues to work, even if some blocked, locked
or long -term operation is carried out, the
interaction with the user continues to work. The
system’s responsiveness feature is increased.
(Server Architecture)
2. Resource sharing: Processes can share their
resources through Shared Memory or Message
Passing techniques. Threads can share the
memory area and other resources of the
process they belong to.
7
BENEFITS OF THREADS
(CONTINUES…)

3. Economy: When creating a process, memory and


resource allocation is a high cost. However, as threads
share the resources of the process they belong to,
the context switch task is executed at a lower
cost.(In the Solaris operating system, thread creation is 30
times faster and the context switch task in threads is 5
times faster.)
4. Scalibility: In multi-processing architectures, threads
can work simultaneously on different core. However,
the process with a single thread structure can only work
on a processor.
Scalability is the measure of a system's ability to increase or decrease in
performance and cost in response to changes in application and system
processing demands.

8
MULTI-CORE PROGRAMMING

• For an application with 4 threads running on a core,


concurrent operation means running the threads at
certain intervals.
• Concurrent operation in multi-core systems means
parallel operation of threads by assigning a thread
to each core.
• Parallelism refers to performing multiple tasks
simultaneously.
• Concurrency allows multiple tasks to progress together by
switching between them in short intervals.

9
MULTI-CORE PROGRAMMING
(CONTINUES…)

Concurrent execution on single-core system:

single core T1 T2 T3 T4 T1 T2 T3 T4 T1 …
time

Parallelism on a multi-core system:

core 1 T1 T3 T1 T3 T1 …

core 2 T2 T4 T2 T4 T2 …
time
10
MULTI-CORE PROGRAMMING
(CONTINUES…)

• CPU schedulers
• Ensure progress (continuity) by constantly switching
between processes and
• give the impression of parallelism.
• In fact, these processes
• They work concurrently.
• They do not work in parallel.

11
AMDAHL LAW
• Amdahl rule: The performance increase in a system according
to the number of cores is expressed as follows:
• S: refers to the ratio of the part that must be serially operated in
the application (non-parallel), and N: refers to the number of cores.

• In an application, if 75% runs parallel and 25% serial (S=0.25),


• When this application is executed on a system with 2 cores (N=2), the speed
increases by 1.6 times.
• When the number of cores is 4, a 2.28 times speed increase is achieved.
• As the number of cores goes to infinity, the speed increases towards
(1/S).

12
TYPES OF PARALLEL
WORKING
• Basically there are two different types of parallel
operation.
• Data parallelism
• Task parallelism

1. Data Parallelism: It focuses on distributing the


parts of the same data set to cores and executing the
same type of operations simultaneously.
• For example: If 2 cores are to be used to sum an array
with N elements, [0]..[(N/2)-1] element is collected in the 1st
core, [N/2]..[N-1] element is collected in the 2nd core.

13
TYPES OF PARALLEL
WORKING ( C O N T I N U E S … )
2. Task Parallelism: It focuses on distributing tasks
(threads) to cores.
• Each thread performs a separate operation. Different
threads can work on the same data or different data.
• For example: Threads that perform different statistical
calculations on the same array elements using the same data
but running on different cores.
• Generally, the most applications use hybrid types of
both data and task parallel operation.

14
THREAD LIBRARIES

• The Thread Library provides API to create and manage the


programmer.
• Two different strategies are used to create multiple thread:
• Asynchronous Threading: Parent continues to work
simultaneously when it creates a new child thread. Web Server
Architecture…
• Synchronous Threading: Parent stops its work when child thread
creates and continues to work when all child threads end (Fork-Join
Strategy).
• Asynchronous threading:
• When data sharing is low among threads,
• Synchronous threading:
• Threads are used when data sharing is high.

15
THREAD LIBRARIES ( C O N T I N U E S … )

• Today, three basic thread libraries are used:


1. POSIX Pthreads
2. Windows threads
3. Java threads

16
PTHREADS

• Pthreads is the API defined by the IEEE1003.1c


standard for creating and managing threads.
• Linux, Unix, Mac OS X and Solaris operating systems
use the Pthreads standard.
• Windows does not support the Pthreads standard.
• In the Pthreads standard, all threads are created as
separate functions.
• All threads share the data defined in the global
scope.

17
PTHREADS EXAMPLE 1

18
PTHREADS EXAMPLE 1
(CONTINUES…)

19
PTHREADS EXAMPLE 1 DETAILED
DESCRIPTION
PTHREADS EXAMPLE 2

• In the previous example, a thread was created. Multiple


threads can be created as in the example below.

21
.NET THREADS EXAMPLE 1

22
.NET THREADS EXAMPLE 1

23
PTHREADS EXAMPLE 1
(CONTINUES…)

• compile
o gcc -pthread -o SumThread SumThread.c
• execute
o ./SumThread 1000

24
PTHREADS EXAMPLE 1
(CONTINUES…)

• Let's change the function as follows. Let's add


sleep().

• gcc -pthread -o SumThread SumThread.c


• ./SumThread 1000
• Let's open the second terminal tab and run pstree
-p.
25
PTHREADS EXAMPLE 1
(CONTINUES…)

• In Linux, thread gets PID. The process is


treated similarly.

26
PTHREADS EXAMPLE 1
(CONTINUES…)

• What happens if we close the line below?


pthread_join(tid,NULL);
• Let's undo the previous change and remove
sleep().
• Let's close the pthread_join line.
• gcc -pthread -o SumThread SumThread.c
• ./SumThread 1000
HAS THE TOTAL RESULT CHANGED?
• Then, let's print the sum value to the screen in
the thread function. What prints on the screen?
• Then add pthread_exit(NULL); it to the last
line of the main thread. What does the screen
print? 27
PTHREADS EXAMPLE 2

28
PTHREADS EXAMPLE 2
(CONTINUES…)

• Compile and execute


• gcc -pthread -o TList TList.c
• ./TList

29
PTHREADS EXAMPLE 2
(CONTINUES…)

• Change

• Compile and execute


• gcc -pthread -o TList TList.c
• ./TList 30
PTHREADS EXAMPLE 2
(CONTINUES…)

• Sırayla join oldular ve sırayla işlerini


bitirdiler.

31
RESOURCES
• Textbook:
• Operating System Concepts, Ninth Edition, Abraham
Silberschatz, Peter Bear Galvin, Greg Gagne

32

You might also like