ES Module 4 (1)
ES Module 4 (1)
Architecture Of Kernel
• The kernel is the core component of an operating system (OS),
serving as the bridge between hardware and software
• It manages system resources, such as CPU, memory, and I/O devices,
and ensures that software processes can effectively utilize these
resources
Architecture of Kernel
• The kernel architecture can be divided into several subsystems, each responsible for
specific tasks:
a. Process Management
• Process Scheduler: Determines which process should run on the CPU at any given time,
using scheduling algorithms (e.g., Round Robin, Priority Scheduling).
• Process Control Block (PCB): Maintains the state of each process, including its ID, status,
priority, and other attributes.
• Inter-Process Communication (IPC): Mechanisms like pipes, message queues, and shared
memory that enable processes to communicate and synchronize with each other.
b. Memory Management
• Memory Allocation: Handles the allocation and deallocation of memory space to
processes.
• Virtual Memory: Allows the system to use more memory than physically available by
swapping data between RAM and disk storage.
• Paging/Segmentation: Techniques used to divide and manage memory, ensuring efficient
c. File System Management: Provides an API for applications to perform
operations like reading, writing, and managing files.
d. Device Management
• I/O Scheduler: Manages the order in which I/O operations are performed,
optimizing performance and resource utilization.
• Interrupt Handlers: Respond to hardware interrupts (signals from devices) and
trigger appropriate actions within the kernel.
e. Security and Access Control
• User Authentication: Ensures that only authorized users can access the system.
• Access Control Lists (ACLs): Define the permissions for each user or process
regarding access to files, directories, and other resources.
• Encryption: Protects data by converting it into a secure format that
unauthorized users cannot easily decode.
Task
• A task, also referred to as a thread or process, is an independent sequence of execution
within the system. The RTOS manages multiple tasks, ensuring they run concurrently and
meet their timing constraints
• Key Characteristics of Tasks
1.Independence: Each task operates independently of other tasks, with its own context
2.Determinism: In an RTOS, tasks must exhibit deterministic behavior, meaning they execute
within predictable time bounds.
3.Priority: Tasks are typically assigned priorities, which determine the order of execution.
4.Context Switching: The RTOS can switch between tasks rapidly through context switching,
saving the state of the current task and loading the state of the next one
5.Blocking and Synchronization: Tasks may need to wait for resources or events. Blocking
operations cause a task to pause until a condition is met, while synchronization
mechanisms ensure tasks cooperate correctly when accessing shared resources
• Types of Tasks: There are four types of tasks that are being handled
by the real-time operating system classification
1. Periodic Tasks:
• In periodic tasks, tasks are released at regular intervals. A task that
takes place after a fixed interval of time is known as a periodic task
2. Dynamic Tasks
• It is a program that is sequential and invoked by the occurrence of an
event. An event may be generated by an external or internal process
in the system. Dynamic tasks are further classified into two types:
Aperiodic Tasks: If jobs have unpredictable release times a task is
termed an aperiodic task. Tasks falling under this category have soft
deadlines or no deadlines
Sporadic Tasks: They are similar to aperiodic tasks as they also
occur randomly in the system without any specific pattern but the
only thing that differentiates them is they have strict deadlines to
meet.
3. Critical Tasks
• Those tasks in RTOS whose timely executions are critical. If
timelines are missed then it can cause great difficulty. So
meeting the timelines is very crucial under this category of
task
4. Non-critical Tasks
• Real time tasks come under the category of non-critical tasks.
As the name suggests, these tasks are not critical to the
application
Task States:
• Fig shows a task and its state
• Task has state, which includes its status at a given instance in the system
• It can be one of the following state: idle (created), ready, running, blocked
and deleted (finished)
1] Idle (created) state: The task has been created and memory allotted to its
structure. However, it is not ready
2] Ready (Active) State: The created task is ready and is schedulable by the
kernel but not running at present as another higher priority task is scheduled
to run and gets the system resources at this instance
3] Running state: Executing the codes and getting the system resources at this
instance. It will run till it wait for an event or till it gets preempted by another
higher priority task than this one
4] Blocked (waiting) state: Execution of task codes suspends after saving the
needed parameters into its context
5] Deleted (finished) state: The created task has memory deallotted to its
structure. It frees the memory. Task has to be re-created
Interrupt Service Routine (ISR)
• An Interrupt Service Routine (ISR) is a special function in a microcontroller or
processor that is executed in response to an interrupt
• When an interrupt occurs, the control of the program is transferred to the ISR,
which contains the code necessary to handle the specific event that triggered
the interrupt
• Key Characteristics of an ISR
• Automatic Execution: An ISR is executed automatically in response to an
interrupt, without any explicit call from the main program.
• Short and Fast: ISRs are typically designed to be as short and fast as possible.
This is because while an ISR is executing, other interrupts might be delayed
or missed, and the main program execution is paused.
• Global Interrupt Disable: During the execution of an ISR, the global interrupt
might be disabled to prevent other interrupts from interfering with the
current one
• Context Saving and Restoring: Before executing the ISR, the current state of
the microcontroller (like register contents, program counter) is saved. After
the ISR completes, this context is restored, allowing the main program to
continue from where it left off
• How ISRs Work
1.Interrupt Generation: An interrupt can be generated by various sources, such as external
devices (e.g., timers, sensors), internal events (e.g., timer overflow), or software
2.Interrupt Detection: The microcontroller continuously monitors its interrupt lines or flags.
When an interrupt occurs, the corresponding flag is set.
3.Interrupt Acknowledgment: Once an interrupt is detected, the microcontroller
acknowledges it by temporarily pausing the execution of the main program.
4.Context Saving: The current state of the CPU, including the program counter (which points
to the next instruction to be executed in the main program) and registers, is saved onto the
stack.
5.ISR Execution: The microcontroller then jumps to a specific memory location where the ISR
code is stored and begins executing it.
6.Context Restoration: After the ISR finishes execution, the saved context is restored,
including the program counter, so the main program can resume execution exactly where it
was interrupted.
7.Return from Interrupt: The microcontroller then resumes normal program execution from
the point where the interrupt occurred.
Semaphore
• A semaphore is used to manage how multiple programs or processes access
shared resources, like memory or files, without causing conflicts
• Imagine you and your friends want to use a single swing in a park. To make
sure only one person uses the swing at a time, you can use a token. Whoever
has the token gets to use the swing, and when they’re done, they pass the
token to the next person.
• Semaphore works like that token. It helps manage the use of resources, like
memory or files, by allowing only a certain number of processes (or programs)
to access the resource at the same time. This prevents conflicts and ensures
smooth operation.
• Types of Semaphores:
1.Binary Semaphore:
• A binary semaphore has two states: "0" (locked) and "1" (unlocked).
• It functions similarly to a mutex (mutual exclusion), where only one task can acquire
the semaphore (lock the resource) at a time.
• When a task takes a binary semaphore, it changes the state to "0". If another task tries
to take the semaphore while it is "0", it will be blocked until the semaphore is released
(state changes to "1").
2.Counting Semaphore:
• A counting semaphore has pool with multiple instances.
• The semaphore is initialized with a count equal to the number of available resources.
• Each time a task takes a semaphore (acquires a resource), the count is decremented.
When the task releases the semaphore (releases the resource), the count is
incremented.
• If the semaphore count reaches zero, tasks attempting to take the semaphore will be
blocked until a resource becomes available (i.e., the count is incremented).
• Working Mechanism:
• Initialization: A semaphore is created and initialized with a value representing
the available resources or an initial state.
• Take (Wait, P): A task attempts to take (or wait on) the semaphore. If the
semaphore's value is greater than zero, the value is decremented, and the task
proceeds. If the value is zero, the task is blocked until the semaphore is
released by another task.
• Give (Signal, V): A task that has finished using the shared resource or
completed its operation signals the semaphore by incrementing its value. If any
tasks were blocked waiting for the semaphore, one of them is unblocked and
allowed to proceed.
• Timeouts: Some RTOS implementations support timeouts, where a task waiting
on a semaphore will be unblocked after a specified time
Mutex
• A mutex (short for "mutual exclusion") is a synchronization primitive used in
concurrent programming to manage access to shared resources
• When multiple threads or processes need to access a shared resource, a mutex
ensures that only one of them can access the resource at a time, thereby
preventing conflicts and potential data corruption
• Purpose of Mutexes
• Prevent Race Conditions: Race conditions occur when multiple threads or
processes read and write shared data concurrently, leading to unpredictable
results. Mutexes help prevent this by serializing access to the shared resource.
• Ensure Data Integrity: By allowing only one thread or process to access a
critical section of code or a resource at a time, mutexes help maintain the
integrity of data and avoid inconsistencies.
• How Mutexes Work
• Locking and Unlocking: A mutex provides operations to lock and unlock the resource it
protects. When a thread or process locks a mutex, it gains exclusive access to the protected
resource. Other threads or processes that attempt to lock the same mutex are blocked until
the mutex is unlocked.
• Critical Sections: The code that accesses shared resources and requires mutual exclusion is
called a critical section. Mutexes are used to guard these critical sections to ensure that only
one thread or process executes them at any given time.
• Types of Mutexes
• Binary Mutex (or Simple Mutex): This is the most basic form of a mutex, which can be either
locked or unlocked. It is used to provide exclusive access to a resource.
• Recursive Mutex: Allows the same thread to lock the mutex multiple times. This is useful in
cases where a thread may need to acquire the same lock recursively.
• Timed Mutex: Allows a thread to attempt to lock a mutex with a specified timeout period. If
the mutex is not acquired within this time, the attempt fails.
• Error Checking Mutex: Provides error checking capabilities to detect and report errors, such as
attempting to unlock a mutex that the thread does not own.
Difference between mutex and
semaphore
• Mutex uses a locking mechanism. When a process uses
a resource, it locks the resource, uses it, then releases it
• On the other hand, a semaphore uses a signaling
mechanism using the wait() and signal() methods to
indicate whether a process is releasing or acquiring a
resource
Mailbox
• A mailbox is a special memory location that one or more tasks can use to
transfer data
• In RTOS, a mailbox is a communication mechanism used for inter-task or inter-
process communication. It allows tasks (or threads) to send and receive
messages in a way that ensures reliable and orderly communication
• The tasks rely on the kernel to allow them to
write to the mailbox via a post operation
Or read from it via a pend operation
A mailbox can only contain one message
• The mail that is passed via the mailbox can be
a single piece of data,
or a pointer to a data structure
• Purpose of Mailboxes