SlideShare a Scribd company logo
1 of 33
National University of Computer
and Emerging Sciences
Spring 2023
Inter-Process Communication
2
Interprocess Communication
 A process has access to the memory which
constitutes its own address space.
 When a child process is created, the only way to
communicate between a parent and a child process
is:
 The variables are replicas
 The parent receives the exit status of the child
 So far, we’ve discussed communication
mechanisms only during process
creation/termination
 Processes may need to communicate during their
life time.
3
Cooperating Processes
 Independent process cannot affect or be affected by the
execution of another process.
 Cooperating process can affect or be affected by the
execution of another process
 Advantages of process cooperation
 Information sharing
 Computation speed-up:
 make use of multiple processing elements
 Modularity
 Convenience:
 editing, printing, compiling in parallel
 Dangers of process cooperation
 Data corruption, deadlocks, increased complexity
 Requires processes to synchronize their processing
4
Purposes for IPC
 IPC allows processes to communicate and
synchronize their actions without sharing the
same address space
 Data Transfer
 Sharing Data
 Event notification
 Resource Sharing and Synchronization
5
IPC Mechanisms
 Mechanisms used for communication and synchronization
 Message Passing
 message passing interfaces, mailboxes and message queues
 sockets, pipes
 Shared Memory: Non-message passing systems
 Synchronization – primitives such as semaphores to higher level
mechanisms such as monitors
 Event Notification - UNIX signals
 We will defer a detailed discussion of synchronization mechanisms and
concurrency until a later class
 Here we want to focus on some common (and fundamental) IPC
mechanisms
6
Message Passing
 In a Message system there are no shared variables.
 IPC facility provides two operations for fixed or variable
sized message:
 send(message)
 receive(message)
 If processes P and Q wish to communicate, they need
to:
 establish a communication link
 exchange messages via send and receive
 Implementation of communication link
 physical (e.g., memory, network etc)
 logical (e.g., syntax and semantics, abstractions)
7
Message Passing Systems
 Exchange messages over a communication link
 Methods for implementing the communication link
and primitives (send/receive):
1. Direct or Indirect communications (Naming)
2. Symmetric or Asymmetric communications
(blocking versus non-blocking)
3. Buffering
4. Send-by-copy or send-by-reference
5. fixed or variable sized messages
Direct Communication – Internet and Sockets
 Processes must name each other explicitly:
 Symmetric Addressing
 send (P, message) – send to process P
 receive(Q, message) – receive from Q
 Asymmetric Addressing
 send (P, message) – send to process P
 receive(id, message) – rx from any; system sets id = sender
 Properties of communication link
 Links established automatically between pairs
 processes must know each others ID
 Exactly one link per pair of communicating processes
 Disadvantage: a process must know the name or ID of
the process(es) it wishes to communicate with
9
Indirect Communication
 Messages are sent to or received from mailboxes (also
referred to as ports).
 Each mailbox has a unique id.
 Processes can communicate only if they share a mailbox.
 Primitives:
 send(A, message) – send a message to mailbox A
 receive(A, message) – receive a message from mailbox A
 Properties of communication link
 Link established only if processes share a common mailbox
 A link may be associated with more than 2 processes.
 Each pair of processes may share several communication links.
Indirect Communication-Ownership
 process owns (i.e. mailbox is implemented in user
space):
 only the owner may receive messages through this mailbox.
 Other processes may only send.
 When process terminates any “owned” mailboxes are destroyed.
 kernel owns
 then mechanisms provided to create, delete, send and receive
through mailboxes.
 Process that creates mailbox owns it (and so may receive
through it)
 but may transfer ownership to another process.
Indirect Communication
 Mailbox sharing:
 P1, P2, and P3 share mailbox A.
 P1, sends; P2 and P3 receive.
 Who gets the message?
 Solutions
 Allow a link to be associated with at most two
processes.
 OR Allow only one process at a time to execute a
receive operation.
 OR Allow the system to select arbitrarily the receiver.
Sender is notified who the receiver was
Synchronization
 Message passing may be either blocking or
non-blocking.
 blocking send:
 sender blocked until message received by mailbox or
process
 nonblocking send:
 sender resumes operation immediately after sending
 blocking receive:
 receiver blocks until a message is available
 nonblocking receive:
 receiver returns immediately with either a valid or null
message.
13
Buffering
 All messaging system require framework to temporarily
buffer messages.
 These queues are implemented in one of three ways:
1. Zero capacity
No messages may be queued within the link, requires sender to
block until receiver retrieves message.
2. Bounded capacity
Link has finite number of message buffers. If no buffers are available
then sender must block until one is freed up.
3. Unbounded capacity
Link has unlimited buffer space, consequently send never needs to
block.
14
Unix IPC
Process
Process Process
Process Process
Process
Shared
info
filesystem
Shared
memory
Kernel
15
Unix IPC
 Message passing
 Pipes
 FIFOs
 Message queues
 Synchronization
 Mutexes
 Condition variables
 read-write locks
 Semaphores
 Shared memory
 Anonymous
 Named
 Procedure calls
 RPC
 Event notification
 Signals
16
File Descriptors
 The PCB (task_struct) of each process contains a
pointer to a file_struct
files …
fd[0]
fd[1]
…
fd[255]
f_mode
f_pos
f_inode
f_op
…
…
.
PCB
file_struct
file
File operation
routines
17
File Descriptors
 The files_struct contains pointers to file data
structures
 Each one describes a file being used by this
process.
 f_mode: describes file mode, read only, read
and write or write only.
 f_pos: holds the position in the file where the
next read or write operation will occur.
 f_inode: points at the actual file
18
File Descriptors
 Every time a file is opened, one of the free
file pointers in the files_struct is used to point
to the new file structure.
 Linux processes expect three file descriptors
to be open when they start.
 These are known as standard input, standard
output and standard error
19
File Descriptors
 The program treat them all as files.
 These three are usually inherited from the
creating parent process.
 All accesses to files are via standard system
calls which pass or return file descriptors.
 standard input, standard output and standard
error have file descriptors 0, 1 and 2.
20
File Descriptors
 char buffer[10];
 Read from standard input (by default it is
keyboard)
 read(0,buffer,5);
 Write to standard output (by default is is
monitor))
 write(1,buffer,5);
 By changing the file descriptors we can write
to files
 fread/fwrite etc are wrappers around the
above read/write functions
21
Pipes: Shared info in kernel’s memory
Pipe
write() read()
Pfd[0] Pfd[1]
Buffer in kernel’s
memory
22
Pipes
 A pipe is implemented using two file data
structures which both point at the same
temporary data node.
 This hides the underlying differences from the
generic system calls which read and write to
ordinary files
 Thus, reading/writing to a pipe is similar to
reading/writing to a file
23
Buffer not File on
Hard disk
files …
fd[2]
fd[3]
…
fd[255]
f_mode
f_pos
f_inode
f_op
…
…
Pipe.
PCB
file table
file
Pipe operation
routines
f_mode
f_pos
f_inode
…
…
file Pipe operation
routines
f_op
Pipes
24
Pipe Creation
 #include <unistd.h>
 int pipe(int filedes[2]);
 Creates a pair of file descriptors pointing to a pipe
inode
 Places them in the array pointed to by filedes
 filedes[0] is for reading
 filedes[1] is for writing.
 On success, zero is returned.
 On error, -1 is returned
25
flow of data
pfds[1]
pfds[0]
Pipe
Process
Kernel
Pipe Creation
int main()
{ int pfds[2];
if (pipe(pfds) == -1)
{ perror("pipe");
exit(1); }
}
26
 int read(int filedescriptor,
char *buffer, int bytetoread);
 int write(int
filedescriptor,char *buffer,int
bytetowrite);
Reading/Writing from/to a Pipe
int main()
{ int pfds[2];
char buf[30];
if (pipe(pfds) == -1) {
perror("pipe");
exit(1); }
printf("writing to file descriptor #%dn",
pfds[1]);
write(pfds[1], "test", 5);
printf("reading from file descriptor
#%dn",pfds[0]);
read(pfds[0], buf, 5);
printf("read %sn", buf);
}
Example
write(1, "test", 5);????
read(0, buf, 5);?????
28
A Channel between two processes
 Remember: the two processes have a parent / child
relationship
 The child was created by a fork() call that was
executed by the parent.
 The child process is an image of the parent process
 Thus, all the file descriptors that are opened by the
parent are now available in the child.
29
 The file descriptors refer to the same I/O entity, in this
case a pipe.
 The pipe is inherited by the child
 And may be passed on to the grand-children by the child
process or other children by the parent.
A Channel between two processes
30
flow of data
Pipe
Process
Kernel
pfds[1]
pfds[0]
Child
pfds[1]
pfds[0]
Parent
2. Fork()
A Channel between two
processes
1. Pipe()
31
flow of data
Pipe
Process
Kernel
pfds[1]
pfds[0]
Child
pfds[1]
pfds[0]
Parent
• To allow one way communication each
process should close one end of the pipe.
A Channel between two
processes
2. Fork()
1. Pipe()
3. close(pfds[1])
4. close(pfds[0])
32
Closing the pipe
 The file descriptors associated with a pipe
can be closed with the close(fd) system call
An Example of pipes with fork
int main() {
int pfds[2];
char buf[30];
pipe(pfds);…………………………………………1
if (!fork()) ………………………………………2
{ close(pfds[0]);…………………………………3
printf(" CHILD: writing to the pipen");
write(pfds[1], "test", 5);
printf(" CHILD: exitingn");
exit(0);
}
else { close(pfds[1]);…………………………………………4
printf("PARENT: reading from pipen");
read(pfds[0], buf, 5);
printf( "PARENT: read "%s"n", buf);
wait(NULL);
}
}

More Related Content

Similar to Lecture03-IPC.ppt

Inter-Process communication in Operating System.ppt
Inter-Process communication in Operating System.pptInter-Process communication in Operating System.ppt
Inter-Process communication in Operating System.pptNitihyaAshwinC
 
Process Management.ppt
Process Management.pptProcess Management.ppt
Process Management.pptJeelBhanderi4
 
5_Interprocess Communication.pptx
5_Interprocess Communication.pptx5_Interprocess Communication.pptx
5_Interprocess Communication.pptxssuser2adefd1
 
Module-6 process managedf;jsovj;ksdv;sdkvnksdnvldknvlkdfsment.ppt
Module-6 process managedf;jsovj;ksdv;sdkvnksdnvldknvlkdfsment.pptModule-6 process managedf;jsovj;ksdv;sdkvnksdnvldknvlkdfsment.ppt
Module-6 process managedf;jsovj;ksdv;sdkvnksdnvldknvlkdfsment.pptKAnurag2
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Ravindra Raju Kolahalam
 
Inter process communication
Inter process communicationInter process communication
Inter process communicationGowriLatha1
 
Task communication
Task communicationTask communication
Task communication1jayanti
 
operating system for computer engineering ch3.ppt
operating system for computer engineering ch3.pptoperating system for computer engineering ch3.ppt
operating system for computer engineering ch3.pptgezaegebre1
 
Characterization of communication.ppt
Characterization of communication.pptCharacterization of communication.ppt
Characterization of communication.pptAthira Ravindranathan
 
Operating system 19 interacting processes and ipc
Operating system 19 interacting processes and ipcOperating system 19 interacting processes and ipc
Operating system 19 interacting processes and ipcVaibhav Khanna
 
Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式艾鍗科技
 
ipc.pptx
ipc.pptxipc.pptx
ipc.pptxSuhanB
 

Similar to Lecture03-IPC.ppt (20)

Inter-Process communication in Operating System.ppt
Inter-Process communication in Operating System.pptInter-Process communication in Operating System.ppt
Inter-Process communication in Operating System.ppt
 
Process Management.ppt
Process Management.pptProcess Management.ppt
Process Management.ppt
 
5_Interprocess Communication.pptx
5_Interprocess Communication.pptx5_Interprocess Communication.pptx
5_Interprocess Communication.pptx
 
Ch4
Ch4Ch4
Ch4
 
Module-6 process managedf;jsovj;ksdv;sdkvnksdnvldknvlkdfsment.ppt
Module-6 process managedf;jsovj;ksdv;sdkvnksdnvldknvlkdfsment.pptModule-6 process managedf;jsovj;ksdv;sdkvnksdnvldknvlkdfsment.ppt
Module-6 process managedf;jsovj;ksdv;sdkvnksdnvldknvlkdfsment.ppt
 
Distributed System
Distributed System Distributed System
Distributed System
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Task communication
Task communicationTask communication
Task communication
 
operating system for computer engineering ch3.ppt
operating system for computer engineering ch3.pptoperating system for computer engineering ch3.ppt
operating system for computer engineering ch3.ppt
 
Lecture 06
Lecture 06Lecture 06
Lecture 06
 
1st year OSI Model .pptx
1st year OSI Model .pptx1st year OSI Model .pptx
1st year OSI Model .pptx
 
Unix Administration 5
Unix Administration 5Unix Administration 5
Unix Administration 5
 
Characterization of communication.ppt
Characterization of communication.pptCharacterization of communication.ppt
Characterization of communication.ppt
 
Operating system 19 interacting processes and ipc
Operating system 19 interacting processes and ipcOperating system 19 interacting processes and ipc
Operating system 19 interacting processes and ipc
 
Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式
 
ipc.pptx
ipc.pptxipc.pptx
ipc.pptx
 
Ipc
IpcIpc
Ipc
 
App A
App AApp A
App A
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 

Recently uploaded

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Recently uploaded (20)

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Lecture03-IPC.ppt

  • 1. National University of Computer and Emerging Sciences Spring 2023 Inter-Process Communication
  • 2. 2 Interprocess Communication  A process has access to the memory which constitutes its own address space.  When a child process is created, the only way to communicate between a parent and a child process is:  The variables are replicas  The parent receives the exit status of the child  So far, we’ve discussed communication mechanisms only during process creation/termination  Processes may need to communicate during their life time.
  • 3. 3 Cooperating Processes  Independent process cannot affect or be affected by the execution of another process.  Cooperating process can affect or be affected by the execution of another process  Advantages of process cooperation  Information sharing  Computation speed-up:  make use of multiple processing elements  Modularity  Convenience:  editing, printing, compiling in parallel  Dangers of process cooperation  Data corruption, deadlocks, increased complexity  Requires processes to synchronize their processing
  • 4. 4 Purposes for IPC  IPC allows processes to communicate and synchronize their actions without sharing the same address space  Data Transfer  Sharing Data  Event notification  Resource Sharing and Synchronization
  • 5. 5 IPC Mechanisms  Mechanisms used for communication and synchronization  Message Passing  message passing interfaces, mailboxes and message queues  sockets, pipes  Shared Memory: Non-message passing systems  Synchronization – primitives such as semaphores to higher level mechanisms such as monitors  Event Notification - UNIX signals  We will defer a detailed discussion of synchronization mechanisms and concurrency until a later class  Here we want to focus on some common (and fundamental) IPC mechanisms
  • 6. 6 Message Passing  In a Message system there are no shared variables.  IPC facility provides two operations for fixed or variable sized message:  send(message)  receive(message)  If processes P and Q wish to communicate, they need to:  establish a communication link  exchange messages via send and receive  Implementation of communication link  physical (e.g., memory, network etc)  logical (e.g., syntax and semantics, abstractions)
  • 7. 7 Message Passing Systems  Exchange messages over a communication link  Methods for implementing the communication link and primitives (send/receive): 1. Direct or Indirect communications (Naming) 2. Symmetric or Asymmetric communications (blocking versus non-blocking) 3. Buffering 4. Send-by-copy or send-by-reference 5. fixed or variable sized messages
  • 8. Direct Communication – Internet and Sockets  Processes must name each other explicitly:  Symmetric Addressing  send (P, message) – send to process P  receive(Q, message) – receive from Q  Asymmetric Addressing  send (P, message) – send to process P  receive(id, message) – rx from any; system sets id = sender  Properties of communication link  Links established automatically between pairs  processes must know each others ID  Exactly one link per pair of communicating processes  Disadvantage: a process must know the name or ID of the process(es) it wishes to communicate with
  • 9. 9 Indirect Communication  Messages are sent to or received from mailboxes (also referred to as ports).  Each mailbox has a unique id.  Processes can communicate only if they share a mailbox.  Primitives:  send(A, message) – send a message to mailbox A  receive(A, message) – receive a message from mailbox A  Properties of communication link  Link established only if processes share a common mailbox  A link may be associated with more than 2 processes.  Each pair of processes may share several communication links.
  • 10. Indirect Communication-Ownership  process owns (i.e. mailbox is implemented in user space):  only the owner may receive messages through this mailbox.  Other processes may only send.  When process terminates any “owned” mailboxes are destroyed.  kernel owns  then mechanisms provided to create, delete, send and receive through mailboxes.  Process that creates mailbox owns it (and so may receive through it)  but may transfer ownership to another process.
  • 11. Indirect Communication  Mailbox sharing:  P1, P2, and P3 share mailbox A.  P1, sends; P2 and P3 receive.  Who gets the message?  Solutions  Allow a link to be associated with at most two processes.  OR Allow only one process at a time to execute a receive operation.  OR Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was
  • 12. Synchronization  Message passing may be either blocking or non-blocking.  blocking send:  sender blocked until message received by mailbox or process  nonblocking send:  sender resumes operation immediately after sending  blocking receive:  receiver blocks until a message is available  nonblocking receive:  receiver returns immediately with either a valid or null message.
  • 13. 13 Buffering  All messaging system require framework to temporarily buffer messages.  These queues are implemented in one of three ways: 1. Zero capacity No messages may be queued within the link, requires sender to block until receiver retrieves message. 2. Bounded capacity Link has finite number of message buffers. If no buffers are available then sender must block until one is freed up. 3. Unbounded capacity Link has unlimited buffer space, consequently send never needs to block.
  • 14. 14 Unix IPC Process Process Process Process Process Process Shared info filesystem Shared memory Kernel
  • 15. 15 Unix IPC  Message passing  Pipes  FIFOs  Message queues  Synchronization  Mutexes  Condition variables  read-write locks  Semaphores  Shared memory  Anonymous  Named  Procedure calls  RPC  Event notification  Signals
  • 16. 16 File Descriptors  The PCB (task_struct) of each process contains a pointer to a file_struct files … fd[0] fd[1] … fd[255] f_mode f_pos f_inode f_op … … . PCB file_struct file File operation routines
  • 17. 17 File Descriptors  The files_struct contains pointers to file data structures  Each one describes a file being used by this process.  f_mode: describes file mode, read only, read and write or write only.  f_pos: holds the position in the file where the next read or write operation will occur.  f_inode: points at the actual file
  • 18. 18 File Descriptors  Every time a file is opened, one of the free file pointers in the files_struct is used to point to the new file structure.  Linux processes expect three file descriptors to be open when they start.  These are known as standard input, standard output and standard error
  • 19. 19 File Descriptors  The program treat them all as files.  These three are usually inherited from the creating parent process.  All accesses to files are via standard system calls which pass or return file descriptors.  standard input, standard output and standard error have file descriptors 0, 1 and 2.
  • 20. 20 File Descriptors  char buffer[10];  Read from standard input (by default it is keyboard)  read(0,buffer,5);  Write to standard output (by default is is monitor))  write(1,buffer,5);  By changing the file descriptors we can write to files  fread/fwrite etc are wrappers around the above read/write functions
  • 21. 21 Pipes: Shared info in kernel’s memory Pipe write() read() Pfd[0] Pfd[1] Buffer in kernel’s memory
  • 22. 22 Pipes  A pipe is implemented using two file data structures which both point at the same temporary data node.  This hides the underlying differences from the generic system calls which read and write to ordinary files  Thus, reading/writing to a pipe is similar to reading/writing to a file
  • 23. 23 Buffer not File on Hard disk files … fd[2] fd[3] … fd[255] f_mode f_pos f_inode f_op … … Pipe. PCB file table file Pipe operation routines f_mode f_pos f_inode … … file Pipe operation routines f_op Pipes
  • 24. 24 Pipe Creation  #include <unistd.h>  int pipe(int filedes[2]);  Creates a pair of file descriptors pointing to a pipe inode  Places them in the array pointed to by filedes  filedes[0] is for reading  filedes[1] is for writing.  On success, zero is returned.  On error, -1 is returned
  • 25. 25 flow of data pfds[1] pfds[0] Pipe Process Kernel Pipe Creation int main() { int pfds[2]; if (pipe(pfds) == -1) { perror("pipe"); exit(1); } }
  • 26. 26  int read(int filedescriptor, char *buffer, int bytetoread);  int write(int filedescriptor,char *buffer,int bytetowrite); Reading/Writing from/to a Pipe
  • 27. int main() { int pfds[2]; char buf[30]; if (pipe(pfds) == -1) { perror("pipe"); exit(1); } printf("writing to file descriptor #%dn", pfds[1]); write(pfds[1], "test", 5); printf("reading from file descriptor #%dn",pfds[0]); read(pfds[0], buf, 5); printf("read %sn", buf); } Example write(1, "test", 5);???? read(0, buf, 5);?????
  • 28. 28 A Channel between two processes  Remember: the two processes have a parent / child relationship  The child was created by a fork() call that was executed by the parent.  The child process is an image of the parent process  Thus, all the file descriptors that are opened by the parent are now available in the child.
  • 29. 29  The file descriptors refer to the same I/O entity, in this case a pipe.  The pipe is inherited by the child  And may be passed on to the grand-children by the child process or other children by the parent. A Channel between two processes
  • 31. 31 flow of data Pipe Process Kernel pfds[1] pfds[0] Child pfds[1] pfds[0] Parent • To allow one way communication each process should close one end of the pipe. A Channel between two processes 2. Fork() 1. Pipe() 3. close(pfds[1]) 4. close(pfds[0])
  • 32. 32 Closing the pipe  The file descriptors associated with a pipe can be closed with the close(fd) system call
  • 33. An Example of pipes with fork int main() { int pfds[2]; char buf[30]; pipe(pfds);…………………………………………1 if (!fork()) ………………………………………2 { close(pfds[0]);…………………………………3 printf(" CHILD: writing to the pipen"); write(pfds[1], "test", 5); printf(" CHILD: exitingn"); exit(0); } else { close(pfds[1]);…………………………………………4 printf("PARENT: reading from pipen"); read(pfds[0], buf, 5); printf( "PARENT: read "%s"n", buf); wait(NULL); } }

Editor's Notes

  1. Two way communication ??