Tuesday, February 2, 2021

Research Presentation: Java vs. Erlang

Java vs. Erlang

Mini research presentation, BSc Computer Science, 2018.


Introduction


Technology has been evolving and as a result, processing power and performance are improving meaning computers are becoming faster and more efficient. Concurrency is the occurrence of many processes happening simultaneously, allowing users to perform tasks on an application at the same time as other tasks happening in the background. 

This has become something that many choose to implement into their programs. There can be issues that arise from concurrent access to shared resources. We will discuss how interference occurs between multiple processes interacting with shared resources on a cloud storage platform such as Dropbox. The issues are highlighted and discussed and solutions are put forward to allow concurrency to perform without any interference between processes. 


Cloud Storage


Cloud storage platforms are becoming a widely used tool by many individuals and businesses to share files and directories among each other. If two clients access a shared document, reading and writing the file, the first client’s input may be overwritten by the second, meaning only the second clients’s input is saved instead of both of them. This highlights a concurrency issue. 

Erlang


Erlang is a language for programming concurrent and distributed systems. The language was designed by Joe Armstrong, Robert Virding and Mike Williams and developed by Ericsson in 1986. Erlang was designed with the intention to improve and develop telephony applications and was implemented into Ericssons telecom switching systems. The programming language is mainly based on parallel processing and pattern matching [3].

Erlang logo

Erlang is a concurrent programming language – this means that any parallel processes can be programmed directly in Erlang, and because Erlang is a single assignment language, i.e. variables are immutable, they don’t need concurrency protection.

Java


Most popular technologies 2018, StackOverflow

Java is one of the top programming languages and development platform. It reduces costs, shortens development timeframes, drives innovation, and improves application services. With millions of developers running more than 51 billion Java Virtual Machines worldwide, Java continues to be the development platform of choice for enterprises and developers. [9]

Java Hello World

The language was designed by James Gosling and developed in May 1995 by Sun Microsystems which is now owned by Oracle Corporation. Java is concurrent, class-based and object-oriented.

Concurrency


With computer systems we often refer to them as multitasking, but we never really realize the meaning of that, we just expect multiple services to run at the same time and no thought goes in to it. While this sounds great for the user, it might sometimes be a headache to developers. Whenever tasks occur in the same time frame we call that concurrent. Software that can do multiple tasks at the same time is referred to as concurrent software.

Concurrency explained

You can read more about concurrency in Erlang by clicking here.

Concurrency issue and interference


Mutex and Solutions


One solution to the interference issue identified would be Mutual Exclusion (Mutex), known as a programming semaphore [4]. Mutex serializes access to a shared resource meaning only one process has access at a time, creating a lock that is set when one process is accessing the file and making changes then releases that lock when the process is done and then allows another process to access the file. 
Concurrency solution: Mutex

The implementation of Mutex to this scenario would prevent the processes interfering as each process will be able to read and write to the file separately. 

Mutex in Erlang

The mutex module has 6 functions that allow the mutex to run when complied and used when running multiple processes to read and write to a file.

Erlang Mutex solution

The start function spawns a PID with the mutex at a free state, where it then awaits a signal from a process which is trying to acquire a lock and activates the busy state, throwing a wait to any other process that communicates with it while its busy.

Mutex in Java

In Java, Mutex is included in the java.util.concurrent library. Including this library allows us to use the mutex semaphore to handle limited access to shared resources. The method semaphore.acquire() is called when a thread wants to access the shared resource.

Java Mutex solution

It then calls semaphore.release() once the thread is done with the resource.


Counting Semaphore

Similar to a Mutex, a counting semaphore [5] controls the access of multiple shared resources. However, with a mutex it only works on a single resource. With the meeting room scenario mentioned previously, we can relate a counting semaphore by picturing three meeting rooms. For each meeting room that is busy, the semaphore increments by one, moving up. 

Counting Semaphore explained

Alternatively, for each meeting room that is then free the count decrements by one and moves down. This means for others who want to use the meeting room must wait until at least one of the rooms are free. This allows control for the multiple resources instead of everyone trying to access at the same time. In this particular instance, a counting semaphore is not needed as we have two clients accessing only one shared resource.

Counting Semaphore solution: Erlang (left) and Java (right)

Monitor

Another solution involves a piece of code called a Monitor. A monitor is a software module that encapsulates:
  • shared data
  • set of procedures
  • synchronization rules
Monitor explained

Provides mutual exclusion between all procedures for the shared data. Only one process can be executed in the monitor at a time. A monitor protects data from unstructured access. If you can only access it through the right procedures, you can only access it in the right way and you won't have any synchronization problems.

Monitor solution: Erlang (right) and Java (left)

Deadlock and Evaluation


In 1971, Edsger Dijkstra wrote a paper called “Hierarchical ordering of sequential processes” where he introduces a scenario called “Five dining philosophers” [6] which relates to the term “Deadlock”. Deadlock happens when an actor requires a resource which is being used by another which is also waiting for a third and so on. This creates an infinite loop of actors waiting for a resource to be released. However, there has been no set order of how this should be executed so that deadlock does not happen. 

The five dining philosophers' scenario

In the five dining philosophers’ scenario, there are five philosophers sitting around a table sharing a bowl of spaghetti. To eat the spaghetti, they must use both the fork at their right and at their left. If all the philosophers want to eat at the same time, this can create a deadlock. 

They may all pick the fork to their left first and when they look to pick the fork at their right there is no fork free. This will cause them all to be stuck in a situation where they all want to eat but none of them can, only when the fork to the right of a philosopher is free they can pick it up to eat yet this deadlock state creates an infinite loop of waiting. 

In order to correct this and to stop deadlock from happening, they could have two philosophers who are sitting opposite each other to eat at a time and then move onto the next pair. This way each philosopher would be able to eventually eat some spaghetti and there would be no deadlock. 

Conclusion


Concurrency has come across to be an important factor when running tasks simultaneously, this journal has been put together to discuss the problems that would occur with concurrent access to shared resources when two users are reading and writing a shared file.


This project was done in a group (ERGANG) with Timofei Vacula, Mehran Joya and Joshua Simmons.

References


"Dropbox (service)", En.wikipedia.org. [Online]. Available: https://en.wikipedia.org/wiki/Dropbox_(service). [Accessed: 11- Feb- 2018]. 

"Dropbox | Company Info",Dropbox.com. [Online]. Available: https://www.dropbox.com/news/company-info. [Accessed: 11- Feb- 2018]. 

Concurrent Programming in ERLANG, 2nd ed. New Jersey: PRENTICE HALL, pp. 8-13. 

J. Magee and J. Kramer, Concurrency, 2nd ed. Chichester: Wiley, 2007, pp. 63-77. 

J. Magee and J. Kramer, Concurrency, 2nd ed. Chichester: Wiley, 2007, pp. 86-89. 

E. Dijkstra, “Hierarchical ordering of sequential processes”, Acta Informatica, vol. 1, no. 2, pp. 115-138, 1971. 

"Concurrency", En.wikipedia.org. [Online]. Available: https://en.wikipedia.org/wiki/Concurrency_(computer_science). [Accessed: 16- Feb- 2018]. 

"Dining philosophers problem", En.wikipedia.org. [Online]. Available: https://en.wikipedia.org/wiki/Dining_philosophers_problem. [Accessed: 17- Feb 

https://www.oracle.com/java/

"Java Thread – Mutex and Semaphore example" [Online]. Available: https://www.mkyong.com/java/java-thread-mutex-and-semaphoreexample/ [Accessed: 13- Apr- 2018].

"Lesson: Concurrency"[Online]. Available: https://docs.oracle.com/javase/tutorial/essential/concurrency/

"Errors and Processes | Learn You Some Erlang for Great Good!", Learnyousomeerlang.com, 2018. [Online]. Available: http://learnyousomeerlang.com/errors-and-processes. [Accessed: 12- Apr- 2018].

"Lecture 3, Unit 1: Introduction to condition variables and monitors", YouTube, 2018. [Online]. Available: https://www.youtube.com/watch?v=xCNhhwVQ4Y8. [Accessed: 12- Apr- 2018].

0 comments:

Post a Comment




 
Loading...