Inter-Process Communication: - Objectives
Inter-Process Communication: - Objectives
• Objectives
– Introduction to IPC
– PIPES
– FIFO
– Permission structure
– Message Queue
• Introduction
• Introduction
• Introduction
– The Kernel is responsible for the data transfer
• Between processes
– The kernel Provides facilities such as
• Signals
• PIPE
• FIFO
• Message Queue
• Shared memory
• Semaphore
• Introduction
– Purpose of Interprocess communication
– Data Transfer
• The amount of data sent vary from
– Sharing data
• Multiple processes may wish to operate on Shared data
• Introduction
– Purpose of Interprocess communication ( Contd.)
– Event Notification
• A process can notify another process about
– An occurrence of an event
• Example
– SIGCHLD,
– SIGHUP etc
• Introduction
– Purpose of Interprocess communication (Contd.)
– Resource Sharing
• A set of process can define their own protocol
– For accessing a specific resource
– Implemented using
• locking or
• Synchronization
• Introduction
– Purpose of Interprocess communication (Contd.)
– Process Control
• A process may take complete control over
– Example
• Debuggers
PIPES
• PIPES
• Unidirectional
• First-in-First-out
• PIPES
• PIPES
• To other readers
– Pipes provides
• PIPES
• PIPES
User Process
fd[0] fd[1]
pipe
kernel
• PIPES
– A pipe is created by calling the pipe system call
• PIPES
– The filedes [ 0 ]
– The filedes [ 1 ]
• PIPES
– of either end
• PIPES
• PIPES
parent child
fd[0] fd[1] fd[0] fd[1]
pipe
kernel
• PIPES
– If the direction of data flow desired is
• PIPES
parent child
fd[0] fd[1] fd[0] fd[1]
pipe
kernel
• PIPES
– Reading from a pipe whose write end has been closed
• PIPES
• Reading or writing
• PIPES
– Write to a pipe whose read end is closed
– The signal SIGPIPE is generated
• If the signal is either
– Ignored or
– Handled
– Write returns an error with errno
» set to EPIPE
• PIPES
– The kernel's pipe buffer size is specified by
• The constant PIPE_BUF
• It might be interleaved
• PIPES
– Example of a pipe from the parent to child
main ( void ) {
int n, fd [ 2 ] ;
pid_t pid ;
char line [ MAX ] ;
pipe ( fd ) ;
pid = fork ( ) ;
if ( pid > 0 ) { / *parent */
close ( fd [ 0 ] ) ; / * read */
write ( fd [ 1 ] , "hello\n" ,6 ) ;
}
• PIPES
– Example of a pipe from the parent to child ( Contd.)
• PIPES
– The Files descriptors returned from pipe
• Can be duplicated
– Example
– The STDIN_FILENO
• PIPES
– Example of redirecting the read end to STDIN_FILENO
// . . . other lines of code
if ( pid = = 0 ) { /* child */
close ( fd [ 1 ] /* write */
if ( fd [ 0 ] ! = STDIN_FILENO ) {
dup2 ( fd [ 0 ], STDIN_FILENO );
close ( fd [ 0 ] );
}
• PIPES
– For bi-directional communication
• PIPE
• PIPES
• Disadvantages of Pipes
– They are half-duplex
• PIPES
• Disadvantages of Pipes (Contd.)
– Bytes in the pipe are treated as byte streams
• No knowledge of message boundaries
• PIPES
• popen call
FILE * popen ( const char *cmdstr,
const char * type
);
Returns file pointer on success
NULL on error
• PIPES
• popen call
– Creates the pipe
– Forks a child
• PIPES
• popen call
– If the type specified is " r "
– The file pointer is connected to the
• Standard output of cmdstr
parent fp child
stdout cmdstr
• PIPES
• popen call
– If the type specified is " w "
– The file pointer is connected to the
• Standard input of cmdstr
parent fp child
stdin cmdstr
• PIPES
• pclose call
• PIPES
• pclose call
– Waits for the command to terminate and returns
• The termination status of the shell
• PIPES
– Example of popen call
FIFOs
• FIFO
– FIFO stands for first-in-first-out
• FIFO
– A FIFO is a type of file
• An entry is made in the directory where it is created
– S_ISFIFO macro
• FIFO
– To create a FIFO
• FIFO
– The specification for the mode argument is same
• As for the open system call
– The fifo created can have the permissions for
• User,
• Group and
• Others
– Once created
• The fifo can be opened with the open system call
• FIFO
– The normal file operations also apply for fifo
• close , read , write , unlink etc . .
– SVR4 and BSD also support
• The mkfifo command
– Hence FIFO can also be created using a shell
$ mkfifo fifoname
– They can also be accessed
• With normal shell I/O redirection
• FIFO
• Opening a FIFO
– Without the O_NONBLOCK flag
• FIFO
• Opening a FIFO With the
– O_NONBLOCK flag specified
– open for read only
• Returns immediately
– open for write only
• Returns an error
• With the errno set to ENXIO
– No process has fifo open for reading
• FIFO
– If write to a FIFO with no process open for reading
• Signal SIGPIPE is generated
• FIFO
• Uses of FIFO
– By shell commands to pass data from one
• shell pipe to another
• Problem statement
• Design
– The server creates the IPC channel
• Well Known
• Design
• and waits
• Design
• created is new
– key is IPC_PRIVATE
• Permission structure
– There is a permission structure associated with
• each IPC structure
– This structure defines the permissions and owner
struct ipc_perm {
uid_t uid ; /* EUID */
gid_t gid ; /* EGID */
uid_t cuid ; /* Creators EUID */
gid_t cgid ; /* Creators EGID */
mode_t mode ; /* access mode */
ulong seq ; /* slot usage seq no */
key_t key ; /* key */
} ;
Unix System Programming Satyam Computer Services Ltd 64
Inter-process Communication
• Permission structure
• except seq
• Permission structure
– The mode field specifies the permissions
• IPC structures
– They are system wide
Message Queues
• Message Queue
– Message Queue are a linked list of messages
• Message Queue
– Each Queue has the structure
• of the queue
struct msquid_ds {
struct ipc_prem msg_perm ;
struct msg * msg_first ;
struct msg * msg_last ;
ulong msg_cbytes ;
ulong msg_qnum ;
ulong msg_qbytes ;
pid_t msg_lspid ;
pid_t msg_lrpid ;
time_t msg_stime ;
time_t msg_rtime ;
time_t msg_ctime ;
} ;
Unix System Programming Satyam Computer Services Ltd 71
Inter-process Communication
• Message Queue
int msgget ( key_t key , int flag ) ;
Returns message queue ID on success
-1 on error
• Message Queue
– msg_ctime is set to the current time.
• Queue ID
• Message Queue
– Data is placed on the message Queue using
• Message Queue
– ptr points to the MyMesg structure
struct MyMesg {
long mtype ; /* positive message type */
} ;
• Message Queue
• Message Queue
• Message Queue
• Message Queue
– Messages are received from a queue by msgrcv
int msgrcv ( int msqid ,
void * ptr ,
size_t nbytes ,
long type ,
int flag
) ;
Returns size of data portion of the message
-1 on error
• Message Queue
– The ptr argument points to the struct MyMesg
• A Long integer followed by
• A data buffer
– nbytes
– specifies the size of the data buffer
• If the returned message is larger than nbytes
• The message is truncated
– If the MSG_NOERROR bit in the flag is set
Unix System Programming Satyam Computer Services Ltd 80
Inter-process Communication
• Message Queue
• Message Queue
– The type argument lets us specify
• Which message is desired
• type = = 0
• type > 0
• Message Queue
– type < 0
• Message Queue
• It causes msgrcv
• Message Queue
– If a flag value of IPC_NOWAIT is not specified
– The process is blocked until
• A message of the specified type is available
• The queue is removed from the system
– An error of EIDRM is returned
• A signal is caught and
– The signal handler returns
– An error of EINTR is returned
Unix System Programming Satyam Computer Services Ltd 85
Inter-process Communication
• Message Queue
– Message control
• Message Queue
– The cmd argument
• IPC_STAT
– Fetches the msqid_ds structure
• IPC_SET
– Set the fields of the structure msg_perm and
– Set the msg_qbytes
– Only a superuser or a process with
» EUID equal to cuid or uid of the structure can issue
• Message Queue
– IPC_RMID
• Removes the message queue
• Any data on the system is also removed
• This removal is immediate
– Other process trying to use this queue
» Gets an error EIDRM
– Only a superuser or a process with
» EUID equal to cuid or uid of the structure can issue
Unix System Programming Satyam Computer Services Ltd 88
IPC
Shared Memory
89
Inter-process Communication
• Shared Memory:
– Allows two or more processes
• To share a given region of memory
• shared memory
– The kernel maintains the shmid_ds structure
• For each shared memory segment
struct shmid_ds {
struct ipc_perm shm_perm;
struct anon_map *shm_amp;
int shm_segsz ;
ushort shm_lkcnt ;
pid_t shm_lpid ;
• shared memory:
pid_t shm_nattch;
ulong shm_cnattch ;
time_t shm_atime;
time_t shm_dtime ;
time_t shm_ctime;
};
• shared memory
– To obtain a shared memory identifier
• The function called is shmget
-1 on error
• shared memory
• shared memory
– For a new segment being created
• In a client
• shared memory
int shmctl (int shmid ,
int cmd ,
struct shmid_ds *buf
);
returns : 0 if ok , -1 on error
• shared memory
– IPC_STAT
– IPC_RMID
• shared memory
– or detaches
• shared memory
– IPC_SET
– shm_perm.uid
– shm_perm.gid
– shm_perm.mode
• shared memory
– SHM_LOCK
– SHM_UNLOCK
• shared memory
– Once a shared memory segment is created
• A process attaches it to its address space by calling shmat
• shared memory
• shared memory
• shared memory
• shared memory
• shared memory
– The segment can be detached using shmdt call
Returns 0 if ok ,
-1 on error