use_msgQ_w_ftok

use_msgQ_w_ftok - char greeting[50]; }; buf msg; int size =

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

View Full Document Right Arrow Icon
// use_msgQ_w_ftok.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> # void child_proc_one() { v // create my msgQ with key value from ftok() int qid = msgget(ftok(".",'u'), IPC_EXCL|IPC_CREAT|0600); i // declare my message buffer struct buf { long mtype; // required char greeting[50]; // mesg content }; } 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() { v // pause child proc two sleep(3); // BAD programming - unreliable and potential bug s int qid = msgget(ftok(".",'u'), 0); i // declare my message buffer struct buf { long mtype;
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: char greeting[50]; }; buf msg; int size = sizeof(msg)-sizeof(long); i // sending a message that will never be read strcpy(msg.greeting, &quot;Fake message&quot;); msgsnd(qid, (struct msgbuf *)&amp;msg, size, 0); m 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 //spawning two child processes pid_t cpid = fork(); if (cpid == 0) { i child_proc_one(); exit(0); } cpid = fork(); if (cpid == 0) { i child_proc_two(); exit(0); } while(wait(NULL) != -1); // waiting for both children to terminate w int qid = msgget(ftok(&quot;.&quot;,'u'), 0); 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_w_ftok - char greeting[50]; }; buf msg; int size =

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