Linux InterProcessCommunication
Linux InterProcessCommunication
Engineering &Technology
G.Laxmi Deepthi
Assistant Professor
Email: [email protected]
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 1
Agenda
§ IPC
§ IPC between processes on a single computer system
§ Pipes-creation
§ FIFOs
§ popen and pcloselibrary functions
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 2
IPC
§ Introduction
o Processes communicate with each other and with the kernel to coordinate their activities
o Communication of 2 types,
1. Related processes
2. Unrelated processes
Terminology
• Pipes
• FIFO
• Message Queues
• Shared memory
• Semaphores
• Signals
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 3
IPC
§ Pipes
o Communication between two related processes.
o half duplex mechanism
§ FIFO
o Communication between two unrelated processes.
o Full duplex mechanism.
§ Message Queues
o The processes will communicate with each other by posting a message
o Full duplex mechanism.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 4
IPC
§ Shared memory
o Communication is possible through a shared region among processes.
o This can be provided with synchronization.
§ Semaphores
o These are used for synchronizing access to multiple processes.
§ Signals
o communication between multiple processes by way of signaling.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 5
IPC
§ Pipes creation
$ ls –l |cat > ls_file
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 6
IPC
§ Pipes-creation
pipe() creates a pipe, a unidirectional data channel that can be used for inter process communication.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 7
IPC
§ FIFOs
o FIFO also known as a named pipe.
o a named pipe appears as a file
o It can be available on the local storage which allows two or more processes to communicate with each
other by reading/writing to/from this file.
o The system call, mknod(), to create a named pipe, which is a kind of a special file.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 8
IPC
File mode
o S_IRWXU
o S_IRUSR
o S_IWUSR
o S_IXUSR
o S_IRWXG
o S_IRGRP
Return values
zero on success and -1 in case of failure
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 9
IPC
§ mkfifo is used to create a FIFO file, prototype as follows,
#include <sys/types.h>
#include <sys/stat.h>
Return values
zero on success and -1 in case of failure.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 10
IPC
§ Differences between unnamed and named pipes
Unnamed pipes named pipes
unnamed pipes is not given a name. It is accessible named type has a specific name which can be given
through two file descriptors to it by the user.
It is used for communication between a parent and It can be used for communication between two
its child process. unnamed process.
If one of the process execution completes (parent or It exists in the file system.
child) the pipe also be closed.
These are used for communication between It provided communication between processes which
processes which exist in a same(single) system. exist on same system or on different system (over a
network).
It is a one-way pipe that typically transfers data It allows multiple processes to communicate.
between a parent process and a child process.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 11
IPC
§ popen and pclose library functions
These functions will be used to pipe stream to or from a process.
#include <stdio.h>
Return values
popen: returns file pointer on success, NULL on error
Pclose: returns termination status of cmdstring, or 1 on error
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 12
THANK YOU
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 13