Monday 25 August 2014

Process in Linux : An Overview



Well, enough of the GUI stuff. Now I think it is the time to move inside Linux. Let us have a little glimpse of the process management done in Linux.
By definition, any instance of a particular program in execution at a time is called a process. So a program, when is being executed, is called a process. The concept of process holds for all modern OS, and hence an understanding of the same helps us understand our OS.



To list the process, we can use the ps command. Type in the following to get a long listing of all processes in the system:

ps -el

The ‘e’ option selects all process, and the ‘l’ option is for long listing mode of display.
Each process has a distinct ID called the process id or ‘pid’. Also, its parent process id is thus referred as ‘ppid’.
To kill a process use the ‘kill’ command with the process id. For example, the following command will end the process with pid 20170:

kill 20170

Don’t you think it is faster than using CTRL+ALT+DEL -> select a process -> end process in Windows ?
Or if you are fond of the GUI side, simply type ‘xkill’ in the terminal. Your cursor will change to a skull. Now you simply need to click on a program window you want to close.  Try it, it is rather fun!


Beware! If you click it on your desktop, your desktop will be gone, and you may have to log out again to bring it back. Though many distributions are intelligent enough to bring the desktop again along with a ‘crash menu’ reporting the unexpected closing of your desktop, without making you log out.

Now let us write a code to create a process. Basically a process is created from another process by using the ‘fork()’ system call. The new process is called the child process.  fork() returns an integer, which may have value:
  • fork() < 0 : This means child process creation failed.
  • fork() = 0 : Child created, executing child process.
  • fork() > 0 : Child created, executing parent process.

System calls to get the pid and ppid are ‘getpid()’ and ‘getppid()’ respectively.
Here is a C code to create a process:

#include <stdio.h>
int main()
{
      printf(“ Current process id: %d\nParent process id: %d”, getpid(), getppid());
      int v=fork();
      if(v<0) printf("Child process creation was not successful!\n");
      else
      {
            if(v==0)
            {
                // code for child process
                printf("Child process started with pid:%d and parent id:%d",getpid(),getppid());
                printf("\nChild prints 1 item\nChild will exit now\n");
            }
          else
           {
              // code for parent process
              printf("Parent process running with pid:%d\n",getpid());
              printf("Parent prints 1 item.");
            }
      }
      printf("Exiting process %d with parent pid: %d\n",getpid(),getppid());
      return 0;
}

Two special categories of process which are worth mentioning are orphan process and zombie process.

Orphan Process:

In the long listing of process, the second column indicates whether the process is sleeping (S) or running (R). If a parent process is killed before its child is killed, the child becomes a ‘orphan process’. In Linux, the very first process is the ‘init’ process. As Linux maintains a tree structure for the processes also, hence an orphan process is a direct violation. Hence, any orphan process is adopted by the init process. Thus there are no orphan process in Linux.
To demonstrate this, let us have a look to this code:

#include <stdio.h>
#include <stdlib.h>
int main()
{
      printf(“ Current process id: %d\nParqent process id: %d”, getpid(), getppid());
      int v=fork();
      if(v<0) printf("Child process creation was not successful!\n");
      else
      {
            if(v==0)
            {
                 // code for child process
                printf("Child process started with pid:%d and parent id:%d",getpid(),getppid());
                printf("\nChild prints 1 item\nChild will sleep now\n");
                sleep(12); //sleep the child for 12 seconds
             }
            else
            {
                // code for parent process
                printf("Parent process running with pid:%d\n",getpid());
                printf("Parent prints 1 item. Parent will be killed now.");
                exit(0); // kill the parent
             }
     }
     return 0;
}

Say you compile it as ‘orphan.out
In the above code, parent gets killed just after creating the child and printing a message on the screen.  Meanwhile the child process sleeps for 12 seconds. In these 12 seconds, simply open up a new terminal and list all the process. Notice the ppid of ‘orphan.out’ is same as that of ‘init’, indicating that now init is the parent of ‘orphan.out’.

Zombie Process:


The name is funny, isn't it? Well it is a good analogy of real world zombies. In ‘Linuxworld’, a zombie is a child process which gets killed before the parent process. The child process, though non existent, is still mentioned in the process register, with a ‘zombie flag’, indicated by ‘Z’ in the second column. The process occupies no resource, except for the pid. Though zombies pose no threat when in less number, they can be very annoying for the system when in large numbers. This is because, Linux has a limited number of pids and it may fall short for pids as they have been already occupied by zombies. Thus no more processes can be started. The process register clears the zombies after a system call, like ‘wait()’ or after the parent is killed. To create a zombie process, you can use this code:

#include <stdio.h>
int main()
{
     printf("Process started...\n");
     printf("process id:%d\tparent process id:%d\n",getpid(),getppid());
     int v=fork();
     if(v<0) printf("Child process creation was not successful!\n");
     else
     {
           if(v==0)
           {
                printf("Child process started with pid:%d and parent id:%d",getpid(),getppid());
                printf("\nChild prints 1 item\nchild will exit now\n");
           }
           else
           {
                printf("Parent process running with pid:%d\n",getpid());
                printf("Parent prints 1 item\nSleeping parent for 20 seconds ");
                sleep(20); //sleep the parent for 20 secs
           }
     }
     printf("Exiting process %d with parent pid: %d\n",getpid(),getppid());
     return 0;
}

Compile this code as ‘zombie.out’. Run the code, and open a new terminal to list the processes.  Notice the ‘Z’ flag in the child process id.

I hope this post helped you get closer to Linux more. Feel free to share your questions and knowledge in the comments below.... till then , happy Linuxing :D !

No comments:

Post a Comment