Question: /* ** 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;
}
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
Get step-by-step solutions from verified subject matter experts
