Man-in-the-middle attack on Diffie-Hellman with shared public keys? Is it correct to say if the public key is known to all users beforehand,  Diffie-Hellman key exchange is not vulnerable to man in the middle attack? 

Sample key exchange ( assuming all users public key is known ) :

\begin{align}
  A \rightarrow B &amp;: Y_b^{N_a} \\
  B  \rightarrow A &amp;: Y_a^{N_b}  \\
Shared Key  &amp;= g^{N_a+N_b}
\end{align}

Where $N_a$ is random int generated by $A$ 
and $Y_a$ = $g^{S_a}$ where $S$ is the secret key generated by $A$.
Yes, for sure, you can perform what is called ephemeral-static Diffie-Hellman or static-static Diffie-Hellman where one or two parties are authenticated respectively.

Multiple schemes have been described in NIST SP 56A: Recommendation for Pair-Wise Key Establishment Schemes Using Discrete Logarithm Cryptography, sections 6.2 and 6.3.

You would of course need an additional (random) nonce if you use two static public/private key pairs. Otherwise there are no variable values in the equation and you would always generate the same session keys.

If you want to have a view of ephemeral-static Diffie Hellman then you don't have to look further than TLS 1.2, any ciphersuite that starts with DH or ECDH (without the E after it). They are not used much, generally ephemeral-ephemeral Diffie-Hellman is used with a separate authentication using RSA or ECDSA signatures. Certificates with a DH or ECDH public key are very rare indeed.

Key pair generation creates both the public and private keys and takes place on the machine of the owner of the key pair. Generally the private key should not leave this machine, except for backup purposes.

I'm not sure your sample algorithm complies with DH as specified in this standard document.