Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.90 KB | None | 0 0
  1. /*
  2.  
  3. 2_6
  4.  
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <pthread.h>
  10. #include <semaphore.h>
  11. #include <string.h>
  12.  
  13. pthread_t t1, t2, t3, t4;
  14. pthread_mutex_t mutex;
  15. int number=0;
  16.  
  17. void* produce(void* arg);
  18. void* display(void* arg);
  19. void zad2_6() {
  20.     int res=0;
  21.     pthread_mutex_init(&mutex, NULL);
  22.     res = pthread_create(&t1, NULL, produce, (void*)1);
  23.     if (res!=0){
  24.         printf("Thread creation failed!\n");
  25.     }
  26.     pthread_create(&t2, NULL, produce, (void*)2);
  27.     if (res!=0){
  28.         printf("Thread creation failed!\n");
  29.     }
  30.     pthread_create(&t3, NULL, produce, (void*)3);
  31.     if (res!=0){
  32.         printf("Thread creation failed!\n");
  33.     }
  34.     pthread_create(&t4, NULL, display, NULL);
  35.     if (res!=0){
  36.         printf("Thread creation failed!\n");
  37.     }
  38.     pthread_join(t4, NULL);
  39. }
  40.  
  41. void *produce(void *arg) {
  42.     while (1) {
  43.         pthread_mutex_lock(&mutex);
  44.         number = (int*)arg;
  45.         pthread_mutex_unlock(&mutex);
  46.         sleep(1);
  47.  
  48.     }
  49. }
  50.  
  51. void* display(void* arg) {
  52.     while (1) {
  53.         pthread_mutex_lock(&mutex);
  54.         printf("%d\n", number);
  55.         pthread_mutex_unlock(&mutex);
  56.         sleep(3);
  57.     }
  58. }
  59.  
  60.  
  61. /*
  62.  
  63. 2_9
  64.  
  65. */
  66.  
  67. #include <stdio.h>
  68. #include <string.h>
  69. #include <stdlib.h>
  70. #include <mqueue.h>
  71. #include <unistd.h>
  72. #include <sys/stat.h>
  73. #include <sys/wait.h>
  74.  
  75. mqd_t queue;
  76. pthread_t t1, t2, t3;
  77. struct message {
  78.     char mtext[128];
  79.     int id;
  80. };
  81.  
  82. void* sender_thread(void* arg) {
  83.     int th_id = (int*)arg;
  84.     struct message buf;
  85.     struct message msg = {
  86.         .mtext = "Wiadomosc",
  87.         .id = th_id
  88.     };
  89.     while (1) {
  90.         mq_send(queue, (const char*)& msg, sizeof(buf), 1);
  91.         sleep(th_id);
  92.     }
  93.  
  94. }
  95.  
  96. void* receiver_thread(void* arg) {
  97.     struct message buf;
  98.     while (1) {
  99.         int n;
  100.         n = mq_receive(queue, (char*) &buf, sizeof(buf), NULL);
  101.         if (n != -1) {
  102.             printf("Otrzymano wiadomość o treści: %s od wątku %d \n", buf.mtext, buf.id);
  103.         }
  104.  
  105.     }
  106. }
  107.  
  108. void zad2_9() {
  109.     struct mq_attr mattr = {
  110.         .mq_maxmsg = 10,
  111.         .mq_msgsize = sizeof(struct message)
  112.     };
  113.     queue = mq_open("/myq",
  114.         O_CREAT | O_RDWR,
  115.         S_IREAD | S_IWRITE,
  116.         &mattr);
  117.  
  118.     pthread_create(&t1, NULL, sender_thread, (void*)1);
  119.     pthread_create(&t2, NULL, sender_thread, (void*)2);
  120.     pthread_create(&t3, NULL, receiver_thread, NULL);
  121.  
  122.     pthread_join(t1, NULL);
  123. }
  124.  
  125. /*
  126.  
  127. 2_10
  128.  
  129. */
  130.  
  131. #include <stdio.h>
  132. #include <stdlib.h>
  133. #include <pthread.h>
  134. #include <semaphore.h>
  135. #include <string.h>
  136.  
  137. pthread_barrier_t bariera;
  138. pthread_t t1, t2, t3;
  139.  
  140. void* calculate(void* arg){
  141.     int id = (int*)arg;
  142.     printf("Watek %d wykonuje obliczenia...\n", id);
  143.     int waittime = rand()%10 + 1;
  144.     sleep(waittime);
  145.     printf("Watek %d skonczyl obliczenia, oczekuje na pozostale watki\n", id);
  146.     pthread_barrier_wait(&bariera);
  147.     printf("Synchronizacja udana\n");
  148.     pthread_exit(0);
  149. }
  150.  
  151. void zad2_10() {
  152.     pthread_barrier_init(&bariera, NULL, 3);
  153.  
  154.     pthread_create(&t1, NULL, calculate, (void*)1);
  155.     pthread_create(&t2, NULL, calculate, (void*)2);
  156.     pthread_create(&t3, NULL, calculate, (void*)3);
  157.  
  158.     pthread_join(t1, NULL);
  159.     pthread_join(t2, NULL);
  160.     pthread_join(t3, NULL);
  161. }
  162.  
  163. /*
  164.  
  165. 3_5
  166.  
  167. */
  168.  
  169. #include <stdio.h>
  170. #include <stdlib.h>
  171. #include <pthread.h>
  172. #include <semaphore.h>
  173. #include <string.h>
  174.  
  175. #define RD 5
  176. #define WR 2
  177.  
  178. pthread_rwlock_t rwlock;
  179. pthread_t readers[RD];
  180. pthread_t writers[WR];
  181.  
  182. int dane[3] = {0,0,0};
  183.  
  184. void* reader_fun(void* arg){
  185.     int id = (int*)arg;
  186.     while(1){
  187.         usleep(500000*id);
  188.         pthread_rwlock_rdlock(&rwlock);
  189.         printf("Czytelnik %d: %d %d %d\n", id, dane[0], dane[1], dane[2]);
  190.         usleep(100000);
  191.         pthread_rwlock_unlock(&rwlock);
  192.     }
  193. }
  194.  
  195. void* writer_fun(void* arg){
  196.     int id = (int*)arg;
  197.     while(1){
  198.         usleep(5000000*id);
  199.         pthread_rwlock_wrlock(&rwlock);
  200.         printf("Pisarz %d zapisuje dane...\n", id);
  201.         usleep(300000);
  202.         dane[0] = id;
  203.         dane[1] = id+1;
  204.         dane[2] = id+2;
  205.         pthread_rwlock_unlock(&rwlock);
  206.     }
  207. }
  208.  
  209. void zad3_5(){
  210.     int i, res;
  211.     pthread_rwlock_init(&rwlock, NULL);
  212.     for (i=0; i<RD; i++){
  213.         res = pthread_create(&(readers[i]), NULL, reader_fun, (void*)(i+1));
  214.         if(res!=0){
  215.             printf("Error!\n");
  216.         }
  217.     }
  218.     for (i=0; i<WR; i++){
  219.         res = pthread_create(&(writers[i]), NULL, writer_fun, (void*)(i+1));
  220.         if(res!=0){
  221.             printf("Error!\n");
  222.         }
  223.     }
  224.  
  225.     pthread_join(readers[0], NULL);
  226. }
  227.  
  228. /*
  229.  
  230. 3_6
  231.  
  232. */
  233.  
  234. #include <stdio.h>
  235. #include <stdlib.h>
  236. #include <pthread.h>
  237. #include <semaphore.h>
  238. #include <string.h>
  239.  
  240. #define NO_OF_PHIL 5
  241.  
  242. pthread_mutex_t kelner;
  243. pthread_mutex_t widelec[NO_OF_PHIL];
  244. pthread_t filozof[NO_OF_PHIL];
  245.  
  246. void* philo_fun(void* arg){
  247.     int id = (int*)arg;
  248.     int thinkTime;
  249.     int fork1 = id;
  250.     int fork2 = id+1;
  251.     if(fork2 == NO_OF_PHIL){
  252.         fork2 = 0;
  253.     }
  254.     while(1){
  255.         //myślenie
  256.         printf("Filozof %d myśli...\n", id+1);
  257.         thinkTime = rand()%8 + 1;
  258.         sleep(thinkTime);
  259.  
  260.         //staje sie glodny
  261.         printf("Filozof %d stal sie glodny\n", id+1);
  262.  
  263.         //podniesienie widelcow
  264.         pthread_mutex_lock(&kelner);
  265.         pthread_mutex_lock(&(widelec[fork1]));
  266.         pthread_mutex_lock(&(widelec[fork2]));
  267.         pthread_mutex_unlock(&kelner);
  268.  
  269.         //jedzenie
  270.         printf("Filozof %d je\n", id+1);
  271.         sleep(3);
  272.  
  273.         //oddawanie widelcow
  274.         pthread_mutex_unlock(&(widelec[fork1]));
  275.         pthread_mutex_unlock(&(widelec[fork2]));
  276.     }
  277. }
  278.  
  279. void zad3_6(){
  280.     int i;
  281.     pthread_mutex_init(&kelner, NULL);
  282.     for(i=0; i<NO_OF_PHIL; i++){
  283.         pthread_mutex_init(&(widelec[i]), NULL);
  284.     }
  285.     for(i=0; i<NO_OF_PHIL; i++){
  286.         pthread_create(&(filozof[i]), NULL, philo_fun, (void*)i);
  287.     }
  288.     pthread_join(filozof[0], NULL);
  289. }
  290.  
  291. /*
  292.  
  293. 3_10
  294.  
  295. */
  296.  
  297. #include <stdio.h>
  298. #include <stdlib.h>
  299. #include <pthread.h>
  300. #include <semaphore.h>
  301. #include <string.h>
  302.  
  303. #define NO_OF_CONS 5
  304.  
  305. pthread_mutex_t cond_mutex;
  306. pthread_cond_t cond;
  307. pthread_cond_t finished;
  308. pthread_t consumers[5];
  309. pthread_t producer;
  310.  
  311. int data = 0;
  312. int count = 0;
  313.  
  314. void* producer_fun(void* arg){
  315.     while(1){
  316.         while(count != 0){
  317.             pthread_mutex_lock(&cond_mutex);
  318.             pthread_cond_wait(&finished, &cond_mutex);
  319.             pthread_mutex_unlock(&cond_mutex);
  320.         }
  321.         printf("Producent modyfikuje dane...\n");
  322.         sleep(5);
  323.         data = rand()%100;
  324.         count = 5;
  325.         pthread_cond_broadcast(&cond);
  326.     }
  327. }
  328.  
  329. void* consumer_fun(void* arg){
  330.     int id = (int*)arg;
  331.     while(1){
  332.         pthread_mutex_lock(&cond_mutex);
  333.         pthread_cond_wait(&cond, &cond_mutex);
  334.         pthread_mutex_unlock(&cond_mutex);
  335.         printf("Konsument %d odczytal informacje %d\n", id, data);
  336.         pthread_mutex_lock(&cond_mutex);
  337.         count--;
  338.         if(count==0){
  339.             pthread_cond_signal(&finished);
  340.         }
  341.         pthread_mutex_unlock(&cond_mutex);
  342.     }
  343. }
  344.  
  345. void zad3_10(){
  346.     int i=0;
  347.     pthread_mutex_init(&cond_mutex, NULL);
  348.     pthread_cond_init(&cond, NULL);
  349.     pthread_cond_init(&finished, NULL);
  350.  
  351.     pthread_create(&producer, NULL, producer_fun, NULL);
  352.     for(i=0; i<NO_OF_CONS; i++){
  353.         pthread_create(&(consumers[i]), NULL, consumer_fun, (void*)i);
  354.     }
  355.  
  356.     pthread_join(producer, NULL);
  357.  
  358. }
  359.  
  360. /*
  361.  
  362. 4.9
  363.  
  364. */
  365.  
  366. #include <stdio.h>
  367. #include <omp.h>
  368.  
  369. #define N 10
  370.  
  371. void zad4_9(){
  372.     float A[N], B[N/2], max;
  373.     int i, th_id;
  374.     for(i=0; i<N; i++){
  375.         A[i] = (float) (rand()%100 + 1);
  376.         printf("%f ", A[i]);
  377.     }
  378.     printf("\n");
  379.     max = A[0];
  380.     omp_set_num_threads(N/2);
  381.     #pragma omp parallel default(none), private(i, max, th_id), shared(A, B)
  382.     {
  383.         th_id = omp_get_thread_num();
  384.         max = A[th_id*2];
  385.         #pragma omp for
  386.         for(i=0; i<N; i++){
  387.             if(A[i] > max)
  388.                 max = A[i];
  389.         }
  390.         B[th_id] = max;
  391.     }
  392.     for(i=0; i<N/2; i++){
  393.         printf("%f ", B[i]);
  394.         if(B[i] > max)
  395.             max = B[i];
  396.     }
  397.     printf("\n");
  398.     printf("Max: %f\n", max);
  399.    // printf("Max: %d\n", max);
  400.  
  401. }
  402.  
  403. /*
  404.  
  405. 4.10
  406.  
  407. */
  408.  
  409. #include <stdio.h>
  410. #include <omp.h>
  411. #include <math.h>
  412.  
  413. #define R 100000
  414. #define SHOTS 10000000
  415.  
  416. float odleglosc(int x1, int y1, int x2, int y2){
  417.     return sqrt( (float)(x2-x1)*(x2-x1) + (float)(y2-y1)*(y2-y1) );
  418. }
  419.  
  420. void zad4_10(){
  421.     int inside=0, total=0, x, y, i;
  422.     float distance, pi;
  423.     omp_set_num_threads(10);
  424.  
  425.     #pragma omp parallel default(none), private(x, y, distance, i), reduction(+:inside,total)
  426.     {
  427.         #pragma omp for
  428.         for(i=0; i<SHOTS; i++){
  429.             x = rand()%(R*2 + 1) - R;
  430.             y = rand()%(R*2 + 1) - R;
  431.             distance = odleglosc(x, y, 0, 0);
  432.             if(distance <= R)
  433.                 inside++;
  434.             total++;
  435.         }
  436.     }
  437.     pi = (float)4*((float)inside/total);
  438.     printf("Inside: %d\nTotal: %d\nPI: %f\n", inside, total, pi);
  439. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement