Skip to content

Instantly share code, notes, and snippets.

@stumped2
Created November 7, 2013 00:25
Show Gist options
  • Save stumped2/7346781 to your computer and use it in GitHub Desktop.
Save stumped2/7346781 to your computer and use it in GitHub Desktop.
void sorter(int processes, pid_t *pids, int toSort[][2], int fromSort[][2])
{
int i, j;
for (i = 0; i < processes; i++) {
switch (pids[i] = fork()) {
case -1: /* Error making shild */
perror("creating child");
exit(EXIT_FAILURE);
case 0: /* Setup child before exec */
/* Cleanup all unneeded FDs */
for (j = 0; j < processes; j++) {
close(toSort[j][1]);
close(fromSort[j][0]);
if (j == i) {
dup2(toSort[j][0], STDIN_FILENO);
dup2(fromSort[j][1], STDOUT_FILENO);
} else {
close(toSort[j][0]);
close(fromSort[j][1]);
}
}
execlp("sort", "sort", (char*) NULL);
default:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment