Wednesday, December 21, 2016

Linux Containers
Today we discuss the Linux Containers. These form isolated environments within the operating system. As we know Linux itself is a modular design with layered components where every component can be replaced with another and the whole operating system can be rebuilt.
The standard layers including the operating system are:

Users
Utility Programs
System Library
Linux OS
Hardware

The interfaces between users and user programs is User Interface.
The interface between utility programs and system library is library interface
The interface between system library and Linux is system call interface.

The LXC hosts more than one containers by being a layer over the OS.  Since the OS can be over real or virtual hardware, we can have the whole stack nested as we nest containers.

A network bridge is established on the host operating system and the containers are usually NAT'ed. This means the containers are not directly reachable from the outside network. With DHCP, they get assigned an ip address so addressing is not a problem but to facilitate inbound and outbound connections, the gateway must be specified in routing table of the containers.  Outbound connections are facilitated with the gateway. If the bridge was established over the original ethernet interface of the host, the inbound connectivity is also established. This way the containers can be accessed by ssh.

Often containers are required to be cut with some customization scripts. These include provisioning a sudo user or enabling ssh access for public keys. All of these can be run as shell commands targeting the specific container from the host with something like:
lxc exec <container_name> -- command


#codingexercise
Find the largest sub matrix rectangle of 1's in a two dimensional binary matrix.
Here's the alternative version to the solution yesterday.
int GetMax(int[,] A, int R, int C)
{
int max = int_min;
for (int i = 0; i < R; i++)
   for (int j = 0; j < C; j++){
        area = AreaWithTopLeft(A,i,j, R, C);
        if (area > max)
             max = area;
     }
return max;
}
int AreaWithTopLeft(int[,] A, int i, int j, int R, int C)
{
int max = 0;
int prev = int_max;
if (A[i,j] != 1) return 0;
for (int x = i; x < R; x++){
   int count = 0;
   for (int y = j; y < C; y++)
   {
      if (A[i,j]  == 1) count++;
   }
   if (count == 0) return max;
   int newmax = min(prev, count) * (x-i+1))
   if (newmax < max)
       return max;
    max = newmax;
   prev = count;
}
return max;
}
we can also optimize the above bailing early so that we dont have to iterate when there are no ones.

No comments:

Post a Comment