Question
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
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.
b) Discuss what happens (and why) when you run two copies of kirk.
c) Discuss what happens (and why) when your run two copies of spock.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SPOCK.C BELOW /* ** 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 BELOW /* ** 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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started