Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //fifo.h//
- #ifndef FIFO_H
- #define FIFO_H
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <errno.h>
- #include <string.h>
- #include <semaphore.h>
- void semaphore();
- void writeLetter(char letter);
- char readLetter();
- void errorhandling(int value, const char *message);
- #endif
- //fifo.c//
- /*
- *aufgabe2.c
- *
- * Created on: 11.11.2014
- * Author:
- * Thorben Schnirpel,
- * MatrNr: 2125495
- * Felix Stoessel
- * Matr.Nr 2141549
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <errno.h>
- #include <string.h>
- #include <semaphore.h>
- #include "fifo.h"
- #define BUFFER_GROESSE 10
- char buffer[BUFFER_GROESSE];
- int read_index=0;
- int write_index=0;
- //Semaphor
- sem_t SemEmpty;
- sem_t SemFull;
- //Mutex
- pthread_mutex_t fifo_mutex = PTHREAD_MUTEX_INITIALIZER;
- void semaphore() {
- //Semaphoren initialisieren
- errorhandling(sem_init(&SemEmpty, 0, 10), "Error: Sem Init Empty\n");
- errorhandling(sem_init(&SemFull, 0, 0), "Error: Sem Init Full\n");
- }
- void writeLetter(char letter) {
- // Wait decrements idleS
- errorhandling(sem_wait(&SemEmpty), "Error: Sem Wait Schreiben\n");
- errorhandling(pthread_mutex_lock(&fifo_mutex), "Error: Lock Mutex\n");
- //Kritischer Abschnitt - Anfang
- if (write_index >= BUFFER_GROESSE) {
- write_index = 0;
- }
- buffer[write_index] = letter;
- write_index++;
- //Kritischer Abschnitt - Ende
- errorhandling(pthread_mutex_unlock(&fifo_mutex), "Error: Unlock Mutex\n");
- // increments busyS
- errorhandling(sem_post(&SemFull), "Error: Sem Post Schreiben\n");
- }
- char readLetter() {
- //tmp fuer spaetere Ausgabe
- char tmp;
- // Wait decrements busyS
- errorhandling(sem_wait(&SemFull), "Error: Sem Wait Lesen\n");
- errorhandling(pthread_mutex_lock(&fifo_mutex), "Error: Lock Mutex\n");
- //Kritischer Abschnitt - Anfang
- if (read_index >= BUFFER_GROESSE) {
- read_index = 0;
- }
- tmp = buffer[read_index];
- read_index++;
- //Kritischer Abschnitt - Ende
- errorhandling(pthread_mutex_unlock(&fifo_mutex), "Error: Lock Mutex\n");
- // increments idleS
- errorhandling(sem_post(&SemEmpty), "Error: Sem Post Lesen\n"); // decrement num of Elem in Buffer
- return tmp;
- }
- //errorhandling.c//
- #include "fifo.h"
- void errorhandling(int value, const char *message);
- void errorhandling(int value, const char *message) {
- if ((value) == -1) {
- perror(message);
- }
- }
- //main.c//
- /*
- *aufgabe2.c
- *
- * Created on: 11.11.2014
- * Author:
- * Thorben Schnirpel,
- * MatrNr: 2125495
- * Felix Stoessel
- * Matr.Nr 2141549
- *
- */
- #include "fifo.h"
- #define ABC 26
- #define THREADSIZE 4
- //Mutex
- pthread_mutex_t p1_mutex = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t p2_mutex = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t consumer_mutex = PTHREAD_MUTEX_INITIALIZER;
- //Threadarray
- pthread_t threads[THREADSIZE];
- int thread_id[THREADSIZE] = { 0, 1, 2, 3 };
- // Prototypen
- void* produce1(void *pid);
- void* produce2(void *pid);
- void* consume(void *pid);
- void* control(void *pid);
- void end();
- void errorhandling(int value, const char *message);
- //Legt ein Array mit grossbuchstaben und eins mit kleinbuchstaben
- char kleinarray[ABC];
- char grossarray[ABC];
- typedef struct alphabetstruct {
- char alpha[ABC];
- void* pid;
- }g,k;
- // Bei 0 wird das Programm beendet
- int running = 1;
- int test = 1;
- int p1 = 1;
- int p2 = 1;
- int c = 1;
- void *control(void *pid) {
- char input = 0;
- printf("Controller gestartet\n");
- while (running == 1) {
- input = getchar();
- switch (input) {
- case '1': //producer 1
- if (p1 == 1) {// Wenn Producer 1 laeft dann wird er gestoppt bis erneut 1 eingegeben wird
- p1 = 0;
- errorhandling(pthread_mutex_lock(&p1_mutex),
- "Error: Lock Mutex\n");
- printf("Producer1 gestoppt.\n");
- } else { // Erneute eingabe von 1 unblocked Producer 1
- printf("Producer1 gestartet.\n");
- errorhandling(pthread_mutex_unlock(&p1_mutex),
- "Error: Unlock Mutex\n");
- p1 = 1;
- }
- ;
- break;
- case '2': //producer 2
- if (p2 == 1) { // Gleiches Verhalten wie Case 1 nur fuer Producer 2
- p2 = 0;
- printf("Producer2 gestoppt\n");
- errorhandling(pthread_mutex_lock(&p2_mutex),
- "Error: Lock Mutex\n");
- } else {
- errorhandling(pthread_mutex_unlock(&p2_mutex),
- "Error: Unlock Mutex\n");
- p2 = 1;
- printf("Producer2 gestartet\n");
- }
- break;
- case 'c':
- case 'C':
- if (c == 1) { // Gleiches Verhalten wie Case 1 nur fuer Consumer
- c = 0;
- printf("Consumer gestoppt\n");
- errorhandling(pthread_mutex_lock(&consumer_mutex),
- "Error: Lock Mutex\n");
- } else {
- c = 1;
- printf("Consumer gestartet\n");
- errorhandling(pthread_mutex_unlock(&consumer_mutex),
- "Error: Unlock Mutex\n");
- }
- break;
- case 'q':
- case 'Q':
- end();
- break;
- case 'h':
- printf("Hilfe:\n");
- printf("1: Starte / Stoppe Producer_1 1\n");
- printf("2: Starte / Stoppe Producer_2 2\n");
- printf("c oder C: Starte / Stoppe Consumer\n");
- printf("q oder Q: Der Main Thread beendet das System\n");
- printf("h: Liefert diese Liste von mo¨glichen Eingaben\n");
- }
- }
- }
- ///////////////////////////////////////////
- ///////////////////////////////////////////
- void *produce(void* buchs) {
- struct alphabetstruct buchstaben = *((struct alphabetstruct*)(buchs));
- //my_struct foo = *((my_struct*)(arg))
- int i;
- printf("Producer Thread gestartet\n");
- i = 0;
- while (running == 1) {
- errorhandling(pthread_mutex_lock(&p1_mutex), "Error: Lock Mutex\n");
- writeLetter(buchstaben.alpha[i]);
- printf("Producer: Zeichen: %c geschrieben\n", buchstaben.alpha[i]);
- errorhandling(pthread_mutex_unlock(&p1_mutex), "Error: Lock Mutex\n");
- i++;
- //Von A anfangen wenn Z erreicht
- if (i == ABC) {
- i = 0;
- }
- sleep(3);
- }
- }
- ///////////////////////////////////////////
- ///////////////////////////////////////////
- // Consumer Thread
- void *consume(void *pid) {
- printf("Consumer Thread gestartet\n");
- char tmp = 0;
- while (running == 1) {
- //Kritischer Abschnitt - Anfang
- errorhandling(pthread_mutex_lock(&consumer_mutex),
- "Error: Lock Mutex\n");
- if (c == 1) {
- errorhandling(pthread_mutex_unlock(&consumer_mutex),
- "Error: Unlock Mutex\n");
- tmp = readLetter();
- printf("Consumer: Zeichen: %c gelesen\n", tmp);
- }
- //Kritischer Abschnitt - Ende
- errorhandling(pthread_mutex_unlock(&consumer_mutex),
- "Error: Unlock Mutex\n");
- sleep(2);
- }
- }
- void end() {
- //Alle Threads laufen ueber running, auf 0 setzen = beenden der threads
- running = 0;
- //unlocken fuer sicheres destroyen
- errorhandling(pthread_mutex_unlock(&p1_mutex), "Error: unlock mutex \n");
- errorhandling(pthread_mutex_unlock(&p2_mutex), "Error: unlock mutex \n");
- errorhandling(pthread_mutex_unlock(&consumer_mutex),
- "Error: unlock mutex \n");
- //destroyen der mutexe
- errorhandling(pthread_mutex_destroy(&p1_mutex), "Error: Destroy mutex \n");
- errorhandling(pthread_mutex_destroy(&p2_mutex), "Error: Destroy mutex \n");
- errorhandling(pthread_mutex_destroy(&consumer_mutex),
- "Error: Destroy mutex \n");
- printf("Programm wurde beendet.\n");
- }
- int main(int argc, char* argv[]) {
- struct alphabetstruct g;
- struct alphabetstruct k;
- int j = 0;
- for (j = 0; j < ABC; j++) {
- g.alpha[j] = 0x41 + j;
- k.alpha[j] = 0x61 + j;
- }
- int x = 0;
- for (x = 0; x < ABC; x++) {
- kleinarray[x] = 0x41 + x;
- grossarray[x] = 0x61 + x;
- }
- semaphore();
- g.pid=&thread_id[2];
- k.pid=&thread_id[1];
- //Threads erstellen
- pthread_create(&threads[0], NULL, control, (void *) thread_id);
- pthread_create(&threads[1], NULL, produce, (void *) &k);
- pthread_create(&threads[2], NULL, produce, (void *) &g);
- pthread_create(&threads[3], NULL, consume, (void *) &thread_id[3]);
- //
- //warten auf terminierung der Threads.
- int i;
- for (i = 0; i < 4; i++) {
- pthread_join(threads[i], NULL);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement