Advertisement
Cosmin3105

so lab 6-7

Nov 24th, 2022
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 0 0
  1. // lab 6
  2.  
  3. // strrev.c
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <pthread.h>
  8. #include <errno.h>
  9.  
  10. void *
  11. reverse(void *v) {
  12.     char *str = (char *)v;
  13.     int n = strlen(str);
  14.     char strrev[1000];
  15.     for (int i = 0; i < 5; i++) {
  16.         strrev[i] = str[n - i - 1];
  17.     }
  18.     strrev[strlen(str)] = '\0';
  19.     printf("%s\n", strrev);    
  20. }
  21.  
  22. int main(int argc, char **argv) {
  23.     char *str = argv[1];
  24.  
  25.     pthread_t thr;
  26.     if (pthread_create(&thr, NULL, reverse, str)) {
  27.         perror(NULL);
  28.         return errno;
  29.     }  
  30.  
  31.     void *result;
  32.     if (pthread_join(thr, &result)) {
  33.         perror(NULL);
  34.         return errno;
  35.     }
  36. }
  37.  
  38. // matrice.c
  39.  
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <pthread.h>
  43. #include <errno.h>
  44. #include <stdlib.h>
  45.  
  46. int r1 = 3, c1 = 3;
  47. int mat1[10][10] = {
  48.     {2, 4, 1},
  49.     {2, 3, 9},
  50.     {3, 1, 8}
  51. };
  52. int r2 = 3, c2 = 3;
  53. int mat2[10][10] = {
  54.     {1, 2, 3},
  55.     {3, 6, 1},
  56.     {2, 4, 7}
  57. };
  58. int produs[10][10];
  59.  
  60. struct pereche {
  61.     int i, j;
  62. };
  63.  
  64. void *
  65. calcul(void *v) {
  66.     struct pereche *a = (struct pereche *)v;
  67.     for (int k = 0; k < c1; k++) {
  68.         produs[a->i][a->j] += mat1[a->i][k] * mat2[k][a->j];
  69.     }
  70. }
  71.  
  72. int main() {
  73.     for (int i = 0; i < r1; i++)
  74.         for (int j = 0; j < c2; j++)
  75.             produs[i][j] = 0;
  76.  
  77.     pthread_t thr[100];
  78.     for (int i = 0; i < r1; i++) {
  79.         for (int j = 0; j < c2; j++) {
  80.             struct pereche *a = malloc(sizeof(struct pereche));
  81.             a->i = i;
  82.             a->j = j;
  83.            
  84.             if (pthread_create(&thr[i * r1 + j], NULL, calcul, a)) {
  85.                 perror(NULL);
  86.                 return errno;
  87.             }  
  88.         }
  89.     }
  90.  
  91.     for (int i = 0; i < r1 * c2; i++) {
  92.         if (pthread_join(thr[i], NULL)) {
  93.             perror(NULL);
  94.             return errno;
  95.         }
  96.     }
  97.    
  98.     for (int i = 0; i < r1; i++) {
  99.         for (int j = 0; j < c2; j++)
  100.             printf("%d ", produs[i][j]);
  101.         printf("\n");
  102.     }
  103.  
  104. }
  105.  
  106. // lab7
  107.  
  108. // count.c
  109. #include <stdio.h>
  110. #include <pthread.h>
  111. #include <errno.h>
  112. #include <stdlib.h>
  113. #include <time.h>
  114.  
  115. #define MAX_RESOURCES 5
  116. int available_resources = MAX_RESOURCES;
  117.  
  118. pthread_mutex_t mtx;
  119.  
  120. int decrease_count(int count) {
  121.     pthread_mutex_lock(&mtx);
  122.     if (available_resources < count) {
  123.         return -1;
  124.         pthread_mutex_unlock(&mtx);
  125.     }
  126.     else {
  127.         available_resources -= count;
  128.         printf("Got %d resources, %d reamaining.\n", count, available_resources);
  129.         pthread_mutex_unlock(&mtx);
  130.     }
  131.     return 1;
  132. }
  133.  
  134. int increase_count(int count) {
  135.     pthread_mutex_lock(&mtx);
  136.     available_resources += count;
  137.     printf("Released %d resources, %d remaining.\n", count, available_resources);
  138.     pthread_mutex_unlock(&mtx);
  139.     return 0;
  140. }
  141.  
  142. void *
  143. t_count(void* v) {
  144.     int count = *(int *)v;
  145.     if (decrease_count(count)) {
  146.         increase_count(count);
  147.     }
  148. }
  149.  
  150. int main() {
  151.     srand(time(0));
  152.     if (pthread_mutex_init(&mtx, NULL)) {
  153.         perror(NULL);
  154.         return errno;
  155.     }
  156.  
  157.     pthread_t thr[10];
  158.    
  159.     for (int i = 0; i < 10; i++) {
  160.         int *count = malloc(sizeof(int));
  161.         *count = rand() % 5 + 1;
  162.         if (pthread_create(&thr[i], NULL, t_count, count)) {
  163.             perror(NULL);
  164.             return errno;
  165.         }
  166.     }
  167.  
  168.     for (int i = 0; i < 10; i++) {
  169.         if (pthread_join(thr[i], NULL)) {
  170.             perror(NULL);
  171.             return errno;
  172.         }
  173.     }
  174.  
  175.     pthread_mutex_destroy(&mtx);
  176. }
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement