use_msgQ - msgsnd(qid, (struct msgbuf *)&msg,...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// use_msgQ.cpp / #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <string.h> #include <iostream.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> # // declare my global message buffer struct buf { long mtype; // required char greeting[50]; // mesg content }; } void child_proc_one(int qid) { v buf msg; int size = sizeof(msg)-sizeof(long); i msgrcv(qid, (struct msgbuf *)&msg, size, 113, 0); m cout << getpid() << ": gets message" << endl; cout << "message: " << msg.greeting << endl; c strcat(msg.greeting, " and Adios."); cout << getpid() << ": sends reply" << endl; msg.mtype = 114; // only reading mesg with type mtype = 114 msgsnd(qid, (struct msgbuf *)&msg, size, 0); cout << getpid() << ": now exits" << endl; } void child_proc_two(int qid) { v buf msg; int size = sizeof(msg)-sizeof(long); i // sending a message that will never be read msg.mtype = 12; strcpy(msg.greeting, "Fake message");
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: msgsnd(qid, (struct msgbuf *)&amp;msg, size, 0); m // prepare my message to send strcpy(msg.greeting, &quot;Hello there&quot;); s cout &lt;&lt; getpid() &lt;&lt; &quot;: sends greeting&quot; &lt;&lt; endl; msg.mtype = 113; // set message type mtype = 113 / msgsnd(qid, (struct msgbuf *)&amp;msg, size, 0); m msgrcv(qid, (struct msgbuf *)&amp;msg, size, 114, 0); cout &lt;&lt; getpid() &lt;&lt; &quot;: gets reply&quot; &lt;&lt; endl; cout &lt;&lt; &quot;reply: &quot; &lt;&lt; msg.greeting &lt;&lt; endl; cout &lt;&lt; getpid() &lt;&lt; &quot;: now exits&quot; &lt;&lt; endl; } int main() { i int qid = msgget(IPC_PRIVATE, IPC_EXCL|IPC_CREAT|0600); i //spawning two child processes pid_t cpid = fork(); if (cpid == 0) { i child_proc_one(qid); exit(0); } cpid = fork(); if (cpid == 0) { i child_proc_two(qid); exit(0); } while(wait(NULL) != -1); // waiting for both children to terminate w msgctl(qid, IPC_RMID, NULL); m cout &lt;&lt; &quot;parent proc: &quot; &lt;&lt; getpid() &lt;&lt; &quot; now exits&quot; &lt;&lt; endl; &lt; exit(0); }...
View Full Document

Page1 / 2

use_msgQ - msgsnd(qid, (struct msgbuf *)&amp;amp;amp;msg,...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online