Академический Документы
Профессиональный Документы
Культура Документы
Unit-V IPC
COURSE OUTCOMES
1. Understand various Linux System utilities.
2. Demonstrate the shell scripts in shell Environment.
3. Understand the basic principles of file system architecture.
4. Apply the core concept of Process management and implement in the Linux Environment.
5. Compare various Inter-Process communication and Client-Server Application techniques In Linux environment
IPC
Inter-Process-Communication (or IPC for short)
are mechanisms provided by the kernel to allow
processes to communicate with each other.
Pipes
Named Pipes or FIFOs
Message Queues
Shared memory
Semaphores
Sockets
Pipe
A pipe is a connection between two processes, such that the
standard output from one process becomes the standard input of
the other process.
In UNIX Operating System, Pipes are useful for communication
between related processes(inter-process communication)
Pipe
Pipe is one-way communication only i.e we can use a pipe such
that One process write to the pipe, and the other process reads
from the pipe.
The pipe can be used by the creating process, as well as all its
child processes, for reading and writing.
If a process tries to read before something is written to the pipe,
the process is suspended until something is written.
The pipe system call finds the first two available positions in the
process’s open file table and allocates them for the read and write
ends of the pipe.
Process Pipes
Parent and child sharing a pipe
When fork is used in any process, file descriptors remain open
across child process and also parent process.
If the fork is called after creating a pipe, then the parent and child
can communicate via the pipe.
Pipes system call
SYSTEM CALL: pipe();
int pipe( int fd[2] );
RETURNS: 0 on success
-1 on error: errno = EMFILE (no free descriptors)
EMFILE (system file table is full)
EFAULT (fd array is not valid)
fd[0] is set up for reading, fd[1] is set up for writing
The first integer in the array (element 0) is set up and opened for
reading, while the second integer (element 1) is set up and opened
for writing.
popen( )
Creating a pipe using popen()
LIBRARY FUNCTION: popen();
PROTOTYPE: FILE *popen ( char *command, char *type);
RETURNS: new file stream on success
NULL on unsuccessful fork() or pipe() call
creates a pipe, and performs fork/exec operations using "command"
This standard library function creates a half-duplex pipeline by
calling pipe() internally.
It then forks a child process, execs the Bourne shell, and executes
the "command" argument within the shell.
Direction of data flow is determined by the second argument,
"type". It can be "r" or "w", for "read" or "write".
Pclose( )
LIBRARY FUNCTION: pclose();
PROTOTYPE: int pclose( FILE *stream );
RETURNS: exit status of wait() call
-1 if "stream" is not valid, or if wait4() fails
This call would return a valid shared memory identifier on success and
-1 in case of failure
shared memory : shmat()
#include <sys/types.h>
#include <sys/shm.h>
void * shmat(int shmid, const void *shmaddr, int shmflg)
The above system call performs attaching a shared memory segment
to the address space of the calling process.
The arguments that need to be passed are as follows −
The first argument, shmid, is the identifier of the shared memory
segment.
The second argument, shmaddr, is to specify the attaching
address. If shmaddr is NULL, the system by default chooses the
suitable address to attach the segment.
If shmaddr is not NULL and SHM_RND is specified in shmflg, the
attach is equal to the address of the nearest multiple of SHMLBA
(Lower Boundary Address).
shared memory : shmat()
The third argument, shmflg, specifies the required shared memory
flag/s such as
SHM_RND (rounding off address to SHMLBA) or
SHM_EXEC (allows the contents of segment to be executed) or
SHM_RDONLY (attaches the segment for read-only purpose, by
default it is read-write)
This call would return the address of attached shared memory
segment on success and -1 in case of failure.
shared memory : shmdt()
#include <sys/types.h>
#include <sys/shm.h>
int shmdt(const void *shmaddr)
The above system call performs shared memory segment of detaching
the shared memory segment from the address space of the calling
process.
The argument that needs to be passed is −
The argument, shmaddr, is the address of shared memory segment to
be detached. The to-be-detached segment must be the address
returned by the shmat() system call.
• Local Address The address of the local end of the socket, presented
as a dotted ip address. The type is string (16 bytes long).Local PortThe
local port number.
• Foreign Address :The address of the remote end of the socket. Like
"netstat" * indicates that the address is unassigned or unavailable.
Socket: Attributes
• Socket State:The state of the socket. The type is integer (32-bit
numeric property) with enumerated values.
• Socket UID: The user ID of the owner of the socket. The following
values are valid: integers.
• Socket Inode : The inode used by the socket.