Docsity

communication entre les processus, Schémas de Informations et informatique

Aperçu partiel du texte

Télécharge communication entre les processus et plus Schémas au format PDF de Informations et informatique sur Docsity uniquement!

Inter-Process

Communication and

Synchronization

B. Ramamurthy

Introduction

  • (^) An important and fundamental feature in modern systems is concurrent execution of processes/threads. This feature is essential for the realization of multiprogramming, multiprocessing, distributed systems, and client- server model of computation.
  • (^) Concurrency encompasses many design issues including communication and synchronization among processes, sharing of and contention for resources.
  • (^) In this discussion we will look at the various design issues/problems and the wide variety of solutions available.

Principles of Concurrency

  • (^) Interleaving and overlapping the execution of processes.
  • (^) Consider two processes P1 and P executing the function echo : { input (in, keyboard); out = in; output (out, display); }

...Concurrency (contd.)

  • (^) P1 invokes echo, after it inputs into in , gets interrupted (switched). P2 invokes echo , inputs into in and completes the execution and exits. When P1 returns in is overwritten and gone. Result: first ch is lost and second ch is written twice.
  • (^) This type of situation is even more probable in multiprocessing systems where real concurrency is realizable thru’ multiple processes executing on multiple processors.
  • (^) Solution: Controlled access to shared resource
    • (^) Protect the shared resource : in buffer; “critical resource”
    • (^) one process/shared code. “critical region”

Interactions ...(contd.)

  • (^) Among the three kinds of interactions indicated by 1, 2 and 3 above:
  • (^) 1 is at the system level: potential problems : deadlock and starvation.
  • (^) 2 is at the process level : significant problem is in realizing mutual exclusion.
  • (^) 3 is more a synchronization problem.
  • (^) We will study mutual exclusion and synchronization here, and defer deadlock,

and starvation for a later time.

Race Condition

Race condition : The situation where

several processes access – and

manipulate shared data concurrently.

The final value of the shared data

depends upon which process finishes

last.

  • (^) To prevent race conditions,

concurrent processes must be

synchronized.

Mutual exclusion

  • (^) Any facility that provides mutual exclusion should meet these requirements:
  1. No assumption regarding the relative speeds of the processes.
  2. A process is in its CS for a finite time only.
  3. Only one process allowed in the CS.
  4. Process requesting access to CS should not wait indefinitely.
  5. A process waiting to enter CS cannot be blocking a process in CS or any other processes.

Software Solutions: Algorithm 1

  • (^) Process 0
  • (^) ...
  • (^) while turn != 0 do
  • (^) nothing;
  • (^) // busy waiting
  • (^) < Critical Section>
  • (^) turn = 1;
  • (^) ... Problems : Strict alternation, Busy Waiting
  • (^) Process 1
  • (^) ...
  • (^) while turn != 1 do
  • (^) nothing;
  • (^) // busy waiting
  • (^) < Critical Section>
  • (^) turn = 0;
  • (^) ...

Algorithm 3

  • (^) Combined shared variables of algorithms 1 and 2.
  • Process P i do { flag [i]:= true; turn = j; while (flag [j] and turn = j) ; critical section flag [i] = false; remainder section } while (1);
  • (^) Solves the critical-section problem for two processes.

Synchronization Hardware

  • (^) Test and modify the content of a word atomically . boolean TestAndSet(boolean &target) { boolean rv = target; target = true; return rv; }

Synchronization Hardware

Atomically swap two variables.

void Swap(boolean &a, boolean &b) {

boolean temp = a;

a = b;

b = temp;

Mutual Exclusion with Swap

  • (^) Shared data (initialized to false ): boolean lock;
  • (^) Process P i do { key = true; while (key == true) Swap(lock,key); critical section lock = false; remainder section }

Page 19 07/06/ Critical Section of n Processes

  • (^) Shared data: Semaphore mutex; // initially mutex = 1
  • (^) Process Pi: do { mutex.wait(); critical section mutex.signal(); remainder section } while (1);

Semaphore Implementation

  • (^) Define a semaphore as a class: class Semaphore { int value; // semaphore value ProcessQueue L; // process queue //operations wait() signal() }
  • (^) In addition, two simple utility operations:
    • (^) block() suspends the process that invokes it.
    • (^) Wakeup() resumes the execution of a blocked process P.