IPC: Message Passing
Notes
- A far more flexible method for communications is message passing
- In general
- send(message), receive(message)
- Works if there is a single sender/receiver
- Unix pipes work like this.
- Pipes
- This is a two way data communications mechanism.
- But generally one process reads and another writes.
- Pipes are created with the
int pipe(int pipefd[2]);command. - fd[0] is the output end (STDIN_FILENO)
- fd[1] is the intput end (STDOUT_FILENO)
- See pipePi.cpp.
- Pipes used this way are tied to a comomon ancestor
- But named pipes exist
-
mknod filename p - And process can open/close this as well.
-
- Pipes are
- limited to a single machine.
- Have no distinct messages
- Have no priorities, are essentially bit streams.
- Message queues
- Allow processes to send messages between each other.
- But not directed at a process, just a queue
- Messages have boundaries, and priorities
- And, as the name implies are queued
- There are both posix and system V message queues
- Again, we will look at posix message queues
- Four maintenance functions
-
mqd_t mq_open(const char *name, int oflag, mode_t mode, struct mq_attr *attr);- name must start with /
- interface is created in /dev/mqueue
- oflags like files
- O_RDONLY, OWRONLY, ORDWR
- plus O_CREAt, O_EXCL, O_NONBLOCK, O_CLOEXEC
- mode only if creating
- attr only if creating.
-
struct mq_attr { long mq_flags; /* Flags (ignored for mq_open()) */ long mq_maxmsg; /* Max. # of messages on queue */ long mq_msgsize; /* Max. message size (bytes) */ long mq_curmsgs; /* # of messages currently in queue (ignored for mq_open()) */ }; - can be nullptr
- seems only needed when trying to make the values greater than the default.
-
-
-
int mq_getattr(mqd_t mqdes, struct mq_attr *attr);- and
int mq_setattr(mqd_t mqdes, const struct mq_attr *restrict newattr, struct mq_attr *restrict oldattr); - Gets and sets the attributes for the queue.
- and
-
int mq_close(mqd_t mqdes); -
int mq_unlink(const char *name);
- Four maintenance functions
- Sending and receiving messages
-
int mq_send( mqd_t mqdes, const char msg_ptr[msg_len], size_t msg_len, unsigned int msg_prio); -
int mq_receive(mqd_t mqdes, const char msg_ptr[msg_len], size_t msg_len, unsigned int msg_prio); - There are also thread safe versions, but that is for later.
-
- You can also
- Set an interrupt like condition for when a new message arrives with
mq_notify - Send a message with a timeout with
mq_timedsend - And a timed receive
mq_timedrecive
- Set an interrupt like condition for when a new message arrives with
- Look at writer.cpp and reader.cpp
- I am only sending ascii messages, but I could also send raw data.
- There is more to message queues, but this will do for this class.