0% found this document useful (0 votes)
24 views15 pages

Embedded RTOS Unit 2 Pyq

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views15 pages

Embedded RTOS Unit 2 Pyq

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Embedded RTOS Unit 2

Q1 (a) What is need of semaphore? How do you create


counting semaphore?
Answer:

A semaphore is a synchronization tool used in Real-Time Operating Systems (RTOS) to control access
to shared resources (e.g., printers, memory, I/O devices) by multiple tasks. It prevents conflicts such
as race conditions and ensures proper coordination among tasks.

Need of Semaphore:

1. Mutual Exclusion (Mutex): Ensures only one task accesses a shared resource at a time.
2. Synchronization: Helps coordinate the sequence of execution between tasks (e.g., one task waits
for another to complete).
3. Resource Sharing: Manages multiple instances of resources effectively.
4. Deadlock Prevention: Provides structured access rules to minimize deadlock chances.

Types of Semaphore:

1. Binary Semaphore: Takes value 0 or 1. Works like a lock (used for mutual exclusion).
2. Counting Semaphore: Can take values greater than 1, used to manage multiple identical
resources.

Creating a Counting Semaphore:

A counter variable is initialized with the number of available resources.


Each time a task requests a resource, the semaphore is decremented.
When the task releases the resource, the semaphore is incremented.
If the counter value is 0, the requesting task is put in a waiting state until a resource is released.

General Representation:

Semaphore S = n // n = number of resources

Wait(S): if S > 0 → S = S - 1 (task continues)

else → task blocked (waits)

Signal(S): S = S + 1 (resource released)

Example:
If there are 3 printers (resources), initialize semaphore S = 3.

First task uses a printer → S becomes 2.


Second task uses a printer → S becomes 1.
Third task uses a printer → S becomes 0.
Fourth task must wait until any printer is released.

Summary (for revision):

Semaphore is needed for mutual exclusion, synchronization, and resource sharing.


Counting semaphore manages multiple resources.
Created by initializing a counter with number of resources.
Tasks perform Wait() to acquire and Signal() to release resources.

Q1 (b) What are the Advantages and Disadvantages of Real


Time kernels?
Answer:

A Real-Time Kernel is the core part of an RTOS that manages task scheduling, resource allocation, and
synchronization to ensure tasks meet their deadlines. Like every system, it has both advantages and
disadvantages.

Advantages of Real-Time Kernels:

1. Deterministic Response:
Provides predictable response times for critical tasks.
Suitable for real-time applications like medical devices, robotics, and automotive systems.
2. Multitasking Support:
Allows multiple tasks to run simultaneously by switching between them.
3. Task Prioritization:
Tasks are assigned priorities, ensuring that critical tasks execute before less important ones.
4. Efficient Resource Management:
Provides mechanisms like semaphores and mutexes for safe access to shared resources.
5. Modularity and Reusability:
Tasks can be developed independently and reused in different applications.

Disadvantages of Real-Time Kernels:

1. Complex Design:
Programming with real-time constraints is difficult and requires expertise.
2. Higher Cost:
Real-time systems often require powerful processors and more memory, increasing hardware
cost.
3. Overhead of Context Switching:
Frequent switching between tasks can reduce efficiency if not managed properly.
4. Debugging Difficulty:
Real-time behavior is hard to test and debug because timing is critical.
5. Resource Consumption:
Consumes more CPU and memory resources compared to simple embedded systems (like
foreground/background systems).

Summary (for revision):

Advantages: Deterministic response, multitasking, prioritization, efficient resource use,


modularity.
Disadvantages: Complex design, higher cost, context switching overhead, debugging difficulty,
resource usage.

Q1 (c) What is task scheduling in RTOS?


Answer:

Task scheduling in RTOS is the process of deciding which task runs at a given time based on its
priority, state, and deadline. Since multiple tasks compete for CPU time, the scheduler ensures that
critical real-time tasks meet their deadlines.

Need for Task Scheduling:

To share CPU time among multiple tasks.


To ensure high-priority tasks get executed first.
To improve system efficiency and responsiveness.

Types of Scheduling in RTOS:

1. Round Robin Scheduling:


Each task gets equal CPU time in a cyclic order.
Simple but not suitable for real-time deadlines.
2. Priority-Based Scheduling:
Each task is assigned a priority.
Higher-priority tasks preempt lower-priority ones.
Commonly used in RTOS.
3. Rate Monotonic Scheduling (RMS):
Tasks with shorter periodic intervals get higher priority.
4. Earliest Deadline First (EDF):
Task with the nearest deadline is executed first.

Key Features in RTOS Scheduling:

Preemptive Scheduling: A higher-priority task can interrupt a lower-priority one.


Non-Preemptive Scheduling: Once a task starts, it completes before the next begins.
Deterministic Behavior: Ensures predictable execution timing.

Example:

Suppose three tasks are running:

Task A (priority 3, high)


Task B (priority 2, medium)
Task C (priority 1, low)

If Task C is running and Task A becomes ready, the scheduler will preempt Task C and give CPU to Task
A.

Summary (for revision):

Task scheduling = deciding which task runs when.


Types: Round Robin, Priority-based, RMS, EDF.
RTOS uses mainly priority-based preemptive scheduling to meet real-time deadlines.

Q2 (a) What is priority inversion? How can it be handled in


RTOS?
Answer:

Priority Inversion is a situation in RTOS where a higher-priority task is blocked because a lower-
priority task holds a resource (like semaphore or mutex) that the higher-priority task needs.

During this time, if a medium-priority task becomes ready, it can preempt the low-priority task,
further delaying the high-priority task. This breaks the real-time guarantee.

Example with Task Diagram:

Task H (High Priority) needs a resource currently held by Task L (Low Priority).
While Task L is running, Task M (Medium Priority) becomes ready and preempts Task L.
As a result, Task H keeps waiting until Task M finishes, even though it has the highest priority.

This blocking is called priority inversion.

Handling Priority Inversion in RTOS:

1. Priority Inheritance Protocol:


The low-priority task temporarily inherits the higher priority of the blocked task.
Once it releases the resource, it goes back to its original priority.
2. Priority Ceiling Protocol:
Each shared resource is assigned a priority ceiling (highest priority among tasks that use it).
A task can lock the resource only if its priority is higher than all tasks currently using lower
ceiling resources.
Prevents multiple levels of blocking.
3. Disabling Preemption (short term):
Preemption is disabled while the low-priority task finishes its critical section.
Used only for very short critical code because it reduces system responsiveness.

Summary (for revision):

Priority inversion = High-priority task blocked by low-priority due to resource holding, worsened
by medium-priority interference.
Solutions: Priority inheritance, priority ceiling, or temporary preemption disable.

Q2 (b) Describe how interrupt latency and context switching


time affect RTOS performance.
Answer:

In an RTOS, performance is mainly judged by how quickly it can respond to external events and switch
between tasks. Two important factors are:

1. Interrupt Latency
Definition: The time taken between the occurrence of an interrupt and the start of the
corresponding Interrupt Service Routine (ISR).
Factors affecting latency:
CPU speed
Length of critical sections (when interrupts are disabled)
RTOS design (lightweight kernels have less latency)

Impact on RTOS:

If latency is high → system responds slowly to external events (bad for real-time applications).
In hard real-time systems, latency must be deterministic and very small.

2. Context Switching Time

Definition: The time taken by the RTOS to save the current task’s state (registers, stack, program
counter) and load the state of the next task.
Factors affecting switching time:
Number of registers to save/restore
Efficiency of kernel design
Scheduler complexity

Impact on RTOS:

Frequent context switches add overhead, reducing CPU time for actual task execution.
If switching is too slow, high-priority tasks may miss deadlines.

Comparison:

Factor Definition Effect on RTOS

Interrupt Latency Time from interrupt to ISR Delays event response


execution

Context Switch Time Time to switch tasks Reduces CPU efficiency, may
delay high-priority tasks

Summary (for revision):

Interrupt Latency: Delay in starting ISR → affects event response time.


Context Switching Time: Time to switch between tasks → affects CPU efficiency and real-time
deadlines.
Both must be minimized in RTOS for better performance.

Q2 (c) Name and describe two types of RTOS kernels.


Answer:
An RTOS Kernel is the core of a real-time operating system that manages task scheduling,
synchronization, and resource allocation. Based on task management, there are mainly two types:

1. Non-Preemptive Kernel (Cooperative Kernel):

Working:
Once a task starts running, it keeps the CPU until it voluntarily gives it up (by blocking or
completing execution).
Scheduler switches tasks only when the running task is finished or explicitly yields.
Advantages:
1. Simple design and easy to implement.
2. No overhead of frequent context switching.
3. Suitable for small systems with limited resources.
Disadvantages:
1. If one task misbehaves (runs too long), it can block other tasks.
2. Not suitable for hard real-time systems.

2. Preemptive Kernel:

Working:
The kernel can interrupt (preempt) a running task if a higher-priority task becomes ready.
Scheduler always ensures the highest-priority ready task gets CPU.
Advantages:
1. Provides deterministic and fast response.
2. Ensures high-priority tasks always meet deadlines.
3. Suitable for real-time critical applications (robotics, aerospace, medical devices).
Disadvantages:
1. More complex to design and debug.
2. Higher overhead due to frequent context switching.

Summary (for revision):

Non-Preemptive Kernel: Tasks run until they finish/yield → simple but risky for real-time
deadlines.
Preemptive Kernel: CPU given to highest-priority task → ensures responsiveness but more
complex.

Q3 (a) Explain different task states with diagram.


Answer:

In an RTOS, a task (or thread) does not always run on the CPU. Depending on scheduling and events, a
task goes through different states.

Task States in RTOS:

1. Running State:
Task is currently executing on the CPU.
Only one task can be in this state at a time (per CPU core).
2. Ready State:
Task is ready for execution but waiting for CPU time.
Scheduler selects from ready tasks based on priority.
3. Blocked (Waiting) State:
Task is waiting for an event/resource (e.g., semaphore, message, or I/O).
It cannot execute until the event occurs.
4. Suspended State:
Task is deliberately stopped by the system or user.
Unlike blocked state, it will not resume automatically until explicitly restarted.
5. Idle State:
When no tasks are ready to run, the RTOS runs an idle task to save power.

Task State Transition Diagram:

Summary (for revision):

States: Running, Ready, Blocked, Suspended, Idle.


Transitions: Ready → Running (when scheduled), Running → Blocked (waiting), Running → Ready
(preempted).
Diagram helps visualize scheduling and resource waiting.

Q3 (b) Explain concept of foreground/background systems.


Answer:
A Foreground/Background system is one of the simplest forms of real-time systems, often used in
small embedded applications where an RTOS is not required.

Concept:

The system is divided into two levels of tasks:


1. Foreground (High Priority) Tasks:
Handle time-critical operations.
Usually implemented as Interrupt Service Routines (ISRs).
Example: Reading sensor data when an interrupt occurs.
2. Background (Low Priority) Tasks:
Handle non-critical tasks that can be delayed.
Runs in the main program loop when no interrupts are being serviced.
Example: Updating a display or logging data.

Working:

1. The main program (background) runs continuously in a loop.


2. When an interrupt occurs, the foreground task (ISR) is executed immediately.
3. Once ISR completes, the system returns to background processing.

Diagram:

+-----------------------+

| Background |

| (main loop tasks) |

+-----------------------+

(Interrupt Occurs)

+-----------------------+

| Foreground |

| (ISR: urgent tasks) |

+-----------------------+

Advantages:

Simple design and low cost.


Fast response to urgent tasks (via interrupts).
No need for complex RTOS.
Disadvantages:

Difficult to manage as system complexity grows.


No built-in task prioritization beyond interrupts.
Not suitable for large multitasking systems.

Examples:

Small appliances (washing machines, microwave controllers).


Simple sensor monitoring systems.

Summary (for revision):

Foreground = urgent tasks via interrupts.


Background = less urgent tasks via main loop.
Simple, low-cost system but limited scalability.

Q3 (c) What is real time system? Explain with suitable example.


Answer:

A Real-Time System (RTS) is a computer system that must not only produce correct results, but also
deliver them within a specified time limit (deadline).

The correctness of such a system depends on:

1. Logical correctness – Producing the right result.


2. Timing correctness – Producing it at the right time.

Types of Real-Time Systems:

1. Hard Real-Time System:


Missing a deadline is unacceptable and may lead to system failure.
Example: Airbag deployment in cars, pacemakers in medical devices.
2. Soft Real-Time System:
Missing a deadline is tolerable but may degrade performance.
Example: Video streaming, online gaming.

Characteristics:

Deterministic response time.


Task scheduling based on priority.
Supports multitasking with synchronization.
Predictable interrupt handling.

Examples:

1. Air Traffic Control System:


Must track aircraft positions in real-time.
Delay can risk collision → hard real-time.
2. Multimedia Streaming (YouTube, Netflix):
Minor delay causes buffering but doesn’t crash system → soft real-time.
Diagram (Simplified View):

Input Event ----> Processing (RTOS) ----> Output (Before Deadline)

Summary (for revision):

Real-Time System = Output must be correct + on time.


Types: Hard (strict deadlines) & Soft (flexible deadlines).
Examples: Airbag (hard), Video streaming (soft).

Q4 (b) What is the difference between preemptive kernel and


non-preemptive kernel?
Answer:

The kernel in an RTOS manages task scheduling and CPU allocation. Based on task preemption,
kernels are classified as preemptive and non-preemptive.

1. Preemptive Kernel:

Definition: The kernel can interrupt a running task if a higher-priority task becomes ready.
Working: Highest-priority ready task always gets the CPU.
Advantages:
Ensures critical tasks meet deadlines.
Provides deterministic and fast response.
Disadvantages:
More complex to implement.
Context switching overhead is higher.

2. Non-Preemptive Kernel (Cooperative):

Definition: Once a task starts, it runs until it voluntarily gives up the CPU (completes or yields).
Working: Scheduler switches tasks only when the running task finishes or yields.
Advantages:
Simple to design and implement.
Minimal context switching overhead.
Disadvantages:
Misbehaving tasks can block higher-priority tasks.
Not suitable for hard real-time applications.

Comparison Table:
Feature Preemptive Kernel Non-Preemptive Kernel

Task Interruption Can preempt running task Cannot preempt; task runs till
completion

Response to high-priority Fast and deterministic Slower, depends on current


tasks task completion

Complexity High Low

Overhead Higher (context switching) Low

Suitable for Hard real-time systems Simple/soft real-time systems

Summary (for revision):

Preemptive: Interrupts running task → fast response, higher overhead.


Non-Preemptive: Task runs till end → simple, risk of delays.

Q4 (c) Explain role of kernel and services provided by kernel.

Answer:

The Kernel is the core component of an RTOS. It manages CPU, tasks, and system resources, providing
the foundation for multitasking and real-time behavior.

Role of Kernel:

1. Task Management:
Creates, deletes, and manages tasks.
Handles task states: Ready, Running, Blocked, Suspended.
2. Task Scheduling:
Decides which task runs next based on priority, deadlines, and scheduling policy.
Ensures high-priority tasks meet real-time constraints.
3. Resource Management:
Provides mechanisms like semaphores, mutexes, and queues for safe sharing of resources.
4. Inter-task Communication:
Facilitates message passing, signals, or events between tasks.
5. Interrupt Handling:
Integrates ISRs with task scheduling.
Minimizes interrupt latency to maintain real-time response.
6. Time Management:
Provides system clock, delays, and timers for task timing and scheduling.

Services Provided by Kernel:


Service Description

Task Creation & Deletion Dynamically create or delete tasks

Task Scheduling Preemptive or non-preemptive scheduling

Synchronization Semaphores, mutexes for mutual exclusion

Communication Message queues, events, signals

Time Management Delay, timers, clock tick

Resource Management Allocate/deallocate memory and peripherals

Interrupt Handling Manage ISRs and integrate with tasks

Summary (for revision):

Kernel = heart of RTOS, manages CPU, tasks, and resources.


Provides task scheduling, resource management, synchronization, inter-task communication,
and time services.
Ensures deterministic and efficient execution of real-time tasks.

Extra Questions & Answers from Unit II Syllabus


1. What is a Critical Section? How is it handled in RTOS?

Answer:

A critical section is a part of code that accesses shared resources (like variables, devices).
Only one task should execute a critical section at a time to avoid race conditions.
Handling methods:
1. Mutex / Binary Semaphore: Prevents multiple tasks from entering simultaneously.
2. Disabling Interrupts: Short-term prevention of preemption.
3. Priority Inheritance: Avoids blocking higher-priority tasks.

Summary: Critical section = shared resource code → protect using semaphores, mutex, or interrupts.

2. What is a Shared Resource?

Answer:

A shared resource is any hardware or software resource used by more than one task.
Examples: Printer, memory, sensors, communication port.
Problem: Multiple tasks accessing it simultaneously can cause data inconsistency or race
conditions.
Solution: Use mutual exclusion mechanisms like semaphores or mutex.

Summary: Shared resource = multiple tasks use → protect with mutual exclusion.

3. What is Multitasking in RTOS?

Answer:

Multitasking = executing multiple tasks concurrently using a single CPU.


RTOS switches between tasks quickly via context switching.
Benefits:
Efficient CPU utilization.
Tasks with different priorities can coexist.
Types:
1. Cooperative (Non-preemptive)
2. Preemptive

Summary: Multitasking = CPU shares time between tasks → improves efficiency & priority handling.

4. What is Context Switching?

Answer:

Context Switching: Saving the state of the currently running task and restoring the state of the
next task.
State includes: CPU registers, stack pointer, program counter.
Importance: Enables multitasking and preemptive scheduling.
Drawback: Adds overhead and reduces CPU efficiency if frequent.

Summary: Context switch = save & restore task state → allows multitasking.

5. What is Scheduler in RTOS?

Answer:

Scheduler: Kernel component that decides which task runs next based on priority and state.
Types:
1. Preemptive: Highest-priority ready task runs immediately.
2. Non-Preemptive: Task runs till it yields or finishes.
Ensures timely execution of real-time tasks.

Summary: Scheduler = chooses next task → ensures deadlines are met.

6. What is Reentrancy? Why is it important?

Answer:

A reentrant function can be safely called by multiple tasks simultaneously without affecting
execution.
Conditions:
No static/global variables modified.
Uses only local variables or protected shared data.
Importance: Prevents data corruption in multitasking systems.

Summary: Reentrant function = safe for concurrent calls → avoids race conditions.

7. Explain Round-Robin Scheduling.

Answer:

Tasks are given fixed time slices in cyclic order.


Each ready task gets CPU for its time quantum.
After the time expires, next task is scheduled.
Advantages: Simple, fair allocation.
Disadvantages: Not suitable for hard real-time systems (ignores priorities).

Summary: Round-robin = cyclic, fixed time allocation → simple but no priority.

8. Explain Task Priorities (Static & Dynamic).

Answer:

Task Priority: Indicates importance of a task. Higher-priority tasks run before lower ones.
Static Priority: Assigned once and does not change during execution.
Dynamic Priority: Can change during execution (e.g., based on aging, deadlines).

Summary: Priority = decides task execution order → static = fixed, dynamic = can change.

9. What is Mutual Exclusion?

Answer:

Ensures only one task accesses a shared resource at a time.


Implemented using:
Mutex / Semaphore / Critical section.
Importance: Prevents data corruption and race conditions.

Summary: Mutual exclusion = one task at a time → use semaphore/mutex.

10. What is Deadlock? How can it be prevented?

Answer:

Deadlock: Two or more tasks wait indefinitely for resources held by each other.
Prevention methods:
1. Avoid circular wait (resource hierarchy).
2. Use timeout on resource requests.
3. Priority inheritance to unblock higher-priority tasks.

Summary: Deadlock = tasks wait forever → prevent by resource management & priority handling.

11. What is Clock Tick & Memory Requirement in RTOS?


Answer:

Clock Tick: Periodic timer interrupt generated by hardware to update system time and schedule
tasks.
Memory Requirements: Memory needed for:
Task stacks
Kernel data structures
Buffers for communication
OS overhead
Must be calculated to avoid system crashes.

Summary: Clock tick = time management; memory requirement = tasks + kernel + buffers.

12. What is an External Interrupt?

Answer:

An external interrupt is a signal from hardware to CPU indicating an event that requires
immediate attention.
Handled by: Interrupt Service Routine (ISR).
Importance: Ensures fast response to time-critical events.

Summary: External interrupt = hardware event → handled immediately by ISR.

You might also like