Click here to Skip to main content
15,851,319 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please give me the code for implementation of mutual exclusion in distributed system.
Posted
Updated 19-Feb-12 5:38am
v2

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.
 
Share this answer
 
Comments
TorstenH. 20-Feb-12 1:49am    
different approach: pay us.
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[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900