AgentSkillsCN

posix-rt

为POSIX实时与系统编程提供专家级的协助,涵盖线程、同步、调度、IPC、信号、定时器、异步I/O、内存管理,以及Linux实时系统配置。当用户编写涉及pthread、互斥锁、条件变量、信号量、共享内存、消息队列、实时信号、POSIX定时器、异步I/O(aio或io_uring)、内存锁定、进程调度(SCHED_FIFO、SCHED_RR、SCHED_DEADLINE)、CPU亲和性、epoll、eventfd、timerfd、signalfd、PREEMPT_RT,或任何POSIX.1b/POSIX.1c实时扩展时使用此功能。在提及优先级反转、优先级继承、无锁编程、确定性延迟、mlockall、clock_gettime、posix_spawn,或为实时工作负载使用cgroups时也会触发此功能。

SKILL.md
--- frontmatter
name: posix-rt
description: >
  Expert-level POSIX real-time and systems programming assistance covering
  threads, synchronization, scheduling, IPC, signals, timers, async I/O,
  memory management, and Linux real-time system configuration. Use when the
  user is writing C/C++ systems code involving pthreads, mutexes, condition
  variables, semaphores, shared memory, message queues, real-time signals,
  POSIX timers, async I/O (aio or io_uring), memory locking, process
  scheduling (SCHED_FIFO, SCHED_RR, SCHED_DEADLINE), CPU affinity, epoll,
  eventfd, timerfd, signalfd, PREEMPT_RT, or any POSIX.1b/POSIX.1c real-time
  extension. Also triggers on mentions of priority inversion, priority
  inheritance, lock-free programming, deterministic latency, mlockall,
  clock_gettime, posix_spawn, or cgroups for real-time workloads.

POSIX Real-Time Programming Expert

Standards Context

All historical POSIX real-time standards are unified into POSIX.1-2024 (IEEE Std 1003.1-2024, Issue 8):

  • POSIX.1b (formerly POSIX.4) — real-time extensions (scheduling, timers, AIO, IPC, memory locking)
  • POSIX.1c (formerly POSIX.4a) — threads (pthreads)

Real-time features are optional feature groups. Check availability:

c
#include <unistd.h>
// _POSIX_TIMERS, _POSIX_PRIORITY_SCHEDULING, _POSIX_MESSAGE_PASSING,
// _POSIX_MEMLOCK, _POSIX_SHARED_MEMORY_OBJECTS, _POSIX_SEMAPHORES,
// _POSIX_THREADS, _POSIX_ASYNCHRONOUS_IO

Compile with: -D_POSIX_C_SOURCE=200809L (or _GNU_SOURCE for Linux extensions). Link with: -lpthread and -lrt (older systems; modern glibc integrates -lrt).

Essential Headers

HeaderPurpose
pthread.hThreads, mutexes, conditions, rwlocks, barriers
sched.hScheduling policies, CPU affinity
time.hclock_gettime, clock_nanosleep, timer_create
signal.hReal-time signals, sigqueue, sigwaitinfo
semaphore.hNamed and unnamed semaphores
mqueue.hPOSIX message queues
sys/mman.hshm_open, mmap, mlockall, mlock
aio.hPOSIX async I/O
sys/epoll.hLinux epoll
sys/timerfd.hLinux timerfd
sys/signalfd.hLinux signalfd
sys/eventfd.hLinux eventfd
liburing.hLinux io_uring (liburing)

Real-Time Application Checklist

  1. Use CLOCK_MONOTONIC for all interval/deadline timing (never CLOCK_REALTIME)
  2. Lock memory early: mlockall(MCL_CURRENT | MCL_FUTURE) + pre-fault stack/heap
  3. Set RT scheduling: SCHED_FIFO or SCHED_DEADLINE
  4. Pin RT threads to isolated CPUs: pthread_setaffinity_np
  5. Use PTHREAD_PRIO_INHERIT on all mutexes shared with RT threads
  6. Never call unbounded-time functions from RT threads (malloc, printf, file I/O)
  7. Pre-allocate all resources before entering the RT loop
  8. Use clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, ...) for periodic loops

Reference Documents

Load these as needed based on the specific topic:

TopicFileWhen to read
Threadsreferences/threads.mdThread creation, attributes, lifecycle, stack management, detach vs join
Synchronizationreferences/synchronization.mdMutexes, condition variables, rwlocks, barriers, spinlocks, priority inheritance/ceiling
Schedulingreferences/scheduling.mdSCHED_FIFO, SCHED_RR, SCHED_DEADLINE, CPU affinity, priority management
Clocks & Timersreferences/clocks-timers.mdclock_gettime, clock_nanosleep, timer_create, timerfd, periodic loops
Signalsreferences/signals.mdReal-time signals, sigqueue, sigwaitinfo, signalfd, signals + threads
Message Queuesreferences/message-queues.mdPOSIX mq_* API, priority messages, notification, attributes
Semaphoresreferences/semaphores.mdNamed/unnamed semaphores, process-shared, sem_timedwait
Shared Memoryreferences/shared-memory.mdshm_open, mmap, process-shared synchronization, memory-mapped files
Memory Lockingreferences/memory-locking.mdmlockall, mlock, pre-faulting, stack pre-allocation, madvise
Async I/Oreferences/async-io.mdPOSIX AIO, io_uring, epoll, eventfd, event loop patterns
Process Managementreferences/process-management.mdfork, exec, posix_spawn, waitpid, process groups, RT and fork
Advanced Threadsreferences/advanced-threads.mdThread-specific data, cancellation, fork safety, signal handling in threads, thread pools
System Setupreferences/system-setup.mdPREEMPT_RT, CPU isolation, cgroups v2, kernel tuning, boot parameters, capabilities
Debuggingreferences/debugging.mdLatency tracing, ftrace, cyclictest, priority inversion detection, common RT pitfalls, helgrind/TSAN