Processes and threads
This presentation is the property of its rightful owner.
Sponsored Links
1 / 67

Processes and Threads PowerPoint PPT Presentation


  • 47 Views
  • Uploaded on
  • Presentation posted in: General

Processes and Threads. 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling. Chapter 2. Agenda. 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling. Process.

Download Presentation

Processes and Threads

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


Processes and threads

Processes and Threads

2.1 Processes

2.2 Threads

2.3 Interprocess communication

2.4 Classical IPC problems

2.5 Scheduling

Chapter 2


Agenda

Agenda

  • 2.1 Processes

  • 2.2 Threads

  • 2.3 Interprocess communication

  • 2.4 Classical IPC problems

  • 2.5 Scheduling


Process

Process

  • The most central concept in any OS

  • An abstraction of a running program

  • Modern Computers

    • Can do more than one thing at the same time

      • Can run user programs

      • Can read disk and work with user terminal

    • In a multiprogramming system

      • More than one user program can be scheduled

      • Each may run for tens of msecs.


Processes the process model

ProcessesThe Process Model

  • Multiprogramming of four programs

  • Conceptual model of 4 independent, sequential processes

  • Only one program active at any instant


Process creation

Process Creation

Principal events that cause process creation

  • System initialization

  • Execution of a process creation system

  • User request to create a new process

  • Initiation of a batch job


Process termination

Process Termination

Conditions which terminate processes

  • Normal exit (voluntary)

  • Error exit (voluntary)

  • Fatal error (involuntary)

  • Killed by another process (involuntary)


Process hierarchies

Process Hierarchies

  • Parent creates a child process, child processes can create its own process

  • Forms a hierarchy

    • UNIX calls this a "process group"

  • Windows has no concept of process hierarchy

    • all processes are created equal


Process states 1

Process States (1)

  • Possible process states

    • running

    • blocked

    • ready

  • Transitions between states shown


Process states 2

Process States (2)

  • Lowest layer of process-structured OS

    • handles interrupts, scheduling

  • Above that layer are sequential processes


Implementation of processes 1

Implementation of Processes (1)

Fields of a process table entry


Implementation of processes 2

Implementation of Processes (2)

Skeleton of what lowest level of OS does when an interrupt occurs


Agenda1

Agenda

  • 2.1 Processes

  • 2.2 Threads

  • 2.3 Interprocess communication

  • 2.4 Classical IPC problems

  • 2.5 Scheduling


Threads

Threads

  • In traditional OS, each process has

    • An address space

    • A single tread of control

  • In modern OS, each process may have

    • Multiple threads of control

    • Same address spare

    • Running in quasi-parallel

  • Thread or a Lightweight Process has

    • a program counter, registers, stack


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)

      • Called upcall


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


Agenda2

Agenda

  • 2.1 Processes

  • 2.2 Threads

  • 2.3 Interprocess communication

  • 2.4 Classical IPC problems

  • 2.5 Scheduling


Interprocess communication

Interprocess Communication

  • Processes may need to communicate

    • E.g., output of one goes to input of the other one

  • Issues

    • How to pass information

      • different address spaces

    • Making critical activities

      • Two process try to grab the last 1MB of memory

    • Proper sequencing

      • One process is producing data for the other one


Interprocess communication race conditions

Interprocess CommunicationRace Conditions

Two processes want to access shared memory at same time


Critical regions 1

Critical Regions (1)

  • Critical Regions or Critical Sections

    • The part of the program where shared memory is accessed.

  • 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 0

Mutual Exclusion with Busy Waiting (0)

  • Mutual Exclusion

    • Only one process can be in the critical section.

  • Disabling Interrupts (HW Solution)

    • Dangerous: May lead to the end of the system

  • Lock Variables (SW Solution)

    • Fatal Flow: Similar to Spooler Directory

  • Strict Alternation (SW Solution)

    • Busy Waiting, Violating Condition 3

  • Peterson’s Solution (SW Solution)

    • Busy Waiting

  • TSL Instruction (HW/SW Solution)

    • Busy Waiting, but Faster


Mutual exclusion with busy waiting 1 strict alternation sw solution

Mutual Exclusion with Busy Waiting (1)Strict Alternation (SW Solution)

Proposed solution to critical region problem

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


Mutual exclusion with busy waiting 2 peterson s solution sw solution

Mutual Exclusion with Busy Waiting (2)Peterson’s Solution (SW Solution)


Mutual exclusion with busy waiting 3 using tsl instruction hw sw solution

Mutual Exclusion with Busy Waiting (3)Using TSL Instruction (HW/SW Solution)

Entering and leaving a critical region using the

TSL instruction


Mutual exclusion with sleep and wakeup 0 producer consumer problem

Mutual Exclusion with Sleep and Wakeup (0)Producer-Consumer Problem

  • Priority Inversion Problem

    • busy waiting with priority!

  • Solution with Fatal Race Condition

    • Waking up a consumer that is not asleep yet!

  • Semaphore

    • An integer that counts the number of wake-up calls.

  • Mutexes

    • Binary semaphores, good for mutual exclusion.

  • Monitors

    • Easier to program (Synchronized in Java).

  • Message Passing

    • N messages used for communication coordination.

  • Barriers

    • Synchronization of N processes/threads.


Mutual exclusion with sleep and wakeup 1 fatal race condition

Mutual Exclusion with Sleep and Wakeup (1)Fatal Race Condition


Mutual exclusion with sleep and wakeup 2 using semaphores

Mutual Exclusion with Sleep and Wakeup (2)Using Semaphores


Mutual exclusion with sleep and wakeup 3 using mutexes

Mutual Exclusion with Sleep and Wakeup (3)Using Mutexes

Implementation of mutex_lock and mutex_unlock


Mutual exclusion with sleep and wakeup 4 using monitors 1

Mutual Exclusion with Sleep and Wakeup (4)Using Monitors (1)

Example of a monitor


Mutual exclusion with sleep and wakeup 4 using monitors 2

Mutual Exclusion with Sleep and Wakeup (4) Using Monitors (2)

  • Outline of producer-consumer problem with monitors

    • only one monitor procedure active at one time

    • buffer has N slots


Processes and threads

Mutual Exclusion with Sleep and Wakeup (4) Using Monitors (3)

Solution to producer-consumer problem in Java (part 1)


Processes and threads

Mutual Exclusion with Sleep and Wakeup (4) Using Monitors (4)

Solution to producer-consumer problem in Java (part 2)


Processes and threads

Mutual Exclusion with Sleep and Wakeup (5) Message Passing

The producer-consumer problem with N messages


Barriers

Barriers

  • Use of a barrier

    • processes approaching a barrier

    • all processes but one blocked at barrier

    • last process arrives, all are let through


Agenda3

Agenda

  • 2.1 Processes

  • 2.2 Threads

  • 2.3 Interprocess communication

  • 2.4 Classical IPC problems

  • 2.5 Scheduling


Dining philosophers 1

Dining Philosophers (1)

  • Philosophers eat/think

  • Eating needs 2 forks

  • Pick one fork at a time

  • How to prevent deadlock


Dining philosophers 2

Dining Philosophers (2)

A nonsolution to the dining philosophers problem


Dining philosophers 3

Dining Philosophers (3)

Solution to dining philosophers problem (part 1)


Dining philosophers 4

Dining Philosophers (4)

Solution to dining philosophers problem (part 2)


The readers and writers problem

The Readers and Writers Problem

A solution to the readers and writers problem


The sleeping barber problem 1

The Sleeping Barber Problem (1)


The sleeping barber problem 2

The Sleeping Barber Problem (2)

Solution to sleeping barber problem.


Agenda4

Agenda

  • 2.1 Processes

  • 2.2 Threads

  • 2.3 Interprocess communication

  • 2.4 Classical IPC problems

  • 2.5 Scheduling


Scheduling

Scheduling

  • Scheduler

    • The part of the OS that make the choice of which process to run next.

  • Scheduling Algorithm

    • The algorithm used for scheduling


Scheduling introduction to scheduling 1

SchedulingIntroduction to Scheduling (1)

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

    • a CPU-bound process: spends most of its time on computing

    • an I/O bound process: spends most of its time waiting for I/O


Introduction to scheduling 2 system algorithm goals

Introduction to Scheduling (2)System Algorithm Goals


Scheduling algorithm goals

Scheduling Algorithm Goals

  • Throughput

    • The number of jobs per hour that the system completes.

  • Turnaround time

    • The statically average time from the moment that a batch job is submitted until the moment it is completed.


Scheduling in batch systems 1

Scheduling in Batch Systems (1)

  • First-Come First-Served

  • Shortest Job First (non-preemptive)

    • An example of shortest job first scheduling

  • Shortest Remaining Time First (preemptive)

  • Three-Level Scheduling


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

  • Priority Scheduling

  • Multiple Queues

  • Shortest Process Next

  • Guaranteed Scheduling

  • Lottery Scheduling

  • Fair-Share Scheduling


Scheduling in interactive systems 2

Scheduling in Interactive Systems (2)

A scheduling algorithm with four priority classes


Scheduling in real time systems

Scheduling in Real-Time Systems

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


Policy versus mechanism

Policy versus Mechanism

  • Separate what is allowed to be done with how it is done

    • a process knows which of its children threads are important and need priority

  • Scheduling algorithm parameterized

    • mechanism in the kernel

  • Parameters filled in by user processes

    • policy set by user process


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


  • Login