Inter-process communication via shared memory vs message passing used in Mach OS

929 views Asked by At

The following paragraph is from Operating System Concepts, 10th Edition by Abraham Silberschatz pg. 138;

The major problem with message systems has generally been poor performance caused by copying of messages from the sender’s port to the receiver’s port. The Mach message system attempts to avoid copy operations by using virtual-memory-management techniques (Chapter 10). Essentially, Mach maps the address space containing the sender’s message into the receiver’s address space. Therefore, the message itself is never actually copied, as both the sender and receiver access the same memory. This message-management technique provides a large performance boost but works only for intrasystem messages.

This section explains ipc via message passing in Mach OS. However I'm confused how this is different from the shared memory model of ipc since, under the hood, it appears that both the sender and receiver processes share a segment of memory for ipc. A discussion on this will be really helpful. Thanks.

1

There are 1 answers

2
Brendan On BEST ANSWER

However I'm confused how this is different from the shared memory model of ipc since, under the hood, it appears that both the sender and receiver processes share a segment of memory for ipc.

Their message passing does use shared memory for common cases; however shared memory alone is inadequate because it lacks any cooperation with the scheduler. Specifically, if a task needs to wait until a message arrives the scheduler is told "Don't give this task any CPU time until/unless a message arrives (and the scheduler switches to another task immediately)", and then when a sender sends a message (via. shared memory) it must also tell the scheduler "Hey, if the receiver was waiting for a message then it can stop waiting and have CPU time again!".

With pure shared memory alone (without this cooperation with the scheduler) you'd end up with hundreds of tasks constantly polling all their message ports wasting a huge amount of CPU time for nothing (and crippling the performance of all useful work).

Note that there is another problems with using shared memory for message passing (and with using shared memory for other types of IPC). If it's possible for a malicious sender to modify the data after the receiver checked that the data is sane but before the receiver acts on that data; then the sender can trick the receiver into doing things that the receiver did not intend to allow; and you end up with a major security risk. I have no idea if Mach does anything to fix this (in theory the kernel could temporarily modify the sender's virtual memory to prevent it from modifying the "already sent" data in shared memory until the receiver has completely finished with the data, but there's performance implications involved and no easy way to tell if/when the receiver actually has completely finished with the data).