Skip to content

Commit

Permalink
Create fork() and Fixes smuos#4
Browse files Browse the repository at this point in the history
  • Loading branch information
yy90axf2010 committed Sep 27, 2014
1 parent d74408d commit 62d8fe5
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions mm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

#define debug 0

Expand Down Expand Up @@ -60,25 +62,42 @@ int main(int argc, char *argv[]) {
pt[i] = (int) strtol(argv[i+1], NULL, 10);
}

// find the mean value.
int meanvalue;
meanvalue = mean(length,pt);
fprintf(stdout, "The mean value is: %d\n",meanvalue);


// Sort numbers
qsort(pt, length, sizeof(int), numcmp);

//find the middle value
int medianvalue;
medianvalue = median(length, pt);
fprintf(stdout, "The median value is: %d\n", medianvalue);


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

//fork
int rc = fork();
if (rc < 0) {
fprintf(stdout, "Cannot fork()\n");
exit(0);
} else if (rc == 0) {

printf("Hello, I am child (pid:%d)\n", (int) rc);
//find the middle value
//int medianvalue;
//medianvalue = median(length, pt);
//fprintf(stdout, "I'm child and the median value is: %d\n", medianvalue);

} else{
int wc = wait(NULL);
printf("Waiting the child, I am %d (wc:%d) (pid:%d)\n",
getpid(), wc, (int) rc);
// find the mean value.
//int meanvalue;
// meanvalue = mean(length,pt);
//fprintf(stdout, "I'm parents and the mean value is: %d\n",meanvalue);
}

return 0;
}

0 comments on commit 62d8fe5

Please sign in to comment.