头歌实训 第1关:生产者与消费者问题

头歌实训 第1关:生产者与消费者问题

任务描述

本关任务:程序 4-1.c 模拟了 1 个生产者和 1 个消费者,请改写该程序,模拟 5 个生产者和 5 个消费者,它们共享一个包含 8 个缓冲区的缓冲池。产品以 4 位编号,最高位表示生产者编号、其他表示该生产者的产品号,参考输出如下:

在这里插入图片描述

知识

  1. 多线程程序如何编译

    输入gcc -pthread XX.c,编译成功后输入./a.out

  2. pthread_create()函数

    1. 原型int pthread_create(pthread_t *tidp, const pthread_attr_t *attr,(void*)(*start_routine)(void*), void *arg);

      参数的意义(从左往右):

      • 传递一个 pthread_t 类型的指针变量。
      • 用于手动设置新建线程的属性。一般我们设置成NULL。
      • 以函数指针的方式指明新建线程需要执行的函数,该函数的参数最多有 1 个(可以省略不写),形参和返回值的类型都必须为 void* 类型。
      • 指定传递给 start_routine 函数的实参。
  3. 主线程结束后,子线程会被杀死

    1. 刚刚开始运行的时候发现输入./a.out只输出几行就结束,然后发现应该是主线程结束的时候杀死了子线程。
    2. 使用pthread_join(pthread_t thread, void ** retval)等待
  4. 怎么创建多个线程 😄

    比如说我们这里要创建五个生产者、五个消费者,我们以生产者为例子。1.首先定义pthread_t 类型的指针变量;2.调用for循环创建五个线程,并且在创建线程的pthread_create函数中执行produce函数;3.在创建线程的时候给其编号,也就是通过pthread_create函数里的第四个参数实现。对于这个给线程编号的事,我刚开始想的是用每个线程的id去对应编号,而pthread.h这个头文件里有专门的函数(pthread_self)可以获取线程的id,但是略麻烦、耗空间。这里我们可以直接在创建的时候利用第四个参数给produce函数传一个编号就好了。

    让我们看看代码。这里是只有与我描述的创建过程相关的代码。

     void *produce(void * arg);//这只是一个声明,看看参数是什么样子就可以了
     
     int res[10], i;
    
      pthread_t t1[8] = {0};
    
    
      for (i = 0; i < 5; i++)
      {
        
        res[i] = pthread_create(&(t1[i]), NULL, produce, (void *)&i);
        
        if (res[i] != 0){
          perror("failed to create thread");
          exit(1);
        }
       }
    
  5. produceconsume怎么实现互斥

    刚刚开始我是用的头文件pthread.h里的pthread_mutex_lock()pthread_mutex_unlock()做的,但是检测的时候说我信号量的数量不对,所以就改了一下。这个部分没有去看pthread_mutex_lock()pthread_mutex_unlock()的原型,所以具体怎么错的也不太清楚。但是下面的这种方法是一定可以的。

    sem_t full, empty, mtx, mtx1;//全局变量
    
    
    
    //在main函数里面进行初始化
      sem_init(&full, 0, 0);
      sem_init(&mtx, 0, 1);   //produce
      sem_init(&mtx1, 0, 1);  //consume
      sem_init(&empty, 0, N);
    
    
    

奇怪的问题

消费者输出

消费者的输出显示一开始还有别的序号的线程在运行,但是不知道为什么后来就只有序号为4或5的线程在运行了。说实话,不知道这个问题该怎么解决,发现解决方案也是很偶然的。

//有问题的情况的代码
void *consume(void * arg){
  int i;
  for (i = 0; i < PRODUCT_NUM; i++){
    sleep_random(2);

    sem_wait(&full);
    sem_wait(&mtx1);
  
    printf("%d consume: %d\n", (*((int *)arg)), buffer[readpos]);
    buffer[readpos++] =  - 1;
    if (readpos >= N)
      readpos = 0;

    sem_post(&mtx1);
    sem_post(&empty);

  }
}

//能够正常运行的代码

void *consume(void * arg){
  int i;
  int id = *((int *)arg);

  for (i = 0; i < PRODUCT_NUM; i++){
    sleep_random(2);

    sem_wait(&full);
    sem_wait(&mtx1);

    printf("%d consume: %d\n", id, buffer[readpos]);
    buffer[readpos++] =  - 1;
    if (readpos >= N)
      readpos = 0;

    sem_post(&mtx1);
    sem_post(&empty);
    //sem_post(&mtx1);
  }
}

段错误(核心已转移)→也出现了总线错误(核心已转移)

emmm网上搜索一下这个报错就出来了。我是觉得自己写的程序应该是不会出现越界的情况,所以我觉得可能是文中所说的“非关联化空指针”的问题。虽然也不是很懂,但我确实是在使用pthread_join等待子线程结束的时候定义了两个空指针。我把定义的地方注释掉再测试就没有这个报错了。

有关这个报错的博客的网址在此:https://blog.csdn.net/qq_29350001/article/details/53780697

//会报错的代码:
  void * thread_res[10];
  void * thread_res1[10];

  pthread_join(t1[0], thread_res[0]);
  pthread_join(t1[1], thread_res[1]);
  pthread_join(t1[2], thread_res[2]);
  pthread_join(t1[3], thread_res[3]);
  pthread_join(t1[4], thread_res[4]);

  pthread_join(t2[0], thread_res1[0]);
  pthread_join(t2[1], thread_res1[1]);
  pthread_join(t2[2], thread_res1[2]);
  pthread_join(t2[3], thread_res1[3]);
  pthread_join(t2[4], thread_res1[4]);
  
  
 //修改之后的代码
 
 
  // void * thread_res[10];
  // void * thread_res1[10];

  pthread_join(t1[0], NULL);
  pthread_join(t1[1], NULL);
  pthread_join(t1[2], NULL);
  pthread_join(t1[3], NULL);
  pthread_join(t1[4], NULL);

  pthread_join(t2[0], NULL);
  pthread_join(t2[1], NULL);
  pthread_join(t2[2], NULL);
  pthread_join(t2[3], NULL);
  pthread_join(t2[4], NULL);


最终代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>



#define N  10 
#define PRODUCT_NUM 10
int buffer[N], readpos = 0, writepos = 0;
sem_t full, empty, mtx, mtx1;


void sleep_random(int t) {
  sleep((int)(t * (rand() / (RAND_MAX *1.0))));
}

void *produce(void * arg){
  int id = *((int *)arg);
  int i;
  for (i = 0; i < PRODUCT_NUM; i++){
    sleep_random(2);

    sem_wait(&empty);
     sem_wait(&mtx);

    buffer[writepos++] = id*1000 + i + 1;
    if (writepos >= N)
    {
      writepos = 0;
    }
    printf("produce:    %d\n", id*1000 + i + 1);
 
    sem_post(&mtx);
    sem_post(&full);
  }
}

void *consume(void * arg){
  int i;
  int id = *((int *)arg);

  for (i = 0; i < PRODUCT_NUM; i++){
    sleep_random(2);

    sem_wait(&full);
    sem_wait(&mtx1);
  
    printf("%d consume: %d\n", id, buffer[readpos]);
    buffer[readpos++] =  - 1;
    if (readpos >= N)
      readpos = 0;
      
    sem_post(&mtx1);
    sem_post(&empty);

  }
}

int main(){
  int res[10], res1[10], i;

  pthread_t t1[8] = {0};
  pthread_t t2[8] = {0};

  for (i = 0; i < N; i++)
    buffer[i] =  - 1;
  srand((int)time(0));

  sem_init(&full, 0, 0);
  sem_init(&mtx, 0, 1);
  sem_init(&mtx1, 0, 1);
  sem_init(&empty, 0, N);

  for (i = 0; i < 5; i++)
  {
    
    res[i] = pthread_create(&(t1[i]), NULL, produce, (void *)&i);
    
    if (res[i] != 0){
      perror("failed to create thread");
      exit(1);
    }  
  }
  
  for (int j = 0; j < 5; j++)
  {
    
    res1[j] = pthread_create(&(t2[j]), NULL, consume, (void *)&j);
    if (res1[j] != 0){
      perror("failed to create thread");
      exit(1);
    }
  }

  pthread_join(t1[0], NULL);
  pthread_join(t1[1], NULL);
  pthread_join(t1[2], NULL);
  pthread_join(t1[3], NULL);
  pthread_join(t1[4], NULL);

  pthread_join(t2[0], NULL);
  pthread_join(t2[1], NULL);
  pthread_join(t2[2], NULL);
  pthread_join(t2[3], NULL);
  pthread_join(t2[4], NULL);

  return 0;
}

emm

这个题目还有点bug。缓冲池的大小是10也能过。然后这样写也能过(见下面),虽然这个情况可能不会生成死锁,但是我觉得这样写还是不太好。


void *produce(void * arg){
    //略
    sem_wait(&mtx);
    sem_wait(&empty);
    
    //略
    sem_post(&mtx);
    sem_post(&full);
    
    //略
}
  • 17
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值