Last active
December 24, 2015 19:19
-
-
Save StefanoFiumara/6849128 to your computer and use it in GitHub Desktop.
Unix process initialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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