What program apes are thinking about while waiting for the elevator?

Author | Cooper Song

Zebian | Elle

Produced | Life program (ID: coder_life)

Have waited for so long, how the elevator has not come yet? ? ? There must be an elevator scheduling problem! Let me give it to design an elevator scheduling algorithm.

Elevator dispatching and scheduling disk operating system are linked. I thought about three years ago elevator scheduling problem, when I had just moved into high-rise residential, but I was still solid enough professional knowledge, there is no in-depth study. Until now I met a disk scheduling algorithms operating system, I think the elevator scheduling algorithm. The same purpose, the same thing is nothing but scheduling. Disk scheduling, the head pointer is moved (relatively to say), and in elevator dispatching, the movement of the elevator.

Then lift the scheduling algorithm, what does? They are applicable to situations in which it?

First come first serve algorithm

Referred to as first come first serve algorithm is FCFS, it is First Come First Serve acronym. As the name suggests, it is the first to come in front of the elevator (or first press the elevator up and down buttons) first passenger elevator service experience.

For example, Lee uncle pressed upward in a floor button, after which the 15 sheets of Uncle floor button pressed down, after which Uncle Wang and 8th floor button is pressed down. Uncle Wang Zhang, who lives with an appointment to go grocery shopping together.

So this time, no matter which floor the elevator now will go to the first floor then Uncle Lee.

After Lee uncle entered the elevator, whether he was going to a few buildings (assuming that Uncle Lee to go to the 20th floor), destination (20th floor), will go to the 15th floor elevator access Uncle Zhang.

Zhang, who lives upstairs in the elevator 15, he was going to jolly, so he went to the first floor, he pressed into the elevator on the first floor of a button.

So began whistling call the elevator down, this time also on the 8th floor of Uncle Wang watched through the 8th floor elevator continues to run down, even ignoring him! ! !

Uncle Zhang successfully reached the first floor, then the elevator came to pick up on the 8th floor king uncle, Uncle Wang this take the elevator to the first floor and Zhang Grandpa and will.

This put Uncle Wang was furious, and my heart is not condemning the property stupid X, is scheduled to write the elevator cursing programmer silly X ......

Malpractice first come first serve algorithm were revealed in the above example, but it also has the advantage of it, which was simple, programmers save! Joke, advantage is relatively fair, passenger elevator service to get the order must first come first served, people will not be cut in line.

The shortest seek time first algorithm

最短寻道时间优先算法的简称是SSTF,是Shortest Seek Time First的缩写,顾名思义,就是距离当前电梯位置最近的乘客,会最先得到电梯服务。

大爷们是否能得到电梯的服务,与电梯当前的位置有关。

还是举上面那个例子,假如在大爷们来到电梯门口前电梯停在1楼。李大爷起初在1楼,无疑是距离电梯最近的,他先上电梯。李大爷来到20楼下了电梯。电梯此时在20楼,距离20楼最近的服务请求来自15楼的张大爷,于是电梯呼呼呼下行来到15楼接上张大爷,此时电梯在15楼,距离15楼最近的服务请求来自8楼的王大爷,这一次电梯没有无视王大爷,接上了王大爷后,王大爷和张大爷一起开开心心坐到1楼去菜市场买菜去了。王大爷和张大爷一边说着物业费没白交,一边夸着写电梯调度的小伙子技术高。

王大爷和张大爷开心了,可把住在30楼的钱大爷气坏了。原来在三位大爷按完按钮之后(电梯刚接上1楼的李大爷)就按了按钮,可是钱大爷看着电梯上行到15楼就改下行了......电梯到达15楼时,所有请求(包含服务请求和目的地到达请求)有这些:张大爷请求到1楼,8楼的王大爷请求上电梯,再就是30楼的钱大爷请求上电梯了。钱大爷距离电梯还差着15层楼呢,按照最短寻道时间优先算法电梯肯定要先去8楼接王大爷。接完王大爷电梯肯定离着目的地1楼最近,也不会上去接钱大爷。

按照这样想下去,如果此时3楼的赵大妈想下楼买菜,钱大爷还得眼睁睁看着电梯从1楼上行到3楼再改下行,估计要是真这样钱大爷连搬家的想法都有了......

最短寻道时间优先算法的弊端在上面这个例子中暴露无遗,那就是距离电梯较远的乘客,可能永远不会得到服务(如果电梯附近的楼层一直有服务请求)。

扫描算法

扫描算法的简称是SCAN,SCAN算法是电梯调度中使用最广泛的一种算法。SCAN算法与当前电梯移动的方向有关(上行/下行),当前移动方向上距离电梯最短的请求将最先得到服务。电梯调度与操作系统磁盘调度不同的是,磁盘调度仅仅是为了读写磁盘,并没有目的地这一说,而电梯调度是有目的地的。乘客进入电梯后按的楼层,就是目的地到达请求的楼层。

这就是为什么现代化的电梯门口都有两个按钮,一个上行,一个下行,乘客按了上行按钮表示乘客想要上楼,乘客按了下行按钮表示乘客想要下楼。

因此在SCAN算法中,仅仅在电梯的移动方向上还不行,目的地方向也要与电梯移动方向一致的乘客才有资格先上电梯。这样在电梯向上行的时候,就只处理向上的服务请求(还有距离最远的向下的服务请求)和向上的目的地到达请求,等到上行方向上不再有任何请求(包括服务请求和目的地到达请求),电梯再换向成下行。

下行也是如此,在电梯向下的时候,就只处理向下的服务请求(还有距离最远的向上的服务请求)和向下的目的地到达请求,等到下行方向上不再有任何请求(包括服务请求和目的地到达请求),电梯再换向成上行。

在最短寻道时间优先算法举的例子中,问题得到了相对完美的解决。电梯送李大爷到了20楼,就立刻去30楼接钱大爷,接到张大爷后电梯转为下行,去15楼接了张大爷,又去8楼接了王大爷。李大爷、张大爷、王大爷、钱大爷都很满意,电梯的利用率也较高。这一次,程序员不再背锅。

结语

磁盘调度与电梯调度有相同的地方,也有不同的地方。我不知道是先有的磁盘调度还是先有的电梯调度,但我能肯定的是,他们两者之间肯定存在着相互借鉴。

每一种算法都不能让所有人都满意,比如在扫描算法中,因为有钱大爷在30楼请求下楼,8楼的王大爷就要眼睁睁地看着电梯经过了8楼上行到30楼再回来接他,15楼的张大爷也是眼睁睁地看着电梯经过了15楼上行到30楼再回来接他,但是这样可以让钱大爷、张大爷、王大爷都相对满意。

在这样一种应用情景下,先来先服务算法和最短寻道时间优先算法都会让其中的一位大爷或几位大爷强烈不满。

针对不同的应用场景,设计或选择合适的算法,也是优秀程序员的优良品质之一。

用计算机科学领域的算法看待生活中的实际问题,也许就是计算思维的体现吧。

热 文 推 荐 

千万不要和程序员一起合租!

Python 编程 5 年后,我转向了 Go!

年终拷问:程序员如何避免拿到绩效 D?

达摩院 2020 预测:模块化降低芯片设计门槛 | 问底中国 IT 技术演进

在调查过基于模型的强化学习方法后,我们得到这些结论

漫话:如何给女朋友解释为什么一到年底,部分网站就会出现日期混乱的现象?

联盟链走向何方

你点的每个“在看”,我都认真当成了喜欢

发布了453 篇原创文章 · 获赞 1万+ · 访问量 446万+

Guess you like

Origin blog.csdn.net/csdnsevenn/article/details/103966865