• C

i'm trying to write a parallel algorithm for sorting a list of words. using fork() and pipe().

i have written this code and i'm not sure why it isn't working. if anyone has any ideas i'd be very grateful.
the program takes in a list of words from a file and sorts them. it is then supposed to output the list to another file but my output list is the
same as my input list.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXCHAR 20 // This is the maximum number of characters allowed
//in a filename.
#define MAXNOWORDS 300 // This is the maximum number of words allowed


// Function declarations
void error_exit(char *s);
int file_length(FILE *file);
int fget_cell(FILE *cell_file, int max, char *cell);
int read_data_file(FILE *datafile, char *p[]);
int print(FILE *datafile, char *p[], int n);
char downify(char c);
void downstring(char *str);
void swap(char **l, char **s);
void bubble_sort(char* p[], int n);
int my_compare(char* p[], int i, int j);

int main(void){
      // Variable declarations
      char* p[MAXNOWORDS];
      char *array[MAXNOWORDS],*a[MAXNOWORDS],*b[MAXNOWORDS],*v[1];
      int i,n,outwords,flag[2][2],value=1,c=0,d=0;
      FILE *datafile;
      char filename[MAXCHAR ];
      printf("Welcome to the Word Sort program!\n");

      
      // Open data file for reading of unsorted wordlist
      datafile=NULL;
      while(datafile==NULL)
      {
            printf("Please enter data input filename (list.txt): ");
            scanf("%s", filename);
            
            datafile=fopen(filename, "r");
            
      }
      
      n=read_data_file(datafile,array);
      fclose(datafile);
      v[1] = calloc(strlen(array[1])+1,sizeof(char));
      v[1]="g";
      
      
      
      for (i=0;i<n;i++)
      {printf("word %d=%s",i,array[i]);
      if (strcmp(v[1],array[i]) > 0)
      {
            a[c]=calloc(strlen(array[i])+1, sizeof(char));
            strcpy(a[c],array[i]);
            c++;
      }
      else
      {
            b[d]=calloc(strlen(array[i])+1, sizeof(char));
            strcpy(a[d],array[i]);
            d++;
      }
      }
      for(i=0; i<c;i++)
      printf("Hi %s ",a[c]);
      printf(" \n");
      for(i=0; i<d;i++)
      printf("Hi %s ",b[d]);
      printf(" \n");

      // If the file contained too many words to load print an error message and
//exit.
      if(n > MAXNOWORDS)
      {
            printf("\n\tError: sorry but your file contains %d words\n\tbut this rogram can only sort %d words.\nExiting.\n", n, MAXNOWORDS);

            return 1; // This means the program exits and an error code is sent to the
//shell.
      }

      // Setup an array of pointers to the words this will improve speed of
//sorting.
      for(i=0;i<n;i++)
      {
            p[i] = array[i];
      }

      for( i = 0; i<2; i++){
            if (pipe(flag[i]) == -1)                                /* create a pipe
*/
                  error_exit("pipe(1,i) failed");
      }
      // Call the actual sorting algorithm
      
      
      
      
      
      for(i=0;i<1;i++){
            value = fork();
            if(value == 0){
                  bubble_sort(p,n);
                  
                  
                  printf("i=%d :: %s, %s, %s, %s\n",i,p[i*4],p[i*4+1],p[i*4+2],p[i*4+3]);
                  exit(0);
            }
            printf("value=%d, i=%d, %s, %s, %s, %s\n",value,i,p[i*4],p[i*4+1],p[i*4+2],p[i*4+3]);

      }

      printf("Sleeping...\n");
      sleep(10);
      for(i=0;i<2;i++){
            printf("value=%d, i=%d, %s, %s, %s, %s\n",value,i,p[i*4],p[i*4+1],p[i*4+2],p[i*4+3]);

      }
      // Open data file for output of sorted wordlist.
      datafile=NULL;
      while(datafile==NULL)
      {
            printf("\nPlease enter data output filename (if the file already exists\nit will be overwritten): ");

            scanf("%s", filename);
            datafile=fopen(filename, "w");
      }
      outwords=print(datafile,p,n);
      fclose(datafile);
      if(outwords!=n)
      {
            printf("\nAn error occurred while printing the sorted wordlist to a file.\n");

            printf("%d words were outputted while %d words were to be sorted.\n",outwords, n);

            return 1;
      }

      // Exit successfully
      return 0;
}

// Prints the sorted list of words to a file, one entry per line.
int print(FILE *datafile, char *p[], int n)
{
      int i;

      for (i=0;i<n;++i)
      {
            fprintf(datafile,"%s\n", p[i]);
      }

      return i;
}

// This function reads each word from the input file into dynamically
//allocated memory
// and returns the number of words in the file.
int read_data_file(FILE *datafile, char* array[])
{
      int n, row, i, newline;
      char c;

      // Find no of rows in file
      n = file_length(datafile);

      printf("Loading word list file....");
      for (row=0; (row < n) && (row < MAXNOWORDS); row++)
      {
            array[row]=(char *)calloc(1,sizeof(char[MAXCHAR ]));
            fscanf(datafile,"%s\n",array[row]);
            printf("word %d=%s",row,array[row]);
      }

      return n;
}

// This function counts the number of words in the file.
int file_length(FILE *file)
{
      int lines;
      char dummy[100];

      rewind(file);

      lines=0;
      while( fgets(dummy, 100, file) != NULL)
      lines++;

      rewind(file);

      return(lines);
}

// This is the bubble sort algorithm. It repeatedly traverses the list of
//words
// bringing the alphabetically last eg 'zzzz' element to the end of the list
//then
// the second last to the second last place etc until all elements have been
//sorted.
void bubble_sort(char *p[], int n)
{
      int i,j;

      
      for (i=0;i<(n-1);++i)
      
            
            for (j=0;j<n-1-i;j++)
            for(j=i+1; j<n; ++j)
            if(strcmp(p[i], p[j])>0)
            swap(&p[i], &p[j]);
      

}
// This function compares two strings alphabetically and returns 1 if the
//second string
// should precede the first string. It works on both upper and lower case
//strings by
// converting them all first to lower case strings.
int my_compare(char *p[], int i, int j)
{
      char str1[MAXCHAR],str2[MAXCHAR];
      int compare;

      // Use temporary variables for strings so converting them to lower case
      // doesn't affect the original string.
      strcpy(str1,p[i]);
      strcpy(str2,p[j]);
      // Convert both strings to lower case so case distinction won't matter
      // when comparing them.
      downstring(str1);
      downstring(str2);

      compare = strcmp(str1,str2);

      if(compare <= 0) // In this case str1 alphabetically precedes str2 or they
//are equal
            return 0;
      else
            return 1; // In this case str2 alphabetically precedes str1
}

// This function converts a string to its lower case equivalent.
void downstring(char *str)
{
  int i;

  for(i=0; *(str+i)!='\0';i++)
  {
  str[i]=downify(str[i]);
  }
}

// This function converts a character to its lower case equivalent
char downify(char c)
{
  if ((c >= 'A') && (c <= 'Z'))
    return (c + 'a' - 'A');
  else
    return (c);
}

// This function swaps two strings
void swap(char **l, char **s)
{
      char *tmp;

      tmp =*l;
      *l=*s;
      *s=tmp;

      //return;
}

void error_exit(char *s)
{
   fprintf(stderr, "\nERROR: %s - bye!\n", s);
   exit(1);
}

ronanh09Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:

A quick look suggests that you're mixing tasks and threads.

The sorted list that is generated in the spawned task does not share memory with the original task.  When it completes the sort, the data is available only to that task.


Kent
0
grg99Commented:
{
          b[d]=calloc(strlen(array[i])+1, sizeof(char));
          strcpy(a[d],array[i]);
          d++;
     }


dont you mean:   strcpy( b[d], ...  ?
0
ronanh09Author Commented:
dont you mean:   strcpy( b[d], ...  ?

ya just changed that but it didn't make a diference . thanks anyway.


"A quick look suggests that you're mixing tasks and threads.

The sorted list that is generated in the spawned task does not share memory with the original task.  When it completes the sort, the data is available only to that task.Kent"

thanks kent. how would i go about sorting this out?



0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Kent OlsenData Warehouse Architect / DBACommented:

Are you familiar with threads?  They are a very commonly used functionality that you'll undoubted see.  (You can't program an effective GUI application without them.)  Threads are nothing more than a way for one program to have multiple execution points.  Tasks are really separate programs.

If you want to keep using pipes, you'll have to use shared memory to contain the shared buffers, or use pipes to transfer data between the threads.


Kent
0
ronanh09Author Commented:
ya i'm familiar with threads. i have to keep using pipes though cause it's part of an assignment.
0
Kent OlsenData Warehouse Architect / DBACommented:
Are you using Windows or linux?

0
ronanh09Author Commented:
linux.
0
Kent OlsenData Warehouse Architect / DBACommented:

You could declare a shared region using mmap().  This would be my approach.

But if your assignment calls for using pipes to transfer the data between tasks, you'd better follow that lead.

After spawning the sort thread, the main pipe should read the sorted data from the sort task.  The sort task, of course, needs to write the sorted data to it's output pipe.


Kent
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ronanh09Author Commented:
i thought i'd done that. what in the program can be altered to do this? sorry but i'm only really a beginner at c!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.