zombie and child processes in linux using C

This is the tutorial about zombies and orphan processes in linux.
In linux, each running process's identity is maintained in the process table.





For eg.  if we use getpid() to get PID of any process, it derives the information from the process table.
Hence the process table contains all the information of any running process.
And when the process stopped or killed, it's entry is erased from the process table.


Now, when a process stops, it's exit code is queried by parent process.
Exit code helps to acknowledge why the process terminated.
Hence the entry of any process is kept in process table till it's exit code is queried by parent process.


Hence there will always be the chance that
  1. Zombie process is created

    As we know that till the parent doesn't query the exit code of
    the child process, the entry of child process will be in process table.
    Hence if the child process is terminated but it's parent process hasn't queried it's exit code.
    Such processes are called as Zombie in linux terminology.
    Because their entry is still in the process table but they have already terminated.
  2. Orphan process is created

    When the child process doesn't get any any parent process to query it's exit code
    such process is called as the Orphan in linux terminology.
    Hence when orphan process is created, then the father of all the processes called as init adopts the child process & queries it's exit code.


In order to create a successful program, it is important to minimize the orphan & zombie process.
Because the exit code is necessary as it gives indication about whether the job assigned to the process was completed successfully or not.

Now I'm using the C program to solve the zombie and orphan process problem


#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>     //for waitpid()
void main()
{
int pid,status;
pid=fork();
if(pid==0)
{
  execl("cp","*.txt","/Desktop",NULL);
  printf("Now the child process is terminating");
}
else
{
  waitpid(pid,&status,0);
  if(WIFEXITED(status))
  printf("Parent process is saying that child process is terminated normally");
  else
    printf("Parent is saying that child terminated abnormally");
}

NOTE:
  • WIFEXITED  is the macro which returns a nonzero value if the child process terminated normally.
    And it returns ZERO if the child process terminates abnormally.
  • waitpid() function is ensuring that the parent process has to
    wait till the child process terminates completely
    1. first parameter of the waitpid is the PID of the child process for which the wait is being performed.
    2. second parameter of the waitpid is the address of integer having the exit code of the child process.
    3. third parameter controls the behaviour of waitpid function.
      As I'm not using it that's why I've passed it 0.
  • Therefore, in the above program
    child process is copying all the text files from current directory to the Desktop


Hence guys, this is all about zombie and orphan processes in linux using C.
Hoping it'll help and if you got any query please post your comment.

3 comments:

  1. Nice Sandeep keep it up you are doing well

    ReplyDelete
  2. Command a device to open files hacked Facebook



    ReplyDelete
  3. I remember my experience with hackers i contacted online, i almost lost hope in finding out if my husband was loyal to me and has been loyal in our 20 years marriage,I lost a lot of funds in paying these so called hackers so i made up my mind that if i get scammed again i'll just forget about finding out what God doesn't want me to know, Then i saw reviews about ( HACKINTECHNOLOGY@GMAIL.COM ) and decided contact him and if and if i hear any stupid excuse won't contact anybody and just accept that God doesn't want me to then he proved to me that he isn't a cheat by working a sample job for me but to my greatest surprise he did the job and sent results to me fast, i was so happy but sad part was i divorced my husband, and i will drop an advise for people who wants any kind of hacking services stopped being scammed by people who impersonate being hackers, (ETHICALHACKERS009@GMAIL.COM) for a fast and clean job.Here is his cell number +1 (669) 225 2253 , Thank me later.

    ReplyDelete