Header Ads

Priority Ceiling Protocol (PCP)

 The Priority Ceiling Protocol (PCP) is one of the most important concepts in OSEK/AUTOSAR OS and is a favorite interview topic because it solves the priority inversion problem in real-time systems.


What is Priority Inversion?

Before understanding PCP, let's understand the problem it solves.

Assume three tasks:

TaskPriority
Brake ControlHigh (3)
Engine ControlMedium (2)
LoggingLow (1)

All three need to access the same shared resource (e.g., CAN driver or shared memory).

Without PCP

Time →

Low Priority Task starts
      |
      | Locks Resource R
      |
High Priority Task becomes READY
      |
High waits (Resource R locked)
      |
Medium Priority Task becomes READY
      |
Medium preempts Low
      |
Medium keeps running...
      |
Low cannot finish
      |
High still waiting

Problem:

  • High-priority task is blocked by a low-priority task.
  • The medium-priority task makes the delay even longer.
  • This is called priority inversion.

Although the high-priority task has the highest priority, it cannot run because the low-priority task holds the resource.


What is Priority Ceiling Protocol (PCP)?

Definition:

The Priority Ceiling Protocol is a synchronization protocol that temporarily raises the priority of a task holding a shared resource to the resource's predefined ceiling priority.

The ceiling priority of a resource is the highest priority of any task that can use that resource.


Example

Suppose:

TaskPriority
Brake5
Engine3
Logger1

All access Resource R.

The resource ceiling is:

Resource R Ceiling = 5

because the Brake task (priority 5) uses it.


Execution with PCP

Logger (Priority 1)
        |
Locks Resource R
        |
Priority becomes 5
        |
Brake Task becomes READY
        |
Brake waits (resource in use)
        |
Engine Task becomes READY
        |
Cannot preempt Logger
        |
Logger finishes critical section
        |
Releases Resource
        |
Priority restored to 1
        |
Brake executes

What happened?

When the Logger locked Resource R:

  • Its priority changed from 1 → 5.
  • The Engine task (priority 3) could not interrupt it.
  • The Logger completed its critical section quickly.
  • The Brake task then obtained the resource with minimal delay.

This prevents unbounded priority inversion.


Visual Comparison

Without PCP

High  : Waiting.............
Medium: Running-------------
Low   : Holding Resource-----

High-priority task waits a long time.


With PCP

High  : Waiting...
Medium: (Cannot run)
Low   : Priority boosted → finishes quickly

The blocking time is much shorter and predictable.


Why is PCP Used?

In real-time systems, meeting deadlines is critical.

Without PCP:

  • High-priority tasks may miss deadlines.
  • Timing becomes unpredictable.
  • Safety-critical functions (e.g., braking, steering) can be affected.

PCP provides:

  • Predictable maximum blocking time.
  • Prevention of unbounded priority inversion.
  • Improved real-time determinism.

When is PCP Used?

Whenever multiple tasks share a resource, such as:

  • Shared memory
  • CAN controller
  • Ethernet controller
  • SPI/I2C peripheral
  • Flash memory
  • EEPROM
  • UART
  • Global variables
  • Diagnostic buffer

Example:

Brake ECU
      \
Engine ECU ---- Shared CAN Driver
      /
Body ECU

Only one task can use the CAN driver at a time.


Resource Example in AUTOSAR/OSEK

Suppose:

Task A (Priority 1)
Task B (Priority 2)
Task C (Priority 5)

All use:

CAN Resource

The resource ceiling becomes:

5

When Task A locks the CAN resource:

Priority = 1

↓

LockResource(CAN)

↓

Priority = 5

After:

ReleaseResource(CAN);

Priority returns to:

1

How It Works in OSEK/AUTOSAR

OSEK/AUTOSAR provides APIs such as:

GetResource(Resource1);

/* Critical section */

ReleaseResource(Resource1);

When GetResource() is called:

  1. The OS checks the resource.
  2. If available, it is locked.
  3. The task's priority is temporarily raised to the resource's ceiling.
  4. On ReleaseResource(), the original priority is restored.

This behavior is handled automatically by the OS.


Advantages

  • Prevents priority inversion.
  • Provides deterministic timing.
  • Avoids unnecessary preemption while a shared resource is held.
  • Simple API for developers (GetResource / ReleaseResource).
  • Required for many safety-critical automotive applications.

Limitations

  • Developers must define resource ceilings correctly.
  • Critical sections should be kept short.
  • Holding a resource for too long can still delay higher-priority tasks, though the delay remains bounded.

PCP vs Priority Inheritance Protocol (PIP)

FeaturePriority Ceiling Protocol (PCP)Priority Inheritance Protocol (PIP)
Priority boostImmediate when resource is lockedOnly after a higher-priority task is blocked
ConfigurationUses predefined ceiling prioritiesNo predefined ceiling
Deadlock preventionYes (classic PCP)No
PredictabilityExcellentGood
Used in OSEK/AUTOSARYesNo (standard OSEK uses PCP)

Real Automotive Example

Imagine a Brake ECU (highest priority) and a Logging task (lowest priority) both need to write to the same CAN controller.

Without PCP:

  • The Logging task locks the CAN controller.
  • The Brake task becomes ready but must wait.
  • Meanwhile, a medium-priority diagnostics task runs, delaying the Logging task from releasing the CAN controller.
  • The Brake task's response is delayed unpredictably.

With PCP:

  • The Logging task's priority is immediately raised to the CAN controller's ceiling priority when it locks the resource.
  • The diagnostics task cannot preempt it.
  • The Logging task quickly finishes using the CAN controller and releases it.
  • The Brake task gets access with a known, bounded delay.

Interview Answer (1 Minute)

The Priority Ceiling Protocol (PCP) is a resource synchronization mechanism used in OSEK/AUTOSAR OS to prevent priority inversion. Each shared resource is assigned a ceiling priority equal to the highest priority of any task that may access it. When a task locks that resource using GetResource(), its priority is temporarily raised to the resource's ceiling. This prevents medium-priority tasks from preempting it while it is in the critical section. Once the task releases the resource with ReleaseResource(), its original priority is restored. PCP is used whenever multiple tasks share resources such as CAN drivers, SPI peripherals, shared memory, or global data, ensuring deterministic timing and bounded blocking in safety-critical automotive systems.