Work to do : Modify your programs so that each program can both receive and send
ID: 3832730 • Letter: W
Question
Work to do:
Modify your programs so that each program can both receive and send messages alternatively.
Message Queues
Message queues allow you to send a block of data from one process to another. Each block of data has a unique type, and a receiving process may receive blocks of data having different type values independently. One can use message queues to synchronize events between processes.
The following are the functions for message queue manipulations:
Use "man" to study each of the message queue functions. In your lab report, briefly, describe each of them and its usage.
Try the following two programs, msg1.cpp, which receives messages and msg2.cpp, which sends messages. Either of the programs is allowed to create the message queue, but the receiver is responsible for deleting it after it receives the last message. You may compile and run them by:
Note that when you run the two programs, you should run them in two different windows ( terminals ). You should be able to send messages from one to the other and terminate them by entering "end".
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> int msgctl(int msqid, int cmd, struct msqid_ds *buf); int msgget(key_t key, int msgflg); int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg); int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
Explanation / Answer
public void delete(int k) {
if (k <= 0) throw new RuntimeException("Invalid value of k");
if (first == null) return;
if (k == 1) {
first = first.next;
return;
}
Node temp = first;
for (int i = 2; i < k; i++) {
temp = temp.next;
if (temp == null) return;
}
if (temp.next == null) return;
temp.next = temp.next.next;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.