How to fork multiple processes from a same parent?
I am trying to create multiple processes from a same parent, but it always ended up with more processes than expected. I couldn't figure out how to do it and need some help here. I found a piece of code online and tried it, int main () { pid_t pid=0; int i=0; for (i=0; i<3; i++) { pid=fork(); switch(pid) { case 0: { cout<<"\nI am a child and my pid is:"<<getpid(); cout<<endl; exit(0); break; } default: { cout<<"\nI am a parent and my pid is: "<<getpid(); cout<<"\nMy child pid is: "<<pid; cout<<endl; wait(NULL); break; } } } return 0; } This code does work and creates 3 children from same paren
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.