Sunday, November 8, 2015

light weight process(LWP)

        Light Weight Process is created by Linux system to support better performance in multitasking programming. Two LWP shares common memory space and their context significantly less than the normal processes.
       
        When a child process created it is almost identical to its parent process and inherit all properties from parent process. it also receives a copy of parent address space. Parent and child share same page for code text but they do have their separate copies of stack and heap segment so the changes done by chide is invisible by parent process and vice versa.
       
        In multitasking programming where dependency exists between parent and child process this invisibility may leads to severe problems some time corrupting memory to crashing the entire execution.
       LWPs may share same resource and address space so changes done by one process is immediately noticed by all the process running together. Synchronization among the LWPs is quite possible so corruption of memory can be prevented. POSIX-compliant  pthread library widely used to create LWP and make them synchronized.

pthread related process creation and synchronization is discussed later in this blog.

How to create a Process in Linux System?

To perform a certain task we required an application so it will act on given command and lifetime set by program. Whatever it needs in application but eventually we need a process to perform a task.
There are several ways in Linux where we can create a process through a program otherwise opening a shell creates a process and running command over there creates another process.

But running command and using pre-installed application is already designed to their job. what will do if we have to create a separate process for our own need. Linux provides several system calls to create process, duplicate processes.


clone() fork() and vfork()

clone() is a wrapper function defined in the C library, which set up the
new light weight process(LWP), clone() system call hidden to the programmer. The
sys_clone() service routine that implements the clone() system call
does not have the fn and arg parameters.

    fork() system call creates a new process which is duplicate of its
parent process. The child process creates a new entry in process table with
many of the same attributes as the current process. it is almost identical to
the original process, execute the same code but with its own data space,
environment and file descriptors.
    the fork() system call is implemented by Linux as a clone() system call
whose flag parameter specifies both a SIGCHILD signal and all the clone flag
cleared, and whose child_stack parameter is the current parent stack pointer.
Therefore, the parent and child temporarily share the same User Mode stack. But
by using Copy On Write mechanism, they usually get separate copies of the User
Mode Stack as soon as one tries to change the stack.

    vfork() system call creates a new process which shares memory address space
of its parent. there may a chance of parents and data overlapping during
concureent execution, so need to make an arrangement to block either of one
during concurrent execution.
    vfork() is also implemented as a clone() system call whose flags parameter
specifies both a SIGCHLD signal and flags CLONE_VM and CLONE_VFORK, and whose
child_stack parameter is equal to the current stack pointer.

Application of all system calls

Saturday, November 7, 2015

Process Identifier (PID)

Whenever a process starts one unique positive integer number is assigned to that called Process Identifier(PID). It is ranged between 2 to 32,768. Number 0 and 1 typically reserved for idle process and special init Process. When a new process is started a next unused in sequence number is assigned to that process.
     All current process status and their PID can be seen by using command

# ps ax  
  PID TTY   STAT  TIME COMMAND  
   1 ?    Ss   0:02 /sbin/init  
   2 ?    S   0:00 [kthreadd]  
   3 ?    S   0:18 [ksoftirqd/0]  
   5 ?    S<   0:00 [kworker/0:0H]  
   7 ?    S   0:29 [rcu_sched]  
   .................  
   ................  
   ................  
  2292 ?    Sl   0:08 /usr/lib/upower/upowerd  
  2367 ?    SNl  0:01 /usr/lib/rtkit/rtkit-daemon  
  2410 ?    Sl   0:00 /usr/lib/colord/colord  
  3987 ?    SLl  0:00 /usr/bin/gnome-keyring-daemon --daemonize --login  
  3989 ?    Ss   0:02 init --user  
  4077 ?    Ss   0:19 dbus-daemon --fork --session --address=unix:abstract=/tmp/dbus-7SdHSkpTgx  
  4088 ?    Ss   0:00 upstart-event-bridge  
  4095 ?    Ss   0:00 /usr/lib/x86_64-linux-gnu/hud/window-stack-bridge  
  4106 ?    Sl   0:17 /usr/lib/x86_64-linux-gnu/bamf/bamfdaemon  
  4112 ?    Sl   0:00 /usr/lib/at-spi2-core/at-spi-bus-launcher  

ps command has many option (-ef, -A..)which gives more resolution. To see more option go to manual page of ps command, type
 #man ps  







What is socket in linux?

Socket is a way of communication between two correlated and uncorrelated process a kind of IPC(Inter Process Communication). It provides an interface  between all the network entities and makes data communication possible. Socket can also be used for data transfer between the processes within a system. Socket is client/server based communication that can be created withing system or across the network.
    Socket generally requires only IP address and Port number of destination to send the packet. IP provides connection between two system based on their logical addresses and port number provides mapping between data packer received and its corresponding application.
      Socket has its transport layer (UDP, TCP and Raw IPs) dependency. based on their transport layer requirement socket is categories into two type Stream Socket and Datagram Socket.

All aspects of socket can be easily understood by seeing the socket system call and the prototype is

 int socket(int domain, int type, int protocol);  

And its prototype defined in following header files

 #include <sys/types.h>  
 #include <sys/socket.h>  //Basically this

Socket system calls accept three arguments and return socket descriptor that can be used in further socket communication.

arg1- domian
Domain parameter specify the address family which will used for communication.

AF_UNIX
Used when socket is created to transfer information within a system also called system socket
AF_INET
Used when socket is created to communicate across the network also called Network socket.
AF_IPX
Novell IPX protocol
AF_NS
Xerox Network Systems protocols
AF_APPLETALK
Appletalk


arg2 - type
Type specify the type of communication is being used and they are..

SOCK_STREAM It is connection oriented, stream based and reliable communication. It provides two way of communication, Acknowledge is sent for each successful packet and re-transmission in case of any drop. TCP communication falls under this category, TCP also provides fragmentation and reassembly for long messages.

SOCK_DGRAM  it is datagram service and connection less transmission. In this type data is directly sent to destination without making any prior connection. So less reliable, no reordering of packets and no acknowledgement. chances of packet drop is more but despite all it is very fast and robust in compare to connection based data transmission. UDP protocol falls under this category.

arg3- Protocol
It is normally Zero. but can be chosen based on Socket domain and type.



What is System calls ?

System calls are set of function which interacts with system from user space. User space where most of the application runs but to make use of system applications like creation/reading/writing a file, creating a task, communication between the process, sending data across the network it required a system call.

Ex: File handling related system calls
open(),read(),write(),close()

Networking related system calls
socket(),bind(), sento(), recvfro(), send(), recv()

Task and process related system calls
fork(),vfork(),exec()


Difference between System call and API(Application Programming Interface)

API is set of functions that completely run in user space and they need system call to interact with system(Kernel).

Inter Process Communication(IPC)


All About Process

In computer to perform any task we requires a process which leads task to an end. Process is nothing but running instance of a program. Each Process is started by some other process called Parent Process and newly created process will become Child Process. Conventionally Linux operating system treats all process in the same way, resource hold by the parent process are duplicated in the child process. 

Whenever we run any application on linux system it creates an entry in process table with a process ID(PID). Based on PID only system tracks the process status. Once process perform its job it immediately gets removed from process table and allow other process to acquire the resource. 

Process table is nothing but data structure which describes of all running process and their status (Sleeping, Running, Orphan etc.). will discuss all the process states later in this blog.

When we execute our simple "Hello world.c" a.out, it creates one process called main process for that application which is currently in running state as long as it not coming back to terminal. 
     If we see  a.out is not the first process created in Linux system, There is a hierarchic of processes from Process 1 or init process to current running process . init process in Linux operating system who take cares of all running process or hung process or orphan process and they starts with starting of operating system. we will see one by one how process gets created, maintained, terminated and all other aspects of it.

How to Create a Process in Linux system