こんにちは、睡眠理髪師問題と呼ばれるマルチスレッド プログラムを実装しています。問題は、N 脚の椅子を備えた理髪店が 1 つあり、顧客がいない場合、床屋は眠りに落ち、顧客が店に来るたびに床屋が目を覚まし、顧客の髪を切り始めます。セマフォではなく条件変数を使用します。問題は、期待する出力が得られなかったことです。
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#define TRUE 1
pthread_mutex_t mutex;
pthread_cond_t fill;
int counter;
int* buffer;
int size;
void* producer(void*);
void* consumer(void*);
void initialize_variables();
void insert(void);
void remove_item(void);
int main(int argc, char *argv[]) {
int random_num;
int num=0;
pthread_t barber;
srand( time(NULL) );
if(argc!=2){
printf("Not enough parameters\n");
exit(0);
}
size=atoi(argv[1]);
initialize_variables();
pthread_create(&barber,NULL,consumer,NULL);
while(TRUE){
pthread_t customer;
pthread_create(&customer,NULL,producer,NULL);
}
printf("Exit the program\n");
return 0;
}
void initialize_variables(){
int i;
if(! (buffer=(int*)malloc(size*sizeof(int))) ){
printf("Error while allocating memory for buffer\n");
exit(1);
}
for(i=0; i<size; i++)
buffer[i]=0;
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&empty, NULL);
pthread_cond_init(&fill, NULL);
counter = 0;
}
void* producer(void* arg){
pthread_mutex_lock(&mutex);
while(counter==size)
pthread_cond_wait(&empty,&mutex);
printf("One Customer has arrived and sit on the #%d chair\n", counter);
insert();
sleep(1);
pthread_cond_signal(&fill);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
void* consumer(void* arg){
while(TRUE){
pthread_mutex_lock(&mutex);
while(counter==0){
printf("Barber is sleeping now\n");
pthread_cond_wait(&fill,&mutex);
}
remove_item();
sleep(1);
pthread_cond_broadcast(&empty);
pthread_mutex_unlock(&mutex);
}
}
void insert(void){
buffer[counter]=1;
counter++;
}
void remove_item(void){
buffer[counter]=0;
printf("The barber is started to cut the hair of the customer at chair #%d\n", counter);
counter--;
}
さらに、プログラムを開始すると出力が"Barber is sleeping now"
最初に出力されますが、メイン スレッドが最初にプロデューサー スレッドの実行を開始し、カウンターがゼロに等しいときに出力され"Barber is sleeping now"
ます。もう 1 つの問題は、メイン スレッドがバッファ全体をいっぱいにしてカウント ダウンを開始することです (これは、ショップがいっぱいになった後に理髪師が仕事を開始しようとしていることを意味します。ここに出力の例を示します。これを明確に示します。バッファがそのバッファであると仮定します)。サイズは10です。
One Customer has arrived and sit on the #0 chair
One Customer has arrived and sit on the #1 chair
One Customer has arrived and sit on the #2 chair
One Customer has arrived and sit on the #3 chair
One Customer has arrived and sit on the #4 chair
One Customer has arrived and sit on the #5 chair
One Customer has arrived and sit on the #6 chair
One Customer has arrived and sit on the #7 chair
One Customer has arrived and sit on the #8 chair
One Customer has arrived and sit on the #9 chair
The barber is started to cut the hair of the customer at chair #10
The barber is started to cut the hair of the customer at chair #9
The barber is started to cut the hair of the customer at chair #8
The barber is started to cut the hair of the customer at chair #7
The barber is started to cut the hair of the customer at chair #6
The barber is started to cut the hair of the customer at chair #5
The barber is started to cut the hair of the customer at chair #4
The barber is started to cut the hair of the customer at chair #3
The barber is started to cut the hair of the customer at chair #2
The barber is started to cut the hair of the customer at chair #1
Barber is sleeping now
One Customer has arrived and sit on the #0 chair
私はすべての返信に感謝し、とにかく感謝します
編集:投稿が機能していなかった最初のコードは申し訳ありませんが、真のもので更新しました。