ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
}
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?
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.
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).
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..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.