Tasks
A task is a logical representation of an execution environment. Tasks are used
in order to divide system resources between each running program. Each task
has its own virtual address space and privilege level. Each task contains one or
more threads. The tasks address space and resources are shared between each
of its threads.
On Mac OS X, new tasks can be created using either the task create() function
or the fork() BSD syscall.
Threads
In Mach, a thread is an independent execution entity. Each thread has its own
registers and scheduling policies. Each thread has access to all the elements
within the task it is contained within.
On Mac OS X, a list of all the threads in a task can be obtained using the
task threads() function shown below.
kern_return_t task_threads
(task_t task,
thread_act_port_array_t thread_list,
mach_msg_type_number_t* thread_count);
The Mach API on Mac OS X provides a variety of functions for dealing with
threads. Through this API, new threads can easily be created, register contents
can be modified and retrieved, and so on.
Msgs
Messages are used in Mach in order to provide communicate between threads.
A message is made up of a collection of data objects. Once a message is created
it is sent to a port for which the invoking task has the appropriate port rights.
Port rights can be sent between tasks as a message. Messages are queued at the
destination and processed at the liberty of the receiving thread.
On Mac OS X, the mach msg() function be used to send and receive messages
to and from a port. The declaration for this function is shown below.
mach_msg_return_t mach_msg
(mach_msg_header_t msg,
mach_msg_option_t option,
mach_msg_size_t send_size,
mach_msg_size_t receive_limit,
mach_port_t receive_name,
mach_msg_timeout_t timeout,
mach_port_t notify);
Ports
A port is a kernel controlled communication channel. It provides the ability to
pass messages between threads. A thread with the appropriate port rights for a
port is able to send messages to it. Multiple ports which have the appropriate
port rights are able to send messages to a single port concurrently. However,
only a single task may receive messages from a single port at any given time.
Each port has an associated message queue.
Port Set
A port set is (unsurprisingly) a collection of Mach ports. Each of the ports in
a port set use the same queue of messages.