SlideShare a Scribd company logo
BITS Pilani
Pilani Campus
Dharmateja Adapa
Department of CSIS
BITS-Pilani
Operating Systems (CS F372)
Tutorial 8
BITS Pilani
Pilani Campus
Tutorial 8
Inter Process Communication
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
3
• Inter Process Communication
• Pipes
• FIFOs
• Message Queues
• Shared Memory
Agenda
CS F372 Operating Systems
BITS Pilani
Pilani Campus
Inter Process Communication
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
5
Introduction
CS F372 Operating Systems
• Inter process communication (IPC) is a mechanism which allows
processes to communicate each other. This involves synchronizing
their actions and managing shared data.
• Communication can be of two types:
– Between related processes initiating from only one process, such as
parent and child processes.
– Between unrelated processes, or two or more different processes.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
6
Introduction (contd.)
CS F372 Operating Systems
1. Pipes-
– Communication between two related processes.
– Half duplex only!
– Another Pipe required for full duplex.
2. FIFO-
– Communication between two unrelated processes.
– Full duplex.
3. Message Queues-
– Communication between two or more processes.
– Full duplex capacity.
– Posting a message and retrieving it out of the queue.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
7
Introduction (contd.)
CS F372 Operating Systems
3. Shared Memory-
– Communication between two or more processes is achieved
through a shared piece of memory among all processes.
4. Semaphores-
– Meant for synchronizing access to multiple processes.
– When one process wants to access the memory (for reading or
writing), it needs to be locked (or protected) and released when the
access is removed.
5. Signals-
– Communication between multiple processes by way of signaling.
– This means a source process will send a signal (recognized by a
number) and the destination process will handle it accordingly.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
8
UNIX IPC Taxonomy
CS F372 Operating Systems
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
9
UNIX IPC Taxonomy
CS F372 Operating Systems
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
10
Communication
CS F372 Operating Systems
• Data Transfer Facility:
– In order to communicate, one process writes data to the IPC
facility, and another process reads the data.
– These facilities require two data transfers between user memory
and kernel memory: one transfer from user memory to kernel
memory during writing, and another transfer from kernel memory
to user memory during reading.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
11
Communication (Data Transfer
Categories)
CS F372 Operating Systems
• Byte stream:
– The data exchanged via Pipes, FIFOs and datagram sockets is an
undelimited byte stream.
– Each read operation may read an arbitrary number of bytes from
the IPC facility, regardless of the size of blocks written by the
writer.
• Message:
– Data exchanged on message queues.
– These are form of delimited messages.
– Each read operation reads entire message.
– It is not possible to read part of a message, leaving the remainder
on the IPC facility; nor is it possible to read multiple messages in a
single read operation.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
12
Communication
CS F372 Operating Systems
• Shared Memory:
– Shared memory allows processes to exchange information by
placing it in a region of memory that is shared between the
processes.
– A process can make data available to other processes by placing it
in the shared memory region. Because communication doesn’t
require system calls or data transfer between user memory and
kernel memory, shared memory can provide very fast
communication.
– There is a need to synchronize operations on the shared memory.
– For example, one process should not attempt to access a data
structure in the shared memory while another process is updating
it.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
13
Pipes (Byte Stream)
CS F372 Operating Systems
• Shell Command
$ls | wc –l
• To execute the above command, the shell creates two processes,
executing ls and wc, respectively using fork() and exec() system calls.
• Two processes are connected to the pipe so that the writing process
(ls) has its standard output (file descriptor 1) joined to the write end
of the pipe, while the reading process (wc) has its standard input (file
descriptor 0) joined to the read end of the pipe.
• Pipes are unidirectional
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
14
Creating and Using Pipes
CS F372 Operating Systems
• Syntax:
#include <unistd.h>
int pipe(int filedes[2]);
• Returns 0 on success, or –1 on error.
• A successful call to pipe() returns two open file descriptors in the
array filedes: one for the read end of the pipe (filedes[0]) and one for
the write end (filedes[1]).
• we can use the read() and write() system calls to perform I/O on the
pipe.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
15
Creating and Using Pipes
CS F372 Operating Systems
BITS Pilani
Pilani Campus
C Program Examples
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
17
Pipe Between Parent and Child
Processes (fork only)
CS F372 Operating Systems
1. int main(void)
2. {
3. int pfds[2];
4. char buf[30];
5. pipe(pfds);
6. if (!fork()) {
7. printf(" CHILD: writing to the
pipen");
8. write(pfds[1], "test", 5);
9. printf(" CHILD: exitingn");
10. exit(0);
11. }
12. else {
13. printf("PARENT: reading from
pipen");
14. read(pfds[0], buf, 5);
15. printf("PARENT: read "%s"n",
buf);
16. wait(NULL);
17. }
18. return 0;
19. }
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
18
Pipe Between Parent and Child
Processes (fork only)
CS F372 Operating Systems
PARENT: reading from pipe
CHILD: writing to the pipe
CHILD: exiting
PARENT: read "test"
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
19
Pipe Between Parent and Child
Processes (close FDs)
CS F372 Operating Systems
1. int main()
2. {
3. int filedes[2];
4. if (pipe(filedes) == -1) printf("error
creating pipe n");
5. else
6. printf("Pipe Created
Successfullynfiledes[0] = %d,
7. filedes[1] = %dn", filedes[0], filedes[1]);
8. switch (fork()) { /* Create a child process */
9. case -1:
10. printf("fork failed n");
11. case 0: /* Child */
12. printf("Child Process.......%dn",getpid());
13. if (close(filedes[1]) == -1) printf("failed to
close n");
14. else
15. printf("Closed write end, read file descr =
%dn",filedes[0]);
16. break;
17. default: /* Parent */
18. wait(NULL);
19. printf("Parent Process.......%dn",getpid());
20. if (close(filedes[0]) == -1) printf("failed to
close n");
21. else
22. printf("Closed read end, write file descr =
%dn",filedes[1]);
23. break; } }
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
20
Pipe Between Parent and Child
Processes (close FDs)
CS F372 Operating Systems
Pipe Created Successfully
filedes[0] = 3,filedes[1] = 4
Child Process.......122
Closed write end, read file descr = 3
Parent Process.......121
Closed read end, write file descr = 4
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
21
Using a pipe to communicate
between a parent and child
process
CS F372 Operating Systems
Int main(int argc, char *argv[])
{
int pfd[2];
char buf[BUF_SIZE];
ssize_t numRead;
if (argc != 2 || strcmp(argv[1], "--help") == 0)
printf("%s %s n", argv[0],argv[1]);
if (pipe(pfd) == -1)
printf("failed to create pipen");
else
printf("Pipe created successfullynread file des = %d, write file des =
%dn",pfd[0],pfd[1]);
switch (fork()) {
case -1:
printf("failed to forkn");
case 0:
/* Child - reads from pipe */
printf("Child Created.........%dn",getpid());
numRead = read(pfd[0], buf, BUF_SIZE);
printf("text read from pipe by child : %s , numRead =
%dn",buf,numRead);
if (numRead == -1) printf("read errorn");
if (numRead == 0) break; /* End-of-file */
if (write(STDOUT, buf, numRead) != numRead)
printf("child - partial/failed writen");
write(STDOUT, "n", 1);
if (close(pfd[0]) == -1)printf("failed to close");
exit(EXIT_SUCCESS);
default:
/* Parent - writes to pipe */
printf("Parent continues %dn",getpid());
if (close(pfd[0]) == -1) /* Read end is unused */
printf("failed to close read end in - parentn");
if(write(pfd[1],argv[1],strlen(argv[1]))!=strlen(argv[1]))
printf("parent - partial/failed writen");
else
printf("Parent successfully write to pipe (%d): %sn",pfd[1],argv[1]);
if (close(pfd[1]) == -1) printf("failed to close");
wait(NULL);
exit(EXIT_SUCCESS);
} }
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
22
Using a pipe to communicate
between a parent and child
process
CS F372 Operating Systems
Pipe created successfully
read file des = 3, write file des = 4
Parent successfully write to pipe (4): BITS
Child Created.........1750
text read from pipe by child : BITS , numRead = 4
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
23
Usage of Pipes to execute
commands like ls | wc
CS F372 Operating Systems
int pfd[2];
pipe(pfd); /* Allocates (say) file descriptors 3 and 4 for pipe */
/* Other steps here, e.g., fork() */
close(STDOUT); /* Free file descriptor 1 */
dup(pfd[1]); /* Duplication uses lowest free file descriptor, i.e., fd 1 */
dup2(pfd[1], STDOUT); /* Close descriptor 1, and reopen bound to write end
of pipe */
BITS Pilani
Pilani Campus
Questions?
BITS Pilani
Pilani Campus
Thank You!

More Related Content

PPT
15237197jvkhcjkgckghckghxckgjckufliufi.ppt
PPTX
Unit 7
PPT
5.IPC.ppt JSS science and technology university
PPTX
Unix-module4 -chap2.123156456484844546pptx
PPTX
Process management
PDF
PDF
Operating System Lecture Notes Mit 6828 Itebooks
15237197jvkhcjkgckghckghxckgjckufliufi.ppt
Unit 7
5.IPC.ppt JSS science and technology university
Unix-module4 -chap2.123156456484844546pptx
Process management
Operating System Lecture Notes Mit 6828 Itebooks

Similar to Operating System Tutorial 8, Bits Pilani (20)

PDF
Inter process communication using Linux System Calls
DOCX
PDF
System Programming - Interprocess communication
PPT
Lecture03-IPC.ppt
PDF
Processes and Threads
DOCX
Program Assignment Process ManagementObjective This program a.docx
PPTX
Process Communication IPC in LINUX Environments
PDF
Mychurch File Upload
PPT
PIPES in Unix programming in computer science and engineering.PPT
PPTX
Process creation and termination In Operating System
PPTX
Inter-Process Communication Using System V Message Queues.pptx
PDF
Inter process communication
DOCX
httplinux.die.netman3execfork() creates a new process by.docx
PDF
Lecture 3_Processes in Operating Systems.pdf
PPT
What is-a-computer-process-os
PDF
Unix Programs
PPTX
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
DOC
Unix systems programming interprocess communication/tutorialoutlet
PPTX
ipc.pptx
Inter process communication using Linux System Calls
System Programming - Interprocess communication
Lecture03-IPC.ppt
Processes and Threads
Program Assignment Process ManagementObjective This program a.docx
Process Communication IPC in LINUX Environments
Mychurch File Upload
PIPES in Unix programming in computer science and engineering.PPT
Process creation and termination In Operating System
Inter-Process Communication Using System V Message Queues.pptx
Inter process communication
httplinux.die.netman3execfork() creates a new process by.docx
Lecture 3_Processes in Operating Systems.pdf
What is-a-computer-process-os
Unix Programs
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Unix systems programming interprocess communication/tutorialoutlet
ipc.pptx
Ad

Recently uploaded (20)

PPT
Mechanical Engineering MATERIALS Selection
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Digital Logic Computer Design lecture notes
PPTX
Welding lecture in detail for understanding
PPTX
web development for engineering and engineering
PPTX
Sustainable Sites - Green Building Construction
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPT
Project quality management in manufacturing
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Lecture Notes Electrical Wiring System Components
DOCX
573137875-Attendance-Management-System-original
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
Mechanical Engineering MATERIALS Selection
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Digital Logic Computer Design lecture notes
Welding lecture in detail for understanding
web development for engineering and engineering
Sustainable Sites - Green Building Construction
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Project quality management in manufacturing
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Lecture Notes Electrical Wiring System Components
573137875-Attendance-Management-System-original
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
R24 SURVEYING LAB MANUAL for civil enggi
Automation-in-Manufacturing-Chapter-Introduction.pdf
Ad

Operating System Tutorial 8, Bits Pilani

  • 1. BITS Pilani Pilani Campus Dharmateja Adapa Department of CSIS BITS-Pilani Operating Systems (CS F372) Tutorial 8
  • 2. BITS Pilani Pilani Campus Tutorial 8 Inter Process Communication
  • 3. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 3 • Inter Process Communication • Pipes • FIFOs • Message Queues • Shared Memory Agenda CS F372 Operating Systems
  • 4. BITS Pilani Pilani Campus Inter Process Communication
  • 5. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 5 Introduction CS F372 Operating Systems • Inter process communication (IPC) is a mechanism which allows processes to communicate each other. This involves synchronizing their actions and managing shared data. • Communication can be of two types: – Between related processes initiating from only one process, such as parent and child processes. – Between unrelated processes, or two or more different processes.
  • 6. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 6 Introduction (contd.) CS F372 Operating Systems 1. Pipes- – Communication between two related processes. – Half duplex only! – Another Pipe required for full duplex. 2. FIFO- – Communication between two unrelated processes. – Full duplex. 3. Message Queues- – Communication between two or more processes. – Full duplex capacity. – Posting a message and retrieving it out of the queue.
  • 7. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 7 Introduction (contd.) CS F372 Operating Systems 3. Shared Memory- – Communication between two or more processes is achieved through a shared piece of memory among all processes. 4. Semaphores- – Meant for synchronizing access to multiple processes. – When one process wants to access the memory (for reading or writing), it needs to be locked (or protected) and released when the access is removed. 5. Signals- – Communication between multiple processes by way of signaling. – This means a source process will send a signal (recognized by a number) and the destination process will handle it accordingly.
  • 8. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 8 UNIX IPC Taxonomy CS F372 Operating Systems
  • 9. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 9 UNIX IPC Taxonomy CS F372 Operating Systems
  • 10. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 10 Communication CS F372 Operating Systems • Data Transfer Facility: – In order to communicate, one process writes data to the IPC facility, and another process reads the data. – These facilities require two data transfers between user memory and kernel memory: one transfer from user memory to kernel memory during writing, and another transfer from kernel memory to user memory during reading.
  • 11. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 11 Communication (Data Transfer Categories) CS F372 Operating Systems • Byte stream: – The data exchanged via Pipes, FIFOs and datagram sockets is an undelimited byte stream. – Each read operation may read an arbitrary number of bytes from the IPC facility, regardless of the size of blocks written by the writer. • Message: – Data exchanged on message queues. – These are form of delimited messages. – Each read operation reads entire message. – It is not possible to read part of a message, leaving the remainder on the IPC facility; nor is it possible to read multiple messages in a single read operation.
  • 12. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 12 Communication CS F372 Operating Systems • Shared Memory: – Shared memory allows processes to exchange information by placing it in a region of memory that is shared between the processes. – A process can make data available to other processes by placing it in the shared memory region. Because communication doesn’t require system calls or data transfer between user memory and kernel memory, shared memory can provide very fast communication. – There is a need to synchronize operations on the shared memory. – For example, one process should not attempt to access a data structure in the shared memory while another process is updating it.
  • 13. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 13 Pipes (Byte Stream) CS F372 Operating Systems • Shell Command $ls | wc –l • To execute the above command, the shell creates two processes, executing ls and wc, respectively using fork() and exec() system calls. • Two processes are connected to the pipe so that the writing process (ls) has its standard output (file descriptor 1) joined to the write end of the pipe, while the reading process (wc) has its standard input (file descriptor 0) joined to the read end of the pipe. • Pipes are unidirectional
  • 14. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 14 Creating and Using Pipes CS F372 Operating Systems • Syntax: #include <unistd.h> int pipe(int filedes[2]); • Returns 0 on success, or –1 on error. • A successful call to pipe() returns two open file descriptors in the array filedes: one for the read end of the pipe (filedes[0]) and one for the write end (filedes[1]). • we can use the read() and write() system calls to perform I/O on the pipe.
  • 15. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 15 Creating and Using Pipes CS F372 Operating Systems
  • 16. BITS Pilani Pilani Campus C Program Examples
  • 17. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 17 Pipe Between Parent and Child Processes (fork only) CS F372 Operating Systems 1. int main(void) 2. { 3. int pfds[2]; 4. char buf[30]; 5. pipe(pfds); 6. if (!fork()) { 7. printf(" CHILD: writing to the pipen"); 8. write(pfds[1], "test", 5); 9. printf(" CHILD: exitingn"); 10. exit(0); 11. } 12. else { 13. printf("PARENT: reading from pipen"); 14. read(pfds[0], buf, 5); 15. printf("PARENT: read "%s"n", buf); 16. wait(NULL); 17. } 18. return 0; 19. }
  • 18. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 18 Pipe Between Parent and Child Processes (fork only) CS F372 Operating Systems PARENT: reading from pipe CHILD: writing to the pipe CHILD: exiting PARENT: read "test"
  • 19. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 19 Pipe Between Parent and Child Processes (close FDs) CS F372 Operating Systems 1. int main() 2. { 3. int filedes[2]; 4. if (pipe(filedes) == -1) printf("error creating pipe n"); 5. else 6. printf("Pipe Created Successfullynfiledes[0] = %d, 7. filedes[1] = %dn", filedes[0], filedes[1]); 8. switch (fork()) { /* Create a child process */ 9. case -1: 10. printf("fork failed n"); 11. case 0: /* Child */ 12. printf("Child Process.......%dn",getpid()); 13. if (close(filedes[1]) == -1) printf("failed to close n"); 14. else 15. printf("Closed write end, read file descr = %dn",filedes[0]); 16. break; 17. default: /* Parent */ 18. wait(NULL); 19. printf("Parent Process.......%dn",getpid()); 20. if (close(filedes[0]) == -1) printf("failed to close n"); 21. else 22. printf("Closed read end, write file descr = %dn",filedes[1]); 23. break; } }
  • 20. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 20 Pipe Between Parent and Child Processes (close FDs) CS F372 Operating Systems Pipe Created Successfully filedes[0] = 3,filedes[1] = 4 Child Process.......122 Closed write end, read file descr = 3 Parent Process.......121 Closed read end, write file descr = 4
  • 21. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 21 Using a pipe to communicate between a parent and child process CS F372 Operating Systems Int main(int argc, char *argv[]) { int pfd[2]; char buf[BUF_SIZE]; ssize_t numRead; if (argc != 2 || strcmp(argv[1], "--help") == 0) printf("%s %s n", argv[0],argv[1]); if (pipe(pfd) == -1) printf("failed to create pipen"); else printf("Pipe created successfullynread file des = %d, write file des = %dn",pfd[0],pfd[1]); switch (fork()) { case -1: printf("failed to forkn"); case 0: /* Child - reads from pipe */ printf("Child Created.........%dn",getpid()); numRead = read(pfd[0], buf, BUF_SIZE); printf("text read from pipe by child : %s , numRead = %dn",buf,numRead); if (numRead == -1) printf("read errorn"); if (numRead == 0) break; /* End-of-file */ if (write(STDOUT, buf, numRead) != numRead) printf("child - partial/failed writen"); write(STDOUT, "n", 1); if (close(pfd[0]) == -1)printf("failed to close"); exit(EXIT_SUCCESS); default: /* Parent - writes to pipe */ printf("Parent continues %dn",getpid()); if (close(pfd[0]) == -1) /* Read end is unused */ printf("failed to close read end in - parentn"); if(write(pfd[1],argv[1],strlen(argv[1]))!=strlen(argv[1])) printf("parent - partial/failed writen"); else printf("Parent successfully write to pipe (%d): %sn",pfd[1],argv[1]); if (close(pfd[1]) == -1) printf("failed to close"); wait(NULL); exit(EXIT_SUCCESS); } }
  • 22. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 22 Using a pipe to communicate between a parent and child process CS F372 Operating Systems Pipe created successfully read file des = 3, write file des = 4 Parent successfully write to pipe (4): BITS Child Created.........1750 text read from pipe by child : BITS , numRead = 4
  • 23. Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus 23 Usage of Pipes to execute commands like ls | wc CS F372 Operating Systems int pfd[2]; pipe(pfd); /* Allocates (say) file descriptors 3 and 4 for pipe */ /* Other steps here, e.g., fork() */ close(STDOUT); /* Free file descriptor 1 */ dup(pfd[1]); /* Duplication uses lowest free file descriptor, i.e., fd 1 */ dup2(pfd[1], STDOUT); /* Close descriptor 1, and reopen bound to write end of pipe */