Skip to content

Instantly share code, notes, and snippets.

@StefanoFiumara
Last active December 24, 2015 19:19
Show Gist options
  • Save StefanoFiumara/6849128 to your computer and use it in GitHub Desktop.
Save StefanoFiumara/6849128 to your computer and use it in GitHub Desktop.
Unix process initialization
void process_manager::create_processes(int n) {
int pid;
for(int i = 1; i <= n; ++i) {
//create pipes before forking
if(pipe(parent_to_child) == -1) {
cout << "Error creating pipe!" << endl;
exit();
}
if(pipe(child_to_parent) == -1) {
cout << "Error creating pipe!" << endl;
exit();
}
pid = fork();
if(pid > 0) {
//This is the parent
//Close appropriate pipe ends here
continue; //continue to next iteration
} else if(pid == 0) {
//This is the child
//Close appropriate pipe ends here
break; //break out of the loop to prevent unwanted forks
} else {
cout << "Fork Error!" << endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment