Advertisement
EBobkunov

ex2

Oct 1st, 2023 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <time.h>
  7.  
  8. #define VECTOR_SIZE 120
  9.  
  10. void generateRandomVector(int vector[], int size) {
  11.     for (int i = 0; i < size; i++) {
  12.         vector[i] = rand() % 100; // Random values in the range [0-99]
  13.     }
  14. }
  15.  
  16. int calculatePartialDotProduct(int u[], int v[], int start, int end) {
  17.     int result = 0;
  18.     for (int i = start; i < end; i++) {
  19.         result += u[i] * v[i];
  20.     }
  21.     return result;
  22. }
  23.  
  24. int main() {
  25.     srand(time(NULL));
  26.  
  27.     int u[VECTOR_SIZE];
  28.     int v[VECTOR_SIZE];
  29.     generateRandomVector(u, VECTOR_SIZE);
  30.     generateRandomVector(v, VECTOR_SIZE);
  31.  
  32.     int n;
  33.     printf("Enter the number of processes (n): ");
  34.     scanf("%d", &n);
  35.  
  36.     if (n <= 0 || n % 2 != 0) {
  37.         printf("Please enter a valid even number of processes.\n");
  38.         return 1;
  39.     }
  40.  
  41.     int pipefd[2];
  42.     if (pipe(pipefd) == -1) {
  43.         perror("Pipe creation failed");
  44.         return 1;
  45.     }
  46.  
  47.     for (int i = 0; i < n; i++) {
  48.         pid_t child_pid = fork();
  49.  
  50.         if (child_pid == -1) {
  51.             perror("Fork failed");
  52.             return 1;
  53.         }
  54.  
  55.         if (child_pid == 0) { // Child process
  56.             close(pipefd[0]); // Close the read end of the pipe in the child
  57.  
  58.             int start = i * VECTOR_SIZE / n;
  59.             int end = (i + 1) * VECTOR_SIZE / n;
  60.  
  61.             int partialResult = calculatePartialDotProduct(u, v, start, end);
  62.  
  63.             // Write the result to the shared file (temp.txt)
  64.             FILE* file = fopen("temp.txt", "a");
  65.             fprintf(file, "Process %d: Partial Result = %d\n", getpid(), partialResult);
  66.             fclose(file);
  67.  
  68.             close(pipefd[1]); // Close the write end of the pipe in the child
  69.             exit(0);
  70.         }
  71.     }
  72.  
  73.     close(pipefd[1]); // Close the write end of the pipe in the parent
  74.  
  75.     // Wait for all child processes to finish
  76.     for (int i = 0; i < n; i++) {
  77.         wait(NULL);
  78.     }
  79.  
  80.     // Read and aggregate the results from the shared file (temp.txt)
  81.     int totalResult = 0;
  82.     FILE* file = fopen("temp.txt", "r");
  83.     if (file != NULL) {
  84.         char line[100];
  85.         while (fgets(line, sizeof(line), file)) {
  86.             int partialResult;
  87.             sscanf(line, "Process %*d: Partial Result = %d", &partialResult);
  88.             totalResult += partialResult;
  89.         }
  90.         fclose(file);
  91.     } else {
  92.         perror("Error opening shared file (temp.txt)");
  93.     }
  94.  
  95.     printf("Dot Product Result = %d\n", totalResult);
  96.  
  97.     return 0;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement