[TAG] Help please :(

Lew Pitcher lpitcher at sympatico.ca
Tue Mar 6 23:06:04 MSK 2007


Cenk Yusuf Ustabas wrote:
> Hi,
> I am new at operations on processes.
 > I wanna create child from parent and after ending
 > the process of child, child will create another child (like grandchild)
 > and this child will create another child it will goes like that but
 > I dont know how will I do this.
 > Will it be in a loop or it will be recursive? Thank you for your help.

It is unclear to me whether you want to do this in program code or as part 
of a shell script. Assuming that you want program code, you need to read the 
documentation on the fork(1) syscall ("man 1 fork"). Your program code will 
looks something like...

   for (;;)
   {
     /* do some useful work here */
     if (fork()) exit(0);
   }

The fork() call creates the child process
The return value from the fork() call will be non-zero in the parent 
process, and zero in the child process. The child process, detecting that it 
/is/ the child (because of the zero return value from fork() ) will continue 
on, loop back to the top of the for(;;) loop, and drop back into the part 
that does the useful work. The parent process, detecting that it /is/ the 
parent (because of the non-zero return value from fork() ) will terminate 
using the exit() call. If the parent process did not terminate, then there 
would soon be too many processes for the system to handle, and the system 
would soon start denying the fork() call. (think about it, the parent would 
keep spawning processes, and each of those processes would spawn processes, 
and ever onwards).

HTH
-- 
Lew





More information about the TAG mailing list