±Waiting for a message: mq_receive(qd, text, buf, buf_size, &prio);
±Close the queue: mq_close(qd);
±Destroy the queue: mq_unlink(“/msg_queue”);
Linux Programming POSIXSemaphores
POSIX semaphores (1)þ
Resources for sharing resources between threads or processes. See man semaphore.h.
±Named semaphores: can be used between unrelated processes.
±Unnamed semaphores: can be used between threads from the same process, or by related processes (parent / child).
POSIX semaphores (2)þ
±sem_open Open and / or create a named semaphore.
±sem_close Close a named semaphore
±sem_unlink Destroy a named semaphore
±sem_init Initialize an unnamed semaphore
±sem_destroy Destroy an unnamed semaphore
±sem_getvalue Get current semaphore count
±sem_wait Try to lock the semaphore. Wait otherwise.
±sem_trywait Just tries to lock the semaphore, but gives up if the semaphore is already locked.
±sem_post Release the semaphore.
Linux Programming POSIXReal-time signals
POSIX real-time signals (1)þ
Regular signals
±Just 2 applications-specific signals: SIGUSR1 and SIGUSR2
±No signal priorities
±Signals can't carry any extra information.
±Signals can be lost. When a signal is sent multiple times, the receiver will just process one instance.
POSIX signals
±Whole range of application specific signals: SIGRTMIN to SIGRTMAX
±Priorities available. Top priority signals delivered first.
±Possible to carry extra information in a signal.
±Signals are queued. All pending signals are processed: no signal is lost.
POSIX real-time signals (2)þ
POSIX signal API available through signal.h 2 ways of processing signals:
±sigwaitinfo() and sigtimedwait() to wait for blocked signals (signals which remain pending until they are processed by a thread waiting for them.)þ
±sigsuspend() to register a signal handler and suspend the thread until the delivery of an unblocked signal (which are delivered without waiting for a thread to wait for them).
These signals are key in real-time applications, to notify processes of asynchronous events: high-resolution timer expiration, asynchronous I/O completion, new message in an empty message queue...
Linux Programming POSIXclocks and timers
POSIX clocks and timers
Compared to standard (BSD) timers in Linux
±Possibility to have more than 1 timer per process.
±Increased precision, up to nanosecond accuracy
±Timer expiration can be notified either with a signal or with a thread.
±Several clocks available.
Available POSIX clocks (1)þ
Defined in /usr/include/linux/time.h
±CLOCK_REALTIME System-wide clock measuring the time in seconds and nanoseconds since Jan 1, 1970, 00:00. Can be modified. Accuracy: 1/HZ (1 to 10 ms)þ
±CLOCK_MONOTONIC System-wide clock measuring the time in seconds and nanoseconds since system boot. Cannot be modified, so can be used for accurate time measurement. Accuracy: 1/HZ
Available POSIX clocks (2)þ
±CLOCK_PROCESS_CPUTIME_ID Measures process uptime. 1/HZ accuracy. Can be changed.
±CLOCK_THREAS_CPUTIME_ID Same, but only for the current thread.
Time management
Functions defined in time.h
±clock_settime Set the specified clock to a value
±clock_gettime Read the value of a given clock
±clock_getres Get the resolution of a given clock.
See man time.h and the manual of each of these functions.
Using timers (1)þ
Functions also defined in time.h
±clock_nanosleep Suspend the current thread for the specified time, using a specified clock.
±nanosleep Same as clock_nanosleep, using the CLOCK_REALTIME clock.
Using timers (2)þ
±timer_create Create a timer based on a given clock.
±timer_delete Delete a timer
±timer_settime Arm a timer.
±timer_gettime Access the current value of a timer.
Using high resolution timers
Only added in recent kernels (2.6.21)þ
±2 new clocks added to POSIX clocks: CLOCK_REALTIME_HR CLOCK_MONOTONIC_HR
±Depending on the hardware capabilities, can give microsecond or nanosecond accuracy.
±Use them with regular clock and timer functions.
Linux Programming AsynchronousI/O
Asynchronous I/O
±Helpful to implement non-blocking I/O.
±Allows to overlap compute tasks with I/O processing, to increase determinism.
±Supported functionality:
®Send multiple I/O requests at once from different sources
®Cancel ongoing I/O requests
®Wait for request completion
®Inquire the status of a request: completed, failed, or in progress.
±API available in aio.h (man aio.h for details)þ
Compiling instructions
±Includes: nothing special to do. Available in the standard path.
Embedded Linux System Design and Development P. Raghavan, A. Lad, S. Neelakandan, Auerbach, Dec. 2005. http://free-electrons.com/redirect/elsdd-book.html Very nice and clear coverage on real-time programming with the POSIX interface. Nice and useful examples.