Add string convertion

This commit is contained in:
Ivaylo Ivanov 2018-12-15 22:27:23 +01:00
parent c70db7363d
commit eb130f4ba4

View File

@ -67,7 +67,6 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
fprintf(stderr, "Process id: %d\n", getpid());
/** /**
* Create the pipe file descriptors and inititalize the pipes * Create the pipe file descriptors and inititalize the pipes
* *
@ -91,14 +90,14 @@ int main(int argc, char *argv[]) {
node_t * current = head; node_t * current = head;
while(fgets(input, __INT8_MAX__, stdin) != NULL) { ///< Read line by line while(fgets(input, __INT8_MAX__, stdin) != NULL) { ///< Read line by line
/// Split the input by whitespace as delimiter char *sep, *end;
char *x = strtok(input, " "); float x = strtof(input, &sep); ///< if no characters were converted then input will point to end
char *y = strtok(NULL, " "); float y = strtof(sep, &end);
if(x != NULL && y != NULL) { if(input != sep || sep != end) {
/// Convert to float and save to the list /// Convert to float and save to the list
current -> points[0] = strtof(x, NULL); current -> points[0] = x;
current -> points[1] = strtof(y, NULL); current -> points[1] = y;
current -> next = malloc(sizeof(node_t)); current -> next = malloc(sizeof(node_t));
current = current -> next; current = current -> next;
point_num++; ///< Increase the list length point_num++; ///< Increase the list length
@ -131,7 +130,6 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
case 0: case 0:
/// Child execution /// Child execution
fprintf(stderr, "Child 1 id: %d\n", getpid());
close(out_pipe_a[0]); close(out_pipe_a[0]);
close(in_pipe_a[1]); close(in_pipe_a[1]);
close(STDIN_FILENO); close(STDIN_FILENO);
@ -152,7 +150,6 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
case 0: case 0:
/// Child execution /// Child execution
fprintf(stderr, "Child 2 id: %d\n", getpid());
close(out_pipe_b[0]); close(out_pipe_b[0]);
close(in_pipe_b[1]); close(in_pipe_b[1]);
close(STDIN_FILENO); close(STDIN_FILENO);
@ -199,27 +196,56 @@ int main(int argc, char *argv[]) {
} }
/** /**
* Send the correct lists to the children *
**/ * Send first half to first child
*
/// First half **/
if(dup2(in_pipe_a[1], STDIN_FILENO) != STDIN_FILENO) { if(dup2(in_pipe_a[1], STDIN_FILENO) != STDIN_FILENO) {
fprintf(stderr, "ERROR: Failed duplicating STDIN"); fprintf(stderr, "ERROR: Failed duplicating STDIN");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (write(in_pipe_a[1], first_part_buff, first_part_len*sizeof(node_t)) < 0) { /// Setup string for pipe
current = first_part;
char first_part_string[first_part_len + 1][__UINT8_MAX__];
int i = 0;
while(current != NULL) {
sprintf(first_part_string[i], "%f %f\n", current -> points[0], current -> points[1]);
i++;
current = current -> next;
}
first_part_string[first_part_len][0] = EOF;
/// Write to pipe
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");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
close(STDOUT_FILENO); close(STDOUT_FILENO);
///Second half
/**
*
* Send second half to second child
*
**/
if(dup2(in_pipe_b[1], STDIN_FILENO) != STDIN_FILENO) { if(dup2(in_pipe_b[1], STDIN_FILENO) != STDIN_FILENO) {
fprintf(stderr, "ERROR: Failed duplicating STDIN"); fprintf(stderr, "ERROR: Failed duplicating STDIN\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (write(in_pipe_b[1], first_part_buff, second_part_len*sizeof(node_t)) < 0) { /// Setup string for pipe
current = second_part;
char second_part_string[second_part_len + 1][__UINT8_MAX__];
i = 0;
while(current != NULL) {
sprintf(second_part_string[i], "%f %f\n", current -> points[0], current -> points[1]);
i++;
current = current -> next;
}
second_part_string[second_part_len][0] = EOF;
/// Write to pipe
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");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -227,7 +253,7 @@ int main(int argc, char *argv[]) {
/// Wait for the correct exit of the children /// Wait for the correct exit of the children
if(wait_for_termination(child_a) != EXIT_SUCCESS || wait_for_termination(child_b) != EXIT_SUCCESS) { if(wait_for_termination(child_a) != EXIT_SUCCESS || wait_for_termination(child_b) != EXIT_SUCCESS) {
fprintf(stderr, "ERROR: Children didn't terminate successfully"); fprintf(stderr, "ERROR: Children didn't terminate successfully\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -339,3 +365,5 @@ int wait_for_termination(pid_t child) {
return WEXITSTATUS(exit_stat); return WEXITSTATUS(exit_stat);
} }
childexec()