• LiteOS同步实验(实现生产者-消费者问题)


    效果如下图:

    给大家解释一下上述效果:在左侧(顶格)的是生产者(Producer);在右侧(空格)的是消费者(Consumer)。生产者有1个,代号为“0”;消费者有2个,代号分别为“0”和“1”。

    生产者首先生产出一个产品,输出“is producing Product”。然后唤醒消费者来消费,输出“is waking Consumer”。

    消费者生成时会报告自己的信息,比如“I am Consumer 0”代表它是0号消费者。如果有东西可以消费,它会输出“Consumer 代号 consume product success!!!!”代表消费成功。

    程序实现的效果是:生产者不断生产“产品”,然后消费者“0”和“1”不断进行消费,如此循环往复。

    代码非常简单,如下:

    1. #include <stdlib.h>
    2. #include <stdio.h>
    3. #include <unistd.h>
    4. #include <sys/mman.h>
    5. #include <pthread.h>
    6. #define CONSUMER_NUM 2
    7. #define PRODUCER_NUM 1
    8. pthread_t pids[CONSUMER_NUM+PRODUCER_NUM];
    9. int ready = 0;
    10. int running =1;
    11. pthread_mutex_t mutex;
    12. pthread_cond_t has_product;
    13. void* producer(void* arg){
    14. int no = (int)arg;
    15. while(running){
    16. pthread_mutex_lock(&mutex);
    17. ready++;
    18. printf("Producer %d is producing Product\n",no);
    19. fflush(stdout);
    20. pthread_cond_signal(&has_product);
    21. printf("Producer %d is waking Consumer\n",no);
    22. fflush(stdout);
    23. pthread_mutex_unlock(&mutex);
    24. sleep(1);
    25. }
    26. return NULL;
    27. }
    28. void* consumer(void* arg){
    29. int num = (int)arg;
    30. while(running){
    31. pthread_mutex_lock(&mutex);
    32. while(ready==0){
    33. printf("\tConsumer %d is waiting...\n",num);
    34. fflush(stdout);
    35. pthread_cond_wait(&has_product,&mutex);
    36. }
    37. ready--;
    38. printf("\tConsumer %d consume product success!!!!!\n",num);
    39. fflush(stdout);
    40. pthread_mutex_unlock(&mutex);
    41. sleep(3);
    42. }
    43. return NULL;
    44. }
    45. void HxSyscall(int num){
    46. pthread_mutex_init(&mutex,NULL);
    47. pthread_cond_init(&has_product,NULL);
    48. printf("init success!\n");
    49. int i;
    50. int thread_ids[CONSUMER_NUM + PRODUCER_NUM];
    51. for(i=0; i<PRODUCER_NUM; i++){
    52. thread_ids[i] = i;
    53. pthread_create(&pids[i], NULL, producer, (void*)i);
    54. }
    55. for(i=0; i<CONSUMER_NUM; i++){
    56. printf("\tI am Consumer %d \n",i);
    57. fflush(stdout);
    58. sleep(2);
    59. thread_ids[PRODUCER_NUM + i] = i;
    60. pthread_create(&pids[PRODUCER_NUM + i], NULL, consumer, (void*)i);
    61. }
    62. for(i=0; i<PRODUCER_NUM + CONSUMER_NUM; i++){
    63. pthread_join(pids[i], NULL);
    64. }
    65. pthread_mutex_destroy(&mutex);
    66. pthread_cond_destroy(&has_product);
    67. return;
    68. }

    大家只需要按照project1的方式,将上述代码放入home/openharmony/kernel/liteos_a/syscall下的hx_syscall.c文件夹下即可(这里为了方便基础较薄弱的同学操作,所以我们仍旧采用勖哥在pro1中的函数命名),接下来大家只需要按照pro1的方式进行编译烧录即可运行。

    【如果觉得有帮助记得点赞+收藏​​​​​​⭐】

  • 相关阅读:
    TreeSet详解
    mmap在嵌入式中的应用
    MongoDB 全方位知识图谱
    CMU 15-445 Project #3 - Query Execution(Task #1、Task #2)
    【无公网IP内网穿透】Java支付宝沙箱环境支付,SDK接口远程调试
    图标、图片、矢量图注册,通过名称来配置icon可以提高性能
    Python调用Prometheus监控数据并计算
    380个python日常库(爬虫、图文、音视频、机器学习、PDF、Office等等)
    MindSpore:无法观看官网线上课程视频
    ESP8266使用记录(一)
  • 原文地址:https://blog.csdn.net/RuanFun/article/details/134550629