IPC using Message Queues

Take a decimal number from the user. Convert it to different bases (e.g: 2,8,16 etc.) and send those values to the message queue. Write three separate programs to read and display the binary, octal, and hex values from the message queue distinctively.

Sender Program

//This Code is Written by Souvik Ghosh and Debmitra Chatterjee
// C Program for Message Queue (Sender Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
  
// structure for message queue
typedef struct {
      // message type
    long mesg_type;
     // array to store payLoad
    int payLoad[100];
    // data size under payLoad
    int size;
    int hex;
} 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);
    //input decimal number
    printf("Write Data: ");
    int n;
    scanf("%d",&n);
    // For Binary
    message m1;
    m1.mesg_type = 2;
    int temp=n;
	// decimal to binary convertion
	int i = 0;
	while (temp > 0) {
		m1.payLoad[i] = temp % 2;
		temp = temp / 2;
		i++;
	}
	m1.size=i;
    // msgsnd to send message
    msgsnd(msgid, &m1, sizeof(m1), 0);
    // display the message
    printf("Send Binary Value: ");
    for (int j = i - 1; j >= 0; j--){
	printf("%d", m1.payLoad[j]);
	}
	printf("\n");
    // For Octal
    message m2;
    m2.mesg_type=8;
    temp=n;
    // decimal to octal convertion
	i = 0;
	while (temp > 0) {
		m2.payLoad[i] = temp % 8;
		temp = temp / 8;
		i++;
	}
	m2.size=i;
    // msgsnd to send message
    msgsnd(msgid, &m2, sizeof(m2), 0);
    // display the message
    printf("Send Octal Value: ");
    for (int j = i - 1; j >= 0; j--){
	printf("%d", m2.payLoad[j]);
	}
	printf("\n");
// For HexaDecimal
    message m3;
    m3.mesg_type=16;
    m3.hex = n;
    // msgsnd to send message
    msgsnd(msgid, &m3, sizeof(m3), 0);
    // display the message
    printf("Send HexaDecimal Value: %X",m3.hex);
    printf("\n");
    
    return 0;
}

Binary Receiver

//This Code is Written by Souvik Ghosh and Debmitra Chatterjee
// C Program for Message Queue ( Binary Receiver Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
  
// structure for message queue
typedef struct {
      // message type
    long mesg_type;
     // array to store payLoad
    int payLoad[100];
    // data size under payLoad
    int size;
} 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 m1;
    printf("Receiving message.....\n");
    // msgrcv to receive message
    msgrcv(msgid, &m1, sizeof(m1), 2, 0);
    // display the message
    printf("Received Binary Value: ");
    int i=m1.size;
    for (int j = i - 1; j >= 0; j--){
		printf("%d", m1.payLoad[j]);
  }
  printf("\n");
    // to destroy the message queue
    msgctl(msgid, IPC_RMID, NULL);
    
    return 0;
}

Octal Receiver

//This Code is Written by Souvik Ghosh
// C Program for Message Queue (Octal Receiver Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
  
// structure for message queue
typedef struct {
      // message type
    long mesg_type;
     // array to store payLoad
    int payLoad[100];
    // data size under payLoad
    int size;
} 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 m2;
    printf("Receiving message.....\n");
    // msgrcv to receive message
    msgrcv(msgid, &m2, sizeof(m2), 8, 0);
    // display the message
    printf("Received Octal Value: ");
    int i=m2.size;
    for (int j = i - 1; j >= 0; j--){
		printf("%d", m2.payLoad[j]);
  }
  printf("\n");
    // to destroy the message queue
    msgctl(msgid, IPC_RMID, NULL);
    
    return 0;
}

Hex Receiver

//This code is Written by Souvik Ghosh
// C Program for Message Queue (Hex Receiver Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
  
// structure for message queue
typedef struct {
      // message type
    long mesg_type;
     // array to store payLoad
    int payLoad[100];
    // data size under payLoad
    int size;
    int hex;
} 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 m3;
    printf("Receiving message.....\n");
    // msgrcv to receive message
    msgrcv(msgid, &m3, sizeof(m3), 16, 0);
    // display the message
    printf("Received HexaDecimal Value: %X",m3.hex);
    printf("\n");
    // to destroy the message queue
    msgctl(msgid, IPC_RMID, NULL);
    
    return 0;
}

Output

output of IPC using message queues

Related Posts

Artificial Intelligence Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Artificial Intelligence Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Mixed Signal Design Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Mixed Signal Design Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Fiber Optic Communication Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Fiber Optic Communication Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Cyber Security Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Cyber Security Suggestions! Elevate Your Learning Experience and Excel with Confidence.

MAKAUT 7th Semester Examination Questions – 2023

Mobile Communication and Networks (PE-EC701C) Neural Network and Fuzzy Logic Control (PE-EC702C/PEROB701B) Principles of Management (HS-HU701)

CSS Cheatsheet

Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…

Leave a Reply

Your email address will not be published. Required fields are marked *