Skip to content

Instantly share code, notes, and snippets.

@StefanoFiumara
Created October 18, 2014 02:50
Show Gist options
  • Save StefanoFiumara/facd2eedc18c29369dce to your computer and use it in GitHub Desktop.
Save StefanoFiumara/facd2eedc18c29369dce to your computer and use it in GitHub Desktop.
for(i = 0; i < n; i++) {
//create 2 pipes per process, then close appropriate ends
if(pipe(parent_to_child[i]) == -1) {
cout << "Error creating pipe!" << endl;
exit(1);
}
if(pipe(child_to_parent[i]) == -1) {
cout << "Error creating pipe!" << endl;
exit(1);
}
pid = fork();
if(pid > 0) {
//This is the parent, close the unnecessary pipe ends
close(parent_to_child[i][READ_END]);
close(child_to_parent[i][WRITE_END]);
continue;
} else if(pid == 0) {
//This is the child, close the opposite pipe ends as above
close(parent_to_child[i][WRITE_END]);
close(child_to_parent[i][READ_END]);
create_child(i);
break;
} else {
cout << "Fork Error!" << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment