2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3....

36
2.3 InterProcess 2.3 InterProcess Communication Communication (IPC) (IPC) Part A Part A

Transcript of 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3....

Page 1: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

2.3 InterProcess 2.3 InterProcess Communication (IPC)Communication (IPC)

Part APart A

Page 2: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

IPC methodsIPC methods

1.1. SignalsSignals2.2. Mutex (MUTual EXclusion)Mutex (MUTual EXclusion)3.3. SemaphoresSemaphores4.4. Shared memoryShared memory5.5. Memory mapped filesMemory mapped files6.6. Pipes & named pipesPipes & named pipes7.7. SocketsSockets8.8. Message queuesMessage queues9.9. MPI (Message Passing Interface)MPI (Message Passing Interface)10.10. BarriersBarriers

Page 3: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

IPC methodsIPC methods

1.1. thread to threadthread to thread

2.2. process to process (both on same process to process (both on same system)system)

3.3. system to system (i.e., processes on system to system (i.e., processes on different systems)different systems)

Page 4: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

IPC methods IPC methods between between threadsthreads

►MutexMutex

►SemaphoresSemaphores

Page 5: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

IPC methods IPC methods between between processesprocesses

►SignalsSignals

►Shared memoryShared memory

►Memory mapped filesMemory mapped files

►Pipes & named pipesPipes & named pipes

►Message queuesMessage queues

Page 6: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

IPC methods IPC methods between between systemssystems

►SocketsSockets

►MPI (Message Passing Interface)MPI (Message Passing Interface) BarriersBarriers

Page 7: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

SignalsSignals

Page 8: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

SignalsSignals

►software interruptssoftware interrupts

►asyncasync

►can be recognized or ignoredcan be recognized or ignored

Page 9: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

SignalsSignals

#include <signal.h>#include <signal.h>

//defn. of signal handler function//defn. of signal handler functiontypedef void (*sighandler_t)(int);typedef void (*sighandler_t)(int);

//function call to establish a signal //function call to establish a signal handlerhandler

sighandler_t signal ( int signum,sighandler_t signal ( int signum,sighandler_t handler );sighandler_t handler );

What is this?

Page 10: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Remember . . .Remember . . .

char* ptr1, ptr2;char* ptr1, ptr2;

is not the same asis not the same as

char* ptr1;char* ptr1;

char* ptr2;char* ptr2;

It really meansIt really means

char *ptr1, ptr2;char *ptr1, ptr2;

UseUse

char *ptr1, *ptr2;char *ptr1, *ptr2;

instead.instead.

Page 11: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Allowed signals (see kill –l)Allowed signals (see kill –l) 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR29) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR213) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO30) SIGPWR 31) SIGSYS 32) SIGRTMIN 33) SIGRTMIN+130) SIGPWR 31) SIGSYS 32) SIGRTMIN 33) SIGRTMIN+134) SIGRTMIN+2 35) SIGRTMIN+3 36) SIGRTMIN+4 37) SIGRTMIN+534) SIGRTMIN+2 35) SIGRTMIN+3 36) SIGRTMIN+4 37) SIGRTMIN+538) SIGRTMIN+6 39) SIGRTMIN+7 40) SIGRTMIN+8 41) SIGRTMIN+938) SIGRTMIN+6 39) SIGRTMIN+7 40) SIGRTMIN+8 41) SIGRTMIN+942) SIGRTMIN+10 43) SIGRTMIN+11 44) SIGRTMIN+12 45) 42) SIGRTMIN+10 43) SIGRTMIN+11 44) SIGRTMIN+12 45)

SIGRTMIN+13SIGRTMIN+1346) SIGRTMIN+14 47) SIGRTMIN+15 48) SIGRTMAX-15 49) SIGRTMAX-1446) SIGRTMIN+14 47) SIGRTMIN+15 48) SIGRTMAX-15 49) SIGRTMAX-1450) SIGRTMAX-13 51) SIGRTMAX-12 52) SIGRTMAX-11 53) SIGRTMAX-1050) SIGRTMAX-13 51) SIGRTMAX-12 52) SIGRTMAX-11 53) SIGRTMAX-1054) SIGRTMAX-9 55) SIGRTMAX-8 56) SIGRTMAX-7 57) SIGRTMAX-654) SIGRTMAX-9 55) SIGRTMAX-8 56) SIGRTMAX-7 57) SIGRTMAX-658) SIGRTMAX-5 59) SIGRTMAX-4 60) SIGRTMAX-3 61) SIGRTMAX-258) SIGRTMAX-5 59) SIGRTMAX-4 60) SIGRTMAX-3 61) SIGRTMAX-262) SIGRTMAX-1 63) SIGRTMAX62) SIGRTMAX-1 63) SIGRTMAX

► Also see man 7 signal for a lengthier description.Also see man 7 signal for a lengthier description.

Page 12: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

As reported by kill -l on As reported by kill -l on SunOS/Solaris (Sun’s Unix)SunOS/Solaris (Sun’s Unix)

►HUP, INT, QUIT, ILL, TRAP, ABRT, EMT, HUP, INT, QUIT, ILL, TRAP, ABRT, EMT, FPE, KILL, BUS, SEGV, SYS, PIPE, ALRM, FPE, KILL, BUS, SEGV, SYS, PIPE, ALRM, TERM, USR1, USR2, CLD, PWR, WINCH, TERM, USR1, USR2, CLD, PWR, WINCH, URG, POLL, STOP, TSTP, CONT, TTIN, URG, POLL, STOP, TSTP, CONT, TTIN, TTOU, VTALRM, PROF, XCPU, XFSZ, TTOU, VTALRM, PROF, XCPU, XFSZ, WAITING, LWP, FREEZE, THAW, WAITING, LWP, FREEZE, THAW, CANCEL, LOST, XRES, JVM1, JVM2, CANCEL, LOST, XRES, JVM1, JVM2, RTMIN, RTMIN+1, RTMIN+2, RTMIN+3, RTMIN, RTMIN+1, RTMIN+2, RTMIN+3, RTMAX-3, RTMAX-2, RTMAX-1, RTMAXRTMAX-3, RTMAX-2, RTMAX-1, RTMAX

Page 13: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Sending a signal to a processSending a signal to a process

►Use the kill command (see man kill).Use the kill command (see man kill). kill [ -s signal | -p ] [ -a ] [ -- ] pid ...kill [ -s signal | -p ] [ -a ] [ -- ] pid ... kill -l [ signal ]kill -l [ signal ]

►Use the kill function (see man 2 kill)Use the kill function (see man 2 kill)#include <sys/types.h>#include <sys/types.h>#include <signal.h>#include <signal.h>

int kill ( pid_t pid, int sig );int kill ( pid_t pid, int sig );

Page 14: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Signal example codeSignal example code

//define the signal hander function//define the signal hander functionvoid myHandler ( int signalNo ) {void myHandler ( int signalNo ) {

……}}

……//define the signal handler//define the signal handler// (typically done once in main)// (typically done once in main)signal( SIGCHLD, myHandler );signal( SIGCHLD, myHandler );

Page 15: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

MutexMutex

Used to control access to Used to control access to shared memory and other shared memory and other

resources, in general.resources, in general.

Page 16: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Race conditionRace condition

► An error whereAn error where one process may wait foreverone process may wait forever or other inconsistencies may resultor other inconsistencies may result

►Occurs when two or more processes are Occurs when two or more processes are reading or writing some shared datareading or writing some shared data

► Applies to threads as wellApplies to threads as well► The final result depends on process or The final result depends on process or

thread runs, precisely when they run, and in thread runs, precisely when they run, and in what order they run.what order they run.

►Difficult to debug and reproduce errors.Difficult to debug and reproduce errors.

Page 17: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Critical region/sectionCritical region/section

►Part of program where shared memory Part of program where shared memory is accessedis accessed Must be identifiedMust be identified

► mutual exclusion (mutex)mutual exclusion (mutex) method to exclude other processes from method to exclude other processes from

using a shared variable until our process using a shared variable until our process is finished with itis finished with it

Page 18: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Process cooperation rules:Process cooperation rules:

1.1. No two processes can be in their critical No two processes can be in their critical sections at the same time.sections at the same time.

2.2. Make no timing assumptions.Make no timing assumptions.► My code is faster/shorter; my processor is faster.My code is faster/shorter; my processor is faster.► My priority is higher.My priority is higher.► The probability is small for us both processes to do The probability is small for us both processes to do

this at the same time.this at the same time.

3.3. (progress) A process should not be (progress) A process should not be blocked from entering a critical region if all blocked from entering a critical region if all other processes are outside the critical other processes are outside the critical region.region.

4.4. (bounded wait) No process should have to (bounded wait) No process should have to wait forever to get into its critical region.wait forever to get into its critical region.

Page 19: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Mutual exclusion w/ busy Mutual exclusion w/ busy waitingwaiting

Methods to implement mutex:Methods to implement mutex:

1.1. Disable interruptsDisable interrupts

2.2. Lock variablesLock variables

3.3. Strict alternationStrict alternation

4.4. Peterson’s solutionPeterson’s solution

5.5. TSL instructionTSL instruction

Page 20: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Mutex method 1: disable Mutex method 1: disable interruptsinterrupts

►OK for (and used by) OSOK for (and used by) OS Consideration for MP systemsConsideration for MP systems

►NOT OK for appsNOT OK for apps Why not?Why not?

Page 21: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Mutex method 2: lock varsMutex method 2: lock vars

►software methodsoftware method

►employs single, shared lock variable employs single, shared lock variable initially = 0initially = 0

►uses busy wait uses busy wait spin lock spin lock

Page 22: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Mutex method 2: lock varsMutex method 2: lock vars

shared int x=0;shared int x=0;//wait for lock//wait for lockwhile (x!=0) ; //while (x!=0) ; // note the empty note the empty

statementstatementx=1; //get lockx=1; //get lock//critical section//critical section……//end critical section//end critical sectionx=0; //release lockx=0; //release lock

►Doesn’t work (w/out hardware support).Doesn’t work (w/out hardware support).►What about performance?What about performance?

Page 23: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Mutex method 3: strict Mutex method 3: strict alternationalternation

Page 24: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Mutex method 3: strict Mutex method 3: strict alternationalternation

► SoftwareSoftware► Problem: violates process cooperation rule Problem: violates process cooperation rule

#3.#3. Because in strict alternation, a process can be Because in strict alternation, a process can be

blocked from entering its C.S. by a process NOT blocked from entering its C.S. by a process NOT in its C.S.in its C.S.

► In general, a process can’t be in it’s C.S. 2x In general, a process can’t be in it’s C.S. 2x in a row.in a row.

► The 2 processes must be running at about The 2 processes must be running at about the same speed.the same speed.

Page 25: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Mutex method 4: Peterson’s (sofware) soln.Mutex method 4: Peterson’s (sofware) soln.

Page 26: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Mutex method 4: Peterson’s Mutex method 4: Peterson’s (software) soln.(software) soln.

Page 27: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Mutex method 4: Peterson’s soln. Mutex method 4: Peterson’s soln. (software)(software)

Page 28: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Mutex method 4: Peterson’s Mutex method 4: Peterson’s (sofware) soln.(sofware) soln.

►Works, but suffers from busy wait.Works, but suffers from busy wait.

►Has been generalized to more than 2 Has been generalized to more than 2 processes (but the above is only for 2).processes (but the above is only for 2).

Page 29: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Mutex method 5: TSL Mutex method 5: TSL instructioninstruction

► TSL = Test and Set LockedTSL = Test and Set Locked► TSL RX, LOCKTSL RX, LOCK

RX = register; LOCK = memory locationRX = register; LOCK = memory location Step 1: read contents of LOCK into RXStep 1: read contents of LOCK into RX Step 2: sets LOCK to 1Step 2: sets LOCK to 1 Indivisible instruction (non interruptible)Indivisible instruction (non interruptible) Memory, not cacheMemory, not cache Locks memory bus (so other processors can’t Locks memory bus (so other processors can’t

access/change LOCK)access/change LOCK)

► IA32: XCHG and LOCK instructions.IA32: XCHG and LOCK instructions.

Page 30: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Mutex method 5: TSL Mutex method 5: TSL instructioninstruction

Page 31: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Priority inversion problemPriority inversion problem

►an unexpected consequence of busy waitan unexpected consequence of busy wait

►given H (a high priority job), and L (low given H (a high priority job), and L (low priority job)priority job)

►scheduling algorithm: whenever H is scheduling algorithm: whenever H is ready to run, L is preempted and H is runready to run, L is preempted and H is run

Page 32: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Priority inversion problemPriority inversion problem

H runs…H runs…

H blocks on I/OH blocks on I/O

I/O completesI/O completes

H runs …H runs …

H attempts to enter C.S.H attempts to enter C.S.

H busy waits forever!H busy waits forever!

L is ready to runL is ready to run

L runs …L runs …

L enters C.S. …L enters C.S. …

……

L is preemptedL is preempted

.

.

.

Page 33: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Using mutex (provided by Using mutex (provided by OS)OS)

►simpler than semaphoresimpler than semaphore►two states:two states:

1.1. lockedlocked

2.2. unlockedunlocked

►functions:functions: declare mutex variabledeclare mutex variable initialize mutex variable (just once)initialize mutex variable (just once) lock ---> C.S. ---> unlocklock ---> C.S. ---> unlock

Page 34: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

#include <errno.h>#include <errno.h>#include <pthread.h>#include <pthread.h>……pthread_mutex_t mutex; ///< declare globalpthread_mutex_t mutex; ///< declare global (i.e., not inside (i.e., not inside

of any function)of any function)

……

//perform this one-time initialization (usually in main)//perform this one-time initialization (usually in main)int ret = pthread_mutex_init( &::mutex, NULL );int ret = pthread_mutex_init( &::mutex, NULL );if (ret) { perror( "main: mutex init error" ); exit(-1); }if (ret) { perror( "main: mutex init error" ); exit(-1); }……

//lock in thread code//lock in thread coderet = pthread_mutex_lock( &::mutex );ret = pthread_mutex_lock( &::mutex );if (ret) { printf( "%d: mutex lock error \n", tp-if (ret) { printf( "%d: mutex lock error \n", tp-

>whoAmI ); }>whoAmI ); } //critical section here//critical section here//unlock in thread code//unlock in thread codepthread_mutex_unlock( &::mutex );pthread_mutex_unlock( &::mutex );

Page 35: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

#include <#include <windowswindows.h>.h>……CRITICAL_SECTION g_cs;CRITICAL_SECTION g_cs;……

//perform this one-time initialization (usually in main)//perform this one-time initialization (usually in main)InitializeCriticalSection( &g_cs );InitializeCriticalSection( &g_cs );……

//lock in thread code//lock in thread codeEnterCriticalSection( &g_cs );EnterCriticalSection( &g_cs );

//critical section here//critical section here

//unlock in thread code//unlock in thread codeLeaveCriticalSection( &g_cs );LeaveCriticalSection( &g_cs );

Page 36: 2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.

Problem:Problem:

► Modify the filter program (a program that Modify the filter program (a program that processes 2D arrays) to also determine processes 2D arrays) to also determine the overall min and max of input data.the overall min and max of input data.

1.1. Can you do this with global variables?Can you do this with global variables?

2.2. Can you do this without global variables?Can you do this without global variables?

► Which method requires mutex?Which method requires mutex?