栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

生产者消费者模型(linux下c语言实现)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

生产者消费者模型(linux下c语言实现)

代码实现

#include
#include
#include
#include
#include


static pthread_mutex_t g_mutex  = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  g_condition_notEmpty = PTHREAD_COND_INITIALIZER;
static pthread_cond_t  g_condition_notFull  = PTHREAD_COND_INITIALIZER;


#define CAPACITY 10


static char g_store[CAPACITY] = {''};


static int g_outPutPosition  = 0;


static int g_inPutPosition  = 0;


char strData;


static int isBufferFull(void)
{
    if(g_outPutPosition == ((g_inPutPosition + 1) % CAPACITY))
        return 1;
    else
        return 0;
}


static int isBufferEmpty(void)
{
    if(g_outPutPosition == g_inPutPosition)
        return 1;
    else
        return 0;
}


static int putChToBuffer(char strData)
{
   
    g_store[g_inPutPosition] = strData;

    
    g_inPutPosition = (g_inPutPosition + 1) % CAPACITY;
    return 0;
}


static int getChFromBuffer(char strData)
{
    strData = g_store[g_outPutPosition];

    
    g_store[g_outPutPosition]  = '*';

    
    g_outPutPosition = (g_outPutPosition + 1) % CAPACITY;
    return 0;
}


void showStore(const char *who, const char *direction, char strData)
{
    int index;
    for (index = 0; index < CAPACITY; index++)
    {
        printf("%c", g_store[index]);
    }
    printf("%s %c(%s)n", direction, strData, who);
}


void *producerThreadFunc(void *arg)
{
    const char *who = (char *)arg;
    while(1)
    {
        //先获得锁
        pthread_mutex_lock(&g_mutex);

        
        if (1 == isBufferFull())
        { 
            printf("仓库满了,%s线程不能进行生产!n", who);
            
           
            pthread_cond_wait(&g_condition_notFull, &g_mutex); 
            
           
            printf("有人进行了消费,%s线程可以进行生产了!n", who);
        }
        
       
        strData = 'a' + rand() % 26;
        putChToBuffer(strData);
        showStore(who, " <--(input) ", strData);
        
       
        pthread_cond_signal(&g_condition_notEmpty);
        
       
        pthread_mutex_unlock(&g_mutex);

        
        sleep(1);
        
    }
    return NULL;
}



void *consumerThreadFunc(void *arg)
{
    const char *who = (const char *)arg;
    while(1)
    {
       
        pthread_mutex_lock(&g_mutex);

        
        if (1 == isBufferEmpty())
        {
            printf("仓库空了,%s等待!n", who);

            
            pthread_cond_wait(&g_condition_notEmpty, &g_mutex);

            
            printf("----------仓库有货,%s可以消费!n", who);
        };
        
        getChFromBuffer(strData);
        showStore(who, "-->(output)", strData);
        
        pthread_cond_signal(&g_condition_notFull);
        //释放锁
        pthread_mutex_unlock(&g_mutex);
        
        sleep(1);
    }
    return NULL;
}

int main()
{
    
    pthread_t producer_tid[2];
    pthread_t consumer_tid[2];
    
    pthread_create(&producer_tid[0], NULL, producerThreadFunc, "producer_1");
    pthread_create(&producer_tid[1], NULL, producerThreadFunc, "producer_2");
    pthread_create(&consumer_tid[0], NULL, consumerThreadFunc, "consumer_1");
    pthread_create(&consumer_tid[1], NULL, consumerThreadFunc, "consumer_2");

    pthread_join(producer_tid[0],NULL);
    pthread_join(producer_tid[1],NULL);
    pthread_join(consumer_tid[0],NULL);
    pthread_join(consumer_tid[1],NULL);

    getchar();
    return 0;
}

编译:gcc xxx.c -o test -lpthread

执行:./test


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1017902.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号