Monday 25 May 2015

MCS locks in Linux kernel


This lock added in kernel from kernel version 3.15 and 3.18. It is fair version of spinlock where each CPU tries to acquire the lock spinning on local variable.

MCS lock is defined as
struct mcs_spinlock {
         struct mcs_spinlock *next;
         int locked; /* 1 if lock acquired */
    };


To acquire lock:-

void mcs_spin_lock(struct mcs_spinlock **global_queue, struct mcs_spinlock *node);

Arg1: global queue one for each type of spinlock.
Arg2: This is defined per CPU of same type as Arg1 , will be added in queue.

This function tries to acquire spinlock by performing "unconditional" atomic exchange operation to store its own spinlock struct address in next field of global spinlock struct (main lock).

global spinlock struct's next points tail of queue of the waiting CPUs.

global spinlock struct's next points to null if no one is holder, its previous
value (null) in this case.

If global spinlock struct's next is not null it means it is already acquired.
it points to last member in queue, so CPU will save its own struct address in that member's next as well. Now it will spin on local variable (locked) till its not zero.

To release lock:

void mcs_spin_unlock(struct mcs_spinlock **global, struct mcs_spinlock *node)

When thread (CPU) finishes with the lock, it will do a compare-and-swap operation on the main lock (global), trying to set the next pointer to NULL on the assumption that this pointer still points to its own structure.

If that operation succeeds, the lock was never contended and the job is done.

If some other CPU has changed by some other thread (CPU), the compare-and-swap will fail. In that case, thread will not change the main lock at all;
instead, it will change the locked value to "0" in lock structure pointed by its next filed.


MCS lock v/s Spinlock

  • MCS lock maintains FIFO order while acquiring lock, spinlock does not maintain FIFO order of acquiring lock. So whoever comes first to acquire the lock will get first.
  • In spinlock every process waits on single variable, MCS lock reduces waiting order from n to 1 by maintaining per CPU local variable and spin on it.
  • Every attempt to acquire a spinlock requires moving the cache line containing that lock to the local CPU, hence lot of cache bouncing happens, this issue is avoided in MCS lock.


References:
http://lwn.net/Articles/590243/

Defined in linux/3.18.1/kernel/locking/mcs_spinlock.h

Friday 22 May 2015

Hard links and Soft links


Soft link (symbolic link / symlink):

It is similar to “shortcuts” in windows operating system. Removing the original file does not remove the attached symbolic link or symlink, but without the original file, the symlink is useless      

To create a soft link already existing file:-
#ln –s <target file> <new file (name of symlink)>

It simply contains the pointer to the location of the destination file. In more technical words, in soft link, a new file is created with a new inode, which have the pointer to the inode location of the original file. It's a special file type whose data part carries a path to another file.

Usage:

  • If you want to link directories, then you must be using Soft links, as you can’t create a hard link to a directory.
  • If you want to link files across the filesystems, e.g EXT4, NFS. you can only use symlinks/soft links. 

Hard link:

It is simply giving one more name or label to existing file.

In technical words, only an entry into directory structure is created for the file with its inode number, but it points to the inode location of the original file. This means there is no new inode creation in the hard link. Inode of file maintains count of links pointing to this file, when count becomes zero, inode is free to deallocate.

To create hard link to existing file:-
#ln <target file> <hardlink name>

If you try #ls -il  , you will see that both files have same inode number ,  files have the same file permissions and the same size. Because that size is reported for the same inode, we can see that a hard link does not occupy any extra space on your space
  •   '..' is a hardlink to the file that implements the immediate parent of the current directory.
  • If you move the source file to some other location on the same filesystem, the hard link will still work, but soft link will fail.
  • Hard links takes very small amount of space, as there are no new inodes created while creating hard links.
  • If you want to make sure safety of your data, you should be using hard link, as in hard link, the data is safe, until all the links to the files are deleted, instead of that in soft link, you will lose the data if the master instance of the file is deleted.

limitation :
  • Hard link can only refer to data that exists on the same file system.
  • Hard link does not work on directory. 



Thursday 21 May 2015

Bash script variables

Below is the list standard variables can be used in bash script.

  • $$ = Expands to the process ID of the shell
  • $0 = Name of bash script file.
  • $# = Number of arguments passed to bash script 
  • $1 = Argument 1
  • $2 = Argument 2
  • $@ = expands to the positional parameters, starting from one. i.e. "$@" is equivalent to "$1" "$2" …
  • $? = Exit status of mostly recently run process