Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* ** spock.c -- reads from a message queue */ #include #include #include #include #include #include struct my_msgbuf { long mtype; char mtext[200]; }; int

/*

** spock.c -- reads from a message queue

*/

#include

#include

#include

#include

#include

#include

struct my_msgbuf {

long mtype;

char mtext[200];

};

int main(void)

{

struct my_msgbuf buf;

int msqid;

key_t key;

if ((key = ftok("kirk.c", 'B')) == -1) { /* same key as kirk.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, captain. ");

for(;;) { /* Spock never quits! */

if (msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0) == -1) {

perror("msgrcv");

exit(1);

}

printf("spock: \"%s\" ", buf.mtext);

}

return 0;

}

/*

** kirk.c -- writes to a message queue

*/

#include

#include

#include

#include

#include

#include

#include

struct my_msgbuf {

long mtype;

char mtext[200];

};

int main(void)

{

struct my_msgbuf buf;

int msqid;

key_t key;

if ((key = ftok("kirk.c", 'B')) == -1) {

perror("ftok");

exit(1);

}

if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) {

perror("msgget");

exit(1);

}

printf("Enter lines of text, ^D to quit: ");

buf.mtype = 1; /* we don't really care in this case */

while(fgets(buf.mtext, sizeof buf.mtext, stdin) != NULL) {

int len = strlen(buf.mtext);

/* ditch newline at end, if it exists */

if (buf.mtext[len-1] == ' ') buf.mtext[len-1] = '\0';

if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */

perror("msgsnd");

}

if (msgctl(msqid, IPC_RMID, NULL) == -1) {

perror("msgctl");

exit(1);

}

return 0;

}

image text in transcribed
Review the programs (spock.c and kirk.c). Answer (or discuss) questions listed below: a) Discuss and evaluate what happens when you're running both in separate windows and you kill one or the other. (3) b) Discuss what happens (and why) when you run two copies of kirk. (3) c) Discuss what happens (and why) when your run two copies of spock.(3) Deliverable: A pdf document with the answers to (a), (b) and (c)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

T Sql Fundamentals

Authors: Itzik Ben Gan

4th Edition

0138102104, 978-0138102104

More Books

Students also viewed these Databases questions

Question

Which of the following is true of disruptive technologies?

Answered: 1 week ago

Question

Explain the concept of shear force and bending moment in beams.

Answered: 1 week ago

Question

Decision Making in Groups Leadership in Meetings

Answered: 1 week ago