UNIT - 5 Linux
UNIT - 5 Linux
communication.
• Pipes are the oldest form of UNIX System IPC and are
provided
by all UNIX systems. Pipes have two limitations.
#include <unistd.h>
int pipe(int filedes[2]);
• What happens after the fork depends on which direction of data flow we
want. For a pipe from the parent to the child, the parent closes the read
end of the pipe (fd[0]), and the child closes the write end (fd[1]).
• For a pipe from the child to the parent, the parent closes fd[1], and the child
closes fd[0]. When one end of a pipe is closed, the following two rules
apply.
If we read from a pipe whose write end has been closed, read returns 0
to
indicate an end of file after all the data has been read.
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 1 with errno set to EPIPE.
Pipes
popen and pclose :
These two functions handle all the dirty work that we've been
doing ourselves: creating a pipe, forking a child, closing the
unused ends of the pipe, executing a shell to run the
command, and waiting for the command to terminate.
#include <stdio.h>
FILE *popen (const char *cmdstring, const char *type);
of cmdstring.
• The pclose function closes the standard I/O stream, waits for
the
FIFOs
• FIFOs are sometimes called named pipes. Pipes can be used
only
between related processes when a common ancestor has
created
the pipe. With FIFOs, unrelated processes can exchange data.
• FIFOs are used by shell commands to pass data from one shell
pipeline to another without creating intermediate temporary
files.
1)Message queues
2)Semaphores
3)Shared Memory
Message Queues:
• A message queue is a linked list of messages stored within the
2.If the value of the semaphore is positive, the process can use
the
resource. In this case, the process decrements the semaphore
value by 1, indicating that it has used one unit of the resource.
struct msg {
A Message Queue
message record
message table
• The kernel copies the message data from the sender process’s virtual
address into the kernel data region, so that the sender process is free to
terminate, and the message can still be read by another process in the
future.
• When a process retrieves a message from a queue, the kernel copies the
message data from a message record to the process’s virtual address
and then discards the message record.
• Retrieve the oldest message in the queue, regardless of its message type.
• Retrieve a message whose message ID matches the one specified by the
process. If there are multiple messages with the given message type
existing in the queue, retrieve the oldest one among them.
• Retrieve a message whose message type is the lowest among those that
are less than or equal to the one specified by the process. If there are
multiple messages that satisfy the same criteria, retrieve the oldest one
among them.
UNIX System V APIs for Messages
• The are four different Unix system V APIs for messages,
1.msgget()
2.msgsend()
3.msgrcv()
4.msgctl()
msgget():
The first function normally called is msgget to either open an existing queue or create a
new queue.
The function prototype of the msgget API IS:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/message.h>
This function “opens” a message queue whose key ID matches the key
actual value and returns a positive integer descriptor. This can be used in other
message APIs to send and receive messages and to query and/or set control
data for the queue.
UNIX System V APIs for Messages
• For example, the following system call creates a message queue with the
key
ID of 15 and access permission of 0644 (that is, read-write for owner and
read-only for group members and others), if such a queue does not
preexist.
The call also returns an integer descriptor for future queue references:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
int msgsnd ( int msgfd, const void* msgPtr, int len,int flag);
• The flag value may be 0, which means the process can be blocked, if needed,
until the function call completes successfully. If it is the IPC_NOWAIT flag, the
function aborts if the process is to be blocked.
• The return value of the API is 0 if it succeeds or -1 if it fails.
msgrcv
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
int msgrcv ( int msgfd, const void* msgPtr, int len,int mtype, int flag);
This API recevies a message of type mtype from a message queue designated
by the msgfd. The message received is stored in the object pointed to by the
msgPtr argument. The len argument specifies the maximum number of
message text bytes that can be received by this call.
message type
-ve integer Receive a message whose message type is
less than or equal to the absolute value of
the
mtype. If there is more than one message in
the queue meeting this criteria, receive the
one that is the oldest and has the smallest
message type value
UNIX System V APIs for Messages
msgctl System Call - Message Queue Control
Function
Ownership and access permissions, established when the
message queue was created, can be examined and modified using the
msgctl system call.
Include
< sys/types.h> <sys/ipc.h> < sys/msg.h>
Command:
int msgctl ( int msqid, int cmd, struct msqid_ds * buf);
Return
Success: 0; Failure: -1; Sets errno: Yes
UNIX System V APIs for Messages
Arguments of msgctl System Call
• int msqid
Command: