The example shown here was derived in Ubuntu 9.04 using the Jaunty Jackalope Kernel Version: 2.6.28-11.

Okay, so first question what exactly is POSIX Message passing and why should I use it?

Well, POSIX stands for “Portable Operating System Interface for Unix” and it is a collection of standards governed by the IEEE.

Message passing is all about processes being able to communicate and synchronize with one another. Why do process need to communicate with one another? Well sometimes processes hold data that other processes may need. This doesn’t just hold true for process though, think about the World Wide Web. Computers need to be able to communicate with one another all the time, so wouldn’t it be nice to have some kind of mechanism in place that demonstrates that functionality? Yeah, I thought so too.

Well here is an example of processes communication utilizing the POSIX API. Think of it as like a client – server relationship; if that helps…

Overview:

Basically four external process will be communicating with a central process relaying temperature data back and forth. The goal here is to get all the processes to have the same temperature. Once that happens the central process will send a message indicating the system is stable and the processes can print out their final temp and end. Until the system is stable the external and central processes will continue to calculate new temperatures and relay them back and forth. [NOTE]: The four external processes are only aware of the central process, but the central process is aware of all the processes.

This starts by utilizing msgget(); This function will request a message queue ID using the mailbox name provided or create one if not existent previously.

Then use msgsnd(); to relay a message to a particular mailbox and use msgrcv(); the get a message from a mailbox.