Created
November 7, 2013 00:25
-
-
Save stumped2/7346781 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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