Reputation: 129
I have to reproduce a process family like this: father --> child --> grandson. I don't understand why the grandson code is never executed. My code scheme is like this:
int main() {
int fatherProcess, p1, p2;
p1 = fork();
if(p1 <0) {
perorr("Failed to create P1\n");
} else if(p1 == 0) {
//child code
p2 = fork();
if(p2 < 0) {
perorr("Failed to create P2\n");
} else if(p2 == 0) {
//grandson code
pritnf("Hello I'm the GRANDSON\n");
} else {
//child code
pritnf("Hello I'm the CHILD\n");
}
} else {
//father code
pritnf("Hello I'm the father\n");
}
return 0;
}
The stamp that I get is: - Hello I'm the GRANDSON - Hello I'm the father
Upvotes: 0
Views: 57
Reputation: 1781
You have made two spelling error. I have fixed it and you can try the following code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main() {
int fatherProcess, p1, p2;
p1 = fork();
if(p1 <0) {
perror("Failed to create P1\n");
} else if(p1 == 0) {
//child code
p2 = fork();
if(p2 < 0) {
perror("Failed to create P2\n");
} else if(p2 == 0) {
//grandson code
printf("Hello I'm the GRANDSON\n");
} else {
//child code
printf("Hello I'm the CHILD\n");
}
} else {
//father code
printf("Hello I'm the father\n");
}
return 0;
}
Your code:
perorr --> perror
pritnf --> printf
Upvotes: 1