Friday 29 January 2016

Learning Java and C++

JAVA:
--------

Video tutorials:
https://javabrains.io/


Exercise for Java:
http://codingbat.com/
https://projecteuler.net/

Advanced Java video tutorials:
https://www.udemy.com/advanced-java-programming/




C++:
------
Complete C++ basics video tutorials:
https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83

Only threads:
https://www.youtube.com/playlist?list=PL1835A90FC78FF8BE

Wednesday 27 January 2016

Where do we use extern keyword in C++ ?

We have two files, foo.c and bar.c.
Here is foo.c
#include <stdio.h>

volatile unsigned int stop_now = 0;
extern void bar_function(void);

int main(void)
{
  while (1) {
     bar_function();
     stop_now = 1;
  }
  return 0;
}
Now, here is bar.c
#include <stdio.h>

extern volatile unsigned int stop_now;

void bar_function(void)
{
   while (! stop_now) {
      printf("Hello, world!\n");
      sleep(30);
   }
}
As you can see, we have no shared header between foo.c and bar.c , however bar.c needs something declared in foo.c when it's linked, and foo.c needs a function from bar.c when it's linked.
By using 'extern', you are telling the compiler that whatever follows it will be found (non-static) at link time, don't reserve anything for it since it will be encountered later.
It's very useful if you need to share some global between modules and don't want to put / initialize it in a header.
Technically, every function in a library public header is 'extern', however labeling them as such has very little to no benefit, depending on the compiler. Most compilers can figure that out on their own. As you see, those functions are actually defined somewhere else.
In the above example, main() would print hello world only once, but continue to enter bar_function(). Also note, bar_function() is not going to return in this example (since it's just a simple example). Just imagine stop_now being modified when a signal is serviced if this doesn't seem practical enough.
Externs are very useful for things like signal handlers, a mutex that you don't want to put in a header or structure, etc. Most compilers will optimize to ensure that they don't reserve any memory for external objects, since they know they'll be reserving it in the module where the object is defined. However, again, there's little point in specifying it with modern compilers when prototyping public functions.

Tuesday 26 January 2016

Why do we use volatile keyword in C++

Consider this code,
int some_int = 100;

while(some_int == 100)
{
   //your code
}
When this program gets compiled, the compiler may optimize this code, if it finds that the programnever ever makes any attempt to change the value of some_int, so it may be tempted to optimize thewhile loop by changing it from while(some_int == 100) to simply while(true) so that the execution could be fast (since the condition in while loop appears to be true always). (if the compiler doesn't optimize it, then it has to fetch the value of some_int (if it's not loaded on a register) and compare it with 100, each time which obviously is a little bit slow.)
However, sometimes, optimization (of some parts of your program) may be undesirable, because it may be that someone else is changing the value of some_int from outside the program which compiler is not aware of, since it can't see it; but it's how you've designed it. In that case, compiler's optimization would not produce the desired result!
So, to ensure the desired result, you need to somehow stop the compiler from optimizing the whileloop. That is where the volatile keyword plays it's role. All you need to do is this,
volatile int some_int = 100; //note the 'volatile' qualifier now!

In others words I would explain this as follows:
volatile tells the compiler that,
"Hey compiler, I'm volatile and, you know, I can be changed by some XYZ that you're not even aware of. That XYZ could be anything. Maybe some alien outside this planet called program. Maybe some lighting, some form of interrupt, volcanoes, etc can mutate me. Maybe. You never know who is going to change me! So O you ignorant, stop playing an all-knowing god, and don't dare touch the code where I'm present. Okay?"
Well, that is how volatile prevents compiler from optimizing code. Now google it to see some sample examples.

Quoting from the C++ Standard ($7.1.5.1/8)
[..] volatile is a hint to the implementation to avoid aggressive optimization involving the objectbecause the value of the object might be changed by means undetectable by an implementation.[...]

vptr and vtable - runtime polymorphism

C++ compiler creates a hidden class member called virtual-pointer or in  short vptr when there are one or more virtual functions. This vptr is a  pointer that points to a table of function pointers. This table is also  created by compiler and called virtual function table or vtable. Each  row of the vtable is a function pointer pointing to a corresponding  virtual function.
To accomplish late binding, the compiler creates this vtable table for  each class that contains virtual functions and for the class derived  from it. The compiler places the addresses of the virtual functions for  that particular class in ‘vtable’.
When virtual function call is made through a base-class pointer, the  compiler quietly inserts code to fetch the VPTR and look up the function  address in the VTABLE, thus calling the right function and causing  late/dynamic binding to take place.


class base
 {   virtual void funct1(void);
 virtual void funct2(void);
}; base b;

b.vptr = address of b.vtable
b.vtable[0]= &base::funct1()
b.vtable[1]= &base::funct2()

#include <> vs #include ""

#include <stdio.h>
When the include file is in brackets the preprocessor,
  1. first searches in paths specified via the -I flag. 
  2. Then it searches the standard include paths (see the above link, and use the -v flag to test on your system).

#include "myFile.h"
When the include file is in quotes the preprocessor,
  1. first searches in the current directory, 
  2. then paths specified by -iquote
  3. then -I paths, 
  4. then the standard paths.

Monday 25 January 2016

Inter-process communication approaches

MethodShort DescriptionProvided by (operating systems or other environments)
FileA record stored on disk, or a record synthesized on demand by a file server, which can be accessed by multiple processes.Most operating systems
Signal; alsoAsynchronous System TrapA system message sent from one process to another, not usually used to transfer data but instead used to remotely command the partnered process.Most operating systems
SocketA data stream sent over a network interface, either to a different process on the same computer or to another computer on the network. Typically byte-oriented, sockets rarely preserve message boundaries. Data written through a socket requires formatting to preserve message boundaries.Most operating systems
Message queueA data stream similar to a socket, but which usually preserves message boundaries. Typically implemented by the operating system, they allow multiple processes to read and write to the message queue without being directly connected to each other.Most operating systems
PipeA two-way data stream between two processes interfaced through standard input and output and read in one character at a time.All POSIX systems, Windows
Named pipeA pipe implemented through a file on the file system instead of standard input and output. Multiple processes can read and write to the file as a buffer for IPC data.All POSIX systems, Windows, AmigaOS 2.0+
SemaphoreA simple structure that synchronizes multiple processes acting on shared resources.All POSIX systems, Windows, AmigaOS
Shared memoryMultiple processes are given access to the same block of memory which creates a shared buffer for the processes to communicate with each other.All POSIX systems, Windows
Message passingAllows multiple programs to communicate using message queues and/or non-OS managed channels, commonly used in concurrency models.Used in MPI paradigm, Java RMICORBADDSMSMQ,MailSlotsQNX, others
Memory-mapped fileA file mapped to RAM and can be modified by changing memory addresses directly instead of outputting to a stream. This shares the same benefits as a standard file.All POSIX systems, Windows

INSTALLING MYSQL ON CENTOS WITHOUT ROOT ACCOUNT:


1. Download MySQL Community Server 5.5.8 Linux - Generic Compressed TAR Archive

2. Unpack it. For example to: /home/martin/mysql

3. Create my.cnf file in your home directory. The file contents should be:
-----
[server]
user=martin
basedir=/home/martin/mysql
datadir=/home/martin/sql_data
socket=/home/martin/socket
port=3666
-----

4. Go to the /home/martin/mysql directory and execute:
-----
./scripts/mysql_install_db --defaults-file=~/my.cnf --user=martin --basedir=/home/martin/mysql --datadir=/home/martin/sql_data --socket=/home/martin/socket
-----
Note that --defaults-file MUST be the first parameter, otherwise it won't work! It's a MySQL bug.

5. Your MySQL server is ready. Start it with this command:
-----
./bin/mysqld_safe --defaults-file=~/my.cnf &
-----

6. Connecting to server:
-----
mysql -h 127.0.0.1 -P 3666 -u root -p (using tcp)
or
mysql --socket=/home/martin/socket -u root -p (using unix socket)
-----

7. Shutting down server:
-----
mysqladmin -h 127.0.0.1 -P 3666 -u root shutdown(using tcp)
or
mysqladmin --socket=/home/martin/socket -u root shutdown (using unix socket)
-----


Source: http://superuser.com/questions/209203/how-can-i-install-mysql-on-centos-without-being-root-su