cs201development/fork1.c

38 lines
628 B
C

#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
void forkAndPrint(int n){
// pid_t pid = fork();
// if(n>0){
// print("Child process, PID = %d\n", getpid());
// }
// else{
// wait(NULL);
// print("Exiting parent process
// }
pid_t pid;
if (n > 0){
pid = fork();
if(pid != 0){
printf("Parent process, PID = %d waiting to finish\n",getpid());
wait(NULL);
printf("Parent process, PID = %d finishing\n",getpid());
}
else {
forkAndPrint(n-1);
}
}
else{
printf("Bottom process!\n");
}
}
int main() {
forkAndPrint(10);
return 0;
}