20130422

How to implement msgq message queue in CPP C C++


//Program to implement msgq
/*CPP Program for msg queue in Linux
pipes work with related process parent-child only.
Fifo can make unrelated process to communicate but
fifo created does not persists for use in future.
Shared memory is similar to fifo but over comes this and can be used later also.
Program to implement IPC Message Queues IPC mechanism.
msgqSendkirk.c adds the message on the message queue.
msgqRecvSpock.c reads and removes the message from the message
queue.To use this program first compile and run msgqSendkirk.c
to add a message to the message queue. To see the
Message Queue in other terminal run msgqRecvSpock.c
ipcs -q shows the msg ques.
ipcrm -q 131073 removes the msgq created say 131071 */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
//1. declaring the msg structure
struct my_msgbuf
{
long mtype;
char mtext[200];
};
int main(void)
{
struct my_msgbuf buf;
int msqid;
key_t key;
//2. creating key with ftok
key = ftok("msgqSendkirk.c", 'B');
///local/kumarsah/comp/linuxdemo/msgqSendkirk.c
//3. creating msgq with msgget
msqid = msgget(key, 0644 | IPC_CREAT);
printf("Enter lines of text, ^D to quit:\n");
buf.mtype = 1;
//4. type and send the message
while(fgets(buf.mtext, sizeof buf.mtext, stdin) != NULL)
{
int len = strlen(buf.mtext);
msgsnd(msqid, &buf, len+1, 0);
}
//5. removing msgq with msgctl with IPC_RMID
msgctl(msqid, IPC_RMID, NULL) ;
return 0;
}

// msgqRecvSpock.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct my_msgbuf {
long mtype;
char mtext[200];
};
int main(void)
{
struct my_msgbuf buf;
int msqid;
key_t key;
if ((key = ftok("msgqSendkirk.c", 'B')) == -1) {/* same key as msgqSendkirk.c */
perror("ftok");
exit(1);
}
if ((msqid = msgget(key, 0644)) == -1) { /* connect to the queue */
perror("msgget");
exit(1);
}
printf("spock: ready to receive messages:\n");
for(;;) { /* Spock never quits! */
if (msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0) == -1) {
perror("msgrcv");
exit(1);
}
printf("spock: \"%s\"\n", buf.mtext);
}
return 0;
}

/*terminal 1:
$ gcc msgqSendkirk.c
$ ./a.out
Enter lines of text, ^D to quit:
dwqdadd
aaaaaaaaa
ddddd
ddd
dd

terminal2:
gcc msgqRecvSpock.c
$ ./a.out
spock: ready to receive messages:.
spock: "dwqdadd"
sldjslds
spock: "aaaaaaaaa"
spock: "ddddd"
ddd
d
*/
Previous                             Home                               Next

2 comments:

  1. How to check message queue create?
    ipcs -q

    How to remove msg que created?
    ipcrm -q msgqid

    ReplyDelete
    Replies
    1. Program to implement shared memory?Howto shared memory?
      a segment of memory that is shared between processes.
      Attach means getting a pointer to the segment.
      */
      #include #include #include #include #include #include
      #define SHM_SIZE 1024 /* make it a 1K shared memory segment */
      int main(int argc, char *argv[])
      {
      key_t key;
      int shmid;
      char *data;
      int mode;
      if (argc > 2) {
      fprintf(stderr, "usage: shmdemo [data_to_write]\n");
      exit(1);
      }
      /* make the key: */
      if ((key = ftok("sharedmemory.c", 'R')) == -1) {
      perror("ftok");
      exit(1);
      }
      /* connect to (and possibly create) the segment: */
      if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) {
      perror("shmget");
      exit(1);
      }
      /* attach to the segment to get a pointer to it: */
      data = shmat(shmid, (void *)0, 0);
      if (data == (char *)(-1)) {
      perror("shmat");
      exit(1);
      }
      /* read or modify the segment, based on the command line: */

      if (argc == 2) {
      printf("writing to segment: \"%s\"\n", argv[1]);
      strncpy(data, argv[1], SHM_SIZE);
      } else
      printf("segment contains: \"%s\"\n", data);
      /* detach from the segment: */
      if (shmdt(data) == -1) {
      perror("shmdt");
      exit(1);
      }
      return 0;
      }


      /*OUTPUT
      * $ gcc sharedmemory.c
      $ ./a.out
      ftok: No such file or directory
      $ gcc sharedmemory.c
      $ ./a.out
      segment contains: ""


      $ ./a.out
      $ ./a.out sasdsd
      writing to segment: "sasdsd"
      * */

      Delete