博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RTOS Semaphore Services
阅读量:4654 次
发布时间:2019-06-09

本文共 3586 字,大约阅读时间需要 11 分钟。

Semaphores are used to guarantee a task mutually exclusive access to a critical resource.

Semaphores synchronize asynchronous occurring activities.
They are an essential part of a multitasking system.
A good description of multitasking systems and semaphores is available
in the book "Operating systems" from Andrew Tanenbaum.

The @CHIP-RTOS API provides two types of semaphores:

A counting semaphore is a semaphore with an associated counter,

which can be incremented (signal) and decremented (wait).
The resource controlled by the semaphore is free (available)
when the counter is greater than 0.

A resource semaphore is a counting semaphore with a maximum count of one.

It can be used to provide mutually exclusive access to a single resource.
A resource semaphore is also called a binary semaphore.
It differs from a counting semaphore in one significant feature:
The resource ownership is tied to a specific task.
No other task except the task owning the resource is allowed
to signal the associated semaphore to release the resource.

The counting and resource semaphores provide automatic timeout.

Tasks can specify the maximum time for waiting on a semaphore.
The tasks wait in FIFO order for a resource.

A semaphore is created with the RTX_Create_Sem() API.

The @CHIP-RTOS needs a unique four byte semaphore name and
on success returns a new semaphore ID (or handle) to the caller.
This handle is needed for the other semaphore services.

Using a counting semaphore:

A counting semaphore is created by specifying an initial count

greater or equal to zero in the call to RTX_Create_Sem().
If a semaphore is initialized with a value n,
it can be used to control access to n resources,
e.g. a counting semaphore with the initial value three assures that
no more than three tasks can own a resource at any one time.
Access to a resource controlled by a counting semaphore is acquired
with a call to RTX_Wait_Sem() or RTX_Get_Sem().
If the resource is available the @CHIP-RTOS gives it to the task immediately.
When the task is finished using the resource,
it signals its release by calling RTX_Signal_Sem().

Using a resource semaphore:

A resource semaphore is created by specifying

an initial count of -1 in the call of RTX_Create_Sem().
The @CHIP-RTOS creates a resource semaphore and automatically
gives it an initial value of one indicating that the resource is free.
A resource is reserved by calling RTX_Reserve_Sem() with the semaphore ID
returned by RTX_Create_Sem().
The resource is released with a call to RTX_Release_Sem().

Semaphore Services:

RTX_Create_Sem() Create a semaphore

RTX_Delete_Sem() Delete a semaphore
RTX_Get_Sem() Get access to semaphore (no wait)
RTX_Wait_Sem() Wait on a semaphore (optional timeout)

--- Resource Semaphore API ----

RTX_Free_Sem() Free a resource semaphore
RTX_Release_Sem() Release a resource semaphore
RTX_Reserve_Sem() Get a resource semaphore

--- Counting Semaphore API ----

RTX_Signal_Sem() Signal a counting semaphore

The Semaphore Management function group is used to manage and protect access to shared resources.

For example, with a Semaphore the access to a group of identical peripherals can be managed.
The number of available resources is specified as parameter of the osSemaphoreCreate function.

Each time a Semaphore token is obtained with osSemaphoreWait the semaphore count is decremented.

When the semaphore count is 0, no Semaphore token can be obtained.
Semaphores are released with osSemaphoreRelease; this function increments the semaphore count.

 

 

转载于:https://www.cnblogs.com/shangdawei/archive/2013/06/07/3125409.html

你可能感兴趣的文章
第二阶段站立会议7
查看>>
JAVA多线程
查看>>
delphi 更改DBGrid 颜色技巧
查看>>
POJ 2031 Building a Space Station
查看>>
面向对象1
查看>>
任意阶幻方(魔方矩阵)C语言实现
查看>>
织梦教程
查看>>
杭电多校 Harvest of Apples 莫队
查看>>
C/C++心得-结构体
查看>>
函数名作为参数传递
查看>>
apt-get for ubuntu 工具简介
查看>>
数值计算算法-多项式插值算法的实现与分析
查看>>
day8-异常处理与网络编程
查看>>
Python基础-time and datetime
查看>>
Linux epoll 笔记(高并发事件处理机制)
查看>>
shell脚本练习01
查看>>
WPF图标拾取器
查看>>
通过取父级for循环的i来理解闭包,iife,匿名函数
查看>>
HDU 3374 String Problem
查看>>
数据集
查看>>