-
Simple Processes Program
Hello,
I try to make a program which gets the number of desired childs from user, then creates them sequentially and destroys them in reverse order.
I just don't understand why it doesn't work :(
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char** argv) {
int pid, status,n_sons,current_son=0;
printf("Enter number of sons:");
scanf("%d",&n_sons);
do {
pid=fork();
if(pid !=0 ) {
printf("Created Son, with PID:%d and PPID:%d\n",pid,getppid());
wait(&status);
printf("Son with PID:%d, terminated with status:%d\n",pid,status);
break;
}
else {
current_son++;
}
}while(current_son < n_sons);
return 0;
}
-
So when does the "son" exit?
I think you are expecting your loop to get updated from the son code, which isn't what happens, so you get an infinite loop of creating new processes (until there is no more space in the process table, and the OS returns an error, which is != 0, so it then "waits" for a process that doesn't exist. And the main process will not end until ALL of the children have died, and they are all busy trying to create new branches of themselves.
--
Mats
-
as long as the number of sons is less than maximum, every son becomes a father. the son whose index is greater than maximum won't be a father, he'll finish the loop, and then all fathers.
so the loop isn't infinite, but it doesn't work in the correct order....
-
Ok, so your loop is really quite weird (because it exits in the middle using a break).
And every child process will try to create n_sons, and every child of those will try to create n_sons dhildren, and so on and so on. Say n_sons is 2, that means 2 children, each creating 2 children -> now we have 6 processes. The last 4 creates another 8 children, which becomes 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768. It never ends (until you have run out of number of processes the machine can produce - but chances are that when that kills one of the children, there is still another that creates a new child, that attempts to create another 2, etc, etc). This is generally a good way to completely prevent anyone else from creating any process on the machine.
--
Mats
-
why?
lets say n_sons is 3, hence
father -> creates first son -> father waits for first son to terminate
first son -> creates second son -> first son waits for second son to terminate
second son -> creates third son -> second son waits for third son to terminate
third son -> creates fourth son -> fourth son ends loop and terminate itself
third son terminates
second son terminates
first son terminates
father terminates
...
well that's how it suppose to work, but it actually works out of order, here's the output for n_sons=5:
Code:
Created Son, with PID:18656 and PPID:18577
Created Son, with PID:18659 and PPID:18657
Created Son, with PID:18660 and PPID:18658
Son with PID:18660, terminated with status:0
Son with PID:18659, terminated with status:0
Created Son, with PID:18658 and PPID:18656
Son with PID:18658, terminated with status:0
Created Son, with PID:18657 and PPID:18655
Son with PID:18657, terminated with status:0
Son with PID:18656, terminated with status:0
-
Ok, so there is no guarantee of which will run first, the child or parent process. I still don't understand your logic and how you avoid doing infinite processes, but be that as it may (I haven't actually looked CAREFULLY at your code).
--
Mats
-
that's right, there's no guarantee who will run first the father or son but in the output you can see that some sons are created after some sons terminated and that shouldn't happen cause every son should wait till his son is terminated, and the termination process should begin only when the "extra" son is created !?
that's what i don't understand !? or maybe it's some delay of output messages !?
-
So, 18660 is terminated before 18658 is terminated. That seems right to me. All other ones also seem OK. But maybe I'm missing something.
--
Mats
-
yes it terminates them in correct order (somehow) but 18658 and 18657 are created after 18660 created and terminated, and 18657 is created just after 18658. so from the output messages it's seen that it creates them in some unsync order but terminates (somehow luckily) in order.
i want them to be created in order, and just after the last one was created to terminate in reverse order.
-
ok i fixed it
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char** argv) {
int pid, status,n_sons,current_son=0;
printf("Enter number of sons:");
scanf("%d",&n_sons);
do {
pid=fork();
if(pid !=0 ) {
wait(&status);
printf("Terminated Son #%d\n",current_son);
break;
}
else {
printf("Created Son #%d\n",current_son);
current_son++;
}
}while(current_son < n_sons);
return 0;
}
and the ouput:
Code:
Created Son #0
Created Son #1
Created Son #2
Created Son #3
Created Son #4
Created Son #5
Terminated Son #5
Terminated Son #4
Terminated Son #3
Terminated Son #2
Terminated Son #1
Terminated Son #0
i just don't really understand why it didn't work before !?