ipc (interprocess communication)faculty.eng.fau.edu/tsorgent/cop4610/cop4610m7slides.pdfipc...

30
Operating systems Module 7 IPC (Interprocess communication) PART I 1 Tami Sorgente

Upload: others

Post on 04-Aug-2020

44 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

Operating systems

Module 7

IPC (Interprocess communication)

PART I

1Tami Sorgente

Page 2: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

INTERPROCESS COMMUNICATION

Processes within a system may be independentor cooperating

Cooperating process can affect or be affected by other processes, including sharing data

Reasons for cooperating processes:o Information sharing

o Computation speedup

o Modularity

o Convenience

Two models of IPCo Shared memory

o Message passing

2Tami Sorgente

Page 3: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

COMMUNICATION MODELS

3

(a) message passing (b) shared memory

Tami Sorgente

Page 4: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

PRODUCER-CONSUMER PROBLEM

Paradigm for cooperating processes,

producer process produces information

that is consumed by a consumer process

o unbounded-buffer places no practical limit

on the size of the buffer

o bounded-buffer assumes that there is a

fixed buffer size

4Tami Sorgente

Page 5: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

IPC – MESSAGE PASSING Mechanism for processes to communicate and to synchronize their

actions

Message system – processes communicate with each other without resorting to shared variables

IPC facility provides two operations:o send(message) – message size fixed or variable

o receive(message)

If P and Q wish to communicate, they need to:o establish a communication link between them

o exchange messages via send/receive

Implementation of communication linko physical (e.g., shared memory, hardware bus)

o logical (e.g., direct or indirect, synchronous or asynchronous)

5Tami Sorgente

Page 6: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

IPC – MESSAGE PASSING

Implementation Questionso How are links established?

o Can a link be associated with more than two processes?

o How many links can there be between every pair of communicating processes?

o What is the capacity of a link?

o Is the size of a message that the link can accommodate fixed or variable?

o Is a link unidirectional or bi-directional?

6Tami Sorgente

Page 7: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

DIRECT COMMUNICATION

Processes must name each other explicitly:o send (P, message) – send a message to process

P

o receive(Q, message) – receive a message from process Q

Properties of direct communication linko A link is associated with exactly one pair of

communicating processes

o Between each pair there exists exactly one link

o The link may be unidirectional, but is usually bi-directional

7Tami Sorgente

Page 8: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

INDIRECT COMMUNICATION

Messages are directed and received from mailboxes (also referred to as ports)o Each mailbox has a unique id

o Processes can communicate only if they share a mailbox

Properties of communication linko Link established only if processes share a common

mailbox

o A link may be associated with many processes

o Each pair of processes may share several communication links

o Link may be unidirectional or bi-directional

8Tami Sorgente

Page 9: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

MESSAGE SYNCHRONIZATION

Message passing may be either blocking or non-blocking

Blocking is considered synchronouso Blocking send has the sender block until the message is

received

o Blocking receive has the receiver block until a message is available

Non-blocking is considered asynchronouso Non-blocking send has the sender send the message and

continue

o Non-blocking receive has the receiver receive a valid message or null

9Tami Sorgente

Page 10: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

BUFFERING

Queue of messages attached to the link;

implemented in one of three ways

1. Zero capacity – 0 messages

Sender must wait for receiver (rendezvous)

2. Bounded capacity – finite length of n

messages

Sender must wait if link full

3. Unbounded capacity – infinite length

Sender never waits

10Tami Sorgente

Page 11: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

Operating systems

Module 7

Thread Concept

PART II

11Tami Sorgente

Page 12: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

THREADS

Overview

Multicore Programming

Multithreading Models

Thread Libraries

Implicit Threading

Thread Cancellation

12Tami Sorgente

Page 13: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

THREADS - OVERVIEW

What is a thread?

o A lightweight process

o A unit of execution of a process

13Tami Sorgente

Page 14: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

THREADS - OVERVIEW

14

single threaded and multithreaded

Tami Sorgente

Page 15: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

THREADS - OVERVIEW

Most modern applications are multithreaded

Multiple tasks can be implemented by separate threads

o Update display

o Fetch data

o Spell checking

o Answer a network request

Process creation is heavy-weight while thread creation is light-weight

Kernels are generally multithreaded

15Tami Sorgente

Page 16: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

BENEFITS OF THREADS Responsiveness o may allow continued execution if part of process is

blocked, especially important for user interfaces

Resource Sharing o threads share resources of process

Economy o cheaper than process creation, thread switching lower

overhead than context switching

Scalability o process can take advantage of multiprocessor

architectures

16Tami Sorgente

Page 17: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

MULTICORE PROGRAMMING

Multicore or multiprocessor systems putting pressure on programmers, challenges include:o Dividing activities

o Balance

o Data splitting

o Data dependency

o Testing and debugging

Parallelism implies a system can perform more than one task simultaneously

Concurrency supports more than one task making progresso Single processor / core, scheduler providing

concurrency

17Tami Sorgente

Page 18: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

CONCURRENCY VS. PARALLELISM

Concurrent execution on single-core system:

Parallelism on a multi-core system:

18Tami Sorgente

Page 19: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

USER THREADS AND KERNEL THREADS

User threads - management done by user-level threads library

Three primary thread libraries:o POSIX Pthreads

o Win32 threads

o Java threads

Kernel threads - Supported by the Kernel

Examples – virtually all general purpose operating systems, including:o Windows

o Solaris

o Linux

o Tru64 UNIX

o Mac OS X19Tami Sorgente

Page 20: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

MULTITHREADING MODELS

Many-to-One

One-to-One

Many-to-Many

Two- level

20Tami Sorgente

Page 21: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

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 muticore system because only one may be in kernel at a time

Few systems currently use this model

21Tami Sorgente

Page 22: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

ONE-TO-ONE

Each user-level thread maps to kernel thread

Creating a user-level thread creates a kernel thread

More concurrency

Number of threads per process sometimes restricted due to overhead

22Tami Sorgente

Page 23: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

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

23Tami Sorgente

Page 24: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

TWO-LEVEL MODEL

Similar to M:M, except that it allows a user thread to be bound to kernel thread

24Tami Sorgente

Page 25: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

THREAD LIBRARIES

Thread library provides programmer

with API for creating and managing

threads

Two primary ways of implementing

o Library entirely in user space

o Kernel-level library supported by the OS

25Tami Sorgente

Page 26: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

IMPLICIT THREADING

Growing in popularity as numbers of threads increase, program correctness more difficult with explicit threads

Creation and management of threads done by compilers and run-time libraries rather than programmers

Three methods exploredo Thread Pools

o OpenMP

o Grand Central Dispatch

Other methods include Microsoft Threading Building Blocks (TBB), java.util.concurrent package

26Tami Sorgente

Page 27: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

THREAD POOLS

Create a number of threads in a pool where they await work

Advantages:

o Usually slightly faster to service a request with an existing thread than create a new thread

o Allows the number of threads in the application(s) to be bound to the size of the pool

o Separating task to be performed from mechanics of creating task allows different strategies for running task

27Tami Sorgente

Page 28: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

OPENMP

Set of compiler directives and an API for C, C++, FORTRAN

Provides support for parallel programming in shared-memory environments

Identifies parallel regions – blocks of code that can run in parallel

#pragma omp parallel

Create as many threads as there are cores

#pragma omp parallel for for(i=0;i<N;i++) {

c[i] = a[i] + b[i];

}

Run for loop in parallel

28Tami Sorgente

Page 29: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

GRAND CENTRAL DISPATCH

Apple technology for Mac OS X and iOS

operating systems

Extensions to C, C++ languages, API, and

run-time library

Allows identification of parallel sections

Manages most of the details of threading

Blocks placed in dispatch queue

o Assigned to available thread in thread pool

when removed from queue

29Tami Sorgente

Page 30: IPC (Interprocess communication)faculty.eng.fau.edu/tsorgent/COP4610/COP4610M7Slides.pdfIPC (Interprocess communication) PART I Tami Sorgente 1 INTERPROCESS COMMUNICATION Processes

THREAD CANCELLATION

Terminating a thread before it has finished

Thread to be canceled is target thread

Two general approaches:

o Asynchronous cancellation terminates

the target thread immediately

o Deferred cancellation allows the target

thread to periodically check if it should be

cancelled

30Tami Sorgente