Advertisement
Guest User

its_bsp_bitches

a guest
Nov 26th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.16 KB | None | 0 0
  1. //fifo.h//
  2.  
  3.  
  4.  
  5. #ifndef FIFO_H
  6. #define FIFO_H
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <pthread.h>
  11. #include <errno.h>
  12. #include <string.h>
  13. #include <semaphore.h>
  14.  
  15.  
  16. void semaphore();
  17. void writeLetter(char letter);
  18. char readLetter();
  19. void errorhandling(int value, const char *message);
  20.  
  21. #endif
  22.  
  23. //fifo.c//
  24.  
  25. /*
  26.  *aufgabe2.c
  27.  *
  28.  *  Created on: 11.11.2014
  29.  *      Author:
  30.  *      Thorben Schnirpel,
  31.  *      MatrNr: 2125495
  32.  *      Felix Stoessel
  33.  *      Matr.Nr 2141549
  34.  *
  35.  */
  36.  
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <pthread.h>
  41. #include <errno.h>
  42. #include <string.h>
  43. #include <semaphore.h>
  44.  
  45. #include "fifo.h"
  46.  
  47.  
  48. #define BUFFER_GROESSE 10
  49.  
  50. char buffer[BUFFER_GROESSE];
  51. int read_index=0;
  52. int write_index=0;
  53.  
  54. //Semaphor
  55. sem_t SemEmpty;
  56. sem_t SemFull;
  57.  
  58. //Mutex
  59. pthread_mutex_t fifo_mutex = PTHREAD_MUTEX_INITIALIZER;
  60.  
  61.  
  62. void semaphore() {
  63.         //Semaphoren initialisieren
  64.     errorhandling(sem_init(&SemEmpty, 0, 10), "Error: Sem Init Empty\n");
  65.     errorhandling(sem_init(&SemFull, 0, 0), "Error: Sem Init Full\n");
  66. }
  67.  
  68.  
  69.  
  70. void writeLetter(char letter) {
  71.  
  72.     // Wait decrements idleS
  73.     errorhandling(sem_wait(&SemEmpty), "Error: Sem Wait Schreiben\n");
  74.  
  75.     errorhandling(pthread_mutex_lock(&fifo_mutex), "Error: Lock Mutex\n");
  76.    
  77.     //Kritischer Abschnitt - Anfang
  78.     if (write_index >= BUFFER_GROESSE) {
  79.         write_index = 0;
  80.     }
  81.  
  82.     buffer[write_index] = letter;
  83.     write_index++;
  84.     //Kritischer Abschnitt - Ende
  85.  
  86.     errorhandling(pthread_mutex_unlock(&fifo_mutex), "Error: Unlock Mutex\n");
  87.    
  88.     // increments busyS
  89.     errorhandling(sem_post(&SemFull), "Error: Sem Post Schreiben\n");
  90.  
  91. }
  92.  
  93. char readLetter() {
  94.     //tmp fuer spaetere Ausgabe
  95.     char tmp;
  96.  
  97.     // Wait decrements busyS
  98.     errorhandling(sem_wait(&SemFull), "Error: Sem Wait Lesen\n");
  99.  
  100.     errorhandling(pthread_mutex_lock(&fifo_mutex), "Error: Lock Mutex\n");
  101.    
  102.  
  103.     //Kritischer Abschnitt - Anfang
  104.     if (read_index >= BUFFER_GROESSE) {
  105.         read_index = 0;
  106.     }
  107.     tmp = buffer[read_index];
  108.     read_index++;
  109.     //Kritischer Abschnitt - Ende
  110.  
  111.    
  112.     errorhandling(pthread_mutex_unlock(&fifo_mutex), "Error: Lock Mutex\n");
  113.    
  114.     // increments idleS
  115.     errorhandling(sem_post(&SemEmpty), "Error: Sem Post Lesen\n"); // decrement num of Elem in Buffer
  116.  
  117.     return tmp;
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. //errorhandling.c//
  130.  
  131.  #include "fifo.h"
  132.  
  133.  
  134.  void errorhandling(int value, const char *message);
  135.  
  136.  void errorhandling(int value, const char *message) {
  137.  
  138.     if ((value) == -1) {
  139.        
  140.         perror(message);
  141.     }
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. //main.c//
  149.  
  150. /*
  151.  *aufgabe2.c
  152.  *
  153.  *  Created on: 11.11.2014
  154.  *      Author:
  155.  *      Thorben Schnirpel,
  156.  *      MatrNr: 2125495
  157.  *      Felix Stoessel
  158.  *      Matr.Nr 2141549
  159.  *
  160.  */
  161.  
  162. #include "fifo.h"
  163.  
  164. #define ABC 26
  165. #define THREADSIZE 4
  166.  
  167.  
  168.  
  169. //Mutex
  170.  
  171. pthread_mutex_t p1_mutex = PTHREAD_MUTEX_INITIALIZER;
  172. pthread_mutex_t p2_mutex = PTHREAD_MUTEX_INITIALIZER;
  173. pthread_mutex_t consumer_mutex = PTHREAD_MUTEX_INITIALIZER;
  174.  
  175.  
  176. //Threadarray
  177. pthread_t threads[THREADSIZE];
  178. int thread_id[THREADSIZE] = { 0, 1, 2, 3 };
  179.  
  180.  
  181. // Prototypen
  182. void* produce1(void *pid);
  183. void* produce2(void *pid);
  184. void* consume(void *pid);
  185. void* control(void *pid);
  186. void end();
  187. void errorhandling(int value, const char *message);
  188.  
  189. //Legt ein Array mit grossbuchstaben und eins mit kleinbuchstaben
  190.  
  191. char kleinarray[ABC];
  192. char grossarray[ABC];
  193. typedef struct alphabetstruct {
  194.     char alpha[ABC];
  195.     void* pid;
  196.     }g,k;
  197.  
  198.    
  199. // Bei 0 wird das Programm beendet
  200. int running = 1;
  201. int test = 1;
  202. int p1 = 1;
  203. int p2 = 1;
  204. int c = 1;
  205.  
  206.  
  207. void *control(void *pid) {
  208.     char input = 0;
  209.  
  210.     printf("Controller gestartet\n");
  211.  
  212.     while (running == 1) {
  213.         input = getchar();
  214.  
  215.         switch (input) {
  216.         case '1':   //producer 1
  217.  
  218.             if (p1 == 1) {// Wenn Producer 1 laeft dann wird er gestoppt bis erneut 1 eingegeben wird
  219.                 p1 = 0;
  220.                 errorhandling(pthread_mutex_lock(&p1_mutex),
  221.                         "Error: Lock Mutex\n");
  222.  
  223.                 printf("Producer1 gestoppt.\n");
  224.  
  225.             } else {            // Erneute eingabe von 1 unblocked Producer 1
  226.  
  227.                 printf("Producer1 gestartet.\n");
  228.                 errorhandling(pthread_mutex_unlock(&p1_mutex),
  229.                         "Error: Unlock Mutex\n");
  230.                 p1 = 1;
  231.             }
  232.             ;
  233.  
  234.             break;
  235.         case '2':   //producer 2
  236.  
  237.             if (p2 == 1) {  // Gleiches Verhalten wie Case 1 nur fuer Producer 2
  238.                 p2 = 0;
  239.                 printf("Producer2 gestoppt\n");
  240.                 errorhandling(pthread_mutex_lock(&p2_mutex),
  241.                         "Error: Lock Mutex\n");
  242.  
  243.             } else {
  244.                 errorhandling(pthread_mutex_unlock(&p2_mutex),
  245.                         "Error: Unlock Mutex\n");
  246.  
  247.                 p2 = 1;
  248.                 printf("Producer2 gestartet\n");
  249.  
  250.             }
  251.             break;
  252.         case 'c':
  253.         case 'C':
  254.  
  255.             if (c == 1) {   // Gleiches Verhalten wie Case 1 nur fuer Consumer
  256.                 c = 0;
  257.                 printf("Consumer gestoppt\n");
  258.                 errorhandling(pthread_mutex_lock(&consumer_mutex),
  259.                         "Error: Lock Mutex\n");
  260.             } else {
  261.  
  262.                 c = 1;
  263.                 printf("Consumer gestartet\n");
  264.                 errorhandling(pthread_mutex_unlock(&consumer_mutex),
  265.                         "Error: Unlock Mutex\n");
  266.             }
  267.  
  268.             break;
  269.         case 'q':
  270.         case 'Q':
  271.             end();
  272.             break;
  273.  
  274.         case 'h':
  275.             printf("Hilfe:\n");
  276.             printf("1: Starte / Stoppe Producer_1 1\n");
  277.             printf("2: Starte / Stoppe Producer_2 2\n");
  278.             printf("c oder C: Starte / Stoppe Consumer\n");
  279.             printf("q oder Q: Der Main Thread beendet das System\n");
  280.             printf("h: Liefert diese Liste von mo¨glichen Eingaben\n");
  281.         }
  282.     }
  283. }
  284.  
  285.  
  286. ///////////////////////////////////////////
  287. ///////////////////////////////////////////
  288.  
  289.  
  290.  
  291. void *produce(void* buchs) {
  292.     struct alphabetstruct buchstaben = *((struct alphabetstruct*)(buchs));
  293.  
  294.  //my_struct foo = *((my_struct*)(arg))
  295.  
  296.  
  297.    
  298.             int i;
  299.  
  300.     printf("Producer Thread gestartet\n");
  301.  
  302.     i = 0;
  303.  
  304.  
  305.  
  306.    
  307.     while (running == 1) {
  308.  
  309.  
  310.    
  311.       errorhandling(pthread_mutex_lock(&p1_mutex), "Error: Lock Mutex\n");
  312.  
  313.       writeLetter(buchstaben.alpha[i]);
  314.       printf("Producer: Zeichen: %c geschrieben\n", buchstaben.alpha[i]);
  315.  
  316.       errorhandling(pthread_mutex_unlock(&p1_mutex), "Error: Lock Mutex\n");
  317.       i++;
  318.      
  319.     //Von A anfangen wenn Z erreicht
  320.        
  321.            
  322.             if (i == ABC) {
  323.                 i = 0;
  324.             }
  325.    
  326.  
  327.  
  328.         sleep(3);
  329.     }
  330. }
  331.  
  332.  
  333. ///////////////////////////////////////////
  334. ///////////////////////////////////////////
  335.  
  336.  
  337.  
  338. // Consumer Thread
  339. void *consume(void *pid) {
  340.  
  341.     printf("Consumer  Thread gestartet\n");
  342.  
  343.     char tmp = 0;
  344.     while (running == 1) {
  345.  
  346.         //Kritischer Abschnitt - Anfang
  347.         errorhandling(pthread_mutex_lock(&consumer_mutex),
  348.                 "Error: Lock Mutex\n");
  349.         if (c == 1) {
  350.             errorhandling(pthread_mutex_unlock(&consumer_mutex),
  351.                     "Error: Unlock Mutex\n");
  352.  
  353.             tmp = readLetter();
  354.             printf("Consumer:  Zeichen: %c gelesen\n", tmp);
  355.  
  356.         }
  357.         //Kritischer Abschnitt - Ende
  358.  
  359.         errorhandling(pthread_mutex_unlock(&consumer_mutex),
  360.                 "Error: Unlock Mutex\n");
  361.         sleep(2);
  362.  
  363.     }
  364. }
  365. void end() {
  366.  
  367.     //Alle Threads laufen ueber running, auf 0 setzen = beenden der threads
  368.     running = 0;
  369.  
  370.     //unlocken fuer sicheres destroyen
  371.     errorhandling(pthread_mutex_unlock(&p1_mutex), "Error: unlock mutex \n");
  372.     errorhandling(pthread_mutex_unlock(&p2_mutex), "Error: unlock mutex \n");
  373.     errorhandling(pthread_mutex_unlock(&consumer_mutex),
  374.             "Error: unlock mutex \n");
  375.  
  376.     //destroyen der mutexe
  377.     errorhandling(pthread_mutex_destroy(&p1_mutex), "Error: Destroy mutex \n");
  378.     errorhandling(pthread_mutex_destroy(&p2_mutex), "Error: Destroy mutex \n");
  379.     errorhandling(pthread_mutex_destroy(&consumer_mutex),
  380.             "Error: Destroy mutex \n");
  381.  
  382.     printf("Programm wurde beendet.\n");
  383.  
  384. }
  385.  
  386. int main(int argc, char* argv[]) {
  387.  
  388.  
  389. struct  alphabetstruct g;
  390. struct  alphabetstruct k;
  391.  
  392. int j = 0; 
  393. for (j = 0; j < ABC; j++) {
  394. g.alpha[j] = 0x41 + j;
  395. k.alpha[j] = 0x61 + j;
  396. }  
  397.  
  398. int x = 0;
  399. for (x = 0; x < ABC; x++) {
  400. kleinarray[x] = 0x41 + x;
  401. grossarray[x] = 0x61 + x;
  402. }
  403.  
  404.     semaphore();
  405.     g.pid=&thread_id[2];
  406.     k.pid=&thread_id[1];
  407.      
  408.  
  409.  
  410.      
  411.     //Threads erstellen
  412.     pthread_create(&threads[0], NULL, control, (void *) thread_id);
  413.     pthread_create(&threads[1], NULL, produce, (void *) &k);
  414.     pthread_create(&threads[2], NULL, produce, (void *) &g);
  415.     pthread_create(&threads[3], NULL, consume, (void *) &thread_id[3]);
  416.    
  417. //
  418.     //warten auf terminierung der Threads.
  419.     int i;
  420.     for (i = 0; i < 4; i++) {
  421.         pthread_join(threads[i], NULL);
  422.     }
  423.     return 0;
  424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement