LEC 04 Md Samin Rahman
A process is an independent program running on a computer, with its own memory and
resources. Each process runs separately from others, meaning one process cannot directly
access another’s memory.
A thread is a smaller unit within a process that executes instructions. Multiple threads in the
same process share the same memory but can run different parts of the program at the
same time.
A task is a general term that can mean either a process or a thread, depending on the
system. In real-time systems, a task often refers to a thread running specific work, like
reading a sensor or controlling a motor.
A Thread of Execution Thread (of execution) - A thread is like a path that a CPU follows
while running a program. It tracks the program's steps using the Program Counter (PC),
which tells the CPU which instruction to execute next. However, when the system switches
from one thread to another (context switch), the OS or RTOS runs some extra code, but
that is not part of the thread itself. To switch back to a thread later, the system at least
needs to remember where the PC was before switching away. This is the minimum
information required to resume the thread correctly, especially in systems where the CPU
can interrupt (preempt) a running thread to start another one.
The simplest thread may not even use memory, like one that runs at power-on reset to
initialize hardware.
User space is the part of a computer's
Attributes of a Thread in Linux: memory where applications (like web
Threads in Linux (NPTL - Native POSIX Threads Library): In Linux, browsers, text editors, or your own
every process starts with at least one thread. This first thread runs the programs) run. It is separate from kernel
program's instructions, and if needed, more threads can be created space, which is where the operating
system (OS) runs. In user space, programs
within the same process. Linux uses the Native POSIX Threads
do not have direct access to hardware
Library (NPTL) to manage threads. When a new thread is created like the CPU, memory, or devices
in user space, Linux maps it onto a task in kernel space. This means (keyboard, disk, etc.). Instead, they must
that while the thread runs in user space, the operating system ask the kernel to perform tasks through
manages it using kernel-level tasks. When you create additional system calls. This separation keeps the
threads, the kernel creates additional tasks for them. This approach system secure and stable because even if
allows multiple threads to run at the same time on different CPU a user program crashes, it won’t affect the
cores while still keeping memory protection and stability. OS. For example, when a program wants
to read a file, it sends a request to the
VxWorks is a real-time operating system (RTOS) designed for embedded systems. kernel, which handles the actual reading
Unlike general-purpose OSs like Linux or Windows, VxWorks is optimized for systems from disk. This design prevents user
that require fast and predictable responses, such as spacecraft, medical devices, programs from accidentally or maliciously
industrial robots, and networking equipment. damaging critical system functions.
Key Features of VxWorks:
Runs in Kernel Space: All tasks run in kernel mode, meaning they have direct POSIX stands for Portable Operating System
access to hardware and system resources. This makes it fast but also increases the risk Interface for Unix. It is a family of standards
of crashes if an error occurs. Real-Time Performance: Designed for systems where timing specified by the IEEE for maintaining compatibility
is critical. It ensures tasks are completed within strict time limits. Deterministic Scheduling: between operating systems. POSIX defines the
Uses priority-based scheduling (like SCHED_FIFO) to ensure high-priority tasks run without application programming interface (API), along with
delays. No Memory Protection Between Tasks: Unlike Linux, tasks in VxWorks share command line shells and utility interfaces, to ensure
memory, making inter-task communication fast but also increasing the chance of system software compatibility across different Unix-like
crashes if one task fails. systems.
Attributes of a Thread in Linux:
Linux vs. VxWorks: In Linux, threads run in user space, but the OS manages them using kernel
tasks. This helps maintain stability because even if one thread crashes, the system can handle it
without affecting other processes. In VxWorks, everything runs in kernel space. This means there is
no strong separation between tasks, so if one thread crashes due to an error (like an unhandled
exception), the entire system may reset. This makes VxWorks less stable compared to Linux.
Thread Scheduling in Linux
Linux uses different scheduling policies to decide how threads should run:
SCHED_FIFO (First In, First Out) → Real-time scheduling where the highest-priority thread runs
until it finishes or is blocked. It does not switch to another thread unless necessary.
SCHED_OTHER → Default non-real-time scheduling, where Linux decides when to run each thread
based on system load.
SCHED_RR (Round-Robin) → Real-time scheduling where threads at the same priority level take
turns running for a fixed time slice before switching to the next thread.
These scheduling policies help Linux manage threads efficiently, balancing performance and system
responsiveness.
Thread Execution and CPU Core Assignment
By default, the operating system assigns a thread to any available CPU core. The OS can also move
threads to less busy cores using load balancing to improve performance. However, if you want a
thread to always run on a specific core, you can use Affinity. Affinity in computing refers to binding a
process or thread to a specific CPU core so that it always runs on that core instead of being moved by
the operating system. This is useful when trying to mimic Asymmetric Multiprocessing (AMP), where
different cores handle specific tasks.
AMP vs. SMP in Real-Time Systems
AMP (Asymmetric Multiprocessing): Each CPU core is assigned specific tasks. This is simpler, easier
to verify, and more deterministic, meaning it follows predictable timing behavior. AMP works well with
rate-monotonic scheduling, which prioritizes tasks with shorter deadlines.
SMP (Symmetric Multiprocessing): The OS manages all CPU cores and assigns threads dynamically.
While SMP can be used in real-time systems, it is more complex to implement correctly.
Because of its stability and reliability, AMP has been widely used for decades in mission-critical
applications like commercial aircraft and hard real-time systems.
Linux in Real-Time Systems:
Linux is not typically designed for hard real-time systems, where tasks must be completed within strict
deadlines. However, with real-time extensions (like PREEMPT_RT patches), Linux can be modified for
real-time applications.
For safety-critical systems (like avionics and medical devices), RTOS (Real-Time Operating
Systems) are preferred because:
1. Smaller codebase (the operating system or software has fewer lines of code) – Easier to verify and
certify.
2. Pre-certified options – Many RTOSs already meet safety standards like ARINC 653 and DO-
178B/C.
Linux is often used in large-scale real-time systems, such as ships and distributed systems. However, in
smaller embedded systems, RTOS or cyclic executive scheduling is often used instead.
Real-Time System Approaches:
1. Cyclic Executive – A simple, loop-based scheduling approach where tasks are executed
in a fixed order.
2. RTOS Approach – Uses a scheduler to manage tasks based on priorities.
3. Linux with Real-Time Extensions – Uses patches to improve real-time performance but
requires deep customization to ensure reliability.
A Task is a Thread + more context
A Task is essentially a thread but with additional context. It includes everything a thread has, such as its
stack, registers, and program counter (PC), but also more information like signal handlers, task
variables, task ID, priority, and an entry point. The task's state and inter-task communication data
are typically contained in a Task Control Block (TCB) in systems like VxWorks or a kernel descriptor in
the Linux kernel. A Thread is the basic unit of execution in both operating systems and real-time
systems. A thread has a minimal execution context, which includes things like the stack, program counter,
and registers. A task, on the other hand, has more associated context and is therefore closer to a
process in terms of the information it carries.
Attributes of a Task in Linux vs. RTOS
Linux: A task in Linux only exists in kernel space, meaning it is part of the operating system's core
execution environment. User-space programs cannot create or control tasks directly in Linux; they can
only create processes or threads within those processes. Tasks are used to represent processes or
threads within a process. Kernel modules in Linux can create kernel tasks, and kernel tasks are
created for user-space processes and their threads as needed.
RTOS (e.g., VxWorks): In an RTOS, the task is the main execution context, and it operates entirely
within kernel space (no user space). Tasks can be created and controlled in the RTOS kernel space.
RTOS tasks often implement simpler, dedicated execution models like those seen in VxWorks, where
tasks are defined with a specific entry point, priority, and task-specific state.
A Process contains 1 or more threads in a protected address space
A process is essentially a thread of execution that has its own stack, register state, and program counter (PC),
along with much more software state. This includes things like I/O descriptors, open files, and a protected
memory data segment. The key difference between a process and a task is that a process also includes the
management of these resources in a protected address space (so one process can't overwrite or corrupt
another’s memory).
Attributes of a Process in Linux
Existence in User Space: A process in Linux primarily exists in user space, which means it runs in its own isolated
environment, separate from the kernel.
Main Thread Mapped to Kernel Task: The main thread of the process is mapped onto a Linux kernel task. This
allows the kernel to manage the execution of the process and its threads.
Container for Threads: A process can contain one or more threads that share the same address space. This
allows threads to share resources like memory, file descriptors, etc., within the process.
Protected Address Space: The memory used by the process is protected. This means that one process cannot
access or modify the memory of another process, preventing bugs or malicious activities in one process from
affecting others.
POSIX Threads: Additional POSIX threads that are created within a process are mapped onto Linux kernel
tasks via the NPTL (Native POSIX Thread Library). This provides efficient thread management and scheduling.
Old Execution Model: The process model in Linux is based on an older, traditional model of multi-programming.
It provides a structured way of managing different tasks within the system.
Isolation and Protection: A process provides protection by isolating user code from the rest of the system. If
one process encounters a bug or performs badly, it cannot destabilize or crash other processes. This ensures
that errors in one application do not affect the operation of others.
What is Scheduling in RTOS?
Scheduling in a Real-Time Operating System (RTOS) refers to deciding which task or thread
will run on the processor at a given time. The decision is made by the scheduler, a key
component of the RTOS. It determines which task is assigned to a processor core (especially in
multi-core systems). In simple terms, the scheduler ensures that one task is always running on a
core, whether it's actively doing work or just waiting idly.
Scheduler's Role and States
A scheduler is essentially a state machine that manages the states of tasks. Each task can be in
one of the following states:
Ready: The task is prepared to run and is waiting for the CPU to become available.
Pending: The task is waiting for some resource (e.g., a semaphore or I/O device) to become
available before it can proceed.
Delayed: The task is intentionally delayed, often using a sleep function or waiting for a set
period. Suspended: The task has been paused, usually because of an error (like a divide-by-
zero error) or because another task or interrupt requests that the task stop temporarily.
Executing: The task is actively running on the CPU.
The scheduler moves tasks between these states based on certain events such as interrupts or
system calls.
Task Execution and Transition
When a task is in the ready state and the processor core is available, the scheduler dispatches
the task, meaning it starts executing that task by setting the program counter (PC) to the entry
point of the task.
A task can leave the executing state and move to another state for various reasons:
Interrupts: If another task with a higher priority becomes ready, or if the current task’s time
slice expires.
System Calls: When the task makes a request that temporarily halts its execution (e.g., waiting
for a resource).
Resource Availability: If the task is pending (waiting for a resource) and that resource
becomes available, the task can enter the ready state.
If a task is delayed or goes to sleep, it stays in that state until it is woken up by an event. If a
task is suspended, it needs to be reactivated or resumed to continue execution.
Fixed Priority Pre-emptive Scheduling
In Fixed Priority Pre-emptive Scheduling, tasks are assigned fixed priorities. The task with the highest
priority always gets the CPU when it is in the ready state. If a new task with a higher priority enters the
ready state, it pre-empts the currently running task (i.e., stops the lower priority task and runs the
higher priority task immediately). Once a task starts executing, it will continue running until: It is
interrupted by a higher priority task. It voluntarily gives up the CPU (e.g., it enters the pending or delayed
state). An error or exception forces it to go to the suspended state.
Context Switching
A context switch occurs when the scheduler switches from one task to another. This happens when:
A system call is made, which temporarily halts the current task to allow another task to execute.
An interrupt occurs, which forces the scheduler to check if a higher priority task is ready to run.
During a context switch, the state of the current task is saved (including registers and program counter),
and the state of the new task is loaded into the CPU.
In real-time systems, tasks are often assigned priorities based on their periods (how frequently they need
to run). Rate Monotonic Scheduling (RMS) assigns higher priorities to tasks that need to run more
frequently (i.e., tasks with shorter periods). The system uses the Rate Monotonic Least Upper Bound (RM
LUB) feasibility test to check whether a set of tasks can meet their deadlines. If tasks meet the test, it
means the system is capable of scheduling them without missing any deadlines.
Feasibility Tests (for Fixed Priority Pre-emptive Scheduling): To ensure that all tasks can run within
their deadlines, feasibility tests are used. Two main types are:
Scheduling Point Algorithm: This checks whether tasks can be scheduled in such a way that
each task completes before its deadline.
Completion Test Algorithm: This tests whether tasks can meet their deadlines by simulating the
execution of tasks over time, often based on their periods.
Over LCM of Periods For tasks that have different periods, the Least Common Multiple (LCM)
of all the task periods is often used to simulate the task execution over time. By simulating the
scheduling over the LCM, we can assess whether all tasks can be executed within their
deadlines.