使用信号量和pthread生产者消费者程序 [英] Producer Consumer program using semaphores and pthreads

查看:166
本文介绍了使用信号量和pthread生产者消费者程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了code的生产者 - 消费者问题。但我没有得到output.There没有编译错误,但在我program.I警告我confused.Trying非常hard.But不能得到it.Please告诉我什么是错误的,我program.What将是正确的program.I正在逐渐frustrated.Please帮助球员。
这里是code -

 的#include< pthreads.h中>
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&; /usr/include/semaphore.h>插槽的#define BUFF_SIZE 5 / *总数* /
生产商的#define NP 3 / *总数* /
消费者的#define NC 3 / *总数* /
#定义NITERS 4 / *生产的项目数/消耗* /typedef结构{
    INT BUF [BUFF_SIZE] / *共享VAR * /
    int类型; / * BUF [在%BUFF_SIZE]是第一个空槽* /
    int的列; / * BUF [OUT%BUFF_SIZE]是第一个全插槽* /
    sem_t十足; / *全守点的数量的轨道* /
    sem_t空的; / *保持空斑的数量轨道* /
    sem_t互斥; / *强制互斥共享数据* /
} sbuf_t;sbuf_t共享;
void *的生产者(无效* ARG){
    INT I,项目,指标;    指数=(int)的精氨酸;
    对于(i = 0; I< NITERS;我++){
        / *农产品项目* /
        项目= I;        / * prepare写项目BUF * /        / *如果没有空的插槽,等待* /
        sem_wait(安培; shared.empty);
        / *如果另一个线程使用缓冲区,等待* /
        sem_wait(安培; shared.mutex);
        shared.buf [shared.in] =项目;
        shared.in =(shared.in + 1)%BUFF_SIZE;
        的printf([P%D]。产%d个... \\ n,索引项); fflush(标准输出);
        / *释放缓冲器* /
        sem_post(安培; shared.mutex);
        / *全递增的插槽数* /
        sem_post(安培; shared.full);        / *交错显示生产者和消费者的执行* /
        如果(ⅰ%2 == 1)睡眠(1);
    }
    返回NULL;
}void *的消费者(无效* ARG){
    INT I,项目,指标;    指数=(int)的精氨酸;
    为(ⅰ= NITERS;我大于0;我 - ){
      sem_wait(安培; shared.full);
      sem_wait(安培; shared.mutex);
      项目= I;
      项目= shared.buf [shared.out]
      shared.out =(shared.out + 1)%BUFF_SIZE;
      的printf([C%D]。领用%d个... \\ n,索引项); fflush(标准输出);
      / *释放缓冲器* /
      sem_post(安培; shared.mutex);
      / *全递增的插槽数* /
      sem_post(安培; shared.empty);      / *交错显示生产者和消费者的执行* /
      如果(ⅰ%2 == 1)睡眠(1);
    }
    返回NULL;
}诠释主(){
    IDP的pthread_t,IDC;
    INT指数;    sem_init(安培; shared.full,0,0);
    sem_init(安培; shared.empty,0,BUFF_SIZE);
    调用pthread_mutex_init(安培; shared.mutex,NULL);
    对于(指数= 0;指数 - LT; NP,指数++){
       / *创建一个新的生产者* /
       在pthread_create(放大器; IDP,NULL,制片人,(无效*)指数);
    }
    / *创建一个新的消费品* /
    对于(指数= 0;指数 - LT;数控;指数++){
        在pthread_create(安培; IDC,NULL,消费类,(无效*)指数);
    }
    了pthread_exit(NULL);
}


解决方案

也许你应该采取的编译器警告更为严重。
不正确的类型和未定义的函数通常显示
作为警告...

我没有检查你的程序的逻辑,但原则应该工作:

 的#include< pthreads.h中>
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&; /usr/include/semaphore.h>//睡眠
#包括LT&;&unistd.h中GT;插槽的#define BUFF_SIZE 5 / *总数* /
生产商的#define NP 3 / *总数* /
消费者的#define NC 3 / *总数* /
#定义NITERS 4 / *生产的项目数/消耗* /typedef结构
{
    INT BUF [BUFF_SIZE] / *共享VAR * /
    int类型; / * BUF [在%BUFF_SIZE]是第一个空槽* /
    int的列; / * BUF [OUT%BUFF_SIZE]是第一个全插槽* /
    sem_t十足; / *全守点的数量的轨道* /
    sem_t空的; / *保持空斑的数量轨道* /    //这里使用正确的类型
    pthread_mutex_t互斥; / *强制互斥共享数据* /
} sbuf_t;sbuf_t共享;
void *的生产者(无效* ARG)
{
    INT I,项目,指标;    指数=(int)的精氨酸;
    对于(i = 0; I< NITERS;我++)
    {        / *农产品项目* /
        项目= I;        / * prepare写项目BUF * /        / *如果没有空的插槽,等待* /
        sem_wait(安培; shared.empty);
        / *如果另一个线程使用缓冲区,等待* /
        调用pthread_mutex_lock(安培; shared.mutex);
        shared.buf [shared.in] =项目;
        shared.in =(shared.in + 1)%BUFF_SIZE;
        的printf([P%D]。产%d个... \\ n,索引项);
        fflush(标准输出);
        / *释放缓冲器* /
        调用pthread_mutex_unlock(安培; shared.mutex);
        / *全递增的插槽数* /
        sem_post(安培; shared.full);        / *交错显示生产者和消费者的执行* /
        如果(ⅰ%2 == 1)睡眠(1);
    }
    返回NULL;
}void *的消费者(无效* ARG)
{
    INT I,项目,指标;    指数=(int)的精氨酸;
    为(ⅰ= NITERS;我大于0;我 - ){
        sem_wait(安培; shared.full);
        调用pthread_mutex_lock(安培; shared.mutex);
        项目= I;
        项目= shared.buf [shared.out]
        shared.out =(shared.out + 1)%BUFF_SIZE;
        的printf([C%D]。领用%d个... \\ n,索引项);
        fflush(标准输出);
        / *释放缓冲器* /
        调用pthread_mutex_unlock(安培; shared.mutex);
        / *全递增的插槽数* /
        sem_post(安培; shared.empty);        / *交错显示生产者和消费者的执行* /
        如果(ⅰ%2 == 1)睡眠(1);
    }
    返回NULL;
}诠释的main()
{
    IDP的pthread_t,IDC;
    INT指数;    sem_init(安培; shared.full,0,0);
    sem_init(安培; shared.empty,0,BUFF_SIZE);
    调用pthread_mutex_init(安培; shared.mutex,NULL);
    对于(指数= 0;指数 - LT; NP,指数++)
    {
        / *创建一个新的生产者* /
        在pthread_create(放大器; IDP,NULL,制片人,(无效*)指数);
    }
    / *创建一个新的消费品* /
    对于(指数= 0;指数 - LT;数控;指数++)
    {
        在pthread_create(安培; IDC,NULL,消费类,(无效*)指数);
    }    了pthread_exit(NULL);
}

我希望这有助于。

I have written a code for producer-consumer problem.But I am not getting the output.There is no compilation error,but warning in my program.I am confused.Trying very hard.But can't get it.Please tell me what is wrong in my program.What will be the correct program.I am getting frustrated.Please help guys. Here is the code-

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include </usr/include/semaphore.h>

#define BUFF_SIZE   5           /* total number of slots */
#define NP          3           /* total number of producers */
#define NC          3           /* total number of consumers */
#define NITERS      4           /* number of items produced/consumed */

typedef struct {
    int buf[BUFF_SIZE];   /* shared var */
    int in;               /* buf[in%BUFF_SIZE] is the first empty slot */
    int out;              /* buf[out%BUFF_SIZE] is the first full slot */
    sem_t full;           /* keep track of the number of full spots */
    sem_t empty;          /* keep track of the number of empty spots */
    sem_t mutex;          /* enforce mutual exclusion to shared data */
} sbuf_t;

sbuf_t shared;


void *Producer(void *arg) {
    int i, item, index;

    index = (int) arg;


    for (i = 0; i < NITERS; i++) {
        /* Produce item */
        item = i;

        /* Prepare to write item to buf */

        /* If there are no empty slots, wait */
        sem_wait(&shared.empty);
        /* If another thread uses the buffer, wait */
        sem_wait(&shared.mutex);
        shared.buf[shared.in] = item;
        shared.in = (shared.in+1)%BUFF_SIZE;
        printf("[P%d] Producing %d ...\n", index, item); fflush(stdout);
        /* Release the buffer */
        sem_post(&shared.mutex);
        /* Increment the number of full slots */
        sem_post(&shared.full);

        /* Interleave  producer and consumer execution */
        if (i % 2 == 1) sleep(1);
    }
    return NULL;
}

void *Consumer(void *arg) {
    int i, item, index;

    index = (int) arg;
    for (i = NITERS; i > 0; i--) {
      sem_wait(&shared.full);
      sem_wait(&shared.mutex);
      item = i;
      item = shared.buf[shared.out];
      shared.out = (shared.out + 1) % BUFF_SIZE;
      printf("[C%d] Consuming  %d ...\n", index, item); fflush(stdout);
      /* Release the buffer */
      sem_post(&shared.mutex);
      /* Increment the number of full slots */
      sem_post(&shared.empty);

      /* Interleave  producer and consumer execution */
      if (i % 2 == 1) sleep(1);
    }
    return NULL;
}

int main() {
    pthread_t idP, idC;
    int index;

    sem_init(&shared.full, 0, 0);
    sem_init(&shared.empty, 0, BUFF_SIZE);
    pthread_mutex_init(&shared.mutex, NULL);
    for (index = 0; index < NP; index++) {
       /* Create a new producer */
       pthread_create(&idP, NULL, Producer, (void*)index);
    }
    /*create a new Consumer*/
    for (index = 0;index < NC;index++) {
        pthread_create(&idC, NULL, Consumer, (void*)index);
    }
    pthread_exit(NULL);
}

解决方案

Maybe you should take the Compiler warnings more serious. Incorrect types and undefined functions are usually shown as warning...

I haven't checked the Logic of your program, but the principle should work:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include </usr/include/semaphore.h>

// for sleep
#include <unistd.h>

#define BUFF_SIZE   5           /* total number of slots */
#define NP          3           /* total number of producers */
#define NC          3           /* total number of consumers */
#define NITERS      4           /* number of items produced/consumed */

typedef struct
{
    int buf[BUFF_SIZE];   /* shared var */
    int in;               /* buf[in%BUFF_SIZE] is the first empty slot */
    int out;              /* buf[out%BUFF_SIZE] is the first full slot */
    sem_t full;           /* keep track of the number of full spots */
    sem_t empty;          /* keep track of the number of empty spots */

    // use correct type here
    pthread_mutex_t mutex;          /* enforce mutual exclusion to shared data */
} sbuf_t;

sbuf_t shared;


void *Producer(void *arg)
{
    int i, item, index;

    index = (int)arg;


    for (i=0; i < NITERS; i++)
    {

        /* Produce item */
        item = i;

        /* Prepare to write item to buf */

        /* If there are no empty slots, wait */
        sem_wait(&shared.empty);
        /* If another thread uses the buffer, wait */
        pthread_mutex_lock(&shared.mutex);
        shared.buf[shared.in] = item;
        shared.in = (shared.in+1)%BUFF_SIZE;
        printf("[P%d] Producing %d ...\n", index, item);
        fflush(stdout);
        /* Release the buffer */
        pthread_mutex_unlock(&shared.mutex);
        /* Increment the number of full slots */
        sem_post(&shared.full);

        /* Interleave  producer and consumer execution */
        if (i % 2 == 1) sleep(1);
    }
    return NULL;
}

void *Consumer(void *arg)
{
    int i, item, index;

    index = (int)arg;
    for (i=NITERS; i > 0; i--) {
        sem_wait(&shared.full);
        pthread_mutex_lock(&shared.mutex);
        item=i;
        item=shared.buf[shared.out];
        shared.out = (shared.out+1)%BUFF_SIZE;
        printf("[C%d] Consuming  %d ...\n", index, item);
        fflush(stdout);
        /* Release the buffer */
        pthread_mutex_unlock(&shared.mutex);
        /* Increment the number of full slots */
        sem_post(&shared.empty);

        /* Interleave  producer and consumer execution */
        if (i % 2 == 1) sleep(1);
    }
    return NULL;
}

int main()
{
    pthread_t idP, idC;
    int index;

    sem_init(&shared.full, 0, 0);
    sem_init(&shared.empty, 0, BUFF_SIZE);
    pthread_mutex_init(&shared.mutex, NULL);
    for (index = 0; index < NP; index++)
    {
        /* Create a new producer */
        pthread_create(&idP, NULL, Producer, (void*)index);
    }
    /*create a new Consumer*/
    for(index=0; index<NC; index++)
    {
        pthread_create(&idC, NULL, Consumer, (void*)index);
    }



    pthread_exit(NULL);
}

I hope this helps.

这篇关于使用信号量和pthread生产者消费者程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆