Fix splitting bug
This commit is contained in:
parent
5d438703a6
commit
f36156d44a
@ -134,12 +134,12 @@ int main(int argc, char *argv[]) {
|
|||||||
close(in_pipe_a[1]);
|
close(in_pipe_a[1]);
|
||||||
close(STDIN_FILENO);
|
close(STDIN_FILENO);
|
||||||
if(dup2(in_pipe_a[0], STDIN_FILENO) != STDIN_FILENO) {
|
if(dup2(in_pipe_a[0], STDIN_FILENO) != STDIN_FILENO) {
|
||||||
fprintf(stderr, "ERROR: Failed duplicating STDIN");
|
fprintf(stderr, "ERROR: Failed duplicating STDIN\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
execlp(argv[0], argv[0], NULL);
|
execlp(argv[0], argv[0], NULL);
|
||||||
|
|
||||||
fprintf(stderr, "ERROR: Failed to load program into child");
|
fprintf(stderr, "ERROR: Failed to load program into child\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
default:
|
default:
|
||||||
/// Parent execution
|
/// Parent execution
|
||||||
@ -154,12 +154,12 @@ int main(int argc, char *argv[]) {
|
|||||||
close(in_pipe_b[1]);
|
close(in_pipe_b[1]);
|
||||||
close(STDIN_FILENO);
|
close(STDIN_FILENO);
|
||||||
if(dup2(in_pipe_b[0], STDIN_FILENO) != STDIN_FILENO) {
|
if(dup2(in_pipe_b[0], STDIN_FILENO) != STDIN_FILENO) {
|
||||||
fprintf(stderr, "ERROR: Failed duplicating STDIN");
|
fprintf(stderr, "ERROR: Failed duplicating STDIN\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
execlp(argv[0], argv[0], NULL);
|
execlp(argv[0], argv[0], NULL);
|
||||||
|
|
||||||
fprintf(stderr, "ERROR: Failed to load program into child");
|
fprintf(stderr, "ERROR: Failed to load program into child\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
default:
|
default:
|
||||||
/// Parent execution
|
/// Parent execution
|
||||||
@ -180,14 +180,16 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
/// Separate the lists based on the mean
|
/// Separate the lists based on the mean
|
||||||
current = head;
|
current = head;
|
||||||
while(current != NULL) {
|
while(current -> next != NULL) {
|
||||||
if(current -> points[0] <= x_mean) {
|
if(current -> points[0] <= x_mean) {
|
||||||
first_part_buff -> points[0] = current -> points[0];
|
first_part_buff -> points[0] = current -> points[0];
|
||||||
|
first_part_buff -> points[1] = current -> points[1];
|
||||||
first_part_buff -> next = malloc(sizeof(node_t));
|
first_part_buff -> next = malloc(sizeof(node_t));
|
||||||
first_part_buff = first_part_buff -> next;
|
first_part_buff = first_part_buff -> next;
|
||||||
first_part_len ++;
|
first_part_len ++;
|
||||||
} else {
|
} else {
|
||||||
second_part_buff -> points[0] = current -> points[0];
|
second_part_buff -> points[0] = current -> points[0];
|
||||||
|
second_part_buff -> points[1] = current -> points[1];
|
||||||
second_part_buff -> next = malloc(sizeof(node_t));
|
second_part_buff -> next = malloc(sizeof(node_t));
|
||||||
second_part_buff = second_part_buff -> next;
|
second_part_buff = second_part_buff -> next;
|
||||||
second_part_len ++;
|
second_part_len ++;
|
||||||
@ -200,22 +202,22 @@ int main(int argc, char *argv[]) {
|
|||||||
* Send first half to first child
|
* Send first half to first child
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
if(dup2(in_pipe_a[1], STDIN_FILENO) != STDIN_FILENO) {
|
/// Setup string for first child pipe
|
||||||
fprintf(stderr, "ERROR: Failed duplicating STDIN");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Setup string for pipe
|
|
||||||
current = first_part;
|
current = first_part;
|
||||||
char first_part_string[first_part_len + 1][__UINT8_MAX__];
|
char first_part_string[first_part_len][__UINT8_MAX__];
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(current != NULL) {
|
while(current -> next != NULL) {
|
||||||
sprintf(first_part_string[i], "%f %f\n", current -> points[0], current -> points[1]);
|
sprintf(first_part_string[i], "%f %f\n", current -> points[0], current -> points[1]);
|
||||||
i++;
|
i++;
|
||||||
current = current -> next;
|
current = current -> next;
|
||||||
}
|
}
|
||||||
first_part_string[first_part_len][0] = EOF;
|
first_part_string[first_part_len][0] = EOF;
|
||||||
|
|
||||||
|
if(dup2(in_pipe_a[1], STDIN_FILENO) != STDIN_FILENO) {
|
||||||
|
fprintf(stderr, "ERROR: Failed duplicating STDIN\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
/// Write to pipe
|
/// Write to pipe
|
||||||
if (write(in_pipe_a[1], first_part_string, first_part_len * __UINT8_MAX__) < 0) {
|
if (write(in_pipe_a[1], first_part_string, first_part_len * __UINT8_MAX__) < 0) {
|
||||||
fprintf(stderr, "ERROR: Failed writing to child\n");
|
fprintf(stderr, "ERROR: Failed writing to child\n");
|
||||||
@ -228,22 +230,22 @@ int main(int argc, char *argv[]) {
|
|||||||
* Send second half to second child
|
* Send second half to second child
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
if(dup2(in_pipe_b[1], STDIN_FILENO) != STDIN_FILENO) {
|
/// Setup string for second child pipe
|
||||||
fprintf(stderr, "ERROR: Failed duplicating STDIN\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Setup string for pipe
|
|
||||||
current = second_part;
|
current = second_part;
|
||||||
char second_part_string[second_part_len + 1][__UINT8_MAX__];
|
char second_part_string[second_part_len + 1][__UINT8_MAX__];
|
||||||
i = 0;
|
i = 0;
|
||||||
while(current != NULL) {
|
while(current -> next != NULL) {
|
||||||
sprintf(second_part_string[i], "%f %f\n", current -> points[0], current -> points[1]);
|
sprintf(second_part_string[i], "%f %f\n", current -> points[0], current -> points[1]);
|
||||||
i++;
|
i++;
|
||||||
current = current -> next;
|
current = current -> next;
|
||||||
}
|
}
|
||||||
second_part_string[second_part_len][0] = EOF;
|
second_part_string[second_part_len][0] = EOF;
|
||||||
|
|
||||||
|
if(dup2(in_pipe_b[1], STDIN_FILENO) != STDIN_FILENO) {
|
||||||
|
fprintf(stderr, "ERROR: Failed duplicating STDIN\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
/// Write to pipe
|
/// Write to pipe
|
||||||
if (write(in_pipe_b[1], second_part_string, second_part_len * __UINT8_MAX__) < 0) {
|
if (write(in_pipe_b[1], second_part_string, second_part_len * __UINT8_MAX__) < 0) {
|
||||||
fprintf(stderr, "ERROR: Failed writing to child\n");
|
fprintf(stderr, "ERROR: Failed writing to child\n");
|
||||||
|
Reference in New Issue
Block a user