0% found this document useful (0 votes)
28 views13 pages

Linux InterProcessCommunication

The document discusses inter-process communication (IPC) mechanisms in Linux, including pipes, FIFOs, and popen/pclose functions. It defines pipes and FIFOs, explaining that pipes allow communication between related processes while FIFOs allow communication between unrelated processes. It also covers creating pipes and FIFOs using pipe(), mknod(), and mkfifo(), and describes how popen() and pclose() can be used to pipe a stream to or from a process.

Uploaded by

yashwanth sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views13 pages

Linux InterProcessCommunication

The document discusses inter-process communication (IPC) mechanisms in Linux, including pipes, FIFOs, and popen/pclose functions. It defines pipes and FIFOs, explaining that pipes allow communication between related processes while FIFOs allow communication between unrelated processes. It also covers creating pipes and FIFOs using pipe(), mknod(), and mkfifo(), and describes how popen() and pclose() can be used to pipe a stream to or from a process.

Uploaded by

yashwanth sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Vallurupalli Nageswara Rao Vignana Jyothi Institute of

Engineering &Technology

Department of Computer Science & Engineering

SUBJECT: Linux Programming

Topic Name: Inter process Communication


III year, sec: B,C

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

o Linux supports these Inter-Process Communication (IPC) mechanisms

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.

int pipe(int pipefd[2]);

int pipe2(int pipefd[2], int flags);

Flags: O_NONBLOCK, O_CLOEXEC


Return Value
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
Errors
EFAULT, EINVAL, EMFILE, ENFILE

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>

int mknod(const char *pathname, mode_t mode, dev_t dev);

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>

int mkfifo(const char *pathname, mode_t mode)

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>

FILE *popen(const char *command, const char *type);


int pclose(FILE *stream);

Return values
popen: returns file pointer on success, NULL on error
Pclose: returns termination status of cmdstring, or 1 on error

Ex: fp = popen("ls", "r");

Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 12
THANK YOU

Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 13

You might also like