板子可以跑NuttX了,继续研究ostest
之前是在讲消息队列的,那么这节就把ostest里面相关消息队列的代码分析一遍。
先从ostest_main()开始。

apps\examples\ostest\ostest_main():mqueue_test()

int ostest_main(int argc, FAR char *argv[])
{int result;/* Verify that stdio works first */stdio_test();/* Set up some environment variables */printf("ostest_main: putenv(%s)\n", g_putenv_value);putenv(g_putenv_value);                   /* Varaible1=BadValue3 */printf("ostest_main: setenv(%s, %s, TRUE)\n", g_var1_name, g_var1_value);setenv(g_var1_name, g_var1_value, TRUE);  /* Variable1=GoodValue1 */printf("ostest_main: setenv(%s, %s, FALSE)\n", g_var2_name, g_bad_value1);setenv(g_var2_name, g_bad_value1, FALSE); /* Variable2=BadValue1 */printf("ostest_main: setenv(%s, %s, TRUE)\n", g_var2_name, g_var2_value);setenv(g_var2_name, g_var2_value, TRUE);  /* Variable2=GoodValue2 */printf("ostest_main: setenv(%s, %s, FALSE)\n", g_var3_name, g_var3_name);setenv(g_var3_name, g_var3_value, FALSE); /* Variable3=GoodValue3 */printf("ostest_main: setenv(%s, %s, FALSE)\n", g_var3_name, g_var3_name);setenv(g_var3_name, g_bad_value2, FALSE); /* Variable3=GoodValue3 */show_environment(true, true, true);/* Verify that we can spawn a new task */result = task_create("ostest", PRIORITY, STACKSIZE, user_main,(FAR char * const *)g_argv);if (result == ERROR){printf("ostest_main: ERROR Failed to start user_main\n");ostest_result = ERROR;}else{printf("ostest_main: Started user_main at PID=%d\n", result);/* Wait for the test to complete to get the test result */if (waitpid(result, &ostest_result, 0) != result){printf("ostest_main: ERROR Failed to wait for user_main to terminate\n");ostest_result = ERROR;}}printf("ostest_main: Exiting with status %d\n", ostest_result);return ostest_result;
}

这里它创建了一个任务:user_main(),再来看一下这个函数。
从上往下,找到了关于消息队列的函数

      /* Verify pthreads and message queues */printf("\nuser_main: message queue test\n");mqueue_test();check_test_memory_usage();

跳转进去。

apps\examples\ostest\mqueue.c:mqueue_test()

void mqueue_test(void)
{pthread_t sender;         // 发送线程pthread_t receiver;       // 接收线程void *result;pthread_attr_t attr;struct sched_111param sparam;FAR void *expected;int prio_min;int prio_max;int prio_mid;int status;/* Reset globals for the beginning of the test */g_send_mqfd = NULL;g_recv_mqfd = NULL;/* Start the sending thread at higher priority */printf("mqueue_test: Starting receiver\n");status = pthread_attr_init(&attr);if (status != 0){printf("mqueue_test: pthread_attr_init failed, status=%d\n", status);}status = pthread_attr_setstacksize(&attr, STACKSIZE);if (status != 0){printf("mqueue_test: pthread_attr_setstacksize failed, status=%d\n", status);}prio_min = sched_get_priority_min(SCHED_FIFO);prio_max = sched_get_priority_max(SCHED_FIFO);prio_mid = (prio_min + prio_max) / 2;sparam.sched_priority = prio_mid;status = pthread_attr_setschedparam(&attr,&sparam);if (status != OK){printf("mqueue_test: pthread_attr_setschedparam failed, status=%d\n", status);}else{printf("mqueue_test: Set receiver priority to %d\n", sparam.sched_priority);}status = pthread_create(&receiver, &attr, receiver_thread, NULL);if (status != 0){printf("mqueue_test: pthread_create failed, status=%d\n", status);}/* Start the sending thread at lower priority */printf("mqueue_test: Starting sender\n");status = pthread_attr_init(&attr);if (status != 0){printf("mqueue_test: pthread_attr_init failed, status=%d\n", status);}status = pthread_attr_setstacksize(&attr, STACKSIZE);if (status != 0){printf("mqueue_test: pthread_attr_setstacksize failed, status=%d\n",status);}sparam.sched_priority = (prio_min + prio_mid) / 2;status = pthread_attr_setschedparam(&attr,&sparam);if (status != OK){printf("mqueue_test: pthread_attr_setschedparam failed, status=%d\n",status);}else{printf("mqueue_test: Set sender thread priority to %d\n",sparam.sched_priority);}status = pthread_create(&sender, &attr, sender_thread, NULL);if (status != 0){printf("mqueue_test: pthread_create failed, status=%d\n", status);}printf("mqueue_test: Waiting for sender to complete\n");pthread_join(sender, &result);if (result != (FAR void *)0){printf("mqueue_test: ERROR sender thread exited with %d errors\n",(int)((intptr_t)result));}/* Wake up the receiver thread with a signal */printf("mqueue_test: Killing receiver\n");pthread_kill(receiver, 9);/* Wait a bit to see if the thread exits on its own */usleep(HALF_SECOND_USEC_USEC);/* Then cancel the thread and see if it did */printf("mqueue_test: Canceling receiver\n");expected = PTHREAD_CANCELED;status = pthread_cancel(receiver);if (status == ESRCH){printf("mqueue_test: receiver has already terminated\n");expected = (FAR void *)0;}/* Check the result.  If the pthread was canceled, PTHREAD_CANCELED is the* correct result.  Zero might be returned if the thread ran to completion* before it was canceled.*/pthread_join(receiver, &result);if (result != expected){printf("mqueue_test: ERROR receiver thread should have exited with %p\n",expected);printf("             ERROR Instead exited with nerrors=%d\n",(int)((intptr_t)result));}/* Message queues are global resources and persist for the life the* task group.  The message queue opened by the sender_thread must be closed* since the sender pthread may have been canceled and may have left the* message queue open.*/if (result == PTHREAD_CANCELED && g_recv_mqfd){if (mq_close(g_recv_mqfd) < 0){printf("mqueue_test: ERROR mq_close failed\n");}}else if (result != PTHREAD_CANCELED && g_recv_mqfd){printf("mqueue_test: ERROR send mqd_t left open\n");if (mq_close(g_recv_mqfd) < 0){printf("mqueue_test: ERROR mq_close failed\n");}}/* Make sure that the receive queue is closed as well */if (g_send_mqfd){printf("mqueue_test: ERROR receiver mqd_t left open\n");if (mq_close(g_send_mqfd) < 0){printf("sender_thread: ERROR mq_close failed\n");}}/* Destroy the message queue */if (mq_unlink("mqueue") < 0){printf("mqueue_test: ERROR mq_unlink failed\n");}
}

整体来说这个任务做了什么呢:
1. 启动一个中优先级的sending thread
2. 启动一个中低优先级的receiver thread
3. 使用pthread_join()等待sending thread停止
4. 给receiver thread发送信号
5. 等待 500000 us
6. 取消receiver thread
7. 检查receiver thread结束时返回的结果
8. 关闭并删除消息队列

看起来最想了解的消息队列主要部分不在这里。消息队列相关的代码被分成了两部分,先从发送看起吧。

apps\examples\ostest\mqueue.c:sender_thread()

static void *sender_thread(void *arg)
{char msg_buffer[TEST_MSGLEN]; struct mq_attr attr;int status = 0;int nerrors = 0;int i;printf("sender_thread: Starting\n");/* Fill in attributes for message queue */attr.mq_maxmsg  = 20;         // 最大消息条数attr.mq_msgsize = TEST_MSGLEN;    // 最大消息长度attr.mq_flags   = 0;

这部分定义了队列的属性。

g_send_mqfd = mq_open("mqueue", O_WRONLY|O_CREAT, 0666, &attr);if (g_send_mqfd == (mqd_t)-1){printf("sender_thread: ERROR mq_open failed\n");pthread_exit((pthread_addr_t)1);}

这里就出现了消息队列的函数mq_open(),详细参考==>mq_open(),这里设置了队列名称为mqueue,写标志位O_WRONLY和不存在创建标志位O_CREAT。同时,启用O_CREAT后需要再填写两个参数,一个是权限,0666按照我之前在文件中找出来的定义来看,只能是(0)(4+2)(4+2)(4+2),也就是rw-rw-rw,也就是说,这个队列是全员可读写的。
最后的参数是前面定义好的队列属性。

  memcpy(msg_buffer, TEST_MESSAGE, TEST_MSGLEN);

把字符串 "This is a test and only a test" 拷贝到一个缓冲器里,其实就是一个变量里。

  for (i = 0; i < TEST_SEND_NMSGS; i++){status = mq_send(g_send_mqfd, msg_buffer, TEST_MSGLEN, 42);if (status < 0){printf("sender_thread: ERROR mq_send failure=%d on msg %d\n", status, i);nerrors++;}else{printf("sender_thread: mq_send succeeded on msg %d\n", i);}}

循环10次发送消息,这里用到了==> mq_send() <==函数。这里标记一下这个优先级的东西。我猜想这里随意发送优先级不同的消息,接收端接收到的应该是排号序的。去代码里验证。mq_send 调用了nxmq_send传递了参数prio。而nxmq_send则是调用了nxmq_do_send
nxmq_do_send里有写:

nuttx\fs\mqueue\mq_open.c:mq_open()

  for (prev = NULL, next = (FAR struct mqueue_msg_s *)msgq->msglist.head;next && prio <= next->priority;prev = next, next = next->next);

循环执行到next指针指向的消息优先级比他大或者不存在时停止。此时将该消息插在指针prev后面。具体的实现方法还得往里走。再往里我就不记录了,有点多。。基本涉及到的函数都是sq开头的,位置:nuttx\libs\libc\queue这里还有dq开头的一些文件,看起来是双向链表。总之,消息最后是会按照优先级排序的。不会存在同优先级覆盖问题。

  if (mq_close(g_send_mqfd) < 0){printf("sender_thread: ERROR mq_close failed\n");}else{g_send_mqfd = NULL;}

这里是关闭消息列表了==>mq_close<==。

  printf("sender_thread: returning nerrors=%d\n", nerrors);return (pthread_addr_t)((uintptr_t)nerrors);
}

最后返回了10

再来看另一个线程

apps\examples\ostest\mqueue.c:receiver_thread()

static void *receiver_thread(void *arg)
{char msg_buffer[TEST_MSGLEN];struct mq_attr attr;int nbytes;int nerrors = 0;int i;printf("receiver_thread: Starting\n");/* Fill in attributes for message queue */attr.mq_maxmsg  = 20;attr.mq_msgsize = TEST_MSGLEN;attr.mq_flags   = 0;

这部分定义了队列的属性。

   g_recv_mqfd = mq_open("mqueue", O_RDONLY|O_CREAT, 0666, &attr);if (g_recv_mqfd < 0){printf("receiver_thread: ERROR mq_open failed\n");pthread_exit((pthread_addr_t)1);}

这一段使用了==>mq_open<==,和sender_thread()中的mq_open是一样的。

for (i = 0; i < TEST_RECEIVE_NMSGS; i++){memset(msg_buffer, 0xaa, TEST_MSGLEN);nbytes = mq_receive(g_recv_mqfd, msg_buffer, TEST_MSGLEN, 0);if (nbytes < 0){/* mq_receive failed.  If the error is because of EINTR then* it is not a failure.*/if (errno != EINTR){printf("receiver_thread: ERROR mq_receive failure on msg %d, errno=%d\n", i, errno);nerrors++;}else{printf("receiver_thread: mq_receive interrupted!\n");}}else if (nbytes != TEST_MSGLEN){printf("receiver_thread: mq_receive return bad size %d on msg %d\n", nbytes, i);nerrors++;}else if (memcmp(TEST_MESSAGE, msg_buffer, nbytes) != 0){int j;printf("receiver_thread: mq_receive returned corrupt message on msg %d\n", i);printf("receiver_thread:                  i  Expected Received\n");for (j = 0; j < TEST_MSGLEN-1; j++){if (isprint(msg_buffer[j])){printf("receiver_thread:                  %2d %02x (%c) %02x (%c)\n",j, TEST_MESSAGE[j], TEST_MESSAGE[j], msg_buffer[j], msg_buffer[j]);}else{printf("receiver_thread:                  %2d %02x (%c) %02x\n",j, TEST_MESSAGE[j], TEST_MESSAGE[j], msg_buffer[j]);}}printf("receiver_thread:                  %2d 00      %02x\n",j, msg_buffer[j]);}else{printf("receiver_thread: mq_receive succeeded on msg %d\n", i);}}

接收11次数据,每次都会检查数据是否正常。最后一次会失败,因为没有数据可以读取了。所以会
printf("receiver_thread: mq_receive interrupted!\n");

if (mq_close(g_recv_mqfd) < 0){printf("receiver_thread: ERROR mq_close failed\n");nerrors++;}else{g_recv_mqfd = NULL;}

关闭消息队列,同sender_thread

  printf("receiver_thread: returning nerrors=%d\n", nerrors);pthread_exit((pthread_addr_t)((uintptr_t)nerrors));return (pthread_addr_t)((uintptr_t)nerrors);
}

最后返回0代表正常。这么一句呢。这里不得不提到之前ostest任务中的pthread_join,这里的pthread_exit就是为了让那里停止等待。顺便mq_timedreceive也有,和这篇差不多,就不看了。就到这里吧。


NuttX的学习笔记 11相关推荐

  1. SpringMVC:学习笔记(11)——依赖注入与@Autowired

    SpringMVC:学习笔记(11)--依赖注入与@Autowired 使用@Autowired 从Spring2.5开始,它引入了一种全新的依赖注入方式,即通过@Autowired注解.这个注解允许 ...

  2. Hadoop学习笔记—11.MapReduce中的排序和分组

    Hadoop学习笔记-11.MapReduce中的排序和分组 一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出 ...

  3. HALCON 20.11:深度学习笔记(11)---目标检测

    HALCON 20.11:深度学习笔记(11)---目标检测 HALCON 20.11.0.0中,实现了深度学习方法. 本章讲解了如何使用基于深度学习的对象检测. 通过对象检测,我们希望在图像中找到不 ...

  4. 台大李宏毅Machine Learning 2017Fall学习笔记 (11)Convolutional Neural Network

    台大李宏毅Machine Learning 2017Fall学习笔记 (11)Convolutional Neural Network 本博客主要整理自: http://blog.csdn.net/x ...

  5. 华为HCIA-datacom 学习笔记11——AAA原理与配置

    华为HCIA-datacom 学习笔记11--AAA原理与配置 AAA原理与配置 1.AAA概述 认证(authentication):验证用户是否获得访问权,确定哪些用户可以访问网络 授权(auth ...

  6. 点云学习笔记11——VoxelNet算法+代码运行

    点云学习笔记11--VoxelNet算法+代码运行 一.算法分析 摘要 介绍 相关工作 1.2. 贡献 2.VoxelNet 2.1.特征学习网络 2.1.1 特征学习网络 二.代码复现 2.1.环境 ...

  7. 凸优化学习笔记 11:对偶原理 拉格朗日函数

    前面讲了凸优化问题的定义,以及一些常见的凸优化问题类型,这一章就要引入著名的拉格朗日函数和对偶问题了.通过对偶问题,我们可以将一些非凸问题转化为凸优化问题,还可以求出原问题的非平凡下界,这对复杂优化问 ...

  8. Python学习笔记11:函数修饰符

    Python学习笔记11:函数修饰符 Python有很多有趣的特性,其中函数修饰符就是一个. 我们在之前的那个web应用示例中用过如下写法: @web.route('/log') @符号后边的,就是一 ...

  9. MIPS汇编语言学习笔记11:整数减法 (mult方法)

    任务:整数相乘,并输出打印. 代码: .dataintA: .word 5intB: .word 2 .textli $v0, 1lw $a1, intAlw $a2, intBmult $a1, $ ...

最新文章

  1. 华人“霸榜”ACL最佳长短论文、杰出论文一作,华为、南理工等获奖
  2. 语义分割--Attention to Scale: Scale-aware Semantic Image Segmentation
  3. 特征工程(2):特征构建
  4. createTemporaryView is deprecated
  5. MySQL is running but PID file could not be found(在macOS系统下解决方法)
  6. 计算机常发故障英语,vipkid英语常见问题解决办法
  7. 2017北京国庆刷题Day5 morning
  8. https://www.runoob.com/python/python-variable-types.html
  9. 2019南昌网络赛H The Nth Item(二阶线性数列递推 + 广义斐波那契循环节 + 分段打表)题解...
  10. PyTorch神经网络搭建入门
  11. 慎重选择博士后(或博士生)导师
  12. APMCM亚太地区数学建模历年赛题
  13. 数字谐音记忆编码连连看网页应用
  14. Proteus和Keil两个软件的联合使用
  15. HDFS High Availability(HA)高可用、单点故障、主备集群、脑裂问题、数据同步问题、HDFS HA解决方案—QJM
  16. ant - java 构建工具
  17. openwrt恢复出厂设置有两种方法
  18. 红队信息收集自动化工具-水泽(ShuiZe)
  19. Beyond Compare 设置
  20. D3.js的v5版本入门教程(第六章)——做一个简单的图表

热门文章

  1. Thin Plate Spline (薄板样条函数)
  2. 米家插件(APP扩展程序)开发环境搭建
  3. 英国语文美国语文对比_在英国或美国托管您的网站更好吗?
  4. 分享:aop 相关术语介绍之 本人通俗易懂大白话介绍
  5. 在中国,什么样的人能当项目经理?
  6. Typora收费了,于是乎我自己写了一个
  7. 网页兼容性测试软件,12款超棒的浏览器兼容性测试工具让你轻松搞定Bug
  8. 美团民宿跨端复用框架设计与实践
  9. HTTP 413错误解决方法
  10. linux虚拟机network服务显示active(exited)