Download presentation
Presentation is loading. Please wait.
Published byLawrence Idler Modified over 9 years ago
1
2.3 InterProcess Communication (IPC) Part A
2
IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files 6. Pipes & named pipes 7. Sockets 8. Message queues 9. MPI (Message Passing Interface) 10. Barriers
3
IPC methods 1. thread to thread 2. process to process (both on same system) 3. system to system (i.e., processes on different systems)
4
IPC methods between threads ► Mutex ► Semaphores
5
IPC methods between processes ► Signals ► Shared memory ► Memory mapped files ► Pipes & named pipes ► Message queues
6
IPC methods between systems ► Sockets ► MPI (Message Passing Interface) Barriers
7
Signals
8
Signals ► software interrupts ► async ► can be recognized or ignored
9
Signals #include #include //defn. of signal handler function typedef void (*sighandler_t)(int); //function call to establish a signal handler sighandler_t signal ( int signum, sighandler_t handler ); What is this?
10
Remember... char* ptr1, ptr2; is not the same as char* ptr1; char* ptr2; It really means char *ptr1, ptr2; Use char *ptr1, *ptr2; instead.
11
Allowed signals (see kill –l) 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 32) SIGRTMIN 33) SIGRTMIN+1 34) SIGRTMIN+2 35) SIGRTMIN+3 36) SIGRTMIN+4 37) SIGRTMIN+5 38) SIGRTMIN+6 39) SIGRTMIN+7 40) SIGRTMIN+8 41) SIGRTMIN+9 42) SIGRTMIN+10 43) SIGRTMIN+11 44) SIGRTMIN+12 45) SIGRTMIN+13 46) SIGRTMIN+14 47) SIGRTMIN+15 48) SIGRTMAX-15 49) SIGRTMAX-14 50) SIGRTMAX-13 51) SIGRTMAX-12 52) SIGRTMAX-11 53) SIGRTMAX-10 54) SIGRTMAX-9 55) SIGRTMAX-8 56) SIGRTMAX-7 57) SIGRTMAX-6 58) SIGRTMAX-5 59) SIGRTMAX-4 60) SIGRTMAX-3 61) SIGRTMAX-2 62) SIGRTMAX-1 63) SIGRTMAX ► Also see man 7 signal for a lengthier description.
12
As reported by kill -l on SunOS/Solaris (Sun’s Unix) ► HUP, INT, QUIT, ILL, TRAP, ABRT, EMT, FPE, KILL, BUS, SEGV, SYS, PIPE, ALRM, TERM, USR1, USR2, CLD, PWR, WINCH, URG, POLL, STOP, TSTP, CONT, TTIN, TTOU, VTALRM, PROF, XCPU, XFSZ, WAITING, LWP, FREEZE, THAW, CANCEL, LOST, XRES, JVM1, JVM2, RTMIN, RTMIN+1, RTMIN+2, RTMIN+3, RTMAX-3, RTMAX-2, RTMAX-1, RTMAX
13
Sending a signal to a process ► Use the kill command (see man kill). kill [ -s signal | -p ] [ -a ] [ -- ] pid... kill -l [ signal ] ► Use the kill function (see man 2 kill) #include #include int kill ( pid_t pid, int sig );
14
Signal example code //define the signal hander function void myHandler ( int signalNo ) { …}… //define the signal handler // (typically done once in main) signal( SIGCHLD, myHandler );
15
Mutex Used to control access to shared memory and other resources, in general.
16
Race condition ► 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 process or thread runs, precisely when they run, and in what order they run. ► Difficult to debug and reproduce errors.
17
Critical region/section ► Part of program where shared memory is accessed Must be identified ► mutual exclusion (mutex) method to exclude other processes from using a shared variable until our process is finished with it
18
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. (progress) A process should not be blocked from entering a critical region if all other processes are outside the critical region. 4. (bounded wait) No process should have to wait forever to get into its critical region.
19
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
20
Mutex method 1: disable interrupts ► OK for (and used by) OS Consideration for MP systems ► NOT OK for apps Why not?
21
Mutex method 2: lock vars ► software method ► employs single, shared lock variable initially = 0 ► uses busy wait spin lock
22
Mutex method 2: lock vars 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). ► What about performance?
23
Mutex method 3: strict alternation
24
► Software ► Problem: violates process cooperation rule #3. Because in strict alternation, 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.
25
Mutex method 4: Peterson’s (sofware) soln.
26
Mutex method 4: Peterson’s (software) soln.
27
Mutex method 4: Peterson’s soln. (software)
28
Mutex method 4: Peterson’s (sofware) soln. ► Works, but suffers from busy wait. ► Has been generalized to more than 2 processes (but the above is only for 2).
29
Mutex method 5: TSL instruction ► TSL = Test and Set Locked ► 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’t access/change LOCK) ► IA32: XCHG and LOCK instructions.
30
Mutex method 5: TSL instruction
31
Priority inversion problem ► an unexpected consequence of busy wait ► given H (a high priority job), and L (low priority job) ► scheduling algorithm: whenever H is ready to run, L is preempted and H is run
32
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......
33
Using mutex (provided by OS) ► simpler than semaphore ► two states: 1.locked 2.unlocked ► functions: declare mutex variable initialize mutex variable (just once) lock ---> C.S. ---> unlock
34
#include #include … pthread_mutex_t mutex; ///< declare global (i.e., not inside of any function) … //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 );
35
#include #include … CRITICAL_SECTION g_cs; … //perform this one-time initialization (usually in main) InitializeCriticalSection( &g_cs ); … //lock in thread code EnterCriticalSection( &g_cs ); //critical section here //critical section here //unlock in thread code LeaveCriticalSection( &g_cs );
36
Problem: ► Modify the filter program (a program that processes 2D arrays) to also determine the overall min and max of input data. 1. Can you do this with global variables? 2. Can you do this without global variables? ► Which method requires mutex?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.