Mutex vs Semaphore

A simple yet clear analogy on the difference between mutex and semaphore

Mutex:

Is a key to a toilet. One person can have the key – occupy the toilet – at the time. When finished, the person gives (frees) the key to the next person in the queue.

Officially: “Mutexes are typically used to serialise access to a section of  re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section.”

(A mutex is really a semaphore with value 1.)

Semaphore:

Is the number of free identical toilet keys. Example, say we have four toilets with identical locks and keys. The semaphore count – the count of keys – is set to 4 at beginning (all four toilets are free), then the count value is decremented as people are coming in. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.

Officially: “A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore).”

Now you got it.

Generate key pair with OpenSSL

I was playing around with OpenSSL again and here is some (hopefully) simple take to get familiarize with it’s capability.

Generate key pair and getting it’s modulus and exponent

Here are the openssl commands that operate to generate key pair and get the information about the generated key

  • Generate key pair
    openssl genrsa -aes256 -out private.pem 2048
  • Get public key
    openssl rsa -in private.pem -pubout -out public.pem
  • Get private key (warning!)
    openssl rsa -in private.pem -out private_plain.pem
    the only difference with the above option is the -pubout option dropped
  • Get the modulus from private.pem
    openssl rsa -in private.pem -modulus -noout
  • Get the modulus from public.pem
    openssl rsa -pubin -in public.pem -modulus -noout
  • Get modulus and exponent from public.pem
    openssl rsa -pubin -in public.pem -text -noout

All commands above that operating on private.pem will require you to enter a pass phrase.
Modulus from both private.pem and public.pem are the same (as it should be).

OpenSSL default format is in PEM format, how about operating in DER format? Suppose we want to convert PEM format to DER, and operating with DER format

  • Converting from PEM to DER
    openssl rsa -pubin -inform PEM -in public.pem -outform DER -out public.der
  • Get modulus from pu/blic.der (DER format)
    openssl rsa -pubin -inform DER -in pub -modulus -noout

 

Print out x509 certificate information

Now that we know how to operate with different format via -inform, and we know how to print out info via -text. We generate a self sign certificate, mycert, https://www.madboa.com/geek/openssl/#how-dnerate-a-self-signed-certificate. Input the necenformation to create a cert.

  • Print certificate information
    openssl x509 -text -inform DER -in mycert.der -noout

 

How to get a getch( )

Bummer, your small c apps compiler doesn’t have a getch().

We can overcome this by installing ncurses or create our own getch() function.

  • ncurses

If you’re using cygwin, we should install ncurses first (it’s not included in cygwin’s default setup). First of all, you have to get libncurses-devel package.

Here is example how to use ncurses

Manual compile: gcc test_ncurses.c -lncurses

CMake example to compile

  • Here is the solution for creating getch() function.

Have fun.

Vim MatchIt and FuzzyFinder

Here is my addition to my .vimrc config file this week.

First part is to load matchit.vim that already part of Vim distribution, it aiding a great help for me to browse through html file, it extend the % vim command.

The second part is a bit tricky for FuzzyFinder plugin user, I want to exclude directory, in this case is “venv” directory, that apparently the regex matching work well with fuf_file_exclude instead of fuf_dir_exclude. You can add you excluded list by extending and following the line #9 from snippet above.

Git cheat sheet

Git: distributed repository is the new centralize.

Common used git command. I keep it brief with no/minimum explanation of each command by hoping that those commands are self explanatory. I also organize by git trifecta of Add/Modify – Branch – Remote workflow.

Add, check and publish. The basic

  1. Starting
    • git init
    • git clone <repository-url>
  2. Adding/making changes
    • git add .
    • git add <path/file.name>
    • git commit -am “”
  3. Checking status
    • git status
    • git log —-all —-graph —-oneline
    • git log branch1..branch2
  4. Getting updates
    • git fetch Fetches latest changes from origin
    • git pull Fetches latest changes from origin and merge
  5. Publishing
    • git push Push changes to origin from current
    • git push <origin> <branch> Push changes to origin from branch

 

Modify, fixing mistakes

  1. Revert commits
    • git revert HEAD
    • git revert <commit-ref>
  2. Revert/undo all changes
    • git checkout - <file>
  3. Fix last commits
    • git commit –amend [-m “updated message”]
  4. Reset modifications
    • git reset HEAD <file>
  5. Diff-erence
    • git diff
    • git diff $id1 $id2 Diff between 2 commits
  6. Resolving changes
    • git checkout –ours <path/file>
    • git checkout –theirs <path/file>
  7. Combine commits
    • git rebase -i HEAD~4

 

Branching

  1. Creating branch
    • git branch <branch name>
  2. Listing branch
    • git branch
    • git branch -a
    • git branch -v
  3. Switching branch
    • git checkout <branch-name>
    • git checkout -b <branch-name>
  4. Merging branch
    • git merge <branch-name>
    • git merge --squash <branch-name>
  5. Deleting branch
    • git branch -d <branch-name>

 

Remote

  1. Adding remote
    • git remote <remote-name> <remote-url>
  2. Listing remote
    • git remote -v
  3. Fetching/Pushing to/from remote
    • git fetch <remote-name>
    • git push <remote-name> <branch-name>
  4. Getting update without removing local
    • git stash
    • git pull
    • git stash pop

 

Also, use .gitignore file for ignore files or directories. For further reference for more detailed git usage, my go to is gitref.org.

My .vimrc

My .vimrc, vim config file that I use everyday.

I want to keep it simple by not having too much configuration in vimrc itself. There is a plugin to manage vimrc to make it cleaner and readable, however it will introduce extra configuration.

My idea with this vimrc is to share easily between machine, main machine, or remote compiler machine that we can ssh it. Only one setup is needed to modify by hand, the vundle package manager, that’s it.

Acquiring Knowledge

Once we’re graduate from school we’re expected to learn some and rightfully knowledgeable on certain subject we’re pursuing.

Learning broad subject in schools or books doesn’t mean we’re really understand it. Once my teacher said that the best way to understand is to teach someone the subject that we want to master. Teaching the same subject make her easily solve many problem variants, that’s make sense.
Repetition is key.

We’re living in continuous learning experience, once we thought that we’re mastering something there’ll be something new that come up and have to deal with. I’d be take a step back and recall that knowledge is understood.
I make 2 mental step in the back of my mind to understand a subject:

  1. I need to explain to myself what is it in the nutshell and then go into it’s details.
  2. I should be able to explain and make them understand the subject matter to a 5 graders or my granny.

If I can’t make to the second step, I might not grasp the nutshell or I simply don’t understand it properly. Back to square one. It might take some time but it’s worth the time invested, once we understand it – it won’t simply goes away.