Next: Message Format, Up: Messaging Interface
To use the mach_msg
call, you can include the header files
mach/port.h and mach/message.h.
The
mach_msg
function is used to send and receive messages. Mach messages contain typed data, which can include port rights and references to large regions of memory.msg is the address of a buffer in the caller's address space. Message buffers should be aligned on long-word boundaries. The message options option are bit values, combined with bitwise-or. One or both of
MACH_SEND_MSG
andMACH_RCV_MSG
should be used. Other options act as modifiers. When sending a message, send_size specifies the size of the message buffer. Otherwise zero should be supplied. When receiving a message, rcv_size specifies the size of the message buffer. Otherwise zero should be supplied. When receiving a message, rcv_name specifies the port or port set. OtherwiseMACH_PORT_NULL
should be supplied. When using theMACH_SEND_TIMEOUT
andMACH_RCV_TIMEOUT
options, timeout specifies the time in milliseconds to wait before giving up. OtherwiseMACH_MSG_TIMEOUT_NONE
should be supplied. When using theMACH_SEND_NOTIFY
,MACH_SEND_CANCEL
, andMACH_RCV_NOTIFY
options, notify specifies the port used for the notification. OtherwiseMACH_PORT_NULL
should be supplied.If the option argument is
MACH_SEND_MSG
, it sends a message. The send_size argument specifies the size of the message to send. Themsgh_remote_port
field of the message header specifies the destination of the message.If the option argument is
MACH_RCV_MSG
, it receives a message. The rcv_size argument specifies the size of the message buffer that will receive the message; messages larger than rcv_size are not received. The rcv_name argument specifies the port or port set from which to receive.If the option argument is
MACH_SEND_MSG|MACH_RCV_MSG
, thenmach_msg
does both send and receive operations. If the send operation encounters an error (any return code other thanMACH_MSG_SUCCESS
), then the call returns immediately without attempting the receive operation. Semantically the combined call is equivalent to separate send and receive calls, but it saves a system call and enables other internal optimizations.If the option argument specifies neither
MACH_SEND_MSG
norMACH_RCV_MSG
, thenmach_msg
does nothing.Some options, like
MACH_SEND_TIMEOUT
andMACH_RCV_TIMEOUT
, share a supporting argument. If these options are used together, they make independent use of the supporting argument's value.