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.

4 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

  4. RELIABLE BUSINESS/HOME/COMPANY/PROJECT/PERSONAL LOAN? AFFORDABLE LOAN IS HERE FOR YOU TODAY Email profdorothyinvestments@gmail.com

    We are financial consultants providing reliable loans to individuals and funding for business, home and projects start up. Are you tired of seeking loans or are you in any financial mess. Do you have a low credit score, and you will find it difficult to get loans from banks and other financial institutions? then worry no more for we are the solution to your financial misfortune. we offer all types of loan ranging from $5,000.00 to $2,000,000.00USD with a low interest rate of 2% and loan duration of 1 to 33 years to pay back the loan secure and unsecured. Are you losing sleep at nights worrying how to get a Legit Loan Lender? Contact us via Email: profdorothyinvestments@gmail.com

    Do you have a bad credit? Do you need money to pay bills? Do you need loan to buy, refinance or renovate your home? Is it necessary to start a new business? Do you have an unfinished project due to poor funding? Do you need money to invest in any specialty that will benefit you? MRS. DOROTHY LOAN INVESTMENTS aims is to provide excellent professional financial services which include the followings

    * Personal loans * Business loans
    * Home loans * Farm Loans
    * Education loans * Debt consolidation loans
    * Truck Loans * Car Loans
    * Hotels Loans * Refinancing Loans
    * Venture capital and many more.

    We are certified, reliable, financially powerful, fast and dynamic, with no credit check and offers a 100% guarantee loans and finances

    Best Regards
    Prof. Dorothy Pilkenton Jean
    profdorothyinvestments@gmail.com

    ReplyDelete