diff --git a/.gitignore b/.gitignore index c0c7e60095ef8097b15511473bccc7002526c27e..77d80bb67394ece9f93940da255bd856105e1425 100644 --- a/.gitignore +++ b/.gitignore @@ -4,11 +4,11 @@ # Do not track local environment settings. -python/.idea/ +.idea/ +.vs/ +.vscode/ python/.env/ python/.venv/ -python/.vs/ -python/.vscode/ python/env/ python/venv/ diff --git a/readme.md b/readme.md index 050659675b95c3a1c93a7a41ab575898fa330294..6f5375c6a7b415a71afe0c7978602c1c89807933 100644 --- a/readme.md +++ b/readme.md @@ -1,9 +1,21 @@ -# Python - Peterson's Algorithm +# Python & C - Peterson's Algorithm ## Description An implementation of the multi-process/multi-threading algorithm called "Peterson's Algorithm". +For comparison, I have also implemented a C version that runs essentially the same (minus multi-processing. C version +only does multi-threading). + + +## C Environment. +Compile by changing to the `c` directory and running `make all`. +Run program with `./main`. + +For the most part, the C version handles identically to the Python version (which is described below), except that +there is no option to run as multi-processing. Also, since C runs faster, the C version counts to a higher number, +per thread. + ## Python Environment This project is tested using Python3.7. @@ -21,25 +33,27 @@ Once created, load this environment with `source .venv/bin/activate` on Linux or ## Running the Program -Run the program via `python ./main.py` while at the project root. +Run the program via `python ./main.py` while at the `python` directory. ### Expected Output There are four possible options when running this project. -The project can run as either `multi-processing` or `multi-threading`. In the context of this program, they both run -identically, and even use the same code, other than the single line where the process/thread pool is created. +The Python version can run as either `multi-processing` or `multi-threading`. In the context of this program, they both +run identically, and even use the same code, other than the single line where the process/thread pool is created. Then, for each of those, there is a `safe` and `unsafe` version. The "safe" versions use Peterson's Algorithm to create critical sections that are thread safe. The "unsafe" versions use no form of locking, and naively attempt to access/update shared variables. +If a `safe` version is called, then you will also be prompted to loop the program indefinitely or just once. + In any of the four possible cases, one process/thread will call function "A" and the other will call function "B". Both function A and B will try to 1 to a shared counter, and do so 100 times. -The expected output of this program is that each process/thread will add 100 total to the counter. But the "unsafe" -functions will do so in a way that's unlikely to count properly to the expected total of 200. +The expected output of this program is that each process/thread will add 10000 total to the counter. But the "unsafe" +functions will do so in a way that's unlikely to count properly to the expected total of 20000. -#### Intersting Notes +#### Interesting Notes Oddly enough, at least on both my personal desktop and laptop, I noticed a quirk with this code. As stated, both "multi-processing" and "multi-threading" use identical code, except for the single line where the