- #1
khdani
- 55
- 0
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 correctly :(
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 correctly :(
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;
}