Skip to content

Commit

Permalink
Resolve issue smuos#7: Make the parent wait for child process
Browse files Browse the repository at this point in the history
  • Loading branch information
JINGJINGTANG committed Sep 27, 2014
1 parent 220c764 commit f830184
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Binary file modified mm
Binary file not shown.
16 changes: 11 additions & 5 deletions mm.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

#define debug 0

Expand Down Expand Up @@ -46,11 +47,9 @@ int main(int argc, char *argv[]) {
// Sort numbers
qsort(pt, length, sizeof(int), numcmp);

// Print out numbers
// Print out numbers message
fprintf(stdout, "%s: Sorted output is: \n", argv[0]);
for (i=0; i<length; i++) {
fprintf(stdout, "%d ", pt[i]);
}

// Fork the process
int rc = fork();

Expand All @@ -60,12 +59,19 @@ int main(int argc, char *argv[]) {
exit(1);
// The child process
} else if (rc == 0) {
// print out numbers
for (i = 0; i < length; i++) {
fprintf(stdout, "%d ", pt[i]);
}
//print out the median
fprintf(stdout, "\nThe median is: %f", median(length, pt));;
// The parent process
} else {
wait(NULL);
//print out the mean
fprintf(stdout, "\nThe mean is: %f", mean(length, pt));
fprintf(stdout, "\n%s: FIN. \n", argv[0]);
}
fprintf(stdout, "\n%s: FIN. \n", argv[0]);

return 0;
}
Expand Down

0 comments on commit f830184

Please sign in to comment.