1 / 88

2.3 InterProcess Communication (IPC)

2.3 InterProcess Communication (IPC). IPC methods. Signals Mutex (MUTual EXclusion) Semaphores Shared memory Memory mapped files Pipes & named pipes Sockets Message queues MPI (Message Passing Interface) Barriers. IPC methods between threads. Mutex Semaphores.

jase
Download Presentation

2.3 InterProcess Communication (IPC)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2.3 InterProcess Communication (IPC)

  2. IPC methods • Signals • Mutex (MUTual EXclusion) • Semaphores • Shared memory • Memory mapped files • Pipes & named pipes • Sockets • Message queues • MPI (Message Passing Interface) • Barriers

  3. IPC methods between threads • Mutex • Semaphores

  4. IPC methods between processes • Signals • Shared memory • Memory mapped files • Pipes & named pipes • Message queues

  5. IPC methods between systems • Sockets • MPI (Message Passing Interface) • Barriers

  6. Signals

  7. Signals • Software interrupts • Async • Can be recognized or ignored

  8. Signals #include <signal.h> //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 );

  9. Allowed signals (see kill –l) 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 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.

  10. 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 <sys/types.h> #include <signal.h> int kill ( pid_t pid, int sig );

  11. Signal example code //define the signal hander function void myHandler ( int signalNo ) { … } … //define the signal handler // (typically done once in main) signal( SIGCHLD, myHandler );

  12. Mutex Used to control access to shared memory and other resources, in general.

  13. 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.

  14. 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

  15. Process cooperation rules: • No two processes can be in their critical sections at the same time. • 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. • A process should not be blocked from entering a critical region if all other processes are outside the critical region. • No process should have to wait forever to get into its critical region.

  16. Mutual exclusion w/ busy waiting Methods to implement mutex: • Disable interrupts • Lock variables • Strict alternation • Peterson’s solution • TSL instruction

  17. Mutex method 1: disable interrupts • OK for (and used by) OS • Consideration for MP systems • NOT OK for apps • Why not?

  18. Mutex method 2: lock vars • Software method • Single, shared lock variable initially = 0 • Busy wait  spin lock

  19. 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?

  20. Mutex method 3: strict alternation

  21. Mutex method 3: strict alternation • 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.

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

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

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

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

  26. 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

  27. Mutex method 5: TSL instruction

  28. 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.

  29. 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 Priority inversion problem

  30. 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

  31. #include <errno.h> #include <pthread.h> … 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 //unlock in thread code pthread_mutex_unlock( &::mutex );

  32. #include <windows.h> … 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 //unlock in thread code LeaveCriticalSection( &g_cs );

  33. Problem: • Modify filter program to also determine overall min and max of input data. • Can you do this with global variables? • Can you do this without global variables? • Which method requires mutex?

  34. Semaphores

  35. (Bounded) Producer-Consumer • A producer produces some item and stores it in a warehouse. • A consumer consumes an item by removing an item from the warehouse. • Notes: • The producer must pause production if the warehouse fills up (bounded). • If the warehouse is empty, the consumer must wait for something to be produced.

  36. (Bounded) producer-consumer problem

  37. (Bounded) Producer-consumer problem problems • Buffer is empty • Consumer checks count. It’s 0. • Scheduler interrupts consumer (puts consumer 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!

  38. Semaphores • Two basic operations: • Up – increment the value of the semaphore • Down – decrement the value of the semaphore

  39. Semaphores • Types: • POSIX • Shared only among threads only. • System V • Can be shared according to user-group-other (can be system-wide).

  40. Binary semaphores = mutex • Create semaphore and initialize it to 1 • 1 = unlocked • 0 = locked • Then to use this as a mutex: • down • c.s. • up

  41. POSIX Semaphores #include <semaphore.h> 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 );

  42. POSIX Semaphores int sem_init ( sem_t *sem, int pshared, unsigned int value ); • initialize • pshared must be 0 on linux -> semaphore is not shared by processes • Why? • Value is initial value for semaphore.

  43. POSIX Semaphores int sem_wait ( sem_t * sem ); • down (if possible/blocking) int sem_trywait ( sem_t * sem ); • nonblocking down • Blocking?

  44. POSIX Semaphores int sem_post ( sem_t * sem ); • up (nonblocking) int sem_getvalue ( sem_t * sem, int * sval ); • get the current semaphore value int sem_destroy ( sem_t * sem ); • finish using the semaphore

  45. System V Semaphores • #include <sys/types.h> • #include <sys/ipc.h> • #include <sys/sem.h> • 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

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

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

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

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

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

More Related