clinuxlinux-kernelreal-timesystems-programming

What is the difference between Message Queues and files in Linux. Also, what is the significance of priorities in message queues?


In Linux, Message queues are a form of IPC. However, I don't understand how they are different from a file. Instead of writing into a message queues, the processes can just write into a file right? What are the benifits of using message queues?

Also, what is the significance of priorities associated with write and read of message queues?


Solution

  • Files are mostly stored on the hard disk and are persistent across multiple system reboots. Message queues are stored in the main memory and are available only while the system is running and not preserved across system reboots. A file is a sequence of bytes, and by default, the system does not enforce a structure on those bytes. So, using a file for IPC is difficult, because extra logic would be required to identify messages. Also, there are problems of multiple programs writing to a file at the same time, deleting old messages, etc. Message queues are fast, because the messages are stored in the main memory. The kernel takes care of the problems like the concurrent access of a message queue by multiple processes, addition and deletion of messages. So files are used for storing information on secondary storage whereas message queues are used for inter-process communication.