You are on page 1of 8

7/21/2017

Introduction
• IPC stands for interprocess communication
Inter-process Communication • Establishing communication between different
processes that are running on system.
(IPC) – System V IPC (added to System V kernels in 1980s)
Harshad B. Prajapati – POSIX IPC (added in 1993-1994)
Associate Professor
Information Technology Department,
Dharmsinh Desai University, Nadiad
• Both System V IPC and POSIX IPC offer similar
functionalities, the difference is in the
interfaces (names of functions)

How information can be exchanged How information can be exchanged


• In UNIX based system, Information can be
shared between processes
– Through file system
– Through shared information residing in kernel
(e.g. PIPE)
– Through shared memory, present in user address
space (page sharing)

Persistence of IPC objects Persistence of IPC objects


• How long an object of IPC type remains in • Process-persistence IPC object
existence. – IPC object exists until the last process that holds
• Three type of persistence possible the object opened closes the object.
• E.g. pipes and FIFOs
– Process-persistence IPC object
– Kernel-persistence IPC object • Kernel-persistence IPC object
– Filesystem-persistence IPC object – IPC object exists until the kernel reboots or until
the object is explicitly deleted.
• E.g. POSIX message queues, semaphores and shared
memory.
– They must be at least kernel persistent, but may be
filesysetm-persistent (Depending upon implementation)

1
7/21/2017

Persistence of IPC objects Name Spaces


• Filesystem-persistence IPC object • Two unrelated processes (i.e., with no parent-
– IPC object exists until the object is explicitly child relation) need some name or identifier
deleted. for IPC object to use them.
• That means, the object retains its value on kernel – For PIPEs, no name
reboots.
– For FIFOs, unix path name

• The set of possible names is called name


space.

Name Spaces Name Spaces for IPC objects


Type of IPC Name space to open or Identification after IPC
• Identification of IPC object create opened for use
– Before referencing Pipe (no name) Descriptor
FIFO Pathname Descriptor
• Name/identifier
Posix shared memory Posix IPC name Descriptor
– After referenced Posix message queue Posix IPC name mqd_t value
• Returned file descriptor/value/address Posix named semaphore Posix IPC name sem_t pointer
Posix memory-based (no name) sem_t pointer
semaphore

TCP socket IP address and TCP port Descriptor


UDP socket IP address and UDP port Descriptor

How to deal with UNIX errors Pipe and FIFO


• When error occurs in any UNIX call • Pipes
– It returns -1 value – Original form of Unix IPC (around 1973 in third
– The value of variable errno is set to positive error version of UNIX)
value. – It has no name.
– Can not be used with unrelated processes.
• FIFO
– Limitation of pipe removed in FIFO.
– It has name.
– Can be used with unrelated processes.

2
7/21/2017

Usage of Pipe and FIFO The read and write calls


• Both are accessed using system calls of • read
reading and writing operations #include <unistd.h>
– read size_t read(int fd, void *buf, size_t nbytes);
– write It returns the number of bytes read from fd.
• Write
#include <unistd.h>
size_t write(int fd, const void *buf, size_t nbytes);
It returns the number of bytes written to fd.

Creation of pipe Two-way communication


• Pipe • One pipe can carry flow of data only in one
– It is created by a pipe function. direction (half duplex).
#include <unistd.h>
int pipe(int fd[2]);
– On success (return value 0)
• A pipe object is created in kernel space. • For two way communication (full duplex) we
• It has two descriptors for two ends of pipe require two such pipe objects.
– Read descriptor: placed in fd[0]
– Write descriptor: placed in fd[1]

Usage of pipe in processes Usage of pipe in processes


• Pipe is used between related processes • We Need to prepare pipe for its proper usage
in parent and child processes
• Close appropriate descriptor in parent and
child process each.

• Pipe before fork Pipe after fork

3
7/21/2017

Program on pipe Program on pipe


• Program: file server client demonstration #include <stdio.h>
using pipe. #include <sys/types.h>
#include <fcntl.h>
– The Client asks the user to enter name of file.
#define MAX 100
– The client sends that name to the server through #define STDOUT 1
one pipe.
– The server sends the content of the specified file void client(int rfd,int wfd);
through second pipe to the client void server(int rfd,int wfd);
– The Client displays the result on console.

Program on pipe Program on pipe


int main(){ if((PID=fork())==0){
int pipectos[2]; // server
int pipestoc[2]; close(pipestoc[0]); //close read
pid_t PID; close(pipectos[1]); //close write
if(pipe(pipestoc)<0){ server(pipectos[0],pipestoc[1]);
fprintf(stderr,"Can't create pipe\n"); exit(0);
exit(0); }
}
if(pipe(pipectos)<0){
fprintf(stderr,"Can't create pipe\n");
exit(0);
}

Program on pipe Program on pipe


//client void client(int rfd,int wfd){
close(pipectos[0]); char buff[MAX];
close(pipestoc[1]); int n;
client(pipestoc[0],pipectos[1]); int len;
waitpid(PID,NULL,0); printf("Enter name of file=");
exit(0); //read name of file from k/b
} fgets(buff,MAX,stdin);
len=strlen(buff);
if(buff[len-1]=='\n')
len--;

4
7/21/2017

Program on pipe Program on pipe


//send name of file to server void server(int rfd,int wfd){
write(wfd,buff,len); int fd;
//read reply(file content) from server int n;
while((n=read(rfd,buff,MAX))>0) char buff[MAX];
write(STDOUT,buff,n); char fileName[MAX];
} //read name of file from client
if((n=read(rfd,fileName,MAX))==0){
fprintf(stderr,"end of file while reading path name");
exit(0);
}
fileName[n]=0;

Program on pipe More on pipe usage


//open requested file • Using pipe in shell commands
if((fd=open(fileName,O_RDONLY))<0){
sprintf(buff,"can not open %s",fileName);
• E.g. who|sort|lp
write(wfd,buff,strlen(buff));
}
else{
while((n=read(fd,buff,MAX))>0)
write(wfd,buff,n);
close(fd);
}
}

More on pipe usage More on pipe usage


• Pipe usage through standard I/O library FILE * popen(const char * command, const char *type);
functions • A pipe created between
– Calling process and
#include <stdio.h>
– the process that is created using command.
FILE * popen(const char * command, const char • Return value FILE* is used to perform read/write
*type); with command’s input and output.
• type is a string
int pclose(FILE* fp); – r, means read from command
– w, means write to command

5
7/21/2017

More on pipe usage Creation of FIFO


int pclose(FILE* fp); • FIFO: First in first out
• The pclose closes standard I/O stream created by popen – It is half-duplex. (Need two fifo objects for two-
• Waits for the termination of command way communication)
• Returns the termination status of the shell command
– It allows communication between two unrelated
processes
– It is created by a mkfifo function.
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char*pathname, mode_t mode);

Creation of FIFO Creation of FIFO


int mkfifo(const char*pathname, mode_t int mkfifo(const char*pathname, mode_t
mode); mode);
• The pathname is normal Unix pathname. • Once FIFO created, must be opened for either
• The mode specifies file permission bits. reading or writing.
(similar to second argument to open call). – FIFO must be opened either read-only or write-
only, not both. (As it is half-duplex).
• The mkfifo function implies
• Write to FIFO, appends data
– O_CREAT|O_EXCL • Read from FIFO, reads at the beginning.
• It creates a new fifo or returns an error EEXIST if the
named fifo already exists. • lseek (i.e. random access) on FIFO results in
• Note: mkfifo command can be used from shell script or command shell. ESPIPE.

Program on FIFO Program on FIFO


Implement Echo server-client programs. fifo-EchoServer.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FILE_MODE
O_CREAT|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH

6
7/21/2017

Program on FIFO Program on FIFO


int main(){
int wfd, rfd; /* open two fifos for usage*/
char buff[100]; wfd=open("/tmp/fifo-stoc",O_WRONLY,0);
int no; rfd=open("/tmp/fifo-ctos",O_RDONLY,0);
/* Create two fifos */ printf("Server is ready for communication....\n");
mkfifo("/tmp/fifo-stoc",FILE_MODE);
mkfifo("/tmp/fifo-ctos",FILE_MODE); /* Whatever you get from one fifo, send back to another*/
while((no=read(rfd,buff,sizeof(buff)))>0)
write(wfd,buff,no);
return 0;
}

Program on FIFO Program on FIFO


Fifo-EchoClient.c /*fifo is already created by the server,
open two fifos for usage*/
#include <stdio.h> rfd=open("/tmp/fifo-stoc",O_RDONLY,0);
#include <sys/types.h> wfd=open("/tmp/fifo-ctos",O_WRONLY,0);
#include <sys/stat.h> //printf("Client is ready for communication....\n");
#include <fcntl.h>
int main(){
int wfd, rfd;
char buff[100];
int no;
int len;

Program on FIFO Pipe and FIFO limits


/* Read a line from user through keyboard*/
• System-imposed limits on pipes and FIFOs are
while(fgets(buff,100,stdin)!=NULL){
len=strlen(buff); • OPEN_MAX
if(buff[len-1]=='\n')
– The maximum number of descriptors open at any
len--;
/* send the line to the server, then receive response from the server*/
time by a process
write(wfd,buff,len); • (Posix requires at least 16)
no=read(rfd,buff,sizeof(buff));
• PIPE_BUF
/* write the response on console*/
write(1,buff,no); – The maximum amount of data that can be written
printf("\n"); to pipe atomically. (Posix requires at least 512
} bytes)
return 0;
}

7
7/21/2017

Pipe and FIFO limits Pipe and FIFO limits


• OPEN_MAX • PIPE_BUF
– Value can be inquired using sysconf command – Its value is defined in <limits.h> header file, but its
value can depend on the pathname (as different
– Value can be changed using ulimit (Bourne-,korn- pathnames can end up on different file systems)
shells ) or limit (C shell) command – Value can be obtained at runtime through one of
– Value can be changed using setrlimit function following calls
• pathconf
• Fpathconf
• About FIFO
– FIFOs can not be used on NFS mounted file system

Handling Multiple Clients Handling Multiple Clients


• Consider a situation • Such problem can be solved
– A number of clients want to utilize the service of a – The server creates one FIFO on well-know public
single server. path, through which all clients initiate
– How the server can send information to clients? communication.
– Clients can send information to the server by – Each client creates its own private FIFO when it
writing it to a FIFO on well-known public path. starts. In naming this FIFO, it uses its process ID,
– But, the server can not write to a well-known which is unique.
public path. I.e., each client needs its own private – The client communicates this private FIFO path to
path for retrieving information from the server. the server through public well-know FIFO path.
– The server can use this private FIFO to
communicate with that particular client.

Handling Multiple Clients

2
2

1
1 3
3

You might also like