An in-depth review of blockchain — cryptography and metaverse (part two- concurrency-part two)

metamondial
4 min readSep 18, 2022

In the previous part, I explained a lot of theoretical topics, but I still need to explain more about the concurrence , and after that I can go to the topics related to blockchain and Web 3.

Introduction to the Too Much Milk Problem

- Alice and Bob always want milk in the fridge.

- However, the two do not coordinate well, so that too much milk always ends up in the fridge.

To solve the too much milk problem, Alice and Bob decide to lock the fridge. There is only one key to open the lock, the one who gets the key can check the fridge. That is to say, if Alice gets the key and checks fridge, Bob can not open the fridge until Alice releases the key. Alice will releases the key until she ensures there is milk in fridge. In Alice _sol.c and Bobs_sol.c, i use semaphore to implement the key. Read the following programs and run them to see what will happen.

Note that programs using the POSIX semaphores API must be compiled with -pthread to link against the real-time library. So you need compile these two programs like this:

/*Alice_sol.c*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
int main(int argc, char * argv[]) {
int fd;
int VALUE=1;
sem_t * mutex;
//If semaphore with name "mutex" does not exist, then create it with VALUE
mutex = sem_open("mutex", O_CREAT, 0666, VALUE);
printf("Mom comes home.\n");
//wait on semaphore "mutex" and decrease it by 1
sem_wait(mutex);
printf("Mom checks the fridge.\n");
//If file "fridge" does not exist, then create it, you can see manual pages for details
fd=open("fridge", O_CREAT|O_RDWR|O_APPEND, 0777);
//"fridge" is empty
if(lseek(fd,0,SEEK_END)==0){
printf("Mom goes to buy milk...\n");
sleep(2);
write(fd,"milk ",5);
printf("Mom puts milk in fridge and leaves.\n");
//there is milk in "fridge" already
if(lseek(fd,0,SEEK_END)>5)
printf("What a waste of food! The fridge can not hold so much milk!\n");
}else{
printf("Mom closes the fridge and leaves.\n");
}
//close file "fridge"
close(fd);
//add semaphore "mutex" by 1
sem_post(mutex);
//Before exit, you need to close semaphore and unlink it, when all processes have
//finished using the semaphore, it can be removed from the system using sem_unlink
sem_close(mutex);
sem_unlink("mutex");
return 0;
}
/*Bob_sol.c*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
int main(int argc, char * argv[]) {
int fd;
int VALUE=1;
sem_t * mutex;
mutex = sem_open("mutex", O_CREAT, 0666, VALUE);
printf("Dad comes home.\n");
sleep(rand()%2+1);
sem_wait(mutex);
printf("Dad checks the fridge.\n");
fd=open("fridge", O_CREAT|O_RDWR|O_APPEND, 0777);
if(lseek(fd,0,SEEK_END)==0){
printf("Dad goes to buy milk...\n");
sleep(rand()%2+1);
write(fd,"milk ",5);
printf("Dad puts milk in fridge and leaves.\n");
if(lseek(fd,0,SEEK_END)>5)
printf("What a waste of food! The fridge can not hold so much milk!\n");
}else{
printf("Dad closes the fridge and leaves.\n");
}
close(fd);
sem_post(mutex);
sem_close(mutex);
sem_unlink("mutex");
return 0;
}
/

I have taken this image and Zira example from calvinkam.github.io

Correctness properties

- There must not be more than one milk carton in the fridge, i.e. no more than one person can go shopping for milk. (SAFETY)

- Alice or Bob go to buy milk when there is no more milk in the fridge. (LIVENESS)

A compliant program must meet standards. The program must function correctly, ie it must Not incorrectly execute our request, apart from the fact that the algorithm must be written correctly, the concurrent Synchronization process must be designed so that the program does not malfunction, now you might be wondering, what does this have to do with blockchain, bitcoin, etc.?

Suppose the designed programs and the smart contracts that work on the blockchain platform are implemented in such a way that their coordination is not accurate? What happens then? It will be a disaster, usually, in the lower layers of Ethereum or other blockchains, many standards are followed and the program is written in such a way that the execution of commands is carried out correctly, even more standards are adhered to as the programs run in a fully decentralized manner, so all competing standards must be carefully programmed either in the blockchain’s platform or in the virtual machine’s platform like the Ethereum virtual machine.

Concurrency and parallelism increases processing speed, and the actual model of parallel processing in a mining pool is much more complex than these simple examples, as multiple processors need to run a program that executes specific instructions or a subset of specific instructions, because of this, programming mining pools is a very difficult task and the speed sometimes changes with optimal or non-optimal algorithms, and it definitely does not need to be explained that the higher the speed, the higher the chance of mining.

Summary

Simultaneous computing is a type of computing in which multiple computations are performed simultaneously (i.e. in shared time slots) — rather than sequentially (in which case the first must be completed before the second can start).

Concurrent computing is a type of computing in which multiple calculations are performed simultaneously — over a period of time — over time — rather than sequentially, with one completing before the next begins.

It is the property of a system — be it a program, a computer, or a network — in which each process has a separate execution point, or “critical section”. A concurrent system is one in which computations can continue without waiting for all other computations to complete. Concurrent computing is a form of modular programming. In their pattern, a general computation is broken down into subsets that can be performed concurrently. Mavar is one of the pioneers in the field of simultaneous computing.

Well, we’re just getting into the topics related to the main topic of the article and from here the topics get more entertaining

Continued in the next article

--

--

metamondial
metamondial

Written by metamondial

Mohammadreza Monshizadeh METAMONDIAL Co-Founder

No responses yet