0% found this document useful (0 votes)
8 views25 pages

Understanding Threads in Operating Systems

This document discusses threads and threading concepts. It begins with definitions of threads and examples of how threads are used in applications like web servers. It then covers advantages of using threads like responsiveness and resource sharing. The document also discusses multicore programming challenges and types of parallelism. Finally, it examines threading models, thread operations and states, programming threads in POSIX, Windows and Java, and other threading concepts like implicit threading and thread-local storage.

Uploaded by

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

Understanding Threads in Operating Systems

This document discusses threads and threading concepts. It begins with definitions of threads and examples of how threads are used in applications like web servers. It then covers advantages of using threads like responsiveness and resource sharing. The document also discusses multicore programming challenges and types of parallelism. Finally, it examines threading models, thread operations and states, programming threads in POSIX, Windows and Java, and other threading concepts like implicit threading and thread-local storage.

Uploaded by

7240707
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Amirkabir University of

Technology
(Tehran Polytechnic)
Department of Computer Engineering and Information
Technology

Threads (‫ نخ‬،‫)ریسمان‬

Hamid R. Zarandi
h_zarandi@[Link]

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2018/10/16


Operating Systems

Definition
A basic unit of CPU utilization
o Private: Thread ID, program counter, register set, stack
o Shared: code section, data section, OS resources (IO & file)

Examples:
o Web browsers
o Word processors
o Database engines
o RPC!

Versus Process:
o Time consuming
o Resource intensive

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 2


Operating Systems

Web server application

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 3


Operating Systems

Advantages of using threads


Responsiveness
o Allowing a program to continue running even part of it is blocked or
lengthy

Resource sharing
o Memory, resources

Economy
o Fast

Scalability
o Threads may be running in parallel on processing cores
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 4
Operating Systems

Multicore programming
Multicore or multiprocessor systems putting pressure on programmers,
challenges include:
o Dividing activities
o Balance
o Data splitting
o Data dependency
o Testing and debugging

Parallelism implies a system can perform more than one task simultaneously

Concurrency supports more than one task making progress


o Single processor / core, scheduler providing concurrency

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 5


Operating Systems

Multicore programming
Concurrent execution on single-core system:

Parallelism on a multi-core system:

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 6


Operating Systems

Types of parallelism
Types of parallelism
o Data parallelism
o Task parallelism

As # of threads grows, so does architectural support for threading


o CPUs have cores as well as hardware threads
o Consider Oracle SPARC T4 with 8 cores, and 8 hardware threads per core

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 7


Operating Systems

RPC using threads

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 8


Operating Systems

User threads and kernel threads


User threads - management done by user-level threads library
Three primary thread libraries:
o POSIX Pthreads (kernel-level lib, user-level lib)
o Windows threads (kernel-level lib)
o Java threads (kernel-level lib)

Kernel threads - Supported by the Kernel

Asynchronous vs. synchronous threading


o Parent & child threads

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 9


Operating Systems

User level vs. kernel level threads

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 10


Operating Systems

Multithreading models
Many-to-One

One-to-One

Many-to-Many

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 11


Operating Systems

Many-to-one
 Many user-level threads mapped to single kernel thread

 One thread blocking causes all to block

 Multiple threads may not run in parallel on multicore


system because only one may be in kernel at a time

 Few systems currently use this model

 Examples:
o Solaris Green Threads
o GNU Portable Threads

 Used in very few systems.

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 12


Operating Systems

One-to-one
Each user-level thread maps to kernel thread

Creating a user-level thread creates a kernel


thread

More concurrency than many-to-one

Number of threads per process sometimes


restricted due to overhead

Examples
o Windows
o Linux
o Solaris 9 and later

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 13


Operating Systems

Many-to-many model
Allows many user level threads to be
mapped to many kernel threads

Allows the operating system to create a


sufficient number of kernel threads

Two-level Model:
o Similar to M:M, except that it allows a user thread
to be bound to kernel thread

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 14


Operating Systems

Thread operations and states


Spawn
o When a new process is spawned, a thread for that process
is also spawned

Block
o When a thread needs to wait for an event, it will block

Unblock
o When the event for which a thread is blocked occurs, the thread is
moved to the Ready queue

Finish
o When a thread completes, its register context and stacks are deallocate

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 15


Operating Systems

Pthread: POSIX thread

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 16


Operating Systems

Pthreads code for joining


Pthreads 10 threads
Code for Joining 10 Threads

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03


Silberschatz, Galvin and Gagne ©2013
17
th 4. 21
Operating Systems

Windows multithread C program

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 18


Operating Systems

Java thread programming

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 19


Operating Systems

Implicit threading
Three methods explored
o Thread Pools (Win)
o OpenMP (C lib)
o Grand Central Dispatch (Mac OS, iOS)

Block is in “^{ }” - ˆ{ printf("I am a block"); } #pragma omp parallel for


for(i=0;i<N;i++) {
c[i] = a[i] + b[i];
}
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 20
Operating Systems

Thread-local storage
Thread-local storage (TLS) allows each thread to have its own copy of data

Useful when you do not have control over the thread creation process (i.e.,
when using a thread pool)

Different from local variables


o Local variables visible only during single function invocation
o TLS visible across function invocations

Similar to static data


o TLS is unique to each thread

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 21


Operating Systems

Thread termination
Thread cancelation
o Asynchronous cancellation
o Deferred cancellation

Who is “target thread”?

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 22


Operating Systems

Windows threads data structures


Implements the one-to-one mapping, kernel-level

Each thread contains


o A thread id
o Register set representing state of processor
o Separate user and kernel stacks for when thread runs in
user mode or kernel mode
o Private data storage area used by run-time libraries and
dynamic link libraries (DLLs)

The register set, stacks, and private storage area are


known as the context of the thread

Data structures:
o Execution thread block, kernel thread block and thread
environment block
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 23
Operating Systems

Linux threads
 Linux refers to them as tasks rather than threads

 Thread creation is done through clone() system call

 clone() allows a child task to share the address space of the parent task (process)
o Flags control behavior

 struct task_struct points to process data structures (shared or unique)

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 24


Operating Systems

Questions?

Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/10/03 25

You might also like