Advertisement
matbiz01

Untitled

May 17th, 2021
873
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.04 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <string.h>
  8. #include <math.h>
  9. pthread_t *tid;
  10.  
  11. int **numbers;
  12. int thread_count;
  13. int width;
  14. int height;
  15. ///gcc -o test main.c -lpthread
  16. /*
  17. void* by_number(void *arg){
  18.     pthread_t id = pthread_self();
  19.     for(int x = 0; x < thread_count; x++){
  20.         if(pthread_equal(id,tid[x])){
  21.             for(int i = 0; i < height; i++){
  22.                 for(int j = 0; j < width; j++){
  23.                     if(can_change(x, numbers[i][j]) == 1){
  24.                         numbers[i][j] = 255 - numbers[i][j];
  25.                     }
  26.                 }
  27.             }
  28.         }
  29.     }
  30. }
  31. */
  32.  
  33. void* by_number(void *arg){
  34.     pthread_t id = pthread_self();
  35.     for(int i = 0; i < height; i++){
  36.         for(int j = 0; j < width; j++){
  37.             for(int x = 0; x < thread_count; x++){
  38.                 if(pthread_equal(id,tid[x])){
  39.                     if(can_change(x, numbers[i][j]) == 1){
  40.                         numbers[i][j] = 255 - numbers[i][j];
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
  47. int can_change(int thread_num, int number){
  48.     int step = 256 / thread_count;
  49.     if(thread_num * step <= number && number < (thread_num + 1) * step) return 1;
  50.     return 0;
  51. }
  52.  
  53.  
  54. void* by_block(void *arg){
  55.     pthread_t id = pthread_self();
  56.     for(int x = 0; x < thread_count; x++){
  57.         if(pthread_equal(id,tid[x])){
  58.             for(int i = 0; i < height; i++){
  59.                 int j_start = (x) * (ceil(width / thread_count) + 1);
  60.                 int j_stop = (x + 1) * (ceil(width / thread_count) + 1) - 1;
  61.                 for(j_start; j_start <= j_stop; j_start++){
  62.                    
  63.                     numbers[i][j_start] = 255 - numbers[i][j_start];
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70.  
  71. int main(int argc, char const *argv[])
  72. {
  73.     thread_count = atoi(argv[1]);
  74.     int mode = atoi(argv[2]); //mode 1 - numbers, mode 2 - blocks
  75.     char *file_path = argv[3];
  76.     char *out_name = argv[4];
  77.     FILE *f = fopen(file_path, "r");
  78.     if(f == NULL){
  79.         printf("failed to open the first file");
  80.         return 0;
  81.     }
  82.  
  83.     FILE *out = fopen(out_name, "w+");
  84.     if(out == NULL){
  85.         printf("failed to open the first file");
  86.         return 0;
  87.     }
  88.  
  89.  
  90.     tid = (pthread_t *)malloc(thread_count * sizeof(pthread_t));
  91.     int bufferLength = 255;
  92.     char buffer[bufferLength];
  93.     for(int i = 0; i < 3; i++){
  94.         fgets(buffer, bufferLength, f);
  95.         fputs(buffer, out);
  96.     }
  97.     char *pch = strtok(buffer," ");
  98.     width = atoi(pch);
  99.     pch = strtok (NULL, " ");
  100.     height = atoi(pch);
  101.  
  102.     numbers = (int **)malloc(height * sizeof(int *));
  103.     for (int i = 0; i < width; i++){
  104.         numbers[i] = (int *)malloc(width * sizeof(int));
  105.     }
  106.  
  107.     for(int i = 0; i < height; i++){
  108.         for(int j = 0; j < width; j++){
  109.             numbers[i][j] = i + j;
  110.         }
  111.     }
  112.  
  113.     fgets(buffer, bufferLength, f);
  114.     fputs(buffer, out);
  115.     pch = NULL;
  116.  
  117.     int number;
  118.  
  119.     for(int i = 0; i < height; i++){
  120.         for(int j = 0; j < width; j++){
  121.             fscanf(f, "%d", &number);
  122.             numbers[i][j] = number;
  123.         }
  124.     }
  125.  
  126.     for(int i = 0; i < height; i++){
  127.         for(int j = 0; j < width; j++){
  128.             printf("%i ", numbers[i][j]);
  129.         }
  130.         printf("\n");
  131.     }
  132.     printf("\n");
  133.  
  134.     if(mode == 1){
  135.         for(int i = 0; i < thread_count; i++){
  136.             pthread_create(&(tid[i]), NULL, &by_block, NULL);
  137.         }
  138.     }
  139.  
  140.     if(mode == 2){
  141.         for(int i = 0; i < thread_count; i++){
  142.             pthread_create(&(tid[i]), NULL, &by_number, NULL);
  143.         }
  144.     }
  145.     sleep(1);
  146.  
  147.     for(int i = 0; i < height; i++){
  148.         for(int j = 0; j < width; j++){
  149.             fprintf(out, "%d ", numbers[i][j]);
  150.             printf("%i ", numbers[i][j]);
  151.         }
  152.         fprintf(out, "\n");
  153.         printf("\n");
  154.     }
  155.  
  156.     printf("%i\n", can_change(0, 3));
  157.     return 0;
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement