From 1b1acabe4281d1051c6bfdd9cd05fcc9b76c19d0 Mon Sep 17 00:00:00 2001 From: Hizenberg Date: Mon, 29 Apr 2024 01:20:25 +0530 Subject: [PATCH] Message Queue --- Message Queue/CMakeLists.txt | 23 ++++++++++++ Message Queue/receiver.c | 68 ++++++++++++++++++++++++++++++++++++ Message Queue/sender.c | 42 ++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 Message Queue/CMakeLists.txt create mode 100644 Message Queue/receiver.c create mode 100644 Message Queue/sender.c diff --git a/Message Queue/CMakeLists.txt b/Message Queue/CMakeLists.txt new file mode 100644 index 0000000..2cc852c --- /dev/null +++ b/Message Queue/CMakeLists.txt @@ -0,0 +1,23 @@ +# CMakeList.txt : CMake project for Message Queue, include source and define +# project specific logic here. +# +cmake_minimum_required (VERSION 3.8) + +# Enable Hot Reload for MSVC compilers if supported. +if (POLICY CMP0141) + cmake_policy(SET CMP0141 NEW) + set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$,$>,$<$:EditAndContinue>,$<$:ProgramDatabase>>") +endif() + +project ( AF_UNIX VERSION 1.0.0 LANGUAGES C CXX) + +set(CMAKE_C_STANDARD 17) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_C_EXTENSIONS OFF) + +# Add source to this project's executable. +add_executable (sender "sender.c") +add_executable (receiver "receiver.c") + + +# TODO: Add tests and install targets if needed. diff --git a/Message Queue/receiver.c b/Message Queue/receiver.c new file mode 100644 index 0000000..a7690bb --- /dev/null +++ b/Message Queue/receiver.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_MESSAGES 10 +#define MAX_MSG_SIZE 256 +#define MSG_BUFFER_SIZE (MAX_MSG_SIZE + 10) +#define QUEUE_PERMISSION 0660 + +int +main(int argc, char** argv) { + + fd_set readfds; + char buffer[MSG_BUFFER_SIZE]; + int msgq_fd = 0; + + if (argc <= 1) { + printf("provide a receipient msgQ name : format \n"); + return 0; + } + + /* To set msgQ attributes */ + struct mq_attr attr; + attr.mq_flags = 0; + attr.mq_maxmsg = MAX_MESSAGES; + attr.mq_msgsize = MAX_MSG_SIZE; + attr.mq_curmsgs = 0; + + if ((msgq_fd = mq_open(argv[1], O_RDONLY | O_CREAT , QUEUE_PERMISSION, &attr)) == -1) { + printf("Client : mq_open failed, errno = %d", errno); + exit(1); + } + + while (1) { + + /* Empty the fd_set data structure. */ + FD_ZERO(&readfds); + + /* Insert MsgQ fd to fd_set. */ + FD_SET( msgq_fd, &readfds); + printf("Receiver blocked on select()....\n"); + + /* Use select system call to detect the data enqueue for message queue. */ + select(msgq_fd + 1, &readfds, NULL, NULL, NULL); + + if (FD_ISSET(msgq_fd, &readfds)) { + printf("Msg recvd msgQ %s\n", argv[1]); + + memset(buffer, 0, MSG_BUFFER_SIZE); + + /* Read the data from message queue. */ + if (mq_receive(msgq_fd, buffer, MSG_BUFFER_SIZE, NULL) == -1) { + printf("mq_receive error, errno = %d\n", errno); + exit(1); + } + printf("Msg received from Queue = %s\n", buffer); + } + } + + return 0; +} \ No newline at end of file diff --git a/Message Queue/sender.c b/Message Queue/sender.c new file mode 100644 index 0000000..42b4c77 --- /dev/null +++ b/Message Queue/sender.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_MSG_SIZE 256 +#define MSG_BUFFER_SIZE (MAX_MSG_SIZE + 10) + + +int +main(int argc, char** argv) { + + char buffer[MSG_BUFFER_SIZE]; + int recvr_msgq_fd = 0; + + if (argc <= 1) { + printf("provide a reciepient msgQ name : format \n"); + return 0; + } + + memset(buffer, 0, MSG_BUFFER_SIZE); + printf("Enter msg to be sent to reciever %s\n", argv[1]); + scanf("%s", buffer); + + if ((recvr_msgq_fd = mq_open(argv[1], O_WRONLY | O_CREAT, 0, 0)) == -1) { + printf("Client: mq_open failed, errno = %d", errno); + exit(1); + } + + if (mq_send(recvr_msgq_fd, buffer, strlen(buffer) + 1, 0) == -1) { + perror("Client: Not able to send message to server"); + exit(1); + } + + mq_close(recvr_msgq_fd); + return 0; +}