Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* #include <cstdlib> */
- #include <iostream>
- #include <cerrno>
- #include <limits>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <stdlib.h>
- #include <string.h>
- using namespace std;
- #define BUFSIZ 512
- /*
- This program sends two types of messages of type 5 and type 1. Two different
- receiving processes receive the messages of different types (msg_receiver and
- msg_receiver2).
- */
- struct my_msg_st {
- long int my_msg_type;
- char some_text[BUFSIZ];
- };
- int main(int argc, char** argv)
- {
- int running = 1;
- int msgid;
- struct my_msg_st some_data;
- struct my_msg_st return_data;
- char buffer[BUFSIZ];
- long int msg_to_receive = 11;
- /* Set up the message queue */
- msgid = msgget((key_t) 1234, 0666 | IPC_CREAT);
- if (msgid == -1) {
- cout << "msgget failed" << endl;
- exit(EXIT_FAILURE);
- }
- /* Input message; send message to the queue until an "end" is encountered. */
- while (running) {
- cout << "Enter some text (5): ";
- // get the text to be put into the queue.
- cin.getline(buffer,BUFSIZ);
- // set message type
- some_data.my_msg_type = 5;
- // copy message into shared memory
- strcpy(some_data.some_text, buffer);
- // if the message send is set to error, end.
- if (msgsnd(msgid, (void *)&some_data, BUFSIZ, 0) == -1) {
- cout << "msgsnd failed" << endl;
- exit(EXIT_FAILURE);
- }
- // else print "sent message"
- cout << "You sent a message." << endl;
- // get text for second output.
- cout << "Enter some text (1): ";
- cin.getline(buffer,BUFSIZ);
- // set message type
- some_data.my_msg_type = 1;
- // copy data
- strcpy(some_data.some_text, buffer);
- //check for error
- if (msgsnd(msgid, (void *)&some_data, BUFSIZ, 0) == -1) {
- cout << "msgsnd failed" << endl;
- exit(EXIT_FAILURE);
- }
- cout << "You sent a message." << endl;
- // INIT TEST OF THREE RUNNING
- cout << "Enter some text (15): ";
- cin.getline(buffer,BUFSIZ);
- // set message type
- some_data.my_msg_type = 15;
- // copy data
- strcpy(some_data.some_text, buffer);
- //check for error
- if (msgsnd(msgid, (void *)&some_data, BUFSIZ, 0) == -1) {
- cout << "msgsnd failed" << endl;
- exit(EXIT_FAILURE);
- }
- cout << "You sent a message." << endl;
- // exit if text is "end"
- if (strncmp(buffer, "end", 3) == 0) {
- running = 0;
- }
- }
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement