Wednesday, September 1, 2010

Mutex & Semaphore

Both are used for controlling the use of a shared resource.
- Mutex: is like a binary flag in the fashion of use and release.
- Semaphore: is a signaling counter, when finish using a resource, signal (increment) the semaphore so the resource can be used by the next task.

A problem can be happen when using Mutices, Priority Inversion, which is when a higher priority ready task cant run because a lower priority task is restricting using a resource usually by a mutex. which will lead to missing a deadline and the system to fail. A developer should consider this when using mutex. A solution to this problem is by using Priority Ceiling, that each resource has its own priority, when a task uses this resource, it is associated with the resource priority till it finishes. The priority of the resource equals the highest priority user plus one.

void TaskA(void)
{
...
SetTaskPriority(RES_X_PRIO);
// Access shared resource X.
SetTaskPriority(TASK_A_PRIO);
...
}

Ref:

No comments: