我正在解决sleeping barber problem作为我学校作业的一部分。当我使用GDB运行它时,它工作正常,但如果我从命令行运行它会卡住。这是代码:
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdlib.h>
#define MAX 5
int nr_clients, open;
sem_t sem_open1, sem_clients, sem_chair, sem_number;
void *Barber(){
sem_wait(&sem_open1);
printf("Barber: Opening the shop\n");
open = 1;
sem_post(&sem_open1);
while(1){
printf("Barber: Sleeping while there is no clients\n");
sem_wait(&sem_clients);
sem_wait(&sem_number);
if(nr_clients > 0){
sem_post(&sem_number);
printf("Barber: starting to work on a client\n");
sem_post(&sem_chair);
sleep(1);
sem_wait(&sem_number);
printf("Barber: Client finished\n");
nr_clients--;
sem_post(&sem_number);
} else {
printf("Barber: Closing the shop\n");
break;
}
}
}
void *Client(void *x){
int i = *((int*)x);
printf("%8sClient(%d): I want a haircut\n", "", i);
sem_wait(&sem_open1);
sem_wait(&sem_number);
if(open == 1 && nr_clients < MAX){
sem_post(&sem_open1);
printf("%8sClient(%d): entering the shop\n", "", i);
nr_clients++;
sem_post(&sem_number);
sem_post(&sem_clients);
sem_wait(&sem_chair);
printf("%8sClient(%d): barber is working on me\n", "", i);
} else {
printf("No more room\n");
}
}
int main(int argc, char const *argv[])
{
if(sem_init(&sem_open1,0,1) == -1 || sem_init(&sem_clients,0,0) == -1
|| sem_init(&sem_chair,0,0) == -1 || sem_init(&sem_number,0,1) == -1){
printf("ERROR!\n");
return 1;
}
int nr_threads = 5+1;
pthread_t thr_id[nr_threads];
if(pthread_create(&thr_id[0], NULL, Barber, NULL) != 0) {
printf("ERROR!\n");
exit(1);
}
int numbers[nr_threads-1];
for (int i = 0; i < nr_threads-1; ++i)
{
numbers[i] = i;
}
for (int i = 1; i < nr_threads; ++i)
{
if(pthread_create(&thr_id[i], NULL, Client, &numbers[i-1]) != 0){
printf("ERROR!\n");
exit(1);
}
}
sleep(10);
sem_wait(&sem_open1);
open = 0;
sem_post(&sem_open1);
sem_post(&sem_clients);
for (int i = 0; i < nr_threads; ++i)
{
pthread_join(thr_id[i], NULL);
}
}
我这样编译:
gcc -g sleeping_barber.c -o barber -lpthread
然后在命令行中启动它。 命令行输出:
Client(4): I want a haircut
No more room
Client(3): I want a haircut
Client(2): I want a haircut
Client(1): I want a haircut
Client(0): I want a haircut
^C
它只是卡在那里。
在GDB中,我这样运行:
(gdb) file barber
Reading symbols from barber...done.
(gdb) r
Starting program: /home/marko/Desktop/barber
GDB输出:
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff77f0700 (LWP 2808)]
Barber: Opening the shop
Barber: Sleeping while there is no clients
[New Thread 0x7ffff6fef700 (LWP 2809)]
Client(0): I want a haircut
Client(0): entering the shop
Barber: starting to work on a client
Client(0): barber is working on me
[New Thread 0x7ffff67ee700 (LWP 2810)]
[Thread 0x7ffff6fef700 (LWP 2809) exited]
Client(1): I want a haircut
Client(1): entering the shop
[New Thread 0x7ffff5fed700 (LWP 2811)]
Client(2): I want a haircut
Client(2): entering the shop
[New Thread 0x7ffff57ec700 (LWP 2812)]
Client(3): I want a haircut
Client(3): entering the shop
[New Thread 0x7ffff4feb700 (LWP 2813)]
Client(4): I want a haircut
Client(4): entering the shop
Barber: Client finished
Barber: Sleeping while there is no clients
Barber: starting to work on a client
Client(1): barber is working on me
[Thread 0x7ffff67ee700 (LWP 2810) exited]
Barber: Client finished
Barber: Sleeping while there is no clients
Barber: starting to work on a client
Client(2): barber is working on me
[Thread 0x7ffff5fed700 (LWP 2811) exited]
Barber: Client finished
Barber: Sleeping while there is no clients
Barber: starting to work on a client
Client(3): barber is working on me
[Thread 0x7ffff57ec700 (LWP 2812) exited]
Barber: Client finished
Barber: Sleeping while there is no clients
Barber: starting to work on a client
Client(4): barber is working on me
[Thread 0x7ffff4feb700 (LWP 2813) exited]
Barber: Client finished
Barber: Sleeping while there is no clients
Barber: Closing the shop
[Thread 0x7ffff77f0700 (LWP 2808) exited]
[Inferior 1 (process 2804) exited normally]
按我们的任务中的描述工作。
信息:
OS - &gt; Xubuntu的-16.04.1
DEBUGGER - &gt; GNU gdb(Ubuntu 7.11.1-0ubuntu1~16.04)7.11.1
答案 0 :(得分:2)
您的代码中存在多个问题。
如果客户无法进入理发店,您再也不会发布信号量sem_open1
和sem_client
。
这就是为什么你的线程明显被阻塞等待sem_open1的原因。你可以扣除这个,因为“我想要理发”之后没有更多的输出。
但是,如果我们从头开始,我们会看到当您的第一个线程进入商店时open
必须为0,因为您的条件if(open == 1 && nr_clients < MAX)
为假。
由于没有人增加nr_clients
,它必须是open
部分。
此外,你可能只看你的输出:理发师尚未开店。 如果商店在第一次尝试时关闭,您可以添加一些逻辑再试一次。
您遇到问题的原因是您假定执行线程的某个顺序。您可以通过输出中的线程编号轻松查看,这不是您所期望的。你根本不能依赖任何订单。
为了确保您的商店被打开,您需要在启动其他线程之前让Barber的线程运行。创建理发后添加一些sleep()
。
...最后 根据我所写的,你可能会得出结论,这都与时间有关。如果在调试器中运行程序或没有调试器,则时序可能会有很大不同。