====== Lösungsvorschlag ====== ==== Aufgabe 1.1: Einfachauswahl-Fragen ==== **a)** 2 **b)** 2 **c)** 3 **d)** 3 **e)** 1 **f)** 2 **g)** 4 **h)** 4 **i)** 2 **j)** 3 **k)** 2 ==== Aufgabe 1.2: Mehrfachauswahl-Fragen ==== **a)** 3.4.8 **b)** 2,3,5,6 ==== Aufgabe 2: vsc ==== #define FRAME_SIZE 640*480 struct data { char frame[FRAME_SIZE]; int eof; }; #include #include #include #include #include #include #include #include #include #include "jitbuf.h" static void die(const char message[]) { perror(message); exit(EXIT_FAILURE); } // weitere Includes, Konstanten // globale Variablen, Funktionsdeklarationen usw. void *showFrame(void); #include // Funktion main() static int main(int argc, char** argv){ if(jbInit() != 0){ fprintf("Error creating jb"); exit(EXIT_FAILURE); } timeslider#10451 phtread_t tid; errno = pthread_create(&tid, NULL, showFrame, NULL);//show Frame soll nach angabe glaube ich (void) als parameter haben if(errno !== 0){ perror("thread"); exit(EXIT_FAILURE); } // Socket erstellen und Verbindungsannahme vorbereiten int ls, cs; struct addrinfo hints={ .sin6_family = AF_INET6, .sin6_port = htons(2014), .sin6_addr = in6addr_any, } if(ls = socket(AF_INET6, SOCK_STREAM, 0) == -1 || bind(ls, (struct sockaddr*) &hints, sizeof(hints)) == -1 || listen(ls, SOMAXCONN) == -1){ perror("socket, bind or listen"); //sehr witzig exit(EXIT_FAILURE); } // Verbindung annehmen und Verarbeitung vorbereiten // cs wurde noch nicht gesocket() if(cs = accept(ls, NULL,NULL) == -1){ perror("accept"); } while (t){ //wo kommt denn dieses t her? FILE* reader = fdopen(cs, "r"); if(reader == NULL){ perror("openStream"); exit(EXIT_FAILURE); } struct data *video = malloc (sizeof(struct data)); // Videobilder einlesen und in den Puffer schreiben size_t bytesRead = 0; while((bytesRead = fread(video->frame, sizeof(byte), 307200, reader)) != 0){ if(bytesRead < 307200){ video->eof = 1; }else{ video->eof = 0; } jbPut(video); video = malloc (sizeof(struct data)); } errno = phtread_join(tid,NULL); if(errno != 0){ perror("pthread join"); exit(EXIT_FAILURE); } if(close(ls) != 0 || close(cs) != 0){ die("close sockets"); } if(fclose(reader) == EOF){ die("closing reader"); } exit(EXIT_FAILURE); } // Ende Funktion main // Funktion showFrame // Ausgabegeräte suchen void * showFrame(void){ DIR *d; if((dir = opendir("/dev") == NULL){ perror("opendir"); exit(EXIT_FAILURE); } struct dirent e; errno = 0; int nFiles = 0; FILE * outFiles[10]; while((e= readdir(d)) != NULL && nFiles < 10 ){ if(fnmatch( "fb*", e->d_name, 0) == 0){ /********edit changed**********/ char path[strlen("/dev/") + strlen(e->d_name)]; sprintf(path, "/dev/%s", e-d_name); outFiles[nFiles] = fopen(path, "a+");/ if(outFiles[nFiles] == NULL){ perror("opening File"); continue; } nFiles++; } } struct data *video; int eof = 0; while(eof == 0){ jbGet(video); for(int i = 0; i < nFiles; i++){ fwrite(video->frame, sizeof(byte), 307200, outFiles[i]); } eof = video->eof; free(video); if(fclose(outFiles[i]) == EOF){ die("fclose"); } } } // Ende Funktion showFrame /************************************************************** * Datei jitbuf.c *************************************************************/ #define CACHE_SIZE 100 #define BUFFER_SIZE 20 static volatile size_t readPos; static volatile size_t writePos; static SEM* fullSlots; static SEM* freeSlots; static SEM* waiter; static size_t itemsBuffered; static struct data cache[CACHE_SIZE]; // Funktion jbInit int jbInit(void){ waiter = semCreate(0); freeSlots = semCreate(CACHE_SIZE); fullSlosts = semCreate(0); if(waiter == NULL || fullSlots == NULL || freeSlots == NULL){ frpintf("error creating needed sems"); semDestroy(waiter); semDestroy(fullSlots); semDestroy(freeSlots); return 1; } itemsBuffered = readPos = writePos = 0;//ist schon implizit auf 0 return 0; } // Funktion jbPut void jbPut(const struct data *data){ if(itemsBuffered >= CACHE_SIZE) return; P(freeSlots); cache[writePos] = data; cache[writePos++] = data; V(fullSlots); itemsBuffered++; if(itemsBuffered == BUFFER_SIZE){ V(waiter); } Slots } // Funktion jbGet void jbGet(struct data *data){ P(waiter); P(fullSlots); data = cache[readPos]; //readPos ++; struct data dat = cache[readPos++]; itemsBuffered--; data = &dat;https://wwwcip.cs.fau.de/~ak53efan/sp/ if(readPos >= CACHE_SIZE) readPos = 0; V(freeSlots); V(waiter); } // Funktion jbFlush void jbFlush(void){ V(waiter); }