IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I...

22
IPC Hui Chen a a CUNY Brooklyn College March 4, 2020 H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 1 / 17

Transcript of IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I...

Page 1: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

IPC

Hui Chen a

aCUNY Brooklyn College

March 4, 2020

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 1 / 17

Page 2: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Outline

1 Motivation

2 IPCShared MemoryMessage Passing

3 Producer and Consumer Problem

4 UNIX (POSIX) and Windows IPC

5 Sharing Data among Threads and Processes

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 2 / 17

Page 3: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Motivation

Outline

1 Motivation

2 IPCShared MemoryMessage Passing

3 Producer and Consumer Problem

4 UNIX (POSIX) and Windows IPC

5 Sharing Data among Threads and Processes

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 3 / 17

Page 4: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Motivation

Indepdent or Cooperating Processes

Processes within a system may be independent or cooperating.I Independent process cannot affect or be affected by the execution of

another processI Cooperating process can affect or be affected by the execution of

another processI Information sharingI Computation speed-upI Modularity

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 3 / 17

Page 5: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Motivation

Multiprocess Architecture

Taking advantatage of indepedent or/and cooperativing processes, designmultiprocess architecture applications

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 4 / 17

Page 6: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Motivation

Example Applications

I The Chromimum projectsI The instructor’s Monte Carlo simulation program to estimate π

I Shell scriptsWhat benefits do we get by using the multiprocess architecture?

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 5 / 17

Page 7: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

IPC

Outline

1 Motivation

2 IPCShared MemoryMessage Passing

3 Producer and Consumer Problem

4 UNIX (POSIX) and Windows IPC

5 Sharing Data among Threads and Processes

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 6 / 17

Page 8: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

IPC

Interprocess Communication

I Cooperative processes communicate with each other to share data.I There are two communication models

I Shared memoryI Message passing

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 6 / 17

Page 9: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

IPC Shared Memory

Shared Memory

I OS must provide a system call to create a shared memory region.I OS must attach the shared memory region to communicating

processes’ address spaces.I OS must removes the restriction that normally one process is

prevented from accessing another process’s memory.I All accesses to the shared memory region are treated as routine

memory accesses, and no assistance from the kernel is required.I The processes are also responsible for ensuring that they are not

writing to the same location simultaneously.

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 7 / 17

Page 10: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

IPC Message Passing

Message Passing

I Processes exchange messages. There is no conflict needed to beavoided.

I IPC facility provides two operations:I send(message)I receive(message)

I Processes establish a communication link between them and exchangemessages via send/receive

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 8 / 17

Page 11: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

IPC Message Passing

Design Message Passing

I Physical communication link can be shared memory, hardware bus, ornetwork.

I Logically, the communication beI direction or indirect communication (like mailbox)I Blocking or non-blocking (synchronous or asynchronous)I explicit buffering or implicit (automatic) buffering

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 9 / 17

Page 12: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Producer and Consumer Problem

Outline

1 Motivation

2 IPCShared MemoryMessage Passing

3 Producer and Consumer Problem

4 UNIX (POSIX) and Windows IPC

5 Sharing Data among Threads and Processes

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 10 / 17

Page 13: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Producer and Consumer Problem

Producer and Consumer Problem

The producer procudes information while the consumer consumesinformation

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 10 / 17

Page 14: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Producer and Consumer Problem

Bounded Buffer via Shared Memory

Shared Buffer1 #d e f i n e BUFFER_SIZE 102 typede f s t r u c t { } i tem ;34 // The f o l l o w i n g a r e sha r ed among c o o p e r a t i n g p r o c e s s e s5 i tem b u f f e r [ BUFFER_SIZE ] ;6 i n t i n = 0 ;7 i n t out = 0 ;8

Producer1 i tem next_produced ;2 w h i l e ( t r u e ) {3 /∗ 1 . produce an i tem i n nex t produced

∗/4 /∗ 2 . do no th i ng when b u f f e r i s f u l l

∗/5 w h i l e ( ( ( i n + 1) % BUFFER_SIZE) == out

) ;6 /∗ 3 . produce an i tem and w r i t e i t to

c u r r e n t s l o t ∗/7 b u f f e r [ i n ] = next_produced ;8 /∗ 4 . advance to next s l o t nex t s l o t

∗/9 i n = ( i n + 1) % BUFFER_SIZE ;

10 }11

Consumer1 i tem next_consumed ;2 w h i l e ( t r u e ) {3 i tem next_consumed ;4 /∗ 1 . do no th i ng when b u f f e r i s empty

∗/5 w h i l e ( t r u e ) { w h i l e ( i n == out ) ;6 /∗ 2 . consume the i tem i n c u r r e n t s l o t

∗/7 next_consumed = b u f f e r [ out ] ;8 /∗ 3 . advance to next s l o t ∗/9 out = ( out + 1) % BUFFER_SIZE ;

10 }11

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 11 / 17

Page 15: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Producer and Consumer Problem

Process Synchronization

Both producer and consumer may read and write to the shared memoryconcurrently ...

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 12 / 17

Page 16: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Producer and Consumer Problem

Producer and Consumer via Blocking Message Passing

Producer1 message next_produced ;2 w h i l e ( t r u e ) {3 /∗ produce an i tem i n next_produced ∗/4 send ( next_produced ) ; /∗ b l o c k i n g ∗/5 }67

Consumer1 message next_consumed ;2 w h i l e ( t r u e ) {3 r e c e i v e ( next_consumed ) ; /∗ b l o c k i n g ∗/

4 /∗ consume the i tem i n next_consumed∗/

5 }6

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 13 / 17

Page 17: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Producer and Consumer Problem

How aboub non-blocking message passing?

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 14 / 17

Page 18: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

UNIX (POSIX) and Windows IPC

Outline

1 Motivation

2 IPCShared MemoryMessage Passing

3 Producer and Consumer Problem

4 UNIX (POSIX) and Windows IPC

5 Sharing Data among Threads and Processes

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 15 / 17

Page 19: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

UNIX (POSIX) and Windows IPC

UNIX IPC

Examine the example programsI POSIX ordinary and named pipesI POSIX shared memoryI POSIX message passingI Berkeley Sockets

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 15 / 17

Page 20: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

UNIX (POSIX) and Windows IPC

Windows IPC

Examine the example programsI Windows anonymous and named pipesI Windows mail slotsI Windows shared memory

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 16 / 17

Page 21: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Sharing Data among Threads and Processes

Outline

1 Motivation

2 IPCShared MemoryMessage Passing

3 Producer and Consumer Problem

4 UNIX (POSIX) and Windows IPC

5 Sharing Data among Threads and Processes

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 17 / 17

Page 22: IPC - GitHub Pages · UNIX (POSIX) and Windows IPC UNIXIPC Examinetheexampleprograms I POSIXordinaryandnamedpipes I POSIXsharedmemory I POSIXmessagepassing I BerkeleySockets H. Chen

Sharing Data among Threads and Processes

Which data sharing or IPC mechanism to use?

I Processes, or threads, or both?I How do processes share data?I How do threads share data?

H. Chen (CUNY) CISC 3320-MW3 March 4, 2020 17 / 17