INTERPROCESS COMMUNICATION
• Processes need to communicate with other processes.
• Shell pipeline - output of the first process passed to the
  second process.
                        Issues
• How one process can pass information to another.
• Two processes do not get into each other's way
  when engaging in critical activities
  (two processes each try to grab the last IOOK of memory).
• proper sequencing when dependencies are present
  process A produces data and process B prints
  B has to wait until A has produced some data
                 Race Conditions
• Processes may share common storage
•each one can read and write.
When a process wants to print a file,
it enters the file name in special spooler directory.
Printer daemon, periodically checks for any files to be printed
•Two or more processes are reading or writing shared data
•final result depends on who runs precisely when
                  Critical Sections
•    Prohibit more than one process from reading and writing
    the shared data at the same time.
• mutual exclusion
  if one process is using a shared variable or file
• the other processes excluded from doing the same thing
  part of the program where the shared memory is accessed
  critical region or section.
• To avoid race conditions
  no two processes ever in their critical regions at same time
  Conditions for Parallel Processes
1. No two processes simultaneously inside their critical regions.
2. No assumptions about speeds or the number of CPUs.
3. No process outside critical region may block other processes.
4. No process should wait forever to enter critical region.
        Solutions – Mutual Exclusion
   Disabling Interrupts.
• disable just after entering critical region.
• re-enable just before leaving it
  no clock interrupts can occur.
• CPU will not be switched to another process
  turn off interrupts – end of system
  Shared memory access by another processor
                   multiprocessor system
• List of ready processes inconsistent – race condition
                     Lock Variables
• single, shared, (lock) variable - initially 0
• process entering critical region
                 first tests the lock
• 0, the process sets it to 1
                 enters critical region.
already 1, the process just waits until it becomes 0
      0 - no process critical region
      1 - some process in critical region
 
                             Lock
• Currently 0.
• Before one process sets to 1
  another process scheduled
  sets to 1.
• First process runs again & also sets to 1.
• Two processes in critical region.
 
                  Strict Alteration
• One process much slower
  process 0 quickly complete the loop.
• Violation condition
  Process 0 blocked by a process not in critical region.
               Peterson’s Solution
• both processes call enter-region
• process 1 stores last - turn is 1
                  TSL Instruction
• TEST AND SET LOCK
  reads memory word into a register
  stores a nonzero value at that memory address
• Reading and storing are indivisible
  no other processor can access the memory word
                until the instruction is finished.
• CPU locks the memory bus
• LOCK:XCHG
 
 
Setting & Clearing Locks using TSL
            Busy Waiting
• Process in a loop.
• Waiting for entering critical Region.
• Approach wastes CPU time.
         Priority Inversion Problem
• H runs when in ready state.
• L in critical region.
• H becomes ready and begins waiting.
• L never scheduled while H running.
• L never leaves critical region.
• H loops forever.
• Block instead of wasting CPU time.
 
    Procedure – Consumer Problem
• Two processes – common shared buffer
• Full buffer – procedure sleeps
• Awakened if consumer removes one or more
• Consumer asleep – empty buffer
• Race condition – variable count – no. of items in buffer
• count 0, consumer sleeps
 
Fatal Race Condition
               Fatal Race Condition
• count read as 0.
• Scheduler stops consumer before blocked by sleep
• producer puts an item and calls wakeup.
• consumer not yet asleep, wakeup signal lost.
• consumer resumes , goes to sleep.
• producer fills buffer, sleeps forever.
 
Terms Related to Concurrency
                      Semaphore
• A semaphore 0 - no wakeups
• some positive value - one or more wakeups pending
• Special variable for signaling – s – integer
• SemSignal (s) – to transmit a signal via s
• SemWait (s) - to receive signal via s
 
          Semaphore Operations
1. Initialized to a nonnegative integer value
2. semWait decrements the value
    Process executing semWait blocks on -ve value
3. semSignal increments the value.
               Binary Semaphore
1. Initialized to 0 or 1
2. semWaitB blocks the process, if 0.
   if 1, changed to 0 and process continues.
3. if 0, process blocked by semWaitB unblocked.
       If no process is blocked, changed to 1.
Semaphore Mechanism
Semaphore Mechanism
Process Accessing Shared Data
Solution to Infinite Buffer
A Scenario
                 Semaphore Uses
1.Mutual Exclusion
 ●
   only one process at a time will be reading or writing the
   buffer and the associated variables
2. Synchronization
  ●
    certain event sequences do or do not occur
 ●
     Producer stops running when the buffer is full
 ●
     Consumer stops running when it is empty.
Solution for Infinite Buffer
Finite Circle Buffer
Solution Bounded Buffer