Operating systems Mdule 7 IPC (Interprcess cmmunicatin) PART I Tami Srgente 1
INTERPROCESS COMMUNICATION Prcesses within a system may be independent r cperating Cperating prcess can affect r be affected by ther prcesses, including sharing data Reasns fr cperating prcesses: Infrmatin sharing Cmputatin speedup Mdularity Cnvenience Tw mdels f IPC Shared memry Message passing Tami Srgente 2
COMMUNICATION MODELS (a) message passing (b) shared memry Tami Srgente 3
PRODUCER-CONSUMER PROBLEM Paradigm fr cperating prcesses, prducer prcess prduces infrmatin that is cnsumed by a cnsumer prcess unbunded-buffer places n practical limit n the size f the buffer bunded-buffer assumes that there is a fixed buffer size Tami Srgente 4
IPC MESSAGE PASSING Mechanism fr prcesses t cmmunicate and t synchrnize their actins Message system prcesses cmmunicate with each ther withut resrting t shared variables IPC facility prvides tw peratins: send(message) message size fixed r variable receive(message) If P and Q wish t cmmunicate, they need t: establish a cmmunicatin link between them exchange messages via send/receive Implementatin f cmmunicatin link physical (e.g., shared memry, hardware bus) lgical (e.g., direct r indirect, synchrnus r asynchrnus) Tami Srgente 5
IPC MESSAGE PASSING Implementatin Questins Hw are links established? Can a link be assciated with mre than tw prcesses? Hw many links can there be between every pair f cmmunicating prcesses? What is the capacity f a link? Is the size f a message that the link can accmmdate fixed r variable? Is a link unidirectinal r bi-directinal? Tami Srgente 6
DIRECT COMMUNICATION Prcesses must name each ther explicitly: send (P, message) send a message t prcess P receive(q, message) receive a message frm prcess Q Prperties f direct cmmunicatin link A link is assciated with exactly ne pair f cmmunicating prcesses Between each pair there exists exactly ne link The link may be unidirectinal, but is usually bidirectinal Tami Srgente 7
INDIRECT COMMUNICATION Messages are directed and received frm mailbxes (als referred t as prts) Each mailbx has a unique id Prcesses can cmmunicate nly if they share a mailbx Prperties f cmmunicatin link Link established nly if prcesses share a cmmn mailbx A link may be assciated with many prcesses Each pair f prcesses may share several cmmunicatin links Link may be unidirectinal r bi-directinal Tami Srgente 8
MESSAGE SYNCHRONIZATION Message passing may be either blcking r nnblcking Blcking is cnsidered synchrnus Blcking send has the sender blck until the message is received Blcking receive has the receiver blck until a message is available Nn-blcking is cnsidered asynchrnus Nn-blcking send has the sender send the message and cntinue Nn-blcking receive has the receiver receive a valid message r null Tami Srgente 9
BUFFERING Queue f messages attached t the link; implemented in ne f three ways 1. Zer capacity 0 messages Sender must wait fr receiver (rendezvus) 2. Bunded capacity finite length f n messages Sender must wait if link full 3. Unbunded capacity infinite length Sender never waits Tami Srgente 10
Operating systems Mdule 7 Thread Cncept PART II Tami Srgente 11
THREADS Overview Multicre Prgramming Multithreading Mdels Thread Libraries Implicit Threading Thread Cancellatin Tami Srgente 12
THREADS - OVERVIEW What is a thread? A lightweight prcess A unit f executin f a prcess Tami Srgente 13
THREADS - OVERVIEW single threaded and multithreaded Tami Srgente 14
THREADS - OVERVIEW Mst mdern applicatins are multithreaded Multiple tasks can be implemented by separate threads Update display Fetch data Spell checking Answer a netwrk request Prcess creatin is heavy-weight while thread creatin is light-weight Kernels are generally multithreaded Tami Srgente 15
BENEFITS OF THREADS Respnsiveness may allw cntinued executin if part f prcess is blcked, especially imprtant fr user interfaces Resurce Sharing threads share resurces f prcess Ecnmy cheaper than prcess creatin, thread switching lwer verhead than cntext switching Scalability prcess can take advantage f multiprcessr architectures Tami Srgente 16
MULTICORE PROGRAMMING Multicre r multiprcessr systems putting pressure n prgrammers, challenges include: Dividing activities Balance Data splitting Data dependency Testing and debugging Parallelism implies a system can perfrm mre than ne task simultaneusly Cncurrency supprts mre than ne task making prgress Single prcessr / cre, scheduler prviding cncurrency Tami Srgente 17
CONCURRENCY VS. PARALLELISM Cncurrent executin n single-cre system: Parallelism n a multi-cre system: Tami Srgente 18
USER THREADS AND KERNEL THREADS User threads - management dne by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads Kernel threads - Supprted by the Kernel Examples virtually all general purpse perating systems, including: Windws Slaris Linux Tru64 UNIX Mac OS X Tami Srgente 19
MULTITHREADING MODELS Many-t-One One-t-One Many-t-Many Tw- level Tami Srgente 20
MANY-TO-ONE Many user-level threads mapped t single kernel thread One thread blcking causes all t blck Multiple threads may nt run in parallel n muticre system because nly ne may be in kernel at a time Few systems currently use this mdel Tami Srgente 21
ONE-TO-ONE Each user-level thread maps t kernel thread Creating a user-level thread creates a kernel thread Mre cncurrency Number f threads per prcess smetimes restricted due t verhead Tami Srgente 22
MANY-TO-MANY MODEL Allws many user level threads t be mapped t many kernel threads Allws the perating system t create a sufficient number f kernel threads Tami Srgente 23
TWO-LEVEL MODEL Similar t M:M, except that it allws a user thread t be bund t kernel thread Tami Srgente 24
THREAD LIBRARIES Thread library prvides prgrammer with API fr creating and managing threads Tw primary ways f implementing Library entirely in user space Kernel-level library supprted by the OS Tami Srgente 25
IMPLICIT THREADING Grwing in ppularity as numbers f threads increase, prgram crrectness mre difficult with explicit threads Creatin and management f threads dne by cmpilers and run-time libraries rather than prgrammers Three methds explred Thread Pls OpenMP Grand Central Dispatch Other methds include Micrsft Threading Building Blcks (TBB), java.util.cncurrent package Tami Srgente 26
THREAD POOLS Create a number f threads in a pl where they await wrk Advantages: Usually slightly faster t service a request with an existing thread than create a new thread Allws the number f threads in the applicatin(s) t be bund t the size f the pl Separating task t be perfrmed frm mechanics f creating task allws different strategies fr running task Tami Srgente 27
OPENMP Set f cmpiler directives and an API fr C, C++, FORTRAN Prvides supprt fr parallel prgramming in sharedmemry envirnments Identifies parallel regins blcks f cde that can run in parallel #pragma mp parallel Create as many threads as there are cres #pragma mp parallel fr fr(i=0;i<n;i++) { c[i] = a[i] + b[i]; } Run fr lp in parallel Tami Srgente 28
GRAND CENTRAL DISPATCH Apple technlgy fr Mac OS X and ios perating systems Extensins t C, C++ languages, API, and run-time library Allws identificatin f parallel sectins Manages mst f the details f threading Blcks placed in dispatch queue Assigned t available thread in thread pl when remved frm queue Tami Srgente 29
THREAD CANCELLATION Terminating a thread befre it has finished Thread t be canceled is target thread Tw general appraches: Asynchrnus cancellatin terminates the target thread immediately Deferred cancellatin allws the target thread t peridically check if it shuld be cancelled Tami Srgente 30