Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, Im doing a project using C++. I have a problem of my fork and im not sure is it because of my program or my platform. The situation is ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    19

    Could not retrieve the actual pid of a process


    Hi,

    Im doing a project using C++. I have a problem of my fork and im not sure is it because of my program or my platform. The situation is like this.

    The parent program will always listen a request from a user. When the parent process receives a request from user via TCP socket, the parent will forward that request to a thread and the thread is responsible to fork the child process base on the parameters given by the user. After the process has been forked, the pid of the forked process is being saved to the database. The parent process will then continue to listen to any other request after the child process has been forked and this iteration process will be done until a sigterm() is being called. As for the child, the child process will also open a TCP socket and listens a request from a user. So this means that the child will also remains running unless it exits gracefully during the fork process.

    The problem is, when i fork the child process, it actually forks 2 process which is one of it is the zombie process and another one is the actual process. I dont mind on that, but what i do mind is that the pid that is being saved in the database is actually the zombie pid, not the actual pid. So, im not sure what is the problem here, is it of my program or my platform. I have been reading about the waitpid() and i understand about it, but in my situation, the parent or child will never close, it will continue running. there is no problem in forking the process coz the process is actually created but there will be a zombie process also, as if that my child process has been executed, and then it exits and it executes back again. Here is an example of my code :-

    Code:
    i = fork();
    if (i < 0)
    {
      FILE_LOG(logERROR << "Cannot fork. Error code = " << i;
    }
    else if (i > 0)
    {
       //This is parent process. Save the pid to the database
       query << "INSERT INTO server(pid) VALUES ( << i << ")";
      //This is where the zombie pid is being saved to the database. Not the actual    child PID
    }
    else
       iResult = execlp("P1", NULL);
    Appreciate for any help for you guys. Im using debian kernel 2.6.24-1-686. Thanks

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,757

    Exclamation

    I'm not sure,whether I understood your problem correctly.Currently you are running insert query from parent with child-pid returned from fork(). Have you tried running child-pid insert query from within child process itself? Within child process you can use getpid() to get child's pid.


    And what's your DB ? I would prefer using API's provided by the database.
    HTH
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  3. #3
    Just Joined!
    Join Date
    Dec 2007
    Posts
    19
    hmm, im not quite sure actually if i use getpid() called from the child process. This is because, i need to retrieve the child pid from the parent, since only the parent process has the access to the database. If i use getpid() which is called from the child process, then i need to transfer the child pid to the parent, n then only the parent can save it to the database. Am i correct on this or i am the confused one? I will give it a try though.
    Im using MySQL ver 14.12. However, im not using the MySQL API directly but i wrapped it with MYSQL++.

  4. #4
    Just Joined!
    Join Date
    Dec 2007
    Posts
    19
    In addition to that, the process that i forked actually forks another process, but after i fork, i killed the parent process once it has been forked. So it is something like this :-

    1. Parent fork a child. Parent will continue to listen to other request after forked.
    2. The child fork another process. So this child will become the parent of the new created process.
    3. The parent mentioned in ( 2) is then killed by itself.

    so, is this the reason why the defunct happened? Hope anyone can help. Im kinda desparate to solve this problem. Thanks

  5. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,757

    Exclamation

    while(1){
    //parent
    if (fork()==0){//child process.
    printf("Child running...");
    if(fork()==0){//grandchild
    system(check.sh);
    }else
    exit(0);
    }
    }
    I perform something like above for my code. Which does what you said,
    1)parent is in infinite loop
    2)Parent (A) forks a child (B)
    3) child (B) forks a child (C) then child B exit.

    Thus Child (C) will continue to run a shell script (check.sh), after executing shell, child C dies.
    It became zombie and this child (C) will be collected by init process after some time.
    Last edited by Lakshmipathi; 02-22-2010 at 05:10 AM.
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  6. #6
    Just Joined!
    Join Date
    Dec 2007
    Posts
    19
    Yes, i manage to debug the error and completely solve it. However, still need to do more test and debugging. Thanks a lot Lakshmipathi.G for your help and it is really appreciated. Credits to you.

    Regards from Malaysia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •