Ipc Using
Ipc Using
MESSAGE QUEUES
ROLL NO: 1601-22-733-028
INTER-PROCESS COMMUNICATION
Importance:
Useful in multi-tasking operating systems to exchange data, coordinate tasks,
and ensure efficient resource sharing.
Types of IPC:
•Pipes
•Message Queues
•Shared Memory
•Semaphores
•Sockets
MESSAGE QUEUES
DEFINITION:
A Message Queue is a linked list of messages stored within the kernel and identified by
a message queue identifier.
•ftok(): Used to generate a unique key for identifying the message queue.
•msgget(): Returns the message queue identifier for a newly created queue or
retrieves the identifier of an existing queue with the same key.
ftok() is a system call used to generate a unique key, which is essential for creating
or accessing IPC mechanisms like message queues, shared memory, and
semaphores.
The key generated by ftok() is used by msgget() to either create a new message
queue or access an existing one.
proj_id: A project identifier (an integer) that helps ensure the uniqueness of the key.
•msgget(key_t key, int msgflg);
msgget() is a system call used to create a new message queue or access an existing
one, using a unique key.
It either creates a message queue if it doesn't already exist or retrieves the identifier (ID) of an
existing queue with the same key. This ID is later used in other system calls like msgsnd(),
msgrcv(), and msgctl() to send, receive, or manage messages.
key: The unique key (often generated by ftok()) to identify the message queue.
msgflg: Flags that determine the creation or access mode of the queue (e.g.,
IPC_CREAT to create the queue if it doesn't exist).
Types of msgflg:
1)IPC_CREAT - For creating msg queue If doesn’t exists
2)IPC_EXCL -Fail If queue exists
3)IPC_PRIVATE -For private queue creation
4)Permissions eg-0666,0764,…
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
msqid:
The message queue identifier, which is returned by msgget().
msgp:
A pointer to the message to be sent. The message should start with a field of type long
representing the message type, followed by the actual data.
msgsz:
The size of the message data in bytes (excluding the message type field).
msgflg:
Flags that determine the behavior of msgsnd() when the queue is full:
0: Block until space is available if the queue is full.
IPC_NOWAIT: Return an error immediately if the queue is full without waiting.
int main()
{
key_t key;
int msgid;
return 0;
}
msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
msqid:
The message queue identifier returned by msgget().
msgp:
A pointer to a buffer where the received message will be stored. The buffer must be
large enough to hold the message structure.
msgsz:
The maximum size of the message data to be received (excluding the message type
field).
msgtyp:
Specifies the type of the message to be received. It can be:
A positive integer: Only messages of that type will be received.
0: Receive the next message in the queue regardless of its type.
-1: Receive the first message in the queue.
msgflg:
Flags that control the behavior of msgrcv():
0: Block until a message is available if the queue is empty.
IPC_NOWAIT: Return immediately with an error if no messages are available.
// C Program for Message Queue (Reader Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int main()
{
key_t key;
int msgid;
return 0;
}