From 49ae89153416fd2fc04cb43b93e8b4dde3171360 Mon Sep 17 00:00:00 2001 From: Dylan Young Date: Tue, 23 Sep 2014 22:46:41 -0300 Subject: [PATCH] Make mm.c fork(), with parent calling mean() and child median() Resolve smuos/simpleStats#4 --- mm.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/mm.c b/mm.c index 559cb15..8f48bae 100644 --- a/mm.c +++ b/mm.c @@ -80,18 +80,24 @@ int main(int argc, char *argv[]) { // Sort numbers qsort(pt, length, sizeof(int), numcmp); - - // Calculate the mean - double m = mean(pt, length, sizeof(int)); + + int rc = fork(NULL); + double m; + if (rc < 0) + exit(-1); + else if (rc == 0){ + // Calculate the median + m = median(pt, length); + } + else{ + wait(NULL); + // Calculate the mean + m = mean(pt, length, sizeof(int)); + } - // Calculate the median - double mm = median(pt, length); - // Print the mean: - fprintf(stdout, "%s: The mean is %f \n", argv[0], m); - - // Print the median: - fprintf(stdout, "%s: The median is %f \n", argv[0], mm); + // Print the mean?: + fprintf(stdout, "%s: The mean? is %f \n", argv[0], m); // Print out sorted numbers fprintf(stdout, "%s: Sorted output is: \n", argv[0]);