Message Queues = can be used to pass messages between related or unrelated processes executing on same machine. Message queues are somewhat like pipes and fifos , but differ in 2 important aspects.
1. mesage boundaries are preserved, so that readers and writers communciate in units of messages.
2. message queues are kerenl persistent
A message queue can be thought of as a linked list of messages in the kernel space. Processes with adequate permissions can put messages onto the queue and processes with adequate permissions can remove messages from the queue.
msgget system call
to create a brand new message queue or to get the identifer of an existing queue we use the msgget system call, which on usccess returns a unique message queue identifier.
if a message queue associated with the first argument, key already eixsit, the call returns th eidentifier of th existing message queue, otherwise it creates a new message queue
Wecan use IPC_PRIVATE constant as first arugment. a parent process creates message queue prior to perfomring a fork, and the child inherits the returned message queue identifier. for unrelated processes we can sue this constant, but in that case the creator porcess has to write the returned message queue identifier in a file that can be read by the other process
msgsnd system call
used to send a message to the message queue identified by its first argument, which is the message queue identifier.
msgrcv system call
reads and removes a message from message queue identified by its first argument msquid, and copies its contents into the buffer pointed to by its second argument msgp.
notice the fourth argument.
after creating message sender process can terminate and message will still be preserved.
later receiver process will use system call to copy kernel data and rmeove message record.
notice eventhough reciever and sender both terminate message table is still there which means it is kernel persistence.
#include <stdio.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 512
struct msgbuf{
long mtype;
char mtext[SIZE];
};
int main(){
key_t key = ftok("./myfile", 65);
int qid = msgget(key, IPC_CREAT | 0666);
//place first message on this message queue
struct msgbuf msg1;
msg1.mtype = 10;
strcpy(msg1.mtext, "Learning is fun with Arif\n");
msgsnd(qid, &msg1, sizeof msg1.mtext, 0);
//place second message on this message queue
struct msgbuf msg2;
msg2.mtype = 20;
strcpy(msg2.mtext, "This is GR8\n");
msgsnd(qid, &msg2, sizeof msg2.mtext, 0);
return 0;
}
we created message queues and inside queue there are 2 messages.
in order to communicate through same message queue we need to have same key. we can get same key by using ftok using same argument.
#include <sys/msg.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <stdio.h>
#define SIZE 512
struct msgbuf{
long mtype;
char mtext[SIZE];
};
int main(){
key_t key = ftok("./myfile", 65);
int qid = msgget(key, IPC_CREAT | 0666);
//get the first message from the message queue
struct msgbuf msg;
msgrcv(qid, &msg, SIZE, 0, IPC_NOWAIT);
printf("Message Received: %s\n",msg.mtext);
return 0;
}
notice even though there are no messages in message queue but it still exisit. because it is kernel persistence.
'Operating System > System Programming(Arif Butt)' 카테고리의 다른 글
Lec31) Synchronization among Threads (0) | 2021.07.25 |
---|---|
Lec29, 30) Programming With Shared Memory , Memory Mapped Files (0) | 2021.07.24 |
Lec26,27) Programming UNIX Pipes and Named Pipes (0) | 2021.07.20 |
Lec25) Design and Code Of Signal Handlers (0) | 2021.07.17 |
Lec24) Overview Of UNIX IPC And Signals On The Shell (0) | 2021.07.16 |