mirror of
https://github.com/Hizenberg469/Inter-Process-Communication-IPC-.git
synced 2026-04-19 18:02:24 +03:00
Message Queue
This commit is contained in:
23
Message Queue/CMakeLists.txt
Normal file
23
Message Queue/CMakeLists.txt
Normal file
@@ -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 "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>: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.
|
||||
68
Message Queue/receiver.c
Normal file
68
Message Queue/receiver.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/select.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <mqueue.h>
|
||||
#include <errno.h>
|
||||
|
||||
#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 </msgq-name>\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;
|
||||
}
|
||||
42
Message Queue/sender.c
Normal file
42
Message Queue/sender.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <mqueue.h>
|
||||
#include <errno.h>
|
||||
|
||||
#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 </msgq-name>\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;
|
||||
}
|
||||
Reference in New Issue
Block a user