Chinaunix首页 | 论坛 | 博客
  • 博客访问: 173847
  • 博文数量: 63
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 181
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 15:50
文章分类
文章存档

2020年(1)

2016年(62)

我的朋友

分类: C/C++

2016-03-14 12:01:06

环形缓冲区是嵌入式系统中十分重要的一种数据结构,比如在一个视频处理的机制中,环形缓冲区就可以理解为数据码流的通道,每一个通道都对应着一个环形缓冲区,这样数据在读取和写入的时候都可以在这个缓冲区里循环进行,程序员可以根据自己需要的数据大小来决定自己使用的缓冲区大小。

    环形缓冲区,顾名思义这个缓冲区是环形的,那么何谓环形这个意思也很好理解,就是用一个指针去访问该缓冲区的最后一个内存位置的的后一位置时回到环形缓冲区的起点。类似一个环一样。这样形容就很好理解了,当然有办法实现了。我在这里采用了2种方式实现了环形缓冲区,一个是用数组的方法,一个是用链表的方法。

    数组是一块连续的内存,所以顺序访问时只要根据下标的增加而增加,但是最后一个元素之后需要回到起始位置,这就需要我们对这个地方进行特殊处理。只要最后一个地址访问结束能顺利回到起始地址,这个缓冲区就可以实现。代码如下:

点击(此处)折叠或打开

  1. /* File name: ringbuf.c
  2.  * Author : wanxiao
  3.  * Function :Implement a circular buffer,
  4.              you can read and write data in the buffer zone.
  5.  */

  6. #include <stdio.h>
  7.   
  8. #define MAXSIZE 8
  9.   
  10. int ringbuf[MAXSIZE];
  11. int readldx=0;
  12. int writeldx=0;

  13. int next_data_handle(int addr)
  14. {
  15.     return (addr+1) == MAXSIZE ? 0:(addr+1) ;
  16. }
  17.   
  18. int write_data(int data)
  19. {
  20.     int i;
  21.     *(ringbuf+writeldx) = data;
  22.     writeldx = next_data_handle(writeldx);
  23.     for(i=0;i<MAXSIZE;i++)
  24.     {
  25.         printf("%4d",*(ringbuf+i));
  26.         if(MAXSIZE-1 == i)
  27.             printf("/n");
  28.     }
  29. }

  30. int read_data()
  31. {
  32.     printf("read data is : %d/n",*(ringbuf+readldx));
  33.     readldx = next_data_handle(readldx);
  34. }
  35.   
  36. int main(int argc , char **argv)
  37. {
  38.     int data;
  39.     char cmd;

  40.     do{
  41.         printf("select:/nw/t--write/nr/t--read/nq/t--quit/n");
  42.         scanf("%s",&cmd);
  43.   
  44.         switch(cmd)
  45.         {
  46.             case 'w' :
  47.                 printf("please input data:");
  48.                 scanf("%d",&data);
  49.                 write_data(data);
  50.                 break;
  51.             case 'r' :
  52.                 data = read_data();
  53.                 break;
  54.             case 'q' :
  55.                 printf("quit/n");
  56.                 break;
  57.             default :
  58.                 printf("Command error/n");
  59.         }
  60.     }while(cmd != 'q');
  61.     return 0;
  62. }

链表实现,实际上就是一个单向循环链表。这个方法的优点是不需要最后一个元素进行特殊处理,但是实现起来比数组稍微麻烦一点,单思路还是很清晰简单的。代码如下:

点击(此处)折叠或打开
  1. #include <stdio.h>
  2. #include <stdlib.h>


  3. typedef struct signal_loop_chain
  4. {
  5.     int data;
  6.     struct signal_loop_chain *next;
  7. }NODE;

  8. NODE *Create_loop_chain(int n)
  9. {
  10.     int i;
  11.     NODE *head , *previous , *current ;
  12.     previous = (NODE *)malloc(sizeof(NODE));
  13.     if(previous == NULL)
  14.         exit(1);

  15.     previous->data =0;
  16.     previous->next = NULL;
  17.     head = previous ;

  18.     for(i=0 ; i<n ; i++)
  19.     {
  20.         current = (NODE*)malloc(sizeof(NODE));
  21.         if(current == NULL)
  22.             exit(1);

  23. //        scanf("%d",¤t->data);
  24.         current->next = head;
  25.         previous->next = current;
  26.         previous = current ;
  27.     }
  28.     return head ;
  29. }

  30. int Show(NODE *head)
  31. {
  32.     NODE *current;
  33.     current = head->next ;
  34.     printf("List:/n");
  35.     while(current != head)
  36.     {
  37.         printf("%4d",current->data);
  38.         current = current->next;
  39.     }
  40.     printf("/n");
  41. }

  42. int read_buf(NODE *head)
  43. {
  44.     NODE *current;
  45.     current = head->next;
  46.     while(1)
  47.     {
  48.         printf("read number is %d/n",current->data);
  49.         current = current->next;
  50.         sleep(1);
  51.     }

  52. }

  53. int write_buf(NODE *head)
  54. {
  55.     NODE *current;
  56.     int i = 0;
  57.     current = head->next;
  58.     while(1)
  59.     {
  60.         current->data = i++;
  61.         printf("write number is %d/n",current->data);
  62.         current = current->next;
  63.         sleep(1);
  64.     }
  65. }

  66. int main(int argc , char **argv)
  67. {
  68.     int num;
  69.     char cmd;
  70.     NODE *head;
  71.     printf("please input node_num /n");
  72.     scanf("%d",&num);
  73.     head = Create_loop_chain(num);
  74.     printf("The ringbuf was found/n");
  75.     Show(head);

  76.     while(1){
  77.         printf("please select r or w/n");
  78.         scanf("%c",&cmd);

  79.         if(cmd == 'r'){
  80.             read_buf(head);
  81.             Show(head);
  82.         }
  83.         
  84.         if(cmd == 'w'){
  85.             write_buf(head);
  86.             Show(head);
  87.         }
  88.         
  89.     }
  90.     return 0;
  91. }
 
以上都是针对单进程而言。对于系统,尤其是嵌入式Linux系统中,缓冲区的保护机制就变得尤为重要了,因为我们的数据时不停的在读写,内存不停的变化,如果牵扯到多任务(多进程,多线程),我们就需要加锁对其进行保护措施。这里我在链表的实现下加了信号量加以保护。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <semaphore.h>

  5. sem_t mutex;

  6. typedef struct signal_loop_chain
  7. {
  8.     int data;
  9.     struct signal_loop_chain *next;
  10. }NODE;

  11. NODE *Create_loop_chain(int n)
  12. {
  13.     int i;
  14.     NODE *head , *previous , *current ;
  15.     previous = (NODE *)malloc(sizeof(NODE));
  16.     if(previous == NULL)
  17.         exit(1);

  18.     previous->data =0;
  19.     previous->next = NULL;
  20.     head = previous ;

  21.     for(i=0 ; i<n ; i++)
  22.     {
  23.         current = (NODE*)malloc(sizeof(NODE));
  24.         if(current == NULL)
  25.             exit(1);

  26.         current->next = head;
  27.         previous->next = current;
  28.         previous = current ;
  29.     }
  30.     return head ;
  31. }

  32. int Show(NODE *head)
  33. {
  34.     NODE *current;
  35.     current = head->next ;
  36.     printf("List:/n");
  37.     while(current != head)
  38.     {
  39.         printf("%4d",current->data);
  40.         current = current->next;
  41.     }
  42.     printf("/n");
  43. }

  44. int read_buf(NODE *head)
  45. {
  46.     NODE *current;
  47.     current = head->next;
  48.     while(1)
  49.     {
  50.         sem_wait(&mutex);
  51.         printf("read number is %d/n",current->data);
  52.         current = current->next;
  53.         sem_post(&mutex);
  54.         sleep(2);
  55.     }

  56. }

  57. int write_buf(NODE *head)
  58. {
  59.     NODE *current;
  60.     int i = 0;
  61.     current = head->next;
  62.     while(1)
  63.     {
  64.         sem_wait(&mutex);
  65.         current->data = i++;
  66.         printf("write number is %d/n",current->data);
  67.         current = current->next;
  68.         sem_post(&mutex);
  69.         sleep(1);
  70.     }
  71. }

  72. int main(int argc , char **argv)
  73. {
  74.     int num,ret;
  75.     char cmd;
  76.     NODE *head;
  77.     pthread_t id1,id2;

  78.     ret = sem_init(&mutex ,0,1);
  79.     if(ret != 0){
  80.         perror("sem_init error");
  81.     }
  82.     printf("please input node_num /n");
  83.     scanf("%d",&num);
  84.     head = Create_loop_chain(num);
  85.     printf("The ringbuf was found/n");
  86.     Show(head);

  87.     ret = pthread_create(&id1,NULL,(void *)write_buf,head);
  88.     ret = pthread_create(&id2,NULL,(void *)read_buf,head);

  89.     pthread_join(id1,NULL);
  90.     pthread_join(id2,NULL);
  91.     

  92.     return 0;
  93. }



阅读(1247) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~