Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
2024-RSOC
【2024-RSOC】D3学习小结(IPC机制,信号量和互斥量)
发布于 2024-07-29 19:37:12 浏览:89
订阅该版
[tocm] # 一,概要 ![IPC机制](https://oss-club.rt-thread.org/uploads/20240729/2a21add464cd28691b8daf5de248d73b.png) # 二,代码汇总 1,各模块相关代码汇总 *信号量:* ```c rt_sem_t rt_sem_create(const char *name,rt_uint32_t value, rt_uint8_t flag);//创建信号量 rt_err_t rt_sem_delete(rt_sem_t sem);//删除信号量 rt_err_t rt_sem_init(rt_sem_t sem,const char *name,rt_uint32_t value,rt_uint8_t flag);//初始化信号量 rt_err_t rt_sem_detach(rt_sem_t sem);//脱离信号量 rt_err_t rt_sem_take (rt_sem_t sem,rt_int32_t time);//获取信号量 rt_err_t rt_sem_trytake(rt_sem_t sem);//无等待获取信号量 rt_err_t rt_sem_release(rt_sem_t sem);//释放信号量 ``` *互斥量(优先级反转)* ```c rt__mutex_t rt_mutex_create (const char* name, rt_uint8_t flag);//创建互斥量 rt_err_t rt_mutex_delete (rt_mutex_t mutex);//删除互斥量 rt_err_t rt_mutex_init (rt_mutex_t mutex, const char* name, rt_uint8_t flag)://初始化互斥量 rt_err_t rt_mutex_detach (rt_mutex_t mutex);//脱离互斥量 rt_err_t rt_mutex_take (rt_mutex_t mutex,rt_int32_t time);//获取互斥量 rt_err_t rt_mutex_trytake(rt_mutex_t mutex);//无等待获取互斥量 rt_err_t rt_mutex_release(rt_mutex_t mutex);//释放取互斥量 ``` *事件集* ```c rt_event_t rt_event_create(const char* name,rt_uint8_tflag);//创建事件 rt_err_t rt_event_delete(rt_event_t event);//删除事件 rt_err_t rt_event_init(rt_event_t event, const char* name, rt_uint8_t flag);//初始化事件 rt_err_t rt_event_detach(rt_event_t event);//脱离事件 rt_err_t rt_event_send(rt_event_t event,rt_uint32_t set);//发送事件 rt_err_t rt_event_recv(rt_event_t event,//接收事件 ``` *消息邮箱* ```c rt_mailbox_t rt_nb_create (const char* name, rt_size_t size, rt_uint8_t flag);//创建邮箱 rt_err_t rt_mb_delete (rt_mailbox_t mb);//删除邮箱 rt_err_t rt_mb_init(rt_mailbox_t mb,const char* name, void* msgpool, rt_size_t size, rt_uint8_t flag);//初始化邮箱 rt_err_t rt_mb_detach(rt_mailbox_t mb);//脱离邮箱 rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value);//发送邮件 rt_err_t rt_mb_send_wait(rt_mailbox_t mb,rt_uint32_t value, rt_int32_t timeout);//等待方式发送邮件 rt_err_t rt_mb_urgent(rt_mailbox_t mb,rt_ubase_t value);//发送紧急邮件 rt_err_t rt_mb_recv (rt_mailbox_t mb,rt_uint32_t* value, rt_int32_t timeout);//接收邮件 ``` *消息队列* ```c rt_mq_t rt_m_create (const char* name, rt size tmss size.rt_sizo_tmax_nsgs.rt_uint8_t flag);//消息队列名称//消息队列中一条消息的最大长度,单位字节// 消息队列的最大个数// 消息队列采用的等待方式 /*消息队列句柄*/ rt_ert_nq_t mq= RT_NULL; /*创建消息队列对象,返回消息队列句柄*/ mq = rt_mq_create("mq",4,10,RT_IPC_FLAG_FIFO): //名称,消息长度,消息个数,等待方式 rt_err_t rt_m_send (rt_nq_tmgq, void*buffer. rt_siza_t mize) ;//消息队列对象的句柄// 消息内容 // 消息大小 rt_err_t rt_m_send (rt_nq_tmgq, //消息队列对象的句柄void*buffer. // 消息内容 rt_siza_t mize) ; // 消息大小 rt_err_t rt_mq_send_wait(rt_mq_tmq, // 消息队列对象的句柄const void *buffer. //消息内容 rt_size_t size. //消息大小rt_int32_t timeout): //超时时间 rt_err_trt_nq_urgent(rt_mq_t mq,// 消息队列对象的句柄void*buffer,//消息内容rt_size_tsize); //消息大小 rt_err_t rt_ma_recv (rt_mq_t mq, //消息队列对象的句柄void* buffer,//消息内容 rt_size_tsize, // 消息大小rt_int32_ttimeout); //指定的超时时间 ``` 三,实操作业 新建项目,在项目的env终端输入”scons —dist"。在dist-project文件夹内打开env并输入“menuconfig.exe”. 在生成的配置页面中依次选择选择RT-Thread online packages--→miscellaneous packages--→samples:kernel and components samples--→a kernel_samples package for rt-thread--jian→选择semphore和mutex。 然后退出,在env敲入“pkgs —update”接下来输入“scons-j12” 最后在打开code,按下快捷键f5运行调试,然后打开全速运行,在在线串口软件,按下tap选择输入semphore *运行如下* ```c msh RT-Thread shell commands: reboot - Reboot System event sample - event sample mailbox sample - mailbox sample mutex_sample - mutex sample semaphore sample - semaphore sample thread_sample thread sample pin pwm pin [option] control pwm device clear clear the terminal screen version show RT-Thread version information list list objects help RT-Thread shell help ps List threads in the system free Show the memory usage in the system backtrace - print backtrace of a thread ``` *信号量如下*: ```c msh >sem semaphore sample msh >semaphore sample create done. dynamic semaphore value = 0. msh >thread1 release a dynamic semaphore. thread2 take a dynamic semaphore. number = 1 thread1 release a dynamic semaphore. thread2 take a dynamic semaphore. number = 2 thread1 release a dynamic semaphore. thread2 take a dynamic semaphore. number = 3 thread1 release a dynamic semaphore. thread2 take a dynamic semaphore. number =4 thread1 release a dynamic semaphore. thread2 take a dynamic semaphore. number =5 thread1 release a dynamic semaphore. thread2 take a dynamic semaphore. number = 6 thread1 release a dynamic semaphore. thread2 take a dynamic semaphore. number = 7 thread1 release a dynamic semaphore. thread2 take a dynamic semaphore. number = 8 thread1 release a dynamic semaphore. thread2 take a dynamic semaphore. number = 9 thread1 release a dynamic semaphore. thread2 take a dynamic semaphore.number = 10 ``` *互斥量如下:* ```c msh >m mailbox sample mutex sample msh >mute mutex sample msh >mutex sample msh >mutex protect ,numberl = mumber2 is 1 mutex protect ,numberl = mumber2 is 2 mutex protect ,number1 = mumber2 is 3 mutex protect ,number1 = mumber2 is 4 mutex protect ,numberl = mumber2 is 5 mutex protect ,number1 = mumber2 is 6 mutex protect ,numberl = mumber2 is 7 mutex protect ,numberl = mumber2 is 8 mutex protect ,number1 = mumber2 is 9 mutex protect ,number1 = mumber2 is 10 mutex protect,numberl = mumber2 is 11 mutex protect ,numberl1 = mumber2 is 12 mutex protect ,numberl = mumber2 is 13 mutex protect ,numberl = mumber2 is 14 mutex protect ,numberl= mumber2 is 15 mutex protect ,numberl = mumber2 is 16 mutex protect ,number1 = mumber2 is 17 mutex protect ,number11 = mumber2 is 18 mutex protect ,numberl = mumber2 is 19 mutex protect ,numberl = mumber2 is 20 mutex protect ,numberl1 = mumber2 is 21 mutex protect ,numberl = mumber2 is 22 mutex protect ,numberl = mumber2 is 23 mutex protect ,numberl1 = mumber2 is 24 mutex protect ,numberl = mumber2 is 25 mutex protect ,numberl= mumber2 is 26 mutex protect ,number11 = mumber2 is 27 mutex protect ,numberl = mumber2 is 28 mutex protect ,number1 = mumber2 is 29 mutex protect ,numberl = mumber2 is 30 mutex protect , number1= mumber2 is 31 ``` # 四,小结 信号量与互斥量本质相通,不同点在于互斥量占用的临界资源只能在线程内释放,在特定应用场景下可以避免优先级反转。
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
LE_hor
这家伙很懒,什么也没写!
文章
6
回答
0
被采纳
0
关注TA
发私信
相关文章
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
UART
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
ulog
C++_cpp
at_device
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
a1012112796
13
个答案
2
次被采纳
张世争
9
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
本月文章贡献
程序员阿伟
8
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
5
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部