Thread: merge sort with fork() and wait()

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    merge sort with fork() and wait()

    Greetings to everyone!

    As the topic title states, I want to implement merge sort, using child processes, but I can't get it right. My algorithm looks like this:

    Code:
    void merge(int arr[], int beg, int end, int pid1, int pid2)
    {
      int mid = (beg+end)/2, i = 0, *v, lo = beg, hi = mid+1;
      
      if (beg>=end) return;
      
      merge(arr, beg, mid); //this must be executed by a child process
      merge(arr, mid+1, end); //this must be executed by another child process
      
      //merge left and right - this must be executed after both child processes finish
      v = (int *)malloc(sizeof(int)*(end-beg+11));
      
        while ((lo<=mid)&&(hi<=end))
          if (arr[lo]<arr[hi]) v[i++] = arr[lo++];
          else v[i++] = arr[hi++];
          if (lo<=mid)
            for (hi = lo; hi <= mid; hi++) v[i++] = arr[hi];
          else
            if (hi<=end)
              for (lo = hi; lo <= end; lo++) v[i++] = arr[lo];
        for (i=beg, lo=0; i<=end; i++) arr[i] = v[lo++];
        free(v);
    }
    How am I suppose to write this? May you help me?

    Cheers.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'm pretty sure that you don't want separate processes sorting the data at once!!! What you want is merely different threads working on it at once. Perhaps try to get started using pthreads.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by iMalc View Post
    I'm pretty sure that you don't want separate processes sorting the data at once!!! What you want is merely different threads working on it at once. Perhaps try to get started using pthreads.
    Ditto. fork() will never work here for this, because the child receives a copy of the parent -- what happens in the child does not happen in the parent. So the children finish, but they never return anything to the parent.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by MK27 View Post
    Ditto. fork() will never work here for this, because the child receives a copy of the parent -- what happens in the child does not happen in the parent. So the children finish, but they never return anything to the parent.

    Was thinking that would not work at all. My question, is, as this interest me - for the OP, is why are you needing to implement threads for this, I do not see how this will make mergesort() any faster, unless that is not the goal here. Also if you were to implement any fork() process for this, that would just explode the resource consumption.

    Granted I do not have much experience with forking of processes much. Interested in how this post will end.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by slingerland3g View Post
    Was thinking that would not work at all. My question, is, as this interest me - for the OP, is why are you needing to implement threads for this, I do not see how this will make mergesort() any faster, unless that is not the goal here.
    Hmm, I've never used threading for this but I assume it will allow you to max out several processors instead of just one. The initial split in mergesort seems a perfect opportunity for that.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    So is there a way to make recursion (in this particular case, merge sort) using forks?

    I tried something like this ...

    Code:
    ...
    
      pid1=fork();
      if (pid1<0) exit(0); //error in creating child process
      if (pid1==0) //child process
             merge(arr, beg, mid);
      else wait();
      
      pid2=fork();
      if (pid2<0) exit(0); //error in creating child process
      if (pid2==0) //child process
            merge(arr,mid+1,end);
      else wait();
    
      if (pid1>0&&pid2>0) //I figured this is the condition for both child processes to terminate
      {
        v = (int *)malloc(sizeof(int)*(end-beg+11));
        while ((lo<=mid)&&(hi<=end))
        ...
      }
    ... yet it doesn't work quite well...

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fireatmyself View Post
    So is there a way to make recursion (in this particular case, merge sort) using forks?
    NO!!!

    It doesn't matter how you try it. fork() launches a separate process. Trying to do a mergesort that way is like saying you will write three programs:

    -program A divides the unsorted list, then gives half to program B, and half to program C.
    -program B sorts the first half.
    -program C sorts the second half.
    -all three programs are now complete.

    Guess what? You cannot join the two halves together that way UNLESS you do some "Inter Process Communcation". That could be as simple as:

    -program B and C both write their sorted lists to seperate files. Then program A reads the files and merges the lists, OR
    -program A opens a server socket, then programs B and C connect and transmit their sorted lists (one at a time) to program A, which merges them.

    There are some other possibilities here but they are just as ridiculous. Do you think this will make for a more efficient sort? It might, if the list is very very big, but it would be simpler to just learn to use threads, which will be better in all cases.

    Write this out 100 times:

    FORKED PROCESSES DO NOT SHARE ANY MEMORY. FORKED PROCESSES DO NOT SHARE ANY MEMORY. FORKED PROCESSES DO NOT SHARE ANY MEMORY...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by fireatmyself View Post
    So is there a way to make recursion (in this particular case, merge sort) using forks?

    I tried something like this ...

    ... yet it doesn't work quite well...
    If you're going to repeat yourself instead of listening to what we're telling you, then I'm going to repeat myself as well, perhaps a little more clearly this time:

    You don't want separate processes sorting their own copy of data at once, which is what fork() would do!
    What you want is merely different threads working on the same data at once.
    Google for "pthreads" or use whatever the native threading API is on your platform.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    Thank you both for your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fork and wait. need output explanation
    By ollie88r in forum C Programming
    Replies: 18
    Last Post: 11-03-2009, 09:26 AM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Signals, fork(), wait() and exec()
    By DJTurboToJo in forum C Programming
    Replies: 7
    Last Post: 03-18-2008, 09:14 AM
  4. Where are fork and wait?
    By xErath in forum C Programming
    Replies: 5
    Last Post: 11-21-2004, 12:43 AM
  5. Procesees, fork(), wait() and exec()
    By Stewdent in forum Linux Programming
    Replies: 1
    Last Post: 02-20-2003, 11:34 AM