Docsity

Operating Systems: Concurrency, IPC, and Sync Lecture Notes - Prof. Shivakant Mishra, Study notes of Operating Systems

Partial preview of the text

Download Operating Systems: Concurrency, IPC, and Sync Lecture Notes - Prof. Shivakant Mishra and more Study notes Operating Systems in PDF only on Docsity!

Interprocess Communication and

Synchronization

-^ A computation typically consists of more than oneprocess or threads, and these processes frequentlyneed to communicate with one another. •^ Example: cat f1, f2, f3 | egrep OS^ – 2 processes are created^ – One process concatenates the files; the second file isblocked^ – Second process starts when the first process terminates.^ – The first process needs to communicate to the secondprocess before terminating.

shared double balance; Code for p^1

... balance = balance + amount;... Code for p^2 ... balance = balance - amount;...

balance+=amount

balance-=amount balance

  • CSCI 3753 Operating Systems Concurrency Interprocess Communication & Synchronization Lecture Notes By Shivakant Mishra Computer Science, CU-Boulder Last Update: 01/31/
  • • What will be the final balance? – balance = $1000 – amount = $

Race Condition

-^ Situations when two are more processes (orthreads) are accessing a shared resource, and thefinal result depends on who run precisely when arecalled race conditions. •^ Race conditions can occur if two are moreprocesses are accessing a shared resource. •^ The part of the program where a shared resource isaccessed is called critical section

-^ We need a mechanism to prohibit multipleprocesses from accessing a shared resource at thesame time.

Mutual Exclusion: Solution

Requirements

  • No assumptions can be made about CPUspeeds or the number of CPUs. • No process running outside its criticalsection can prevent another process fromentering its critical section. • No process should have to wait for ever toenter its critical section: no deadlock orstarvation.

Disabling interrupts

-^ Ensure that when a process is executing inits critical section, it cannot be preempted. •^ Disable all interrupts before entering a CS. •^ Enable all interrupts upon exiting the CS.

-^ Problems:^ 1.^ If a user forgets to enable interrupts???^ 2.^ Two or more CPUs??? •^ Interrupts could be disabled arbitrarilylong •^ Really only want to prevent p

and pfrom 1 2

interfering with one another; this blocksall processes

Software solution

  • Attempt 1^ lock: shared variable = 1; (initially)^ while (lock == 0);^ lock = 0;^ CS^ lock = 1;

-^ Attempt 2 (assume 2 processes)^ Process 1:

while (1) {^ while (turn != 0);^ CS^ turn = 1;^ other code^ } Process 2: while (1) {^ while (turn != 1);^ CS^ turn = 0;^ other code^ }

  • Problem: Processes 0 and 1 need toalternate to enter CS.

-^ Software solutions indicate the difficulty insolving mutual exclusion problem. •^ Need help from hardware. •^ Need to be able to look at a variable and set it upto some value without being interrupted.

y = read (x); x = value;

-^ Modern computing systems provide such aninstruction called

test-and-set (TS);

-^ TS(m)^ – Load contents of

m^ into a CPU register (with the condition code register contents set toreflect the value of

m ), and

  • set^ m^ to true. • Important: TS(m) is an atomic

operation

  • When the system begins to execute TS, it mustexecute it without interruption, or – It must not execute it at all