0% found this document useful (0 votes)
46 views16 pages

CH 4

Uploaded by

rahaf.alzuhairi
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)
46 views16 pages

CH 4

Uploaded by

rahaf.alzuhairi
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

Chapter 4: Threads &

Concurrency

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Problems w th Processes

▪ Process creation is heavyweight (i.e. slow)


• Space must be allocated for the new process
• fork() copies all state of the parent to the child
▪ IPC mechanisms are cumbersome
• Difficult to use fine-grained synchronization
• Message passing is slow
4 Each message may have to go through the kernel

2
Operating System Concepts – 10th Edition 4.2 Silberschatz, Galvin and Gagne ©2018
Threads

▪ Light-weight processes that share the same memory and state space
▪ A thread is executed within the address space of a process
▪ A process consists of an address space, a collection of operating
system state and one or multiple threads
▪ Unlike processes,
• threads are not independent of one another
• all threads can access every space in the process
• Threads are designed to assist one other
• Processes might or might not assist one another

3
Operating System Concepts – 10th Edition 4.3 Silberschatz, Galvin and Gagne ©2018
S ngle and Mult threaded Processes

Operating System Concepts – 10th Edition 4.4 Silberschatz, Galvin and Gagne ©2018
Mult threaded Server Arch tecture

Operating System Concepts – 10th Edition 4.5 Silberschatz, Galvin and Gagne ©2018
Processes vs. Threads

▪ Threads are better if:


• You need to create new ones quickly, on-the-fly
• You need to share lots of state

▪ Processes are better if:


• You want protection
4 One process that crashes or freezes doesn’t impact the others
• You need high security
4 Only way to move state is through well-defined, sanitized message
passing interface

6
Operating System Concepts – 10th Edition 4.6 Silberschatz, Galvin and Gagne ©2018
Benef ts

▪ 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 multicore
architectures

Operating System Concepts – 10th Edition 4.7 Silberschatz, Galvin and Gagne ©2018
Ut l zat on of Mult processor Arch tecture

▪ The utilization of multithreading can be greatly increased in a


multiprocessor architecture, where threads may be running in parallel
on different processors
▪ A single threaded process can run on one CPU, no matter how many
are available
▪ 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 – 10th Edition 4.8 Silberschatz, Galvin and Gagne ©2018
Concurrency vs. Parallel sm
▪ Concurrent execution on single-process system:

▪ Parallelism on a multi-process system:

Operating System Concepts – 10th Edition 4.9 Silberschatz, Galvin and Gagne ©2018
Context Sw tch

▪ The threads share a lot resources with other peer threads belonging
to the same process. So a context switch among threads for the
same process is easy.
▪ It involves switch of register set, the program counter and the stack.
▪ Context switch among processes are expensive. Before a process
can be switched its PCB must be saved by the operating system. The
PCB consists of a lot of information the process state, the program
counter, the values of different registers, memory management
information regrading the process, etc.
▪ When the PCB of the currently executing process is saved , the
operating system loads the PCB of the next process that has to be
run on CPU, This is heavy task and it takes a lot of time.

Operating System Concepts – 10th Edition 4.10 Silberschatz, Galvin and Gagne ©2018
Mult thread ng Models

▪ Support for threads may be prov ded at e ther:


• The User level, for user threads
4 User-threads are supported above the kernel and are
managed without kernel support. Management done by user-
level threads library.
• The Kernel, for kernel threads
4 Kernel-threads are supported and managed directly by the OS.

Operating System Concepts – 10th Edition 4.11 Silberschatz, Galvin and Gagne ©2018
User and Kernel Threads

▪ Three ways of establishing relationship between user-threads and


kernel-threads:
• Many-to-one model
• One-to-one model
• Many-to-many model

Operating System Concepts – 10th Edition 4.12 Silberschatz, Galvin and Gagne ©2018
User and Kernel Threads

▪ Many-to-one model

▪ Many user-level threads are mapped to one kernel thread.


▪ Advantages:
• Thread management is done by the thread library in user space,
so it is efficient.
▪ Disadvantages:
• The entire process will block if a thread makes a blocking system-
call.
• Multiple threads are unable to run in parallel on multiprocessors.

Operating System Concepts – 10th Edition 4.13 Silberschatz, Galvin and Gagne ©2018
User and Kernel Threads

▪ One-to-one model

▪ Each user thread is mapped to a kernel thread.


▪ Advantages:
• It provides more concurrency by allowing another thread to run
when a thread makes a blocking system-call.
• Multiple threads can run in parallel on multiprocessors.
▪ Disadvantages:
• Creating a user thread requires creating the corresponding kernel
thread.
Operating System Concepts – 10th Edition 4.14 Silberschatz, Galvin and Gagne ©2018
User and Kernel Threads

▪ Many-to-many model

▪ Many user-level threads are multiplexed to a smaller number of kernel


threads
▪ Advantages:
• Developers can create as many user threads as necessary
• The kernel threads can run in parallel on a multiprocessor.
• When a thread performs a blocking system-call, kernel can
schedule another thread for execution

Operating System Concepts – 10th Edition 4.15 Silberschatz, Galvin and Gagne ©2018
End of Chapter 4

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018

You might also like