Monday, June 29, 2020

Linux File System

A file system is a process that manages how and where data on a storage disk is stored, accessed and managed.

In a UNIX/Linux type OS, "Everything is a file", which means that everything in the computer system from processes, files, directories, sockets, pipes, ... is represented by a file descriptor abstracted over the virtual file-system layer in the kernel. The virtual file system is an interface provided by the kernel. Hence the more accurate phrase is "Everything is a file descriptor", or to make it even more accurate, Linus Torvalds himself corrected it again as :"Everything is a stream of bytes".

Linux main directories list:

/(root file system)
The root file system is the top level directory of the file system. It must contain all of the files required to boot the Linux system before other file systems are mounted. It must include all of the required executables and libraries required to boot the remaining file systems. After the system is booted, all other file system are mounted on standard, well-defined mount points as sub-directories of the root file system.

/bin
Contains User executable files.

/boot
Contains the static boot loader and kernel executable and configuration files required to boot a Linux computer.

/dev
This abstract directory contains the device files for every hardware device attached to the system. These are not device drivers, rather they are files that represent each device on the computer and facilitate access to those devices.

/proc
Another abstracted directory which is created when the system boots. Contains information about the processed on your system.

/etc
Contains the local system configuration files for the host computer.

/home
Home directory storage for user files. Each user has a sub-directory in /home.

/lib
Contains shared library files that are required to boot the system.

/media
A place to mount external removable media devices such as USB thumb drives that may be connected to the host.

/mnt
A temporary mount point for regular file systems (as in not removable media) that can be used while the administrator is repairing or working on a file system.

/opt
Optional files such as vendor supplied application programs should be located here.

/root
This is not the root (/) file system. It is the home directory for the root user.

/sbin
System binary files. These are executables used for system administration.

/tmp
Temporary directory. Used by the operating system and many programs to store temporary files., Users may also store files here temporarily. Not that files stored here may be deleted at any time without prior notice.

/usr
These are shareable, read-only files, including executable binaries and libraries, man files, and other types of documentation.

/var
Variable data files are stored here. This can include things like log files, MySQL, and other database files, web server data files, email inboxes, and much more.


Sunday, June 28, 2020

C++ concepts

Vector




Vectors are sequence containers representing arrays that can change in size. Vectors in C++ are preferable when managing ever-changing data elements.

Example: 
vector PersonalId (1000);

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.

Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).

Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

Compared to the other dynamic sequence containers (dequeslists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.


Virtual

A virtual function is a member function in the base class that you expect to redefine in derived classes.

The goal of object-oriented programming is to divide a complex problem into small sets. This helps understand and work with problem in an efficient way. Sometimes, it's desirable to use inheritance just for the case of better visualization of the problem. In C++, you can create an abstract class that cannot be instantiated (you cannot create object of that class). However, you can derive a class from it and instantiate object of the derived class. Abstract classes are the base class which cannot be instantiated.

A virtual function whose declaration ends with =0 is called a pure virtual function.
Any C++ class with at least one pure virtual function is considered to be an abstract class.

Function Template

Function templates are functions that serve as a pattern for creating other similar functions. The basic idea behind function templates is to create a function without having to specify the exact type(s) of some or all of the variables. Instead, we define the function using placeholder types, called template type parameters. Once we have created a function using these placeholder types, we have effectively created a “function stencil”.

When you call a template function, the compiler “stencils” out a copy of the template, replacing the placeholder types with the actual variable types from the parameters in your function call! Using this methodology, the compiler can create multiple “flavors” of a function from one template! 

Example:

template
T add(T num1, T num2)
{
   return (num1 + num2);
}

int main() {

    int result1;
    double result2;
    // calling with int parameters
    result1 = add(2, 3);
    cout << result1 << endl;

    // calling with double parameters
    result2 = add(2.2, 3.3);
    cout << result2 << endl;

    return 0;
}