睡觉的理发师问题 [英] Sleeping barber problem

查看:266
本文介绍了睡觉的理发师问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<errno.h>
#include<sys/ipc.h>
#include<semaphore.h>

#define N 5

time_t end_time;/*end time*/
sem_t mutex,customers,barbers;/*Three semaphors*/
int count=0;/*The number of customers waiting for haircuts*/

void barber(void *arg);
void customer(void *arg);

int main(int argc,char *argv[])
{
	pthread_t id1,id2;
	int status=0;
	end_time=time(NULL)+20;/*Barber Shop Hours is 20s*/

	/*Semaphore initialization*/
	sem_init(&mutex,0,1);
	sem_init(&customers,0,0);
	sem_init(&barbers,0,1);

	/*Barber_thread initialization*/
	status=pthread_create(&id1,NULL,(void *)barber,NULL);
	if(status!=0)
		perror("create barbers is failure!\n");
	/*Customer_thread initialization*/
	status=pthread_create(&id2,NULL,(void *)customer,NULL);
	if(status!=0)
		perror("create customers is failure!\n");

	/*Customer_thread first blocked*/
	pthread_join(id2,NULL);
	pthread_join(id1,NULL);

	exit(0);
}

void barber(void *arg)/*Barber Process*/
{
	while(time(NULL)<end_time || count>0)
	{
		sem_wait(&customers);/*P(customers)*/            
		sem_wait(&mutex);/*P(mutex)*/
		count--;
		printf("Barber:cut hair,count is:%d.\n",count);
		sem_post(&mutex);/*V(mutex)*/
		sem_post(&barbers);/*V(barbers)*/
		sleep(3);       
	}
}

void customer(void *arg)/*Customers Process*/
{
	while(time(NULL)<end_time)
	{
		sem_wait(&mutex);/*P(mutex)*/
		if(count<N)
		{
			count++;
			printf("Customer:add count,count is:%d\n",count);
			sem_post(&mutex);/*V(mutex)*/
			sem_post(&customer);/*V(customers)*/
			sem_wait(&barbers);/*P(barbers)*/
		}
		else
			/*V(mutex)*/
			/*If the number is full of customers,just put the mutex lock let go*/
			sem_post(&mutex);
		sleep(1);
	}
}



这是流程之间通信的典型问题。我在上面的代码中不理解函数sleep()。有人可以告诉我详细的含义。谢谢你,圣诞快乐。 :)


This is a classic problem of communication between processes. I do not understand the function, sleep(), in the above code. Could someone can tell me the meaning in detail.Thank you and Merry Christmas. :)

推荐答案

睡眠只是作为程序的非活动时间,它在给定的毫秒时间内什么都不做
the sleep just worked as the inactive time for the program that it do nothing during given miliseconds of time


我认为只是暂停处理指定的毫秒数(或秒?)。因此,sleep(1)意味着当处理该行时,您的程序将在1毫秒内完全没有任何作用。但是,根据运行代码的系统,它可能不是1毫秒。例如,我的计算机似乎只能执行大约15毫秒的精度操作。因此,sleep(1)可能更像sleep(15)。
I think that just halts processing for the specified number of milliseconds (or seconds?). So, "sleep(1)" would mean that when that line is processed, your program will do absolutely nothing for 1 millisecond. However, depending on the system that code is run on, it may not be 1 millisecond. For example, my computer seems to only be able to perform operations to a precision of about 15 milliseconds. So, "sleep(1)" might act more like "sleep(15)".


除了aspdotnetdev所说的,当一个线程正在休眠时允许另一个线程执行。如果理发师线程没有睡觉并放弃处理器,那么客户线程可能永远不会有机会做任何事情。随着多核处理器的普及,这可能不是一个问题,但情况并非总是如此。
In addition to what aspdotnetdev said, when one thread is sleeping that allows another to execute. If the barber thread didn''t sleep and give up the processor then the customer thread might never get a chance to do anything. With multi-core processors being more prevalent this might be less of a problem but that wasn''t always the case.


这篇关于睡觉的理发师问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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