Я ищу хороший способ создать 2 одинаковых дочерних процесса, которые будут выполнять 1 задачу - сортировать отдельно, один от другого, массив с числовыми данными с разными алгоритмами, например Merge-Sort , и QuickSort .
Первый процесс, выполнивший задачу, должен завершить другой процесс (и распечатать результат)
Также это будет лучший способ передачи данные для процессов (каналы?)
Также есть что-то, что я не учел?
Я должен использовать системные функции, Posix и Linux код
int main() {
pid_t child_a, child_b;
int unsortedData[MAX] = getNumericDataFromFile(/*filename*/);
// Open two pipes for communication
// The descriptors will be available to both
// parent and child.
int in_fd[2];
int out_fd[2];
if (pipe(in_fd) == -1) { // For child's stdin
fprintf(stderr, "Pipe In Failed");
return 1;
}
if (pipe(out_fd) == -1) { // For child's stdout
fprintf(stderr, "Pipe Out Failed");
return 1;
}
if ((child_a = fork()) < 0) { // create first child process
fprintf(stderr, "Fork of first child process Failed");
return 1;
}
if (child_a == 0) {
/* Child A code, execute first algorithm */
int sortedData[MAX] = firstChildProcess(/*unsortedData*/);
/* terminate other process properly if its firs ? */
} else {
if ((child_b = fork()) < 0) { // create second child process
fprintf(stderr, "Fork of second child process Failed");
return 1;
}
if (child_b == 0) {
/* Child B code, execute second algorithm */
int sortedData[MAX] = ChildProcess(/*unsortedData*/)
/* terminate other process properly if its first? */
} else {
parentCode(..);/* Parent Code */
}
}
return 0;
}
Ввод: 6, 5, 4, 3, 2, 1
Выход: Второй алгоритм завершил первую задачу сортировки с результатом:
1 , 2, 3, 4, 5, 6
Заранее спасибо