2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
1 Implementations: User-level Kernel-level User-level threads package each u.process defines its own thread policies! flexible mgt, scheduling etc…kernel.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 2 Processes and Threads
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Interprocess Communication
2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
1 Tuesday, June 27, 2006 "If the 8086 architects had designed a car, they would have produced one with legs, to be compatible with the horse." - Anonymous.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
S -1 Shared Memory. S -2 Motivation Shared memory allows two or more processes to share a given region of memory -- this is the fastest form of IPC because.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
2.3 InterProcess Communication (IPC)
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
1 Concurrency: Mutual Exclusion and Synchronization Module 2.2.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Synchronizing Threads with Semaphores
Thread Implementations; MUTEX Reference on thread implementation –text: Tanenbaum ch. 2.2 Reference on mutual exclusion (MUTEX) –text: Tanenbaum ch
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
© 2006 RightNow Technologies, Inc. Synchronization September 15, 2006 These people do not actually work at RightNow.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Semaphores Reference –text: Tanenbaum ch
Web Server Architecture Client Main Thread for(j=0;j
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
Semaphores Reference text: Tanenbaum ch
Background on the need for Synchronization
Chapter 5: Process Synchronization
Lecture 13: Producer-Consumer and Semaphores
MODERN OPERATING SYSTEMS Third Edition ANDREW S
PTHREADS AND SEMAPHORES
Thread Implementations; MUTEX
Synchronization and Semaphores
Concurrency: Mutual Exclusion and Process Synchronization
Lecture 13: Producer-Consumer and Semaphores
Thread Implementations; MUTEX
CS333 Intro to Operating Systems
Synchronization.
Chapter 6: Synchronization Tools
Semaphores Reference text: Tanenbaum ch
Presentation transcript:

2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)

Race conditions ► An error where  one process may wait forever  or other inconsistencies may result ► Occurs when two or more processes are reading or writing some shared data ► Applies to threads as well ► The final result depends on who runs precisely when ► Difficult to debug and reproduce

Critical region/section ► Part of program where shared memory is accessed  Must be identified ► mutual exclusion – method to exclude other processes from using a shared variable until our process is finished with it

Process cooperation rules: 1. No two processes can be in their critical sections at the same time. 2. Make no timing assumptions. ► My code is faster/shorter; my processor is faster. ► My priority is higher. ► The probability is small for us both processes to do this at the same time. 3. A process should not be blocked from entering a critical region if all other processes are outside the critical region. 4. No process should have to wait forever to get into its critical region.

Mutual exclusion w/ busy waiting Methods to implement mutex: 1.Disable interrupts 2.Lock variables 3.Strict alternation 4.Peterson’s solution 5.TSL instruction

Mutex method: disable interrupts ► OK for (and used by) OS  Consideration with MP systems ► NOT OK for apps

Mutex method: lock vars ► Software method ► Single, shared lock variable initially = 0 ► Busy wait  spin lock shared int x=0; //wait for lock while (x!=0) ; //  note the empty statement x=1; //get lock //critical section … //end critical section x=0; //release lock ► Doesn’t work (w/out hardware support).

Mutex method: strict alternation

► Software ► Problem: Violates #3. A process can be blocked from entering its C.S. by a process NOT in its C.S. ► In general, a process can’t be in it’s C.S. 2x in a row. ► The 2 processes must be running at about the same speed.

Mutex method: Peterson’s soln. (software)

Mutex method: TSL instruction ► TSL RX, LOCK  RX = register; LOCK = memory location  Step 1: read contents of lock into RX  Step 2: sets LOCK to 1  Indivisible instruction (non interruptible)  Memory, not cache  Locks memory bus (so other processors can access/change LOCK) ► IA32 xchg and lock instructions

Mutex method: TSL instruction

Priority inversion problem ► Unexpected consequence of busy wait ► Given H (a high priority job) and L (low priority job) ► Scheduling: whenever H is ready to run, L is preempted and H is run.

Priority inversion problem H runs… H blocks on I/O I/O completes H runs… H attempts to enter C.S. H busy waits forever L is ready to run L runs… L enters C.S…. … L is preempted

Using mutex (provided by OS) ► Simpler than semaphore ► Two states: locked or unlocked ► Functions:  Declare mutex variable  Initialize mutex variable (just once)  Lock, C.S., unlock

#include #include … pthread_mutex_t mutex; //declare global … //perform this one-time initialization (usually in main) int ret = pthread_mutex_init( &::mutex, NULL ); if (ret) { perror( "main: mutex init error" ); exit(-1); } … //lock in thread code ret = pthread_mutex_lock( &::mutex ); if (ret) { printf( "%d: mutex lock error \n", tp->whoAmI ); } //critical section here //critical section here //unlock in thread code pthread_mutex_unlock( &::mutex );

Problem: ► Modify filter program to also determine overall min and max of input data. ► Why do we need a mutex do to this (correctly in a multithreaded app)?

(Bounded) producer- consumer problem

(Bounded) Producer-consumer problem problems ► Buffer is empty ► Consumer checks count. It’s 0. ► Scheduler interrupts consumer (puts comsumer on ready queue). ► Producer runs.  Insert data into buffer.  Count is 1 so producer wakes up consumer.  But consumer is not asleep (yet)!  Producer keeps inserting data into buffer until it’s full. Then producer goes to sleep! ► Scheduler runs consumer. Consumer thinks count=0 so it goes to sleep! ► Both sleep forever!

Semaphores ► Types:  POSIX ► Shared only among threads only.  System V ► Can be shared according to user-group-other.  Two basic operations: ► Up – increment the value of the semaphore ► Down – decrement the value of the semaphore

Binary semaphores (mutex) ► Create semaphore and initialize it to 1 ► Then  down  c.s.  up

POSIX Semaphores #include #include int sem_init ( sem_t *sem, int pshared, unsigned int value ); int sem_wait ( sem_t * sem ); int sem_trywait ( sem_t * sem ); int sem_post ( sem_t * sem ); int sem_getvalue ( sem_t * sem, int * sval ); int sem_destroy ( sem_t * sem );

System V Semaphores ► #include ► #include ► int semget ( key_t key, int nsems, int semflg );  create/access existing ► int semctl ( int semid, int semnum, int cmd,... );  delete from system ► int semop ( int semid, struct sembuf *sops, unsigned nsops );  used for up and down

Create/access existing //using the key, get the semaphore id const int sid = semget( mySemKey, 1, IPC_CREAT | 0700 ); if (sid==-1) { perror( "semget: " ); perror( "semget: " ); exit( -1 ); exit( -1 );} printf( "sem id=%d \n", sid ); create if necessary system-wide permissions system-wide unique number

Access and delete //using the key, get the semaphore id const int sid = semget( mySemKey, 1, 0700 ); if (sid==-1) { perror( "semget: " ); perror( "semget: " ); exit( -1 ); exit( -1 );} printf( "sem id=%d \n", sid ); //delete the semaphore semctl(sid, 0, IPC_RMID, 0);

Down function static void down ( const int whichSid ) { struct sembuf sem_lock; struct sembuf sem_lock; sem_lock.sem_num = 0; //semaphore number: 0 = first sem_lock.sem_num = 0; //semaphore number: 0 = first sem_lock.sem_op = -1; //semaphore operation sem_lock.sem_op = -1; //semaphore operation sem_lock.sem_flg = 0; //operation flags sem_lock.sem_flg = 0; //operation flags if (semop(whichSid, &sem_lock, 1) == -1) { if (semop(whichSid, &sem_lock, 1) == -1) { perror("semop"); perror("semop"); exit(-1); exit(-1); }}

Up function static void up ( const int whichSid ) { struct sembuf sem_unlock; struct sembuf sem_unlock; sem_unlock.sem_num = 0; //semaphore number: 0 = first sem_unlock.sem_num = 0; //semaphore number: 0 = first sem_unlock.sem_op = 1; //semaphore operation sem_unlock.sem_op = 1; //semaphore operation sem_unlock.sem_flg = 0; //operation flags sem_unlock.sem_flg = 0; //operation flags if (semop(whichSid, &sem_unlock, 1) == -1) { if (semop(whichSid, &sem_unlock, 1) == -1) { perror("semop"); perror("semop"); exit(-1); exit(-1); }}

Solution to (bounded) producer-consumer problem using semaphores.

Shared memory, pipes, and named pipes ► Shared memory  shmget  shmop  shmctl  shmat  shmdt ► Pipes  pipe  popen ► Named pipes  Mkfifo ► Message queues

Other IPC mechanisms: ► Monitors ► Message passing (MPI) ► Barriers