Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Task 2: Develop a program that reads a large text file (data.txt) containing numeric…

  1. User

    Task 2: Develop a program that reads a large text file (data.txt) containing numeric data separated by spaces across multiple lines. The program should perform three different analyses on the file:

    1. Calculating the total sum
    2. Finding the average of all numbers
    3. Identifying the maximum and minimum values. Ensure that all the analyses are performed efficiently and simultaneously, and the results are aggregated and printed at the end. Hint: Use 3 processes to perform the 3 tasks listed above. Task 3: Create a program that processes a few text files to analyze word usage. The program should:
    4. Count the frequency of each word in the text.
    5. Identify the top 10 most frequently occurring words.
    6. Print the results in descending order of frequency. Hint: Process the text files simultaneously. Take text file names through command line arguments.

    Task 4: Create a program that sorts the contents of a text file (unsorted_numbers.txt) containing a list of integers, one per line. The program should:

    1. Divide the list into two halves and sort each half separately
    2. Use execlp() to call an external sorting utility (such as the sort command in Linux) for each half.
    3. Merge the two sorted halves back into a single, sorted list.
    4. Output the fully sorted list to a new file (sorted_numbers.txt)
  2. Assistant

    To accomplish the tasks you've outlined, we'll create separate programs for each task. Below are the implementations for Tasks 2, 3, and 4 as per your requirements.


    Task 2: Numeric Data Analysis Using Processes

    Objective: Read data.txt, perform three analyses simultaneously using three processes:

    1. Calculate the total sum.
    2. Find the average of all numbers.
    3. Identify the maximum and minimum values.

    Implementation Details:

    • Use three child processes to perform the calculations concurrently.
    • Utilize shared memory to store and aggregate the results.
    • Ensure the parent process waits for all child processes to complete before printing the results.

    Code Implementation:

    // Filename: task2.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/mman.h>
    #include <sys/wait.h>
    
    typedef struct {
        double sum;
        double average;
        double min;
        double max;
    } Results;
    
    int main() {
        // Create shared memory for results
        Results *results = mmap(NULL, sizeof(Results), PROT_READ | PROT_WRITE,
                                MAP_SHARED | MAP_ANONYMOUS, -1, 0);
        if (results == MAP_FAILED) {
            perror("mmap");
            exit(EXIT_FAILURE);
        }
    
        // Initialize min and max
        results->min = __DBL_MAX__;
        results->max = -__DBL_MAX__;
    
        pid_t pid1, pid2, pid3;
    
        // Fork process for sum
        pid1 = fork();
        if (pid1 == -1) {
            perror("fork");
            exit(EXIT_FAILURE);
        }
        if (pid1 == 0) {
            // Child process 1: Calculate sum
            FILE *fp = fopen("data.txt", "r");
            if (fp == NULL) {
                perror("fopen");
                exit(EXIT_FAILURE);
            }
            double num;
            double sum = 0;
            while (fscanf(fp, "%lf", &num) == 1) {
                sum += num;
            }
            fclose(fp);
            results->sum = sum;
            exit(EXIT_SUCCESS);
        }
    
        // Fork process for average
        pid2 = fork();
        if (pid2 == -1) {
            perror("fork");
            exit(EXIT_FAILURE);
        }
        if (pid2 == 0) {
            // Child process 2: Calculate average
            FILE *fp = fopen("data.txt", "r");
            if (fp == NULL) {
                perror("fopen");
                exit(EXIT_FAILURE);
            }
            double num;
            double sum = 0;
            int count = 0;
            while (fscanf(fp, "%lf", &num) == 1) {
                sum += num;
                count++;
            }
            fclose(fp);
            if (count > 0) {
                results->average = sum / count;
            } else {
                results->average = 0;
            }
            exit(EXIT_SUCCESS);
        }
    
        // Fork process for min and max
        pid3 = fork();
        if (pid3 == -1) {
            perror("fork");
            exit(EXIT_FAILURE);
        }
        if (pid3 == 0) {
            // Child process 3: Find min and max
            FILE *fp = fopen("data.txt", "r");
            if (fp == NULL) {
                perror("fopen");
                exit(EXIT_FAILURE);
            }
            double num;
            double min = __DBL_MAX__;
            double max = -__DBL_MAX__;
            while (fscanf(fp, "%lf", &num) == 1) {
                if (num < min) min = num;
                if (num > max) max = num;
            }
            fclose(fp);
            results->min = min;
            results->max = max;
            exit(EXIT_SUCCESS);
        }
    
        // Wait for child processes to finish
        waitpid(pid1, NULL, 0);
        waitpid(pid2, NULL, 0);
        waitpid(pid3, NULL, 0);
    
        // Print results
        printf("Total Sum: %lf\n", results->sum);
        printf("Average: %lf\n", results->average);
        printf("Minimum Value: %lf\n", results->min);
        printf("Maximum Value: %lf\n", results->max);
    
        // Unmap shared memory
        if (munmap(results, sizeof(Results)) == -1) {
            perror("munmap");
            exit(EXIT_FAILURE);
        }
    
        return 0;
    }
    

    Instructions to Compile and Run:

    1. Compile the program:

      gcc -o task2 task2.c
      
    2. Run the program:

      ./task2
      

    Note: Ensure that data.txt is present in the same directory as the executable and contains the numeric data separated by spaces across multiple lines.


    Task 3: Word Frequency Analysis Across Multiple Files

    Objective: Process multiple text files to analyze word usage:

    1. Count the frequency of each word in the text files.
    2. Identify the top 10 most frequently occurring words.
    3. Print the results in descending order of frequency.

    Implementation Details:

    • Take filenames as command-line arguments.
    • Use threading to process files simultaneously.
    • Use a mutex lock to update a shared hash map safely.
    • Aggregate word counts from all files.
    • Sort and display the top 10 words by frequency.

    Code Implementation:

    // Filename: task3.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pthread.h>
    #include <ctype.h>
    #include <unistd.h>
    
    #define MAX_WORD_LEN 100
    #define HASH_SIZE 1000
    
    typedef struct WordNode {
        char *word;
        int count;
        struct WordNode *next;
    } WordNode;
    
    WordNode *hashTable[HASH_SIZE];
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    
    unsigned int hash(const char *word) {
        unsigned int hash = 5381;
        int c;
        while ((c = *word++))
            hash = ((hash << 5) + hash) + tolower(c); /* hash * 33 + c */
        return hash % HASH_SIZE;
    }
    
    void insertWord(const char *word) {
        unsigned int idx = hash(word);
    
        pthread_mutex_lock(&mutex);
    
        WordNode *node = hashTable[idx];
        while (node != NULL) {
            if (strcasecmp(node->word, word) == 0) {
                node->count++;
                pthread_mutex_unlock(&mutex);
                return;
            }
            node = node->next;
        }
    
        // Create new node
        WordNode *newNode = (WordNode *)malloc(sizeof(WordNode));
        newNode->word = strdup(word);
        newNode->count = 1;
        newNode->next = hashTable[idx];
        hashTable[idx] = newNode;
    
        pthread_mutex_unlock(&mutex);
    }
    
    void *processFile(void *arg) {
        char *filename = (char *)arg;
        FILE *fp = fopen(filename, "r");
        if (fp == NULL) {
            perror("fopen");
            pthread_exit(NULL);
        }
    
        char word[MAX_WORD_LEN];
        int c, idx = 0;
    
        while ((c = fgetc(fp)) != EOF) {
            if (isalpha(c)) {
                word[idx++] = c;
                if (idx >= MAX_WORD_LEN - 1)
                    idx = MAX_WORD_LEN - 2;
            } else if (idx > 0) {
                word[idx] = '\0';
                insertWord(word);
                idx = 0;
            }
        }
    
        // Insert the last word if file doesn't end with a non-alpha character
        if (idx > 0) {
            word[idx] = '\0';
            insertWord(word);
        }
    
        fclose(fp);
        pthread_exit(NULL);
    }
    
    int compareWords(const void *a, const void *b) {
        WordNode *wordA = *(WordNode **)a;
        WordNode *wordB = *(WordNode **)b;
        return wordB->count - wordA->count;
    }
    
    void printTopWords(int topN) {
        // Collect all words into an array
        int totalWords = 0;
        WordNode *nodes[HASH_SIZE * 10]; // Arbitrary size
        for (int i = 0; i < HASH_SIZE; i++) {
            WordNode *node = hashTable[i];
            while (node != NULL) {
                nodes[totalWords++] = node;
                node = node->next;
            }
        }
    
        // Sort words by frequency
        qsort(nodes, totalWords, sizeof(WordNode *), compareWords);
    
        // Print top N words
        printf("Top %d words:\n", topN);
        for (int i = 0; i < topN && i < totalWords; i++) {
            printf("%s: %d\n", nodes[i]->word, nodes[i]->count);
        }
    }
    
    int main(int argc, char *argv[]) {
        if (argc < 2) {
            printf("Usage: %s <text_file1> [text_file2 ...]\n", argv[0]);
            exit(EXIT_FAILURE);
        }
    
        int numFiles = argc - 1;
        pthread_t threads[numFiles];
    
        // Create threads to process files simultaneously
        for (int i = 0; i < numFiles; i++) {
            if (pthread_create(&threads[i], NULL, processFile, argv[i + 1]) != 0) {
                perror("pthread_create");
                exit(EXIT_FAILURE);
            }
        }
    
        // Wait for all threads to finish
        for (int i = 0; i < numFiles; i++) {
            pthread_join(threads[i], NULL);
        }
    
        // Print the top 10 most frequent words
        printTopWords(10);
    
        // Cleanup
        for (int i = 0; i < HASH_SIZE; i++) {
            WordNode *node = hashTable[i];
            while (node != NULL) {
                WordNode *temp = node;
                node = node->next;
                free(temp->word);
                free(temp);
            }
        }
        pthread_mutex_destroy(&mutex);
    
        return 0;
    }
    

    Instructions to Compile and Run:

    1. Compile the program with threading support:

      gcc -o task3 task3.c -pthread
      
    2. Run the program with text files as arguments:

      ./task3 file1.txt file2.txt file3.txt
      

    Note: Replace file1.txt, file2.txt, file3.txt with the actual filenames you wish to process.


    Task 4: Sorting Integers Using External Sorting Utility

    Objective: Sort the integers from unsorted_numbers.txt and output to sorted_numbers.txt by:

    1. Dividing the list into two halves and sorting each half separately.
    2. Using execlp() to call an external sorting utility (the sort command).
    3. Merging the two sorted halves into a single sorted list.
    4. Outputting the fully sorted list to sorted_numbers.txt.

    Implementation Details:

    • Read the input file and split the data into two temporary files.
    • Use fork() and execlp() to call the sort command on each half.
    • Read the sorted halves and merge them.
    • Write the final sorted list to sorted_numbers.txt.
    • Clean up temporary files.

    Code Implementation:

    // Filename: task4.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    #define MAX_LINE 1024
    
    void mergeFiles(const char *file1, const char *file2, const char *outputFile) {
        FILE *fp1 = fopen(file1, "r");
        FILE *fp2 = fopen(file2, "r");
        FILE *out = fopen(outputFile, "w");
    
        if (!fp1 || !fp2 || !out) {
            perror("fopen");
            exit(EXIT_FAILURE);
        }
    
        char line1[MAX_LINE];
        char line2[MAX_LINE];
        int num1, num2;
        int hasNum1 = fscanf(fp1, "%d", &num1);
        int hasNum2 = fscanf(fp2, "%d", &num2);
    
        while (hasNum1 == 1 && hasNum2 == 1) {
            if (num1 <= num2) {
                fprintf(out, "%d\n", num1);
                hasNum1 = fscanf(fp1, "%d", &num1);
            } else {
                fprintf(out, "%d\n", num2);
                hasNum2 = fscanf(fp2, "%d", &num2);
            }
        }
    
        while (hasNum1 == 1) {
            fprintf(out, "%d\n", num1);
            hasNum1 = fscanf(fp1, "%d", &num1);
        }
    
        while (hasNum2 == 1) {
            fprintf(out, "%d\n", num2);
            hasNum2 = fscanf(fp2, "%d", &num2);
        }
    
        fclose(fp1);
        fclose(fp2);
        fclose(out);
    }
    
    int main() {
        FILE *input = fopen("unsorted_numbers.txt", "r");
        if (!input) {
            perror("fopen");
            exit(EXIT_FAILURE);
        }
    
        // Create two temporary files
        FILE *temp1 = fopen("temp1.txt", "w");
        FILE *temp2 = fopen("temp2.txt", "w");
        if (!temp1 || !temp2) {
            perror("fopen");
            exit(EXIT_FAILURE);
        }
    
        // Read numbers and split into two halves
        int num;
        int count = 0;
        int totalCount = 0;
        while (fscanf(input, "%d", &num) == 1) {
            totalCount++;
        }
        fseek(input, 0, SEEK_SET); // Reset file pointer
    
        int half = totalCount / 2;
        while (fscanf(input, "%d", &num) == 1) {
            if (count < half) {
                fprintf(temp1, "%d\n", num);
            } else {
                fprintf(temp2, "%d\n", num);
            }
            count++;
        }
    
        fclose(input);
        fclose(temp1);
        fclose(temp2);
    
        pid_t pid1, pid2;
    
        // Sort first half
        pid1 = fork();
        if (pid1 == -1) {
            perror("fork");
            exit(EXIT_FAILURE);
        }
        if (pid1 == 0) {
            execlp("sort", "sort", "-n", "temp1.txt", "-o", "sorted1.txt", (char *)NULL);
            perror("execlp");
            exit(EXIT_FAILURE);
        }
    
        // Sort second half
        pid2 = fork();
        if (pid2 == -1) {
            perror("fork");
            exit(EXIT_FAILURE);
        }
        if (pid2 == 0) {
            execlp("sort", "sort", "-n", "temp2.txt", "-o", "sorted2.txt", (char *)NULL);
            perror("execlp");
            exit(EXIT_FAILURE);
        }
    
        // Wait for both sorts to finish
        waitpid(pid1, NULL, 0);
        waitpid(pid2, NULL, 0);
    
        // Merge the sorted halves
        mergeFiles("sorted1.txt", "sorted2.txt", "sorted_numbers.txt");
    
        // Cleanup temporary files
        remove("temp1.txt");
        remove("temp2.txt");
        remove("sorted1.txt");
        remove("sorted2.txt");
    
        printf("Sorting complete. Output written to 'sorted_numbers.txt'\n");
    
        return 0;
    }
    

    Instructions to Compile and Run:

    1. Compile the program:

      gcc -o task4 task4.c
      
    2. Run the program:

      ./task4
      

    Prerequisites:

    • Ensure that unsorted_numbers.txt exists in the same directory and contains the list of integers to sort.
    • The sort command-line utility must be available (standard on Linux systems).

    Note to User:

    • Make sure to have the necessary input files (data.txt, text files for Task 3, unsorted_numbers.txt) in the correct format and accessible to the programs.
    • The programs assume standard formats and may need adjustments based on specific data characteristics.
    • Error handling is minimal; for production use, consider adding more robust error checking and handling mechanisms.