Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to do a merge sort of pipes and forks, and I have no idea what I\'m doing

ID: 3658884 • Letter: I

Question

I need to do a merge sort of pipes and forks, and I have no idea what I'm doing. Please help My code is below #include #include #include #include /* Compare int function */ int compare(const void *num_1, const void *num_2) { const int *inum_1 = (const int *)num_1; // casting pointer types const int *inum_2 = (const int *)num_2; return *inum_1 - *inum_2; } int main() { FILE *fp = NULL; char userFile[20]; int num[800]; /*Array to hold numbers from file.*/ int number; pid_t child_pid = 0; int fd[2]; /*Pipe variable for in and out functions*/ int i, j, n; int count = 1; printf("Please enter the name of your file in which the numbers are located. "); scanf("%s", userFile); fp = fopen(userFile, "r"); fscanf(fp, "%d", &number); num[0] = number; while (!feof(fp)){ /*Reads the file and inputs the numbers into the array num*/ fscanf(fp, "%d", &number); num[count] = number; count++; } fclose(fp); if(pipe(fd) == -1){ /*test pipe*/ perror("Failed"); return 1; } printf("Current process PID: %ld Parent PID: %ld ", (long)getpid(), (long)getppid()); printf(" "); printf("%ld", (long)getpid()); printf(": 800 numbers received. "); write(fd[1], num, sizeof(int)*800); /*Writes the array of numbers from file to pipe*/ printf("%ld", (long)getpid()); printf(": Creating child processes. "); for(i = 1; i < 3; i++) if((child_pid = fork()) <= 0) break; if(child_pid == 0){ //third tier children read(fd[0], num, sizeof(int)*400); /*Reads the array of numbers from pipe divided by two*/ printf(" Current process PID: %ld Parent PID: %ld ", (long)getpid(), (long)getppid()); printf(" "); printf("%ld", (long)getpid()); printf(": 400 numbers received. "); write(fd[1], num, sizeof(int)*400); printf("%ld", (long)getpid()); printf(": Creating child processes. "); for(i = 1; i < 3; i++) if((child_pid = fork()) <= 0) break; if(child_pid == 0){ //second tier children read(fd[0], num, sizeof(int)*200); printf("Current process PID: %ld Parent PID: %ld ", (long)getpid(), (long)getppid()); printf(" "); printf("%ld", (long)getpid()); printf(": 200 numbers received. "); write(fd[1], num, sizeof(int)*200); printf("%ld", (long)getpid()); printf( ": Creating child processes. "); for(i = 1; i < 3; i++) if((child_pid = fork()) <= 0) break; if(child_pid == 0){ //bottom tier children read(fd[0], num, sizeof(int)*100); printf("Current process PID: %ld Parent PID: %ld ", (long)getpid(), (long)getppid()); printf(" "); printf("%ld", (long)getpid()); printf( ": 100 numbers received. "); /*sort read array and then write to pipe*/ printf("%ld", (long)getpid()); printf(": Sorting the numbers. "); qsort(num, 100, sizeof(int), compare); /*qsort() function*/ write(fd[1], num, sizeof(int)*100); } else{ //second tier children wait(NULL); wait(NULL); read(fd[0], num, sizeof(int)*200); /*append read array and then write to pipe again*/ printf("%ld", (long)getpid()); printf(": 200 numbers received from pipe. "); printf("%ld", (long)getpid()); printf(": Merging the results. "); qsort(num, 200, sizeof(int), compare); write(fd[1], num, sizeof(int)*200); } } else{ //third tier children wait(NULL); wait(NULL); read(fd[0], num, sizeof(int)*400); /*append read array and then write to pipe again*/ printf("%ld", (long)getpid()); printf(": 400 numbers received from pipe. "); printf("%ld", (long)getpid()); printf(": Merging the results. "); qsort(num, 400, sizeof(int), compare); write(fd[1], num, sizeof(int)*400); } } else{ //Parent wait(NULL); wait(NULL); read(fd[0], num, sizeof(int)*800); /*append read array*/ printf("%ld", (long)getpid()); printf(": 800 numbers received from pipe. "); printf("%ld", (long)getpid()); printf(": Merging the results. "); qsort(num, 800, sizeof(int), compare);

Explanation / Answer

i am not able to read your question, so i am giving you the code for merge sort using pipes an forks, hope you follow that :) static void forkSort(int* parentNums, int size, int write_fd) { int fd[2]; printf("PPid:%ld Pid:%ld Size:%d ", (long)getppid(), (long)getpid(), size); int childNums[size/2], left, right; if(size
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote