Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1090973
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类: LINUX

2008-03-19 13:09:31

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define BUFFSIZE 1024 * 1024

#define min(x, y) ((x) < (y) ? (x) : (y))

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

struct cycle_buffer {
        unsigned char *buf; /* store data */
        unsigned int size; /* cycle buffer size */
        unsigned int in; /* next write position in buffer */
        unsigned int out; /* next read position in buffer */
        pthread_mutex_t lock;
};

static struct cycle_buffer *fifo = NULL;

static int init_cycle_buffer(void)
{
        int size = BUFFSIZE, ret;

        ret = size & (size - 1); /* size must be power of 2 */
        if (ret)
                goto err;

        fifo = (struct cycle_buffer *) malloc(sizeof(struct cycle_buffer));
        if (!fifo)
                return -1;

        memset(fifo, '\0', sizeof(struct cycle_buffer));
        fifo->size = size;
        fifo->in = fifo->out = 0;
        pthread_mutex_init(&fifo->lock, NULL);
        fifo->buf = (unsigned char *) malloc(size);
        if (!fifo->buf)
                goto malloc_err;

        memset(fifo->buf, '\0', size);

        return 0;
malloc_err:
        free(fifo);
err:
        return ret;
}

unsigned int fifo_get(unsigned char *buf, unsigned int len)
{
        unsigned int l;

        len = min(len, fifo->in - fifo->out);
        l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
        memcpy(buf, fifo->buf + (fifo->out & (fifo->size - 1)), l);
        memcpy(buf + l, fifo->buf, len - l);

        fifo->out += len;

        return len;
}

unsigned int fifo_put(unsigned char *buf, unsigned int len)
{
        unsigned int l;

        len = min(len, fifo->size - fifo->in + fifo->out);
        l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
        memcpy(fifo->buf + (fifo->in & (fifo->size - 1)), buf, l);
        memcpy(fifo->buf, buf + l, len - l);

        fifo->in += len;

        return len;
}

static void * thread_read(void *arg)
{
        char buf[1024];
        unsigned int n;

        pthread_detach(pthread_self());

        for (;;) {
                memset(buf, '\0', sizeof(buf));
                pthread_mutex_lock(&fifo->lock);
                n = fifo_get(buf, sizeof(buf));
                pthread_mutex_unlock(&fifo->lock);
                write(STDOUT_FILENO, buf, n);
        }

        return NULL;
}

static void * thread_write(void *arg)
{
        unsigned char buf[] = "hello world";

        pthread_detach(pthread_self());

        for (;;) {
                pthread_mutex_lock(&fifo->lock);
                fifo_put(buf, strlen(buf));
                pthread_mutex_unlock(&fifo->lock);
        }

        return NULL;
}

int main(void)
{
        int ret;
        pthread_t wtid, rtid;

        ret = init_cycle_buffer();
        if (ret == -1)
                goto err;

        pthread_create(&wtid, NULL, thread_write, NULL);
        pthread_create(&rtid, NULL, thread_read, NULL);

        pthread_exit(NULL);
err:
        return ret;
}

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

chinaunix网友2008-03-28 16:41:48

非常感谢!

@sky2008-03-28 13:58:21

我写那个只是个大概意思, 而且估计有些缺陷, 内核里的KFIFO有完整的实现

chinaunix网友2008-03-28 10:56:39

有些不懂,能指教一下吗? 1、这个l是做什么用的啊? l = min(len, fifo->size - (fifo->in & (fifo->size - 1))); 2、fifo->buf的指针什么时候变化的呢? memcpy(fifo->buf + (fifo->in & (fifo->size - 1)), buf, l); 3、这句话我也没看明白干什么用? memcpy(fifo->buf, buf + l, len - l); 4、可能由于没有看明白前面几句的原因,不知道环形这个概念是怎么实现的,能简单讲一下你的思路吗?