头歌实训 第1关:生产者与消费者问题
任务描述
本关任务:程序 4-1.c
模拟了 1 个生产者和 1 个消费者,请改写该程序,模拟 5 个生产者和 5 个消费者,它们共享一个包含 8 个缓冲区的缓冲池。产品以 4 位编号,最高位表示生产者编号、其他表示该生产者的产品号,参考输出如下:
知识
-
多线程程序如何编译
输入
gcc -pthread XX.c
,编译成功后输入./a.out
。 -
pthread_create()
函数-
原型
int pthread_create(pthread_t *tidp, const pthread_attr_t *attr,(void*)(*start_routine)(void*), void *arg);
参数的意义(从左往右):
- 传递一个
pthread_t
类型的指针变量。 - 用于手动设置新建线程的属性。一般我们设置成NULL。
- 以函数指针的方式指明新建线程需要执行的函数,该函数的参数最多有 1 个(可以省略不写),形参和返回值的类型都必须为 void* 类型。
- 指定传递给
start_routine
函数的实参。
- 传递一个
-
-
主线程结束后,子线程会被杀死
- 刚刚开始运行的时候发现输入
./a.out
只输出几行就结束,然后发现应该是主线程结束的时候杀死了子线程。 - 使用
pthread_join(pthread_t thread, void ** retval)
等待
- 刚刚开始运行的时候发现输入
-
怎么创建多个线程 😄
比如说我们这里要创建五个生产者、五个消费者,我们以生产者为例子。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); } }
-
produce
、consume
怎么实现互斥刚刚开始我是用的头文件
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);
//略
}