分布式系统中互斥的代码 [英] a code for mutual exclusion in distributed system

查看:90
本文介绍了分布式系统中互斥的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我代码在分布式系统中实现互斥.

Please give me the code for implementation of mutual exclusion in distributed system.

推荐答案

简短回答:不,我们不提供按订单提供的代码.
更长的答案:对您正在研究的主题进行一些研究,如果遇到特定的技术问题,请回到此处.
Short answer: No, we do not provide code to order.
Longer answer: go and do some research into the subject you are studying, and come back here when you have a specific technical question.


我不能给您代码,因为它就我而言,这很长而且很耗时.

但是,我可以为您提供解决方法方面的帮助.

您需要使用信号量,以阻止其他进程/线程访问代码块.信号量有点像布尔值或整数,但是更聪明.基本上,如果线程或进程评估代码块,则将信号量设置为一个值(通常为1),因此,如果其他进程/线程尝试访问该代码块,则在释放该信号量之前不允许它们.

使用信号量而不是整数或布尔值的原因是因为将其视为火车轨道.如果两端都有一个交通信号灯并且只有一条轨道.如果两列火车同时进入隧道,则其中一列火车都不知道另一列火车已进入隧道,因为红灯同时发出来警告其他火车,隧道中还有另一列火车,即使它们确实知道,他们将无法及时停车以纠正该问题.要解决此问题,您有多个火车轨道,并使用信号来防止当另一列火车已经在隧道中时,火车无法进入隧道.

http://www.mathcs.emory.edu/~cheung/课程/455/Syllabus/5c-pthreads/semaphore.gif [ http://docs.oracle.com/javase/1.5. 0/docs/api/java/util/concurrent/Semaphore.html [ http://programmingexamples.wikidot.com/java-semaphore [
I can''t give you the code, as it is quite long and time consuming on my part.

However I can give you help on how to go about solving the problem.

You need to use semaphores, to stop other processes/threads from accessing a block of code. Semaphores are a bit like booleans or integers but are a bit more clever. Basically if a thread or a process assesses a block of code, you set the semaphore to a value (normally 1) therefore if other processes/threads try to access that block of code then they are not allowed to until that semaphore has been released.

The reason you use semaphores instead of integers or booleans is because think of it like a train track. If there is a traffic light at both ends and there is one track. If two trains go into the tunnel at the same time, neither one of the train knows the other train has gone into the tunnel because the red light comes at the same time to warn other trains there is another train in the tunnel and even if they did know then they wouldn''t be able to stop the train in time to rectify that issue. To solve this problem you have multiple train tracks and use signals to prevent the train from accessing the tunnel when another train is already in the tunnel.

http://www.mathcs.emory.edu/~cheung/Courses/455/Syllabus/5c-pthreads/semaphore.gif[^]

Now you may think well that is common sense to have signals but I still can''t get my head around it, so I will give you another example that I like:

Think of booking a plane ticket. There is one seat left on the plane, so one operator sees this one seat and goes on to book this ticket. The operator needs to load up the database and enter the information to book the seat. This can take lets say 5 minutes to complete. During that 5 minutes another operator sees there is still one seat left on the plane, so also accesses that database and enters the information. When the first operator finishes, that seat is booked with the first passengers details entered. However the second operator doesn''t see that the seat is now already booked because she is already in the database entering the second passengers details, therefore when that second operator finishes, overwrites the first passengers details with the second passengers details. Semaphores are used to prevent the operator from actually entering the database in the first place, so the signal has time to be sent to show that this seat has been taken by another operator but they are still entering the details of the passenger that this seat belongs to.

You can actually see this in action, if you look at the VUE cinema website. When you book a seat online, that seat shades in red when you have requested it and blocks anyone else from picking that seat however after a certain amount of time if the customer hasn''t entered their details quick enough the seat becomes available again. There was a problem when I went to book a seat, and because I already requested that seat I couldn''t pick that seat again. After about half an hour the seat became available.

All these examples use semaphores.

Now I am unsure how to do this in Java, as I only used semaphores in C, but here are a few websites I have found that will hopefully help you.

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Semaphore.html[^]

http://programmingexamples.wikidot.com/java-semaphore[^]


这篇关于分布式系统中互斥的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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