0% found this document useful (0 votes)
7 views7 pages

Chapter 4 OS Thread

Dke

Uploaded by

trancongtu2901
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)
7 views7 pages

Chapter 4 OS Thread

Dke

Uploaded by

trancongtu2901
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/ 7

04/09/23

Operating Systems

Web server application

Threads

Operating Systems Operating Systems

Definition Advantages of using threads


A basic unit of CPU utilization Responsiveness
o Private: Thread ID, program counter, register set, stack o Allowing a program to continue running even part of it is blocked or
o Shared: code section, data section, OS resources (IO & file) lengthy

Examples: Resource sharing


o Web browsers o Memory, resources
o Word processors
o Database engines Economy
o RPC!
o Fast

Versus Process: Scalability


o Time consuming o Threads may be running in parallel on processing cores
o Resource intensive
04/09/23

Operating Systems Operating Systems

Multicore programming Types of parallelism


Multicore or multiprocessor systems putting pressure on programmers, Types of parallelism
challenges include:
o Dividing activities o Data parallelism
o Balance o Task parallelism
o Data splitting
o Data dependency
o Testing and debugging
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
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

Operating Systems Operating Systems

Multicore programming RPC using threads


Concurrent execution on single-core system:

Parallelism on a multi-core system:


04/09/23

Operating Systems Operating Systems

User threads and kernel threads Multithreading models


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

Kernel threads - Supported by the Kernel

Asynchronous vs. synchronous threading


o Parent & child threads

Operating Systems Operating Systems

User level vs. kernel level threads 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.


04/09/23

Operating Systems Operating Systems

One-to-one Thread operations and states


Each user-level thread maps to kernel thread Spawn (sinh sản)
o When a new process is spawned, a thread for that process
Creating a user-level thread creates a kernel is also spawned
thread
Block
More concurrency than many-to-one o When a thread needs to wait for an event, it will block

Number of threads per process sometimes Unblock


restricted due to overhead o When the event for which a thread is blocked occurs, the thread is
moved to the Ready queue
Examples
o Windows
o Linux Finish
o Solaris 9 and later
o When a thread completes, its register context and stacks are deallocate

Operating Systems Operating Systems

Many-to-many model Pthread: POSIX thread


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
04/09/23

Operating Systems Operating Systems

Pthreads code for joining 10 threads Java thread programming

Operating Systems Operating Systems

Windows multithread C program 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];
}
04/09/23

Operating Systems Operating Systems

Thread-local storage Windows threads data structures


Thread-local storage (TLS) allows each thread to have its own copy of data Implements the one-to-one mapping, kernel-level

Each thread contains


Useful when you do not have control over the thread creation process (i.e., o A thread id
when using a thread pool) o Register set representing state of processor
o Separate user and kernel stacks for when thread runs in
user mode or kernel mode
Different from local variables o Private data storage area used by run-time libraries and
o Local variables visible only during single function invocation dynamic link libraries (DLLs)
o TLS visible across function invocations
The register set, stacks, and private storage area are
known as the context of the thread
Similar to static data
o TLS is unique to each thread
Data structures:
o Execution thread block, kernel thread block and thread
environment block

Operating Systems Operating Systems

Thread termination Linux threads


Thread cancelation  Linux refers to them as tasks rather than threads
o Asynchronous cancellation
o Deferred cancellation  Thread creation is done through clone() system call

 clone() allows a child task to share the address space of the parent task (process)
Who is “target thread”? o Flags control behavior

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


04/09/23

Operating Systems

Questions?

You might also like