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

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
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 ©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)
Mutual Exclusion.
Interprocess Communication
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
1 Select and poll and Signals CS 241 April 6, 2012 University of Illinois.
1 Friday, June 16, 2006 "In order to maintain secrecy, this posting will self-destruct in five seconds. Memorize it, then eat your computer." - Anonymous.
EEE 435 Principles of Operating Systems Interprocess Communication Pt I (Modern Operating Systems 2.3)
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Avishai Wool lecture Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling.
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Chapter 2.3 : Interprocess Communication
1 Signals COS Goals of Today’s Lecture Overview of signals  Notifications sent to a process  UNIX signal names and numbers  Ways to generate.
B. RAMAMURTHY Pag e 1 Task Control: Signals and Alarms Chapter 7 and 8 7/2/2015.
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.
COMP5102 Lecture 4 Operating Systems (OS) Inter-process Communication phones off (please)
Process states inWindows 2000 and Linux Module 2.1.
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 DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
1 Concurrency: Mutual Exclusion and Synchronization Module 2.2.
The kernel considers each program running on your system to be a process A process lives as it executes, with a lifetime that may be short or long A process.
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R S I X Exception Handling.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
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.
Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Interprocess Communication Mechanisms. IPC Signals Pipes System V IPC.
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.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 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.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Transmitter Interrupts Review of Receiver Interrupts How to Handle Transmitter Interrupts? Critical Regions Text: Tanenbaum
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
UNIX signals.
Interprocess Communication Race Conditions
Background on the need for Synchronization
Task Control: Signals and Alarms Chapter 7 and 8
Threads and Cooperation
Lecture 11: Mutual Exclusion
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Lecture 14: Pthreads Mutex and Condition Variables
CSC Advanced Unix Programming, Fall 2015
Thread Implementations; MUTEX
Jonathan Walpole Computer Science Portland State University
Transmitter Interrupts
Lecture 2 Part 2 Process Synchronization
Inter-Process Communication ENCE 360
Thread Implementations; MUTEX
Lecture 11: Mutual Exclusion
CS333 Intro to Operating Systems
Signals.
Presentation transcript:

2.3 InterProcess Communication (IPC) Part A

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

IPC methods 1. thread to thread 2. process to process (both on same system) 3. system to system (i.e., processes on different systems)

IPC methods between threads ► Mutex ► Semaphores

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

IPC methods between systems ► Sockets ► MPI (Message Passing Interface)  Barriers

Signals

Signals ► software interrupts ► async ► can be recognized or ignored

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?

Remember... char* ptr1, ptr2; is not the same as char* ptr1; char* ptr2; It really means char *ptr1, ptr2; Use char *ptr1, *ptr2; instead.

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.

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

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 );

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

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

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.

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

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.

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 1: disable interrupts ► OK for (and used by) OS  Consideration for MP systems ► NOT OK for apps  Why not?

Mutex method 2: lock vars ► software method ► employs single, shared lock variable initially = 0 ► uses busy wait  spin lock

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?

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.

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

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

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

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

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.

Mutex method 5: TSL instruction

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

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: 1.locked 2.unlocked ► functions:  declare mutex variable  initialize mutex variable (just once)  lock ---> C.S. ---> unlock

#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 );

#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 );

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?