190 likes | 382 Views
Interprocesss communication is the technique for processes to communicate with each other The different IPC types are, Pipes (half duplex) FIFO (named pipes) Stream Pipes(Full duplex) Named stream pipes Message queues Semaphores Shared memory Sockets Streams
E N D
Interprocesss communication is the technique for processes to communicate with each other The different IPC types are, Pipes (half duplex) FIFO (named pipes) Stream Pipes(Full duplex) Named stream pipes Message queues Semaphores Shared memory Sockets Streams The first seven form of IPC are usually restricted to inter process communication between processes on the same host. The sockets and streams, are only two that are generally supported for inter process communication between processes on different host. Interprocess Communication
PIPES • It is the oldest form of UNIX IPC and are provided by all UNIX systems. Limitations: • They are half-duplex. • They can be used only between process, that have a common ancestor. • Although it has a limitations, the pipes are most common used form of IPC. • Stream pipes will not have the 1st limitation. • FIFO and named stream pipes will not have the 2nd limitation. Normally a pipe is created by a process, that process calls fork, and the pipe is used between the parent and child. • Pipe is created by calling the function #include <unistd.h> int pipe (int fildes[2]); Returns:0 if OK , -1 on error.
Two file descriptors are returned through file descriptor argument. • filedes[0] is open for reading • filedes[1] is open for writing • The out put of the filedes[1] is input for the fildes[0]. There are two ways to view the UNIX pipe. The output of fd[1] is the input for fd[2].
Pipe in a single process is next to useless. • Normally, the process that calls pipe then calls fork, creating an IPC channel from the parent to the child or vice-versa. After the fork, we need to consider the direction of data flow. • For a pipe from the parent to child, the parent process closes read end of the pipe (fd[0]) and the child closes the write end (fd[1]). • For a pipe from child to parent, parent closes the fd[1] and child closes fd[0].
When one end of the pipe is closed the following rules apply: • If we read from a pipe whose write end has been closed, after all the data has been read, read returns 0 to indicate and end of file. • If we write to a pipe whose read end has been closed, the signal SIGPIPE is generated. If we either ignore the signal or catch it and return from the signal handler, write returns error with errno set to EPIPE.
PROGRAM FOR PIPE #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<unistd.h> int main() { int n,fd[2]; pid_t pid; char line[100]; if(pipe(fd)<0) printf("Error in PIPE Creation\n\n"); if((pid=fork()<0) printf("Process cannot be created\n\n'); else if(pid>0) //Parent { close(fd[0]); write(fd[1],"Hello",5); write(fd[1],"Welome",8); } else { //Child close(fd[1]); read(fd[0],line,15); printf("%s",line);} exit(0); }
//PROGRAM FOR FULL-DUPLEX PIPE #include<stdio.h> #include<fcntl.h> #include<sys/types.h> #include<sys/stat.h> int main() { int p[2],pid,status; char c1='a',c2='b',d1,d2; pipe(p); pid=fork(); if (pid==0) { write(p[1],&c1,1);
printf("Child writes %c\n",c1); sleep(2); read(p[0],&d2,1); printf("Child reads %c\n",d2); } else { sleep(2); read(p[0],&d1,1); printf("Parent reads %c\n",d1); write(p[1],&c2,1); printf("Parent writes %c\n",c2); wait(&status); } }
FIFO • It is some times called named pipes. It is also half-duplex. • In FIFO unrelated process can exchange data. Creating FIFO is similar to creating a file #include<sys/types.h> #include<sys/stat.h> Int mkfifo(const char*pathname, mode_t mode) Returns: 0 if OK, -1 on error. • It works with all normal file I/O functions such as open, read,write and lseek .
There are two uses for FIFOs. • FIFOs are used by shell commands to pass data from one shell pipeline to another, without creating intermediate temporary files. • FIFOs are used as meeting points in a Client-Server application to pass data between the clients and the server.
PROGRAM FOR FIFO #include<fcntl.h> #include<sys/types.h> #include<sys/stat.h> #include<stdio.h> #include<stdlib.h> int main() { char c='a',d; int status,rfd,wfd; mkfifo("fif1",O_CREAT|0644); if (fork()==0) { wfd=open("fif1",O_WRONLY);
if (write(wfd,&c,1)!=1) printf("write error \n"); printf("Child writes : %c\n",c); } else { rfd=open("fif1",O_RDONLY); if(read(rfd,&d,1)!=1) printf("read error \n"); printf("Parent reads : %c\n",d); } exit(0); }
XSI IPC Identifiers and Keys: • There are three types of IPC that we will call XSI IPC- Message queue, semaphores and shared memory. • Each IPC structure in the kernel is referred to by a non negative integer. • Unlike file descriptor, the IPC identifiers are not small integers. • Whenever the IPC structure is created (msgget, semget or shmget) a key must be specified. • The data type of the key is primitive system data type, key_t. It is defined in the header <sys/types.h> • The key is converted to identifier by the kernel.
Permissions structure: • XSI IPC associates and IPC-perm structure with each IPC structure. This struct ipc_perm defines the permissions and owner. struct ipc-perm{ uid_t uid; gid_t gid; uid_t cuid; gid_t cgid; mode_t mode; ulong seq; ket_t key; }; • All fields other than seq are initialized when the IPC structure is created. • uid, gid, mode fields can be modified by calling msgctl,semctl or shmctl functions. • To change these values the calling process must be either the creator of the IPC structure or the super user. • Message queues and shared memory use terms read and write,semaphore use the term read and alter.
Configuration Limits: • All three forms of IPC have built-in limits. • Most of these can be changed by reconfiguring the kernel Advantages and Disadvantages: Advantages: • The name space used by XSI IPC (identifier). • Other advantage of message queue is reliable, flow controlled, record oriented. Disadvantages: • IPC structures are system wide do not have reference count. • These IPC structures are not known by names in the file system. • Since these forms of IPC do not use file descriptors, we can not use multiplexed I/O functions with them: select and poll. This makes it harder to use more than one of these IPC structures at a time.