0% found this document useful (0 votes)
11 views

Ipc Using

This shows Message queue implementation

Uploaded by

bowatag628
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Ipc Using

This shows Message queue implementation

Uploaded by

bowatag628
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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.

System Calls Used for Message Queues:

•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.

•msgsnd(): Used to place data onto a message queue.

•msgrcv(): Retrieves messages from the queue.

•msgctl(): Performs various control operations on a queue, commonly used to delete


or modify the queue.
..
•key_t ftok(const char *pathname, int proj_id);

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.

pathname: A valid path to a file that exists on the system.

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.

On success returns 0,Otherwise -1


// C Program for Message Queue (Writer Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX 10

// structure for message queue


struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;

int main()
{
key_t key;
int msgid;

// ftok to generate unique key


key = ftok("progfile", 65);

// msgget creates a message queue


// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);
message.mesg_type = 1;

printf("Write Data : ");


fgets(message.mesg_text,MAX,stdin);

// msgsnd to send message


msgsnd(msgid, &message, sizeof(message), 0);

// display the message


printf("Data send is : %s \n", message.mesg_text);

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>

// structure for message queue


struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;

int main()
{
key_t key;
int msgid;

// ftok to generate unique key


key = ftok("progfile", 65);

// msgget creates a message queue


// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);

// msgrcv to receive message


msgrcv(msgid, &message, sizeof(message), 1, 0);

// display the message


printf("Data Received is : %s \n",
message.mesg_text);

// to destroy the message queue


msgctl(msgid, IPC_RMID, NULL);

return 0;
}

You might also like