Date Archives August 2016

Hard Link vs. Soft Link

linkbuilding

Hard Link

Hard links are just different names for the same file.

  • One file can have many different names or hard links.
  • Both original file and link file have same Inode number(Index Number).
  • If one is modified, the other one is also modified automatically.
  • Both files’ link number increases to 2.
  • Even though original is deleted, link file works properly.
  • Form: ln originalFile linkFile

Screen Shot 2016-08-24 at 10

Soft Link (Symbolic Link)

Soft links are similar to MS windows shortcut.

  • One file can have many shortcuts pointing to it.
  • Original file and link file have Different Inode number(Index Number).
  • If one is modified, the other one is also modified automatically.
  • Link number doesn’t change.
  • If original is deleted, link file doesn’t work.
  • Form: ln -s originalFile linkFile

Screen Shot 2016-08-24 at 11

 

Linux Hierarchy Structure

flowchart-LinuxDirectoryStructure

/ (Root directory)

The Root Directory is the starting point of the file system hierarchy. Everything in the system, including the other directories and files of the system, are stored in the root directory.

Screen Shot 2016-08-24 at 4.37.12 PM

/bin

The /bin directory contains the binaries and other executable programs. Most of the common Linux commands that you may utilize as a user of the system are stored here. Similar to the /bin directory is the /sbin directory, which contains the essential system administration binaries.

Screen Shot 2016-08-24 at 4.38.26 PM

/boot

The /boot directory contains the static boot loader files needed to boot the operating system. It contains the GRUB boot loader files along with the Linux kernels.

Screen Shot 2016-08-24 at 4.41.05 PM

/dev

The /dev directory contains device files which are typically under the control of the operating system and the system administrators. The terminal devices, disk drives, USB’s etc. are all included in the /dev directory and are exposed as files in Linux, instead of being represented as devices.

Screen Shot 2016-08-24 at 4.43.09 PM

/etc

The /etc directory contains the system configuration files. All files in the /etc directory should be text files and cannot be executable binary files. It contains the /etc/passwd and the /etc/shadow files which contain essential user identification and authentication data.

Screen Shot 2016-08-24 at 4.45.05 PM

/home

The /home directory contains a home directory for each user. Each user’s personal files and user-specific configuration files are stored in their respective home directories. Each user has write access only to their own home directory. One can not modify the files in the home directory of the other user unless they have acquired the permissions to do so.

Screen Shot 2016-08-24 at 4.46.00 PM

/lib

The /lib directory contains the essential system libraries and kernel modules which would be required by the binaries located under the /bin or /sbin directories. Similarly, the /lib64 directory contains libraries required for 64-bit support in systems.

Screen Shot 2016-08-24 at 4.46.32 PM

/media

The /media directory is used to temporary mount removable media like external hard disks, CD-ROM’s etc. Once you attach a removable media to a system, a subdirectory will automatically be created inside the /media directory. You shall be able to access the contents of the device from that subdirectory.

Screen Shot 2016-08-24 at 4.47.45 PM

/opt

The /opt directory contains sub-directories for optional or third-party software. Most proprietary software keep their files here in the /opt directory.

Screen Shot 2016-08-24 at 4.48.41 PM

/proc

The /proc directory contains information about the processes running on your system in directories named /proc/PID where PID is the process id. It also contains information about system resources.

Screen Shot 2016-08-24 at 4.49.35 PM

/tmp

The /tmp directory is used by the system and its applications to store their temporary files. All these files are typically cleared on system reboot.

Screen Shot 2016-08-24 at 4.50.19 PM

/usr

The /usr directory is used to contain the applications and file pertaining to a particular user as compared to the files and applications of the system. For example, the essential binaries and essential system administration binaries are stored in the /bin & /sbin directories while the non-essential ones are stored in /usr/bin & /usr/sbin directories respectively.

Screen Shot 2016-08-24 at 4.50.58 PM

/var

The /var directory is an important part of the Linux Directory Structure & contains variable system files whose data that may grow over time. Log files, packages and database files can be found in the /var directory.

Screen Shot 2016-08-24 at 4.51.36 PM

 

source: http://fossbytes.com/linux-lexicon-linux-directory-structure/

Serialization #2

beans

JavaBeans

JavaBeans are classes that encapsulate many objects into a single object (the bean), and  they are serializable.

  • It provides a default, no-argument constructor.
  • It should be serializable and implement the Serializable interface.
  • It may have a number of properties which can be read or written.
  • It may have a number of “getter” and “setter” methods for the properties.

Example

package com.chang;
import java.io.Serializable;

public class StudentsBean implements Serializable{
   private String firstName = null;
   private String lastName = null;
   private int age = 0;

   public StudentsBean(){
   }

   public String getFirstName(){
      return firstName;
   }

   public String getLastName(){
      return lastName;
   }

   public int getAge(){
      return age;
   }

   public void setFirstName(String firstName){
      this.firstName = firstName;
   }
 
   public void setLastName(String lastName){
      this.lastName = lastName;
   }

   public void setAge(Integer age){
      this.age = age;
   }
}

Serialization #1

Definition

Serialization is a process in which current state of Object will be saved in stream of bytes. As byte stream create is platform neutral hence once objects created in one system can be deserialized in other platform.

This Byte stream can be used for different purpose.

  • Write to Disk
  • Store in Memory
  • Sent byte stream to other platform over network
  • Save byte stream in DB(As BLOB)

Java has already provided out of the box way(java.io.Serializable  Interface) to serialize an Object. If you want any class to be serialized then that class needs to implement this interface.

* Serializable interface is just a marker interface. There is no methods in it.

Serialization-deserialization-in-Java-Object-Streams

Example. Serializable Class

The serialVersionUID is used as a version control in a Serializable class. If you do not explicitly declare a serialVersionUID, JVM will do it for you automatically, based on various aspects of your Serializable class.

package com.chang;
import java.io.Serializable;

public class Employee implements Serializable{
   public String firstName;
   public String lastName;

   // serialVersionUID
   private static final long serialVersionUID=1L;
   
}

Serializable class usually makes files variables as private and interact with them using setter and getter.

Example. Serialization Class

package com.chang;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializationClass{
   public static void main(String[] args){
      Employee emp = new Employee();
      emp.firstName = "Chang";
      emp.lastName = "Park";

      try{
         FileOutputStream fileOut = new FileOutputStream("./employee.txt");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.wirteObject(emp);
         out.close();
         fileOut.close();
         System.out.println("Serialized data is saved in  ./employee.txt file");
      } catch (IOException i){
         i.printStackTrace();
      }
    
   }
}

Example. Deserialization Class

package com.chang;
import java.io.*;

public class DeserializationClass{

   public static void main(String[] args){
      Employee emp = null;
      try{
         FileInputStream fileIn = new FileInputStream("./employee.txt");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         emp = (Employee) in.readObject();
         in.close();
         fileIn.close();
      } catch (IOException i){
         i.printStackTrace();
         return;
      } catch (ClassNotFoundException c){
         System.out.println("Employee class not found");
         c.printStackTrace();
         return;
      }
      System.out.println("First Name of Employee: " + emp.firstName);
      System.out.println("Last Name of Employee: " + emp.lastName);
   }
}

 

Chmod

permissions11

A chmod is used to change permission of files and directories.

  • form:     chmod  [options]  permissions  filename

permissions

can be represented in two ways.

  • Alphabetic combination
  • Numeric combination

 

Alphabetic combination

  • User
    • a: All people
    • u: owner
    • g: group
    • o: others
  • Permission
    • r: read
    • w: write
    • x: execute
  • Example:
    • chmod u=rwx practice.txt : Owner gets all permissions on practice.txt.
    • chmod g-w practice.txt : Group’s write permission on practice.txt is removed.
    • chmod o+rx practice.txt : Others get additional read and execute permissions.
    • chmod a=rwx practice.txt : All people get all permissions.

 

Numeric Combination

chmod-775

  • Permission
    • 4: read
    • 2: write
    • 1: execute
  • Example:
    • chmod 755 practice.txt : owner gets all permission. Group and others get read and execute permission.
    • chmod 777 practice.txt : All people get all the permissions.

 

options

Screen Shot 2016-08-22 at 2.12.06 PM

 

Difference between /bin vs. /sbin

Differences

/bin

  • This directory contains executable programs which are needed in single user mode and to bring the system up or repair it.

/sbin

  • Like /bin, this directory holds commands needed to boot the system, but which are usually not executed by normal users.

/usr/bin

  • This is the primary directory for executable programs. Most programs executed by normal users which are not needed for booting or for repairing the system and which are not installed locally should be placed in this directory.

/usr/local

  • This is where programs which are local to the site typically go.

/usr/local/bin

  • Binaries for programs local to the site.
  • Recommended place to put your own created code.

/usr/local/sbin

  • Locally installed programs for system administration.

 

Tip: By typing man hier on terminal, you will find brief explanation and hierarchy information.

lol

Android Support Library

android-support-library-23-1

The Android Support Library offers a number of features that are not built into the framework.

Support libraries provide a range of different features:

  • Backward-compatible versions of framework components.
  • UI elements to implement the recommended Android layout patterns.
  • Support for different form factors.
  • Miscellaneous utility functions.

 

Backward-compatible

  • Support libraries allow apps running on older versions of the Android platform to support features made available on newer versions of the platform.
  • If the framework provides the necessary functionality, the support library calls on the framework to perform the task.
  • If the app is running on an earlier version of Android, and the framework doesn’t expose the needed functionality, the support library may try to provide the functionality itself, or may act as a no-op.
  • The app does not need to check the system version at run time. The app can rely on the support library class to do the appropriate system checks, and modify its behavior as necessary.

 

Support for General Layout Patterns

  • Support libraries provide user interface elements not offered by the Android framework.
    • ex) DrawerLayout, RecylcerView
  • By using these support library classes, you can avoid having to reinvent the wheel.
  • If your app has a particular user interface requirement, you can draw on existing code, which provides a user interface that users will already be familiar with.
  • These elements also help you build an app that looks and feels like a part of the Android ecosystem.

 

Support for Different Form Factors

  • The Android SDK provides libraries for a number of different form factors, such as TV and wearables.
  • An app can depend on the appropriate support library to provide functionality across a wide range of platform versions, and can provide content on external screens, speakers, and other destination devices.

 

General Utilities

  • The Android Support Library provides backward-compatible utility functions. Apps can use these utility functions to provide an appropriate user experience across a wide range of Android system versions.

    ex) support library permission methods behave appropriately depending on what platform version your app is running on. If the platform supports the runtime permissions model, these methods request the appropriate permission from the user; on platform versions that do not support the runtime permissions model, the methods check whether the appropriate permission was granted at install time.

Direct link to download

https://dl-ssl.google.com/android/repository/support_r[API version].zip

  • Example
    • https://dl-ssl.google.com/android/repository/support_r04.zip
    • https://dl-ssl.google.com/android/repository/support_r19.0.1.zip

Zombie(Defunct) process vs. Orphan process

Differences

zombie

Zombie(Defunct) process

  • On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution but still has an entry in the process table.
  • This entry is still needed to allow the parent process to read its child’s exit status.
  • You cannot kill zombie process directly because it is already dead.
  • To kill the zombie process, you should kill the parent process. However if the parent process is init(which is 1), then only thing you can do is reboot.
  • It takes very tiny memory(description info) but will take process ID which is limited. So, it is better not to have zombie process.

 

orphan

Orphan process

  • An orphan process is a computer process whose parent process has finished or terminated, though it remains running itself.
  • parent process ID is changed to init which is 1.
  • It is possible to find the orphan process and kill by kill -9 pid command.
  • It still takes resources, and having too many orphan process will overload init process. It is better not to have many orphan process.

Alias

An alias is a (usually short) name that the shell translates into another (usually longer) name or command. Aliases allow you to define new commands by substituting a string for the first token of a simple command. They are typically placed in the ~/.bashrc (bash) or ~/.tcshrc (tcsh) startup files so that they are available to interactive subshells. If you type on regular terminal, it will be gone when you close the terminal. If you set frequently using command (which uses pipe and redirection), it will be so productive.


$ alias ll ='ls -al'

 

 

Sandbox

Screen Shot 2016-08-14 at 10.35.18 PM

Android has another layer of protection in that it doesn’t give one app access to the resource of another app. This is known as the ‘sandbox’ where every app gets to play in its own sandbox and can’t use another app’s toys! Android does this by giving each app a unique user id (a UID) and by running that app as a separate process with that UID. Only processes with the same UIDs can share resources which, as each ID is uniquely assigned, means that no other apps have permission.

Screen Shot 2016-08-14 at 10.32.48 PM

“Like all security features, the Application Sandbox is not unbreakable. However, to break out of the Application Sandbox in a properly configured device, one must compromise the security of the the Linux kernel.”  – Google