Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

POSIX as RTOS

POSIX
• POSIX, an acronym for “portable operating system interface,”
• An open operating system (OS) interface standard that has been developed by
the Institute of Electrical and Electronics Engineers (IEEE)
POSIX Processes and Threads
• In POSIX, each executing instance of a program is called a process
• Each process has its own protected address space
• At the lowest level, this is handled by the memory management unit (MMU)
• Processes are isolated so that they cannot inadvertently infringe on
each other
• Because of this enforced separation, communications between
processes can take place only by using OS kernel services.
• A POSIX thread is a single flow of execution that runs within a process
• When a process starts to run, it is actually running a thread, which is
often called the “main” thread because the entry point of a C program
is the main() function
• The main thread can create additional threads if it is so programmed.
• A process is virtually a collection of one or more threads
• As a single flow of execution, each thread within a process has its own
stack and values for the various registers.
• The threads running within a process share at least the following:
• the virtual address space of the process
• variables that are not on the stack
• signal handlers and process-level signal control structures.
POSIX.1 services
Process creation
• starter program:
• For a large (embedded) system consisting of multiple programs, one master
program is called starter program.
• A process (actually a thread within the process) can invoke system
calls to create new processes.
• The newly created process is called a child process of the calling
process, and the calling process is referred to as the parent of the new
process.
• A child process and its parent process run independently, but a child inherits
many attributes from its parent.
• POSIX provides the following system calls for starting up new
processes:
• fork(). This call creates a new process which duplicates the calling
process:
• (a) both processes execute the same application code
• (b) their executions are synchronized at this call
• exec() family of functions. The calling process image is overlaid with
the new process image, which is constructed from an executable file.
• posix_spawn() family of functions. The fork() implementation
normally relies on MMU services such as memory swapping or
dynamic address translation, which is generally too slow for real-time
applications.
• system(). This call takes a command, starting up a shell (command
language interpreter) to execute the command.
Thread creation
• The POSIX function pthread_create() is used for creating a new thread
within a process.
• Threads within a process do not have a parent-child relationship with
each other.
• A thread can terminate itself.
• If any thread of a process calls exit(), the process and all threads
within it terminate.
POSIX Real-Time Extensions
• Many real-time embedded systems have only limited resources
• For such a system, it is desirable to implement it upon an OS that
supports only a particular subset of the POSIX functions.
• The real-time profile standard IEEE Std 1003.13-1998 was first published
in 1998 to meet this need.
• It consists of four real-time application environment profiles:
• Minimum profile: for building small embedded systems without an MMU, or a
file system, or an I/O terminal. Only one process but multiple threads are
allowed.
• Minimum profile for real-time systems: for building real-time controllers with a
file system and I/O terminals. Only one process but multiple threads are allowed.
• Limited profile for embedded systems: for building large embedded systems with
no file system. Multiple processes and threads are allowed.
• Maximum profile: for building large real-time systems and embedded systems
with all the features supported.
Priority-based scheduling
• Each POSIX thread, upon its creation, is associated with a numeric value
called its scheduling priority
• We consider three principles in priority-based scheduling
• Precedence principle. When no thread is running, and multiple threads are
ready to be scheduled for execution, a thread with a higher priority is always
scheduled prior to threads with a lower priority.
• Preemption principle. When a thread is currently running, and another thread
with a higher priority becomes ready for execution, the higher-priority thread
preempts the execution of the current thread.
• Fairness principle. When multiple threads, with the same or different priorities,
compete for use of the processing resources, the system’s scheduling behavior is
regulated by the scheduling policies associated with the threads.
Precedence Principle
• The precedence principle is the basis of priority-based scheduling
• But what happens if a thread becomes ready while a lower-priority
thread is currently running?
• Nonpreemptive scheduling
• Task Ti = (pi, ri, ei, di)
Preemptive scheduling
POSIX.1 scheduling policies
• The POSIX.1-2008 standard specifies four scheduling policies:
• SCHED_FIFO
• An integer constant indicating the FIFO policy
• SCHED_RR
• An integer constant indicating the round-robin policy
• SCHED_SPORADIC
• An integer constant indicating the sporadic server policy
• SCHED_OTHER
• An integer constant indicating any other policy
• Each POSIX process/thread can be associated with a scheduling policy
and attributes pertinent to the chosen scheduling policy.
• When a process is created, its single thread inherits the process’s
scheduling policy and the associated scheduling attributes
Task synchronization
• The POSIX.1-2008 standard defines functions to manage process
synchronization with semaphores.
• A semaphore is an OS kernel object that allows mutually exclusive access to
shared resources.
• The POSIX.1-2008 standard also defines mutexes for thread synchronization
within a process.
• A mutex object, if configured appropriately, can also be used for inter-process
synchronization.
Mutex Attributes
• A POSIX mutex has following important attributes:
• Ownership:
• When a call to pthread_mutex_lock() on a mutex object is successful, the mutex is
locked by the calling thread, which becomes the owner of the mutex.
• A locked mutex can be unlocked only by its owner, via a call to pthread_mutex_unlock().
• Priority ceiling:
• This defines the minimum priority level at which the critical section guarded by the
mutex is executed
• In order to avoid priority inversion, at design time the priority ceiling of a mutex should
be set to a priority higher than or equal to the highest priority of all the threads that may
lock that mutex.
• Protocol:
• This can be set to one of three protocols by a call to the function
pthread_mutexattr_setprotocol()
1. PTHREAD_PRIO_NONE,
2. PTHREAD_PRIO_INHERIT
3. PTHREAD_PRIO_PROTECT
Mutex effects on priority and scheduling
1. The priority and scheduling of a thread is not affected by owning a mutex
with the PTHREAD_PRIO_NONE protocol attribute.
2. When a thread is blocking higher-priority threads because of owning one or
more mutexes with the PTHREAD_PRIO_INHERIT protocol attribute, it shall
execute at the higher of its priority and the highest priority of the threads
waiting on any of the mutexes owned by this thread and initialized with this
protocol.
3. When a thread owns one or more mutexes initialized with the
PTHREAD_PRIO_PROTECT protocol, it shall execute at the higher of its
priority and the highest of thepriority ceilings of all the mutexes owned by
this thread and initialized with this protocol, regardless of whether other
threads are blocked on any of these mutexes or not.
POSIX Timers
• POSIX timers can also be used to control the execution of real-time tasks
to meet their timing requirements.
• A process can create many timers to count certain time intervals.
• The timers created by one process cannot be inherited by its child process
• A timer relies on the use of signals, and it has to reference a real-time
clock as its timing basis.
• A timer can have two time intervals specified:
• timer period: is used to define a periodic timer
• expiration value: specifies a time-out interval for a one-shot timer or the first
expiration value for a periodic timer
• When the specified time-out interval has elapsed, a timer can generate a
“time-out” signal directed to the process that created the timer. For a
given timer, at any point in time only a single time-out signal can be
queued to the process.
POSIX
The QNX microkernel
architecture
QNX Neutrino is
widely used as the basis for many embedded
real-time applications such as automotive
electromechanical components, medical
instruments, defense systems, and nuclear
power plants.
Famous RTOS
• RTLinux
• Windows CE,
• LynxOS,
• VxWorks,
• QNX

You might also like