Threads
Download
1 / 43

Threads - PowerPoint PPT Presentation


  • 143 Views
  • Uploaded on

Threads. CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5. Threads The Thread Model (1). (a) Three processes each with one thread (b) One process with three threads. The Thread Model (2). Items shared by all threads in a process Items private to each thread.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Threads' - kiora


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Threads

Threads

CSCE 351: Operating System Kernels

Witawas Srisa-an

Chapter 4-5


Threads the thread model 1
ThreadsThe Thread Model (1)

(a) Three processes each with one thread

(b) One process with three threads


The thread model 2
The Thread Model (2)

  • Items shared by all threads in a process

  • Items private to each thread


The thread model 3
The Thread Model (3)

Each thread has its own stack


Thread usage 1
Thread Usage (1)

A word processor with three threads


Thread usage 2
Thread Usage (2)

A multithreaded Web server


Thread usage 3
Thread Usage (3)

  • Rough outline of code for previous slide

    (a) Dispatcher thread

    (b) Worker thread


Thread usage 4
Thread Usage (4)

Three ways to construct a server


Implementing threads in user space
Implementing Threads in User Space

A user-level threads package


Implementing threads in the kernel
Implementing Threads in the Kernel

A threads package managed by the kernel


Hybrid implementations
Hybrid Implementations

Multiplexing user-level threads onto kernel- level threads


Scheduler activations
Scheduler Activations

  • Goal – mimic functionality of kernel threads

    • gain performance of user space threads

  • Avoids unnecessary user/kernel transitions

  • Kernel assigns virtual processors to each process

    • lets runtime system allocate threads to processors

  • Problem: Fundamental reliance on kernel (lower layer)

    calling procedures in user space (higher layer)


Pop up threads
Pop-Up Threads

  • Creation of a new thread when message arrives

    (a) before message arrives

    (b) after message arrives


Making single threaded code multithreaded 1
Making Single-Threaded Code Multithreaded (1)

Conflicts between threads over the use of a global variable


Making single threaded code multithreaded 2
Making Single-Threaded Code Multithreaded (2)

Threads can have private global variables


Interprocess communication race conditions
Interprocess CommunicationRace Conditions

Two processes want to access shared memory at same time


Critical regions 1
Critical Regions (1)

  • Four conditions to provide mutual exclusion

    • No two processes simultaneously in critical region

    • No assumptions made about speeds or numbers of CPUs

    • No process running outside its critical region may block another process

    • No process must wait forever to enter its critical region


Critical regions 2
Critical Regions (2)

Mutual exclusion using critical regions


Mutual exclusion with busy waiting 1
Mutual Exclusion with Busy Waiting (1)

Proposed solution to critical region problem

(a) Process 0. (b) Process 1.

No process running outside its critical region may block another process



Mutual exclusion with busy waiting 3
Mutual Exclusion with Busy Waiting (3)

Entering and leaving a critical region using the

TSL instruction


Sleep and wakeup
Sleep and Wakeup

Producer-consumer problem with fatal race condition


Semaphores
Semaphores

  • A variable type

    • 0 or any positive values (counting)

    • 0 or 1 (binary)

  • Support two operations

    • down (p)

      • if value > 0 then decrement

      • if value = 0 then suspend process without completing the down

      • indivisible atomic action

    • up (v)

      • increment the semaphore value and if there are processes sleeping on the semaphore, wake one of them up


Semaphore
Semaphore

arena

queue


Semaphores1
Semaphores

The producer-consumer problem using semaphores


Mutexes
Mutexes

Implementation of mutex_lock and mutex_unlock


Example program
Example Program

  • suspend.c and wake.c


Monitors
Monitors

  • Language construct

    • higher level synchronization primitive

  • Only one process can be active in a monitor at any instant

  • Use condition variables to block processes

    • wait operation

    • signal operation


Monitors 1
Monitors (1)

Example of a monitor


Monitors 2
Monitors (2)

  • Outline of producer-consumer problem with monitors

    • only one process can be active in a monitor at one time

    • buffer has N slots


The readers and writers problem
The Readers and Writers Problem

A solution to the readers and writers problem



The sleeping barber problem 2
The Sleeping Barber Problem (2)

Solution to sleeping barber problem.


Scheduling introduction to scheduling 1
SchedulingIntroduction to Scheduling (1)

  • Bursts of CPU usage alternate with periods of I/O wait

    • a CPU-bound process

    • an I/O bound process


Introduction to scheduling 2
Introduction to Scheduling (2)

Scheduling Algorithm Goals


Scheduling in batch systems 1
Scheduling in Batch Systems (1)

An example of shortest job first scheduling

(8 + 12 + 16 + 20) / 4

= 14 units

(4 + 8 + 12 + 20) / 4

= 11 units


Scheduling in batch systems 2
Scheduling in Batch Systems (2)

Three level scheduling


Scheduling in interactive systems 1
Scheduling in Interactive Systems (1)

  • Round Robin Scheduling

    • list of runnable processes

    • list of runnable processes after B uses up its quantum


Scheduling in interactive systems 2
Scheduling in Interactive Systems (2)

A scheduling algorithm with four priority classes


Scheduling in real time systems 1
Scheduling in Real-Time Systems (1)

Schedulable real-time system

  • Given

    • m periodic events

    • event i occurs within period Pi and requires Ci seconds

  • Then the load can only be handled if


Scheduling in real time systems 2
Scheduling in Real-Time Systems (2)

  • Example

    • four periodic events: 100, 200, 400, 600 ms

    • required CPU times: 25, 40, 100, 120 ms

      Is this system schedulable?


Thread scheduling 1
Thread Scheduling (1)

Possible scheduling of user-level threads

  • 50-msec process quantum

  • threads run 5 msec/CPU burst


Thread scheduling 2
Thread Scheduling (2)

Possible scheduling of kernel-level threads

  • 50-msec process quantum

  • threads run 5 msec/CPU burst


ad