LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2006, 04:51 PM   #1
rastadevil
LQ Newbie
 
Registered: Feb 2006
Posts: 7

Rep: Reputation: 0
Red face 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
 
Old 03-15-2006, 05:18 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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);
    }
  }
}

Last edited by paulsm4; 03-15-2006 at 05:19 PM.
 
Old 03-15-2006, 05:28 PM   #3
rastadevil
LQ Newbie
 
Registered: Feb 2006
Posts: 7

Original Poster
Rep: Reputation: 0
Unhappy 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?
 
Old 03-15-2006, 06:30 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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);
      }
    }
  ...

Last edited by paulsm4; 03-15-2006 at 06:32 PM.
 
Old 03-15-2006, 07:31 PM   #5
rastadevil
LQ Newbie
 
Registered: Feb 2006
Posts: 7

Original Poster
Rep: Reputation: 0
Unhappy

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..

Last edited by rastadevil; 03-15-2006 at 07:33 PM.
 
Old 03-15-2006, 08:33 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 03-15-2006, 08:45 PM   #7
rastadevil
LQ Newbie
 
Registered: Feb 2006
Posts: 7

Original Poster
Rep: Reputation: 0
Unhappy

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..
 
Old 03-15-2006, 10:13 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181
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.
 
Old 03-16-2006, 01:12 PM   #9
rastadevil
LQ Newbie
 
Registered: Feb 2006
Posts: 7

Original Poster
Rep: Reputation: 0
Smile

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
 
Old 03-16-2006, 01:38 PM   #10
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
So what exactly was the problem, and what was your solution?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
orphan child process happy78 Programming 3 08-30-2005 01:40 AM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration