LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Child process state. (https://www.linuxquestions.org/questions/programming-9/child-process-state-425158/)

rastadevil 03-15-2006 04:51 PM

Child process state.
 
Hi all.
I have a problem..
I'm developing a shell and it's almost done, but the thing is that i have to keep track of background running child processes.
What i do is put an int variable to increment every time a child process is running and then decrement when the child process is dead. But if i decrement it on the return of the parent it gives me zero and the child process is still running! where the heck do i decrement the variable to tell the program that the child is dead? In the very child process? NO, coz it doesn't afect my "outside" variable..

here's my sample test function :

Code:

if (strcmp(argument[0],"test")==0){       

                running ++;
                if ((pid1=fork())==0){
                       
                        printf("I'm a child process with PID %d \n",getpid());
                        sleep(5);
                       
                        exit(0);
                        }
               
                else{
                waitpid(-1,NULL,WNOHANG); 
                running --;                  //wrong place!
                continue;
                }
}

help plz

paulsm4 03-15-2006 05:18 PM

You might want to try something like this:
Code:

  int running = 0;
  for ( ;; )
  {
    running++;
    if ((pid1=fork())==0)
    {
      printf("I'm a child process with PID %d \n",getpid());
      sleep(5);
      exit(0);
    }
    else
    {
      int actual_pid = waitpid(-1,NULL,0); 
      running --;
      printf ("pid terminated: expected pid= %d, actual pid= %d\n",
        pid1, actual_pid);

    }
  }
}


rastadevil 03-15-2006 05:28 PM

hhmm
 
Thank you for your time paul, but the thing is that i have to have the WNOHANG on wait coz the idea is the child process to run in background. It returns to prompt while the child "sleeps" as it is said in the code. The thing is that the child is running and the "running" variable decrements before the child ends.. where do i put this variable decrementing to tell the program that the child is finished after those 5 seconds?

paulsm4 03-15-2006 06:30 PM

What does "waitpid(..., WNOHANG)" do if a child hasn't terminated?

If you really want WNOHANG, you'll probably have to do something like this:
Code:

else
    {
      int retval = waitpid(-1,NULL,WNOHANG); 
      if (retval > 0)
      {
        running--;
        printf ("pid terminated: expected pid= %d, actual pid= %d\n",
          pid1, retval);
      }
    }
  ...


rastadevil 03-15-2006 07:31 PM

WNOHANG basicly makes the parent process not wait for the child to end, and so the prompt of the shell is shown while the process runs in background.. i basicaly want the program to be noticed when the background ends, that's why i created that running var that counts the processes running in background.
That last suggested way doesn't work also.. The perfect thing was for the variable running decrement inside the child process but i've tried and it doesn't change nothing if i do running -- inside the child process.
It's like i want the parent and the child to share a variable..
Hope this was clear enough for more suggestions..

paulsm4 03-15-2006 08:33 PM

If you want the parent and child to share a variable, then you get to create a shared memory segment to hold the variable. And you get to create a semaphore to synchronize access to that variable.

That's probably indicative of a poor design.

The thing you really want to do is just keep track of the #/children in the *parent* process (assuming, of course, that the children don't spawn their own subprocesses) (which might also be indicative of possible design issues).

Book recommendation:
Advanced Programming in the UNIX(R) Environment (2nd Edition), W. Richard Stevens
http://www.amazon.com/gp/product/020...lance&n=283155


'Hope that helps .. PSM

rastadevil 03-15-2006 08:45 PM

I've searched for hours and can't find an answer to my problem.. I would appreciate a cheaper solution rather than buying a book coz i have some and none have the answer to my problem.. :scratch:

sundialsvcs 03-15-2006 10:13 PM

What you do is to capture the process-ID of the process that you have forked. Then use a wait() system call to "reap" the process. See man -a wait.

rastadevil 03-16-2006 01:12 PM

Ok, i made it.. After a "bingo" research i have solved the problem.. If anyone has the same problem, just message me.

Thanx to you all ;):p

paulsm4 03-16-2006 01:38 PM

So what exactly was the problem, and what was your solution?


All times are GMT -5. The time now is 08:03 AM.