SlideShare a Scribd company logo
Linux System
Programming
Inter Process Communication (IPC)
Pipes
Engr. Rashid Farid Chishti
chishti@iiu.edu.pk
https://youtube.com/rfchishti
https://sites.google.com/site/chishti
 Processes and Signals form a fundamental part of the Linux operating environment. They
control almost all activities performed by Linux computer system.
 We use the term pipe to mean connecting a data flow from one process to another.
 Sometimes while using shell commands you attach, or pipe, the output of one process to
the input of another. e.g. $ ls -al | less
 The shell arranges the standard input and output of the two commands, so that
 The standard input to the command ls -al comes from the terminal keyboard.
 The standard output from the command ls -al is fed to another command less as its
standard input.
 The standard output from the command less is connected to the terminal screen.
What Is a Pipe?
What Is a Pipe?
Visual representation of the process
 The easiest way of passing data between two programs is with the popen and
pclose functions.
 These have the following prototypes:
#include <stdio.h>
FILE *popen(const char *command, const char *open_mode);
int pclose(FILE *stream_to_close);
 The popen function allows a program to invoke another program as a new
process and either pass data to it or receive data from it.
 The command string is the name of the program to run.
 open_mode must be either “r” or “w”.
Process Pipes
 stream_to_close the value (a pointer) returned by popen(), is passed to
pclose().
 pclose() closes the pipe descriptor
 Let’s try a simple popen and pclose example, popen1.c.
 You’ll use popen in a program to access information from uname. The uname -
a command prints system information, including the machine type, the OS
name, version and release, and the machine’s network name.
 Having initialized the program, you open the pipe to uname, making it
readable and setting read_fp to point to the output.
 At the end, the pipe pointed to by read_fp is closed.
Programming Example
#include <unistd.h> #include <stdlib.h>
#include <stdio.h> #include <string.h>
int main(){
int chars_read; FILE *read_fp;
char buffer[BUFSIZ + 1]; // 8192+1
memset(buffer, '0', sizeof(buffer));
read_fp = popen("uname -a", "r"); // Open a pipe to a command "uname –a"
if (read_fp != NULL) {
// read output of uname -a command from a pipe
chars_read = fread( buffer, sizeof(char),BUFSIZ, read_fp);
if (chars_read > 0) {
printf("Output was:-n %s n", buffer);
}
pclose(read_fp); exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}
Reading Output from an External Program popen1.c
 Let’s try a simple popen and pclose example, popen1.c.
 Now compile and run this program
 How It Works
 The program uses the popen call to invoke the uname command with the -a parameter.
 It then uses the returned file stream to read data up to BUFSIZ characters (as this is a
#define from stdio.h) and then prints it out so it appears on the screen.
 Because you’ve captured the output of uname inside a program, it’s available for
processing.
Programming Example
rashid@rashid-VirtualBox:~/sp$ gcc -o popen1 popen1.c
rashid@rashid-VirtualBox:~/sp$ ./popen1
Output was:-
Linux rashid-VirtualBox 6.5.0-28-generic #29~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC
Thu Apr 4 14:39:20 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
FILE *write_fp; char buffer[BUFSIZ + 1];
sprintf(buffer, "Once upon a time, there was...n");
write_fp = popen("od -c", "w"); // open pipe to a command "od -c"
if (write_fp != NULL) {
// write input string to od –c command
fwrite( buffer, sizeof(char), strlen(buffer), write_fp);
pclose(write_fp);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}
Sending Output to popen popen2.c
rashid@rashid-VirtualBox:~/sp$ gcc -o popen2 popen2.c
rashid@rashid-VirtualBox:~/sp$ ./popen2
0000000 O n c e u p o n a t i m e
0000020 , t h e r e w a s . . . n
0000037
 The program uses popen with the parameter “w” to start the od -c command, so that it can
send data to that command.
 It then sends a string that the od -c command receives and processes.
 the od -c command then prints the result of the processing on its standard output.
 From the command line, you can get the same output with the command
$ echo “Once upon a time, there was...” | od -c
How it works ?
 You’ve seen the high-level popen function, but now let’s move on to look at
the lower-level pipe function.
 The pipe function has the following prototype:
#include <unistd.h>
int pipe(int file_descriptor[2]);
 pipe is passed (a pointer to) an array of two integer file_descriptor. It fills the
array with two new file descriptors and returns a zero.
 On failure, it returns -1 and sets errno to indicate the reason for failure.
 The two file descriptors returned are connected in a special way.
 Any data written to file_descriptor[1] can be read back from file_descriptor[0].
 The data is processed in a “first in, first out" (FIFO) basis.
The Pipe Function
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
int data_processed; int file_pipes[2];
const char some_data[] = "123"; char buffer[BUFSIZ + 1];
memset(buffer, '0', sizeof(buffer));
if (pipe(file_pipes) == 0) {
// always use index 1 to write data on a pipe
data_processed = write( file_pipes[1], some_data, strlen(some_data));
printf("Wrote %d bytesn", data_processed);
// use index 0 to read data from a pipe
data_processed = read( file_pipes[0], buffer, BUFSIZ);
printf("Read %d bytes: %sn", data_processed, buffer); exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}
The Pipe Function pipe1.c
 The program creates a pipe using the two file descriptors in the array
file_pipes[].
 It then writes data into the pipe using the file descriptor file_pipes[1] and
reads it back from file_pipes[0].
 The real advantage of pipes comes when you want to pass data between two
processes.
 As you saw in Chapter 12, when a program creates a new process using the
fork call, file descriptors that were previously open remain open.
 By creating a pipe in the original process and then forking to create a new
process, you can pass data from one process to the other down the pipe.
How it works ?
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
int data_processed; int file_pipes[2];
char buffer[BUFSIZ + 1]; pid_t fork_result;
const char some_data[] = "123";
memset(buffer, '0', sizeof(buffer));
if (pipe(file_pipes) == 0) {
fork_result = fork();
if (fork_result == -1) {
fprintf(stderr, "Fork failure"); exit(EXIT_FAILURE);
}
// if the fork worked and the fork_result equals zero, we're in the child process.
if (fork_result == 0) {
data_processed = read(file_pipes[0], buffer,BUFSIZ);
printf("Read %d bytes: %sn", data_processed, buffer);
exit(EXIT_SUCCESS);
}
Pipes across a fork pipe2.c
// Otherwise, we must be the parent process.
else {
data_processed = write(file_pipes[1], some_data,strlen(some_data));
printf("Wrote %d bytesn", data_processed);
}
}
exit(EXIT_SUCCESS);
}
Pipes across a fork pipe2.c
 This program firstly creates a pipe with pipe() call. Then creates a new
process using fork() call.
 The parent writes the data to the pipe, while child reads from the pipe.
 Both parent and child exit after a write() and read() call.
How it works ?
 So far, you have only been able to pass data between related programs, that
is, programs that have been started from a common ancestor process.
 Often this isn’t very convenient, because you would like unrelated processes
to be able to exchange data.
 You do this with FIFOs, often referred to as named pipes.
 A named pipe is a special type of file (remember that everything in Linux is a
file!) that exists as a name in the file system but behaves like the unnamed
pipes.
 You can create named pipes from the command line and from within a
program. e.g. $ mknod filename p
 Alternatively you can also use $ mkfifo filename
Named Pipes: FIFOs
 From inside a program, you can use mkfifo to create a pipe.
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *filename, mode_t mode);
 The mkfifo takes a filename (as what you like to name your fifo), and the
creation mode (user's permission), normally read and write for all (0755).
 It returns 0 on success, otherwise indicates failure.
Named Pipes: FIFOs
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
int main() {
int res = mkfifo("/tmp/my_fifo", 0777);
if (res == 0)
printf("FIFO createdn");
exit(EXIT_SUCCESS);
}
Creating a Named Pipe fifo1.c
How It Works:
The program uses the
mkfifo function to create a
special file and you are
asking for a mode of 0777
1. First, try reading the (empty) FIFO:
$ cat < /tmp/my_fifo
2. Now try writing to the FIFO. You will have to use a different terminal because the first
command will now be hanging, waiting for some data to appear in the FIFO.
$ echo “Hello World” > /tmp/my_fifo
 You will see the output appear from the cat command. If you don’t send any data down
the FIFO, the cat command will hang until you interrupt it, conventionally with Ctrl+C.
3. You can do both at once by putting the first command in the background:
$ cat < /tmp/my_fifo &
$ echo “Hello World” > /tmp/my_fifo
 Because there was no data in the FIFO, the cat and echo programs both block, waiting for
some data to arrive and some other process to read the data, respectively.
Accessing a FIFOs
 Since FIFO is a file exists in the file system, we can use open() and close() which are used for
regular files to open and close the FIFO.
open(const char *path, open_flag);
 The open_flag can be four legal combinations of
O_RDONLY, O_WRONLY, O_NONBLOCK
 There are four cases of open() as followed:
1) open(const char *path, O_RDONLY);
This open call will block, i.e. not return untill a process opens the same FIFO for writing.
When a process opens a FIFO for reading, if there is no process currently holding the FIFO
open for writing, the open call will block until some other process opens the FIFO for
writing. This ensures that the reader doesn't start reading until there's something to read.
Opening a FIFO with Open
2) open(const char *path, O_WRONLY);
This open call will block untill a process open the same FIFO for reading.
When a process opens a FIFO for writing, if there are no processes currently holding the
FIFO open for reading, the open call will block until some other process opens the FIFO for
reading. This ensures that the writer doesn't start writing until there's a reader ready to
consume the data.
3) open(const char *path, O_RDONLY|O_NONBLOCK);
This open call will now succeed and return immediately, even if the FIFO had been opened
but not written by any proecess.
4) open(const char *path, O_WRONLY|O_NONBLOCK);
This open call will return immediately, but if no process has the FIFO open for reading, open
will return -1 indicating error, and the FIFO won't be opened.
If a process does have the FIFO open for reading, the file descriptor returned can be used
for writing to the FIFO.
Opening a FIFO with Open
 A read on an empty, full blocking FIFO will wait untill some data can be read.
 A read on an empty, non-blocking FIFO will return 0 bytes.
 A write on a full blocking FIFO will wait untill the data can be written.
 A write on a FIFO that can't accept all of the bytes being written will either:
 Fail if the request is the PIPE-BUF bytes or less and the data can't be written.
 Write part of the data if the request is more than PIPE-BUF bytes, returning
the number of bytes actually been written.
 The size of the FIFO is important consideration. There is a system-imposed
limitation on how much data can be in FIFO at any one time. This is defined in
< limits.h >
#define PIPE_BUF 4096
Reading and Writing FIFOs
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_NAME "/tmp/my_fifo"
#define BUFFER_SIZE PIPE_BUF
#define TEN_MEG (1024 * 1024 * 10)
int main() {
int pipe_fd;
int res;
int open_mode = O_WRONLY;
int bytes_sent = 0;
char buffer[BUFFER_SIZE + 1];
Reading and Writing FIFOs Examples fifo3.c
if (access(FIFO_NAME, F_OK) == -1)
{
res = mkfifo(FIFO_NAME, 0777);
if (res != 0)
{
fprintf(stderr, "Could not create fifo %sn", FIFO_NAME);
exit(EXIT_FAILURE);
}
}
printf("Process %d opening FIFO O_WRONLYn", getpid());
pipe_fd = open(FIFO_NAME, open_mode);
printf("Process %d result %dn", getpid(), pipe_fd);
Reading and Writing FIFOs Examples fifo3.c
if (pipe_fd != -1){
while(bytes_sent < TEN_MEG) {
res = write(pipe_fd, buffer, BUFFER_SIZE);
if (res == -1) {
fprintf(stderr, "Write error on pipen");
exit(EXIT_FAILURE);
}
bytes_sent += res;
}
(void)close(pipe_fd);
}
else {
exit(EXIT_FAILURE);
}
printf("Process %d finishedn", getpid());
exit(EXIT_SUCCESS);
}
Reading and Writing FIFOs Examples fifo3.c
//A reader reads and discards data from the FIFO
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_NAME "/tmp/my_fifo"
#define BUFFER_SIZE PIPE_BUF
int main() {
int pipe_fd; int res;
int open_mode = O_RDONLY;
char buffer[BUFFER_SIZE + 1];
int bytes_read = 0;
memset(buffer, '0', sizeof(buffer));
Reading and Writing FIFOs Examples fifo4.c
printf("Process %d opening FIFO O_RDONLYn", getpid());
pipe_fd = open(FIFO_NAME, open_mode);
printf("Process %d result %dn", getpid(), pipe_fd);
if (pipe_fd != -1) {
do {
res = read(pipe_fd, buffer, BUFFER_SIZE);
bytes_read += res;
} while (res > 0);
(void)close(pipe_fd);
}
else {
exit(EXIT_FAILURE);
}
printf("Process %d finished, %d bytes readn", getpid(), bytes_read);
exit(EXIT_SUCCESS);
}
Reading and Writing FIFOs Examples fifo4.c

More Related Content

Similar to Linux Systems Programming: Inter Process Communication (IPC) using Pipes

C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
Rajeshkumar Reddy
 
Program Assignment Process ManagementObjective This program a.docx
Program Assignment  Process ManagementObjective This program a.docxProgram Assignment  Process ManagementObjective This program a.docx
Program Assignment Process ManagementObjective This program a.docx
wkyra78
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
Dushmanta Nath
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
adampcarr67227
 
Unit5 (2)
Unit5 (2)Unit5 (2)
Unit5 (2)
mrecedu
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
patcha535
 
18CS56-UP-Module 4 - IPC.pptx
18CS56-UP-Module 4 - IPC.pptx18CS56-UP-Module 4 - IPC.pptx
18CS56-UP-Module 4 - IPC.pptx
MdshadabArshad
 
Data file handling
Data file handlingData file handling
Data file handling
TAlha MAlik
 

Similar to Linux Systems Programming: Inter Process Communication (IPC) using Pipes (20)

lec4.docx
lec4.docxlec4.docx
lec4.docx
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
 
Program Assignment Process ManagementObjective This program a.docx
Program Assignment  Process ManagementObjective This program a.docxProgram Assignment  Process ManagementObjective This program a.docx
Program Assignment Process ManagementObjective This program a.docx
 
Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
Unit5
Unit5Unit5
Unit5
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdf
 
File management
File managementFile management
File management
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
 
Unit5 (2)
Unit5 (2)Unit5 (2)
Unit5 (2)
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
18CS56-UP-Module 4 - IPC.pptx
18CS56-UP-Module 4 - IPC.pptx18CS56-UP-Module 4 - IPC.pptx
18CS56-UP-Module 4 - IPC.pptx
 
Linux Systems Programming: Process CommunCh11 Processes and Signals
Linux Systems Programming: Process CommunCh11 Processes and SignalsLinux Systems Programming: Process CommunCh11 Processes and Signals
Linux Systems Programming: Process CommunCh11 Processes and Signals
 
What is-a-computer-process-os
What is-a-computer-process-osWhat is-a-computer-process-os
What is-a-computer-process-os
 
C Language Unit-5
C Language Unit-5C Language Unit-5
C Language Unit-5
 
Systemcall1
Systemcall1Systemcall1
Systemcall1
 
Prog i
Prog iProg i
Prog i
 
Data file handling
Data file handlingData file handling
Data file handling
 

More from RashidFaridChishti

More from RashidFaridChishti (20)

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
 

Recently uploaded

Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
Kamal Acharya
 
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DrGurudutt
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
Kamal Acharya
 

Recently uploaded (20)

NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdfONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker project
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
 
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
 
Maestro Scripting Language CNC programacion
Maestro Scripting Language CNC programacionMaestro Scripting Language CNC programacion
Maestro Scripting Language CNC programacion
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Electrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission lineElectrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission line
 
retail automation billing system ppt.pptx
retail automation billing system ppt.pptxretail automation billing system ppt.pptx
retail automation billing system ppt.pptx
 
An improvement in the safety of big data using blockchain technology
An improvement in the safety of big data using blockchain technologyAn improvement in the safety of big data using blockchain technology
An improvement in the safety of big data using blockchain technology
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
Electrical shop management system project report.pdf
Electrical shop management system project report.pdfElectrical shop management system project report.pdf
Electrical shop management system project report.pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 

Linux Systems Programming: Inter Process Communication (IPC) using Pipes

  • 1. Linux System Programming Inter Process Communication (IPC) Pipes Engr. Rashid Farid Chishti chishti@iiu.edu.pk https://youtube.com/rfchishti https://sites.google.com/site/chishti
  • 2.  Processes and Signals form a fundamental part of the Linux operating environment. They control almost all activities performed by Linux computer system.  We use the term pipe to mean connecting a data flow from one process to another.  Sometimes while using shell commands you attach, or pipe, the output of one process to the input of another. e.g. $ ls -al | less  The shell arranges the standard input and output of the two commands, so that  The standard input to the command ls -al comes from the terminal keyboard.  The standard output from the command ls -al is fed to another command less as its standard input.  The standard output from the command less is connected to the terminal screen. What Is a Pipe?
  • 3. What Is a Pipe? Visual representation of the process
  • 4.  The easiest way of passing data between two programs is with the popen and pclose functions.  These have the following prototypes: #include <stdio.h> FILE *popen(const char *command, const char *open_mode); int pclose(FILE *stream_to_close);  The popen function allows a program to invoke another program as a new process and either pass data to it or receive data from it.  The command string is the name of the program to run.  open_mode must be either “r” or “w”. Process Pipes
  • 5.  stream_to_close the value (a pointer) returned by popen(), is passed to pclose().  pclose() closes the pipe descriptor  Let’s try a simple popen and pclose example, popen1.c.  You’ll use popen in a program to access information from uname. The uname - a command prints system information, including the machine type, the OS name, version and release, and the machine’s network name.  Having initialized the program, you open the pipe to uname, making it readable and setting read_fp to point to the output.  At the end, the pipe pointed to by read_fp is closed. Programming Example
  • 6. #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int main(){ int chars_read; FILE *read_fp; char buffer[BUFSIZ + 1]; // 8192+1 memset(buffer, '0', sizeof(buffer)); read_fp = popen("uname -a", "r"); // Open a pipe to a command "uname –a" if (read_fp != NULL) { // read output of uname -a command from a pipe chars_read = fread( buffer, sizeof(char),BUFSIZ, read_fp); if (chars_read > 0) { printf("Output was:-n %s n", buffer); } pclose(read_fp); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE); } Reading Output from an External Program popen1.c
  • 7.  Let’s try a simple popen and pclose example, popen1.c.  Now compile and run this program  How It Works  The program uses the popen call to invoke the uname command with the -a parameter.  It then uses the returned file stream to read data up to BUFSIZ characters (as this is a #define from stdio.h) and then prints it out so it appears on the screen.  Because you’ve captured the output of uname inside a program, it’s available for processing. Programming Example rashid@rashid-VirtualBox:~/sp$ gcc -o popen1 popen1.c rashid@rashid-VirtualBox:~/sp$ ./popen1 Output was:- Linux rashid-VirtualBox 6.5.0-28-generic #29~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Apr 4 14:39:20 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
  • 8. #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int main(){ FILE *write_fp; char buffer[BUFSIZ + 1]; sprintf(buffer, "Once upon a time, there was...n"); write_fp = popen("od -c", "w"); // open pipe to a command "od -c" if (write_fp != NULL) { // write input string to od –c command fwrite( buffer, sizeof(char), strlen(buffer), write_fp); pclose(write_fp); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE); } Sending Output to popen popen2.c rashid@rashid-VirtualBox:~/sp$ gcc -o popen2 popen2.c rashid@rashid-VirtualBox:~/sp$ ./popen2 0000000 O n c e u p o n a t i m e 0000020 , t h e r e w a s . . . n 0000037
  • 9.  The program uses popen with the parameter “w” to start the od -c command, so that it can send data to that command.  It then sends a string that the od -c command receives and processes.  the od -c command then prints the result of the processing on its standard output.  From the command line, you can get the same output with the command $ echo “Once upon a time, there was...” | od -c How it works ?
  • 10.  You’ve seen the high-level popen function, but now let’s move on to look at the lower-level pipe function.  The pipe function has the following prototype: #include <unistd.h> int pipe(int file_descriptor[2]);  pipe is passed (a pointer to) an array of two integer file_descriptor. It fills the array with two new file descriptors and returns a zero.  On failure, it returns -1 and sets errno to indicate the reason for failure.  The two file descriptors returned are connected in a special way.  Any data written to file_descriptor[1] can be read back from file_descriptor[0].  The data is processed in a “first in, first out" (FIFO) basis. The Pipe Function
  • 11. #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int main(){ int data_processed; int file_pipes[2]; const char some_data[] = "123"; char buffer[BUFSIZ + 1]; memset(buffer, '0', sizeof(buffer)); if (pipe(file_pipes) == 0) { // always use index 1 to write data on a pipe data_processed = write( file_pipes[1], some_data, strlen(some_data)); printf("Wrote %d bytesn", data_processed); // use index 0 to read data from a pipe data_processed = read( file_pipes[0], buffer, BUFSIZ); printf("Read %d bytes: %sn", data_processed, buffer); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE); } The Pipe Function pipe1.c
  • 12.  The program creates a pipe using the two file descriptors in the array file_pipes[].  It then writes data into the pipe using the file descriptor file_pipes[1] and reads it back from file_pipes[0].  The real advantage of pipes comes when you want to pass data between two processes.  As you saw in Chapter 12, when a program creates a new process using the fork call, file descriptors that were previously open remain open.  By creating a pipe in the original process and then forking to create a new process, you can pass data from one process to the other down the pipe. How it works ?
  • 13. #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int main() { int data_processed; int file_pipes[2]; char buffer[BUFSIZ + 1]; pid_t fork_result; const char some_data[] = "123"; memset(buffer, '0', sizeof(buffer)); if (pipe(file_pipes) == 0) { fork_result = fork(); if (fork_result == -1) { fprintf(stderr, "Fork failure"); exit(EXIT_FAILURE); } // if the fork worked and the fork_result equals zero, we're in the child process. if (fork_result == 0) { data_processed = read(file_pipes[0], buffer,BUFSIZ); printf("Read %d bytes: %sn", data_processed, buffer); exit(EXIT_SUCCESS); } Pipes across a fork pipe2.c
  • 14. // Otherwise, we must be the parent process. else { data_processed = write(file_pipes[1], some_data,strlen(some_data)); printf("Wrote %d bytesn", data_processed); } } exit(EXIT_SUCCESS); } Pipes across a fork pipe2.c
  • 15.  This program firstly creates a pipe with pipe() call. Then creates a new process using fork() call.  The parent writes the data to the pipe, while child reads from the pipe.  Both parent and child exit after a write() and read() call. How it works ?
  • 16.  So far, you have only been able to pass data between related programs, that is, programs that have been started from a common ancestor process.  Often this isn’t very convenient, because you would like unrelated processes to be able to exchange data.  You do this with FIFOs, often referred to as named pipes.  A named pipe is a special type of file (remember that everything in Linux is a file!) that exists as a name in the file system but behaves like the unnamed pipes.  You can create named pipes from the command line and from within a program. e.g. $ mknod filename p  Alternatively you can also use $ mkfifo filename Named Pipes: FIFOs
  • 17.  From inside a program, you can use mkfifo to create a pipe. #include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *filename, mode_t mode);  The mkfifo takes a filename (as what you like to name your fifo), and the creation mode (user's permission), normally read and write for all (0755).  It returns 0 on success, otherwise indicates failure. Named Pipes: FIFOs
  • 18. #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> int main() { int res = mkfifo("/tmp/my_fifo", 0777); if (res == 0) printf("FIFO createdn"); exit(EXIT_SUCCESS); } Creating a Named Pipe fifo1.c How It Works: The program uses the mkfifo function to create a special file and you are asking for a mode of 0777
  • 19. 1. First, try reading the (empty) FIFO: $ cat < /tmp/my_fifo 2. Now try writing to the FIFO. You will have to use a different terminal because the first command will now be hanging, waiting for some data to appear in the FIFO. $ echo “Hello World” > /tmp/my_fifo  You will see the output appear from the cat command. If you don’t send any data down the FIFO, the cat command will hang until you interrupt it, conventionally with Ctrl+C. 3. You can do both at once by putting the first command in the background: $ cat < /tmp/my_fifo & $ echo “Hello World” > /tmp/my_fifo  Because there was no data in the FIFO, the cat and echo programs both block, waiting for some data to arrive and some other process to read the data, respectively. Accessing a FIFOs
  • 20.  Since FIFO is a file exists in the file system, we can use open() and close() which are used for regular files to open and close the FIFO. open(const char *path, open_flag);  The open_flag can be four legal combinations of O_RDONLY, O_WRONLY, O_NONBLOCK  There are four cases of open() as followed: 1) open(const char *path, O_RDONLY); This open call will block, i.e. not return untill a process opens the same FIFO for writing. When a process opens a FIFO for reading, if there is no process currently holding the FIFO open for writing, the open call will block until some other process opens the FIFO for writing. This ensures that the reader doesn't start reading until there's something to read. Opening a FIFO with Open
  • 21. 2) open(const char *path, O_WRONLY); This open call will block untill a process open the same FIFO for reading. When a process opens a FIFO for writing, if there are no processes currently holding the FIFO open for reading, the open call will block until some other process opens the FIFO for reading. This ensures that the writer doesn't start writing until there's a reader ready to consume the data. 3) open(const char *path, O_RDONLY|O_NONBLOCK); This open call will now succeed and return immediately, even if the FIFO had been opened but not written by any proecess. 4) open(const char *path, O_WRONLY|O_NONBLOCK); This open call will return immediately, but if no process has the FIFO open for reading, open will return -1 indicating error, and the FIFO won't be opened. If a process does have the FIFO open for reading, the file descriptor returned can be used for writing to the FIFO. Opening a FIFO with Open
  • 22.  A read on an empty, full blocking FIFO will wait untill some data can be read.  A read on an empty, non-blocking FIFO will return 0 bytes.  A write on a full blocking FIFO will wait untill the data can be written.  A write on a FIFO that can't accept all of the bytes being written will either:  Fail if the request is the PIPE-BUF bytes or less and the data can't be written.  Write part of the data if the request is more than PIPE-BUF bytes, returning the number of bytes actually been written.  The size of the FIFO is important consideration. There is a system-imposed limitation on how much data can be in FIFO at any one time. This is defined in < limits.h > #define PIPE_BUF 4096 Reading and Writing FIFOs
  • 23. #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <fcntl.h> #include <limits.h> #include <sys/types.h> #include <sys/stat.h> #define FIFO_NAME "/tmp/my_fifo" #define BUFFER_SIZE PIPE_BUF #define TEN_MEG (1024 * 1024 * 10) int main() { int pipe_fd; int res; int open_mode = O_WRONLY; int bytes_sent = 0; char buffer[BUFFER_SIZE + 1]; Reading and Writing FIFOs Examples fifo3.c
  • 24. if (access(FIFO_NAME, F_OK) == -1) { res = mkfifo(FIFO_NAME, 0777); if (res != 0) { fprintf(stderr, "Could not create fifo %sn", FIFO_NAME); exit(EXIT_FAILURE); } } printf("Process %d opening FIFO O_WRONLYn", getpid()); pipe_fd = open(FIFO_NAME, open_mode); printf("Process %d result %dn", getpid(), pipe_fd); Reading and Writing FIFOs Examples fifo3.c
  • 25. if (pipe_fd != -1){ while(bytes_sent < TEN_MEG) { res = write(pipe_fd, buffer, BUFFER_SIZE); if (res == -1) { fprintf(stderr, "Write error on pipen"); exit(EXIT_FAILURE); } bytes_sent += res; } (void)close(pipe_fd); } else { exit(EXIT_FAILURE); } printf("Process %d finishedn", getpid()); exit(EXIT_SUCCESS); } Reading and Writing FIFOs Examples fifo3.c
  • 26. //A reader reads and discards data from the FIFO #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <fcntl.h> #include <limits.h> #include <sys/types.h> #include <sys/stat.h> #define FIFO_NAME "/tmp/my_fifo" #define BUFFER_SIZE PIPE_BUF int main() { int pipe_fd; int res; int open_mode = O_RDONLY; char buffer[BUFFER_SIZE + 1]; int bytes_read = 0; memset(buffer, '0', sizeof(buffer)); Reading and Writing FIFOs Examples fifo4.c
  • 27. printf("Process %d opening FIFO O_RDONLYn", getpid()); pipe_fd = open(FIFO_NAME, open_mode); printf("Process %d result %dn", getpid(), pipe_fd); if (pipe_fd != -1) { do { res = read(pipe_fd, buffer, BUFFER_SIZE); bytes_read += res; } while (res > 0); (void)close(pipe_fd); } else { exit(EXIT_FAILURE); } printf("Process %d finished, %d bytes readn", getpid(), bytes_read); exit(EXIT_SUCCESS); } Reading and Writing FIFOs Examples fifo4.c