Understanding Threads and Processes: A Comprehensive Guide, Slides of Computer Science

An in-depth exploration of threads and processes, including their states, creation methods, inter-process communications, benefits, and examples. Learn about thread encapsulation, process vs. Thread differences, control blocks, and more.

Typology: Slides

2012/2013

Uploaded on 03/28/2013

ekana
ekana 🇮🇳

4

(44)

370 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Threads
Chapter 4
1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Understanding Threads and Processes: A Comprehensive Guide and more Slides Computer Science in PDF only on Docsity!

Threads

Chapter 4

Review

  • Process is a program in execution that can be in a

number of states

  • New, running, waiting, ready, terminated
  • Process creation
  • fork() and exec() system calls
  • Inter-process communications
  • Shared memory, and message passing
  • Client-server communication
  • Socket, RPC, RMI

Single and Multithreaded Processes

Threads encapsulate concurrency: “Active” component Address spaces encapsulate protection: “Passive” part Keeps buggy program from trashing the system

Processes vs. Threads

Which of the following belong to the process and which to

the thread?

Program code: local or temporary data: global data: allocated resources: execution stack: memory management info: Program counter: Parent identification: Thread state: Registers:

Process Thread Process Process Thread Process Thread Process Thread Thread

Why use threads?

  • Because threads have minimal internal state,

it takes less time to create a thread than a

process (10x speedup in UNIX).

  • It takes less time to terminate a thread.
  • It takes less time to switch to a different

thread.

  • A multi-threaded process is much cheaper

than multiple (redundant) processes.

Examples of Using Threads

  • Threads are useful for any application with multiple tasks that can be run with separate threads of control.
  • A Word processor may have separate threads for:
    • User input
    • Spell and grammar check
    • displaying graphics
    • document layout
  • A web server may spawn a thread for each client
    • Can serve clients concurrently with multiple threads.
    • It takes less overhead to use multiple threads than to use multiple processes.

Benefits

  • Responsiveness:
    • Threads allow a program to continue running even if part is blocked.
    • For example, a web browser can allow user input while loading an image.
  • Resource Sharing:
    • Threads share memory and resources of the process to which they belong.
  • Economy:
    • Allocating memory and resources to a process is costly.
    • Threads are faster to create and faster to switch between.
  • Utilization of Multiprocessor Architectures:
    • Threads can run in parallel on different processors.
    • A single threaded process can run only on one processor no matter how many are available.

User Threads

  • Thread management may be done at either the user

level or the kernel (OS) level.

User Threads:

  • Thread management done by user-level threads

library

  • The kernel is unaware that the process is

multithreaded.

  • Thread creation and scheduling is done in user space

without kernel intervention.

Kernel Level Threads

  • Threads managed by the Operating System.
    • Kernel does creation, scheduling and management of threads.
  • user-level threads are mapped to kernel thread.
  • Examples
    • Windows 95/98/NT/
    • OS/
    • Solaris-
    • Linux
    • Some flavors of UNIX

Many-to-One

  • Many user-level threads

mapped to single kernel

thread

  • Examples:
    • Solaris Green Threads
    • GNU Portable Threads

Properties of User Level Threads:

  • Pros:
    • Fast switch between threads (kernel is not needed).
    • Can have multi-threaded process on a system that does not understand threads.
    • Cheap to create and destroy threads.
    • User has complete control over threads (e.g. can assign priorities, etc.)
  • Cons:
    • System call from one thread blocks all threads in the same process (many-to-one mapping).
    • Process cannot use multiple processors.
    • Extra work for user.

User and Kernel Level Threads

• User level threads :

– are implemented with user thread libraries.

– The kernel can be unaware of the threads.

• Kernel level threads:

– Implemented by the Operating systems.

Pros and Cons of One-to-one Mapping

  • Pros:
    • System call from a thread does not block other threads in the same process.
    • One process can use multiple processors.
    • Create/destroy/switch of threads is less expensive than for processes.
  • Cons:
    • Create/destroy/switch of Kernel Level threads is more expensive than for user level threads.
    • CPU scheduling algorithms are unfair: Each thread is given the same time slice. Tasks with more threads are given more CPU time than those with fewer threads.

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

  • Solaris prior to version 9
  • Windows NT/2000 with

the ThreadFiber package