Du befindest dich hier: FSI Informatik » jahrgaenge » 2006 » "Muster"-Lösungen » "Muster"-Lösungen zu Klausuren aus Softwaresysteme 1 » Vordiplomsklausuren » September 2005
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
Beide Seiten, vorherige ÜberarbeitungVorherige ÜberarbeitungNächste Überarbeitung | Vorherige Überarbeitung | ||
jahrgaenge:2006:loesungen:softwaresysteme_1:vordiplom:sep2005 [05.09.2007 10:12] – 131.188.30.85 | jahrgaenge:2006:loesungen:softwaresysteme_1:vordiplom:sep2005 [07.09.2007 00:37] (aktuell) – Raim | ||
---|---|---|---|
Zeile 15: | Zeile 15: | ||
* k) 3 | * k) 3 | ||
* l) 3 | * l) 3 | ||
- | * m) 3 (Natürlich ist das möglich, man muß eben ggf. umorganisieren; | + | * m) 3 (Natürlich ist das möglich, man muß eben ggf. umorganisieren; |
* n) 2 | * n) 2 | ||
Zeile 22: | Zeile 22: | ||
* a) | * a) | ||
+ | sem.c: | ||
<code c> | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | SEM *sem_init(int n) { | ||
+ | SEM *s; | ||
+ | s = (SEM *) malloc(sizeof(SEM)); | ||
+ | if (s == NULL) { | ||
+ | perror(" | ||
+ | exit(EXIT_FAILURE); | ||
+ | } | ||
+ | pthread_mutex_init(& | ||
+ | pthread_cond_init(& | ||
+ | s->s = n; | ||
+ | |||
+ | return s; | ||
+ | } | ||
+ | |||
+ | void P(SEM *s) { | ||
+ | pthread_mutex_lock(& | ||
+ | while (s->s < 1) { | ||
+ | pthread_cond_wait(& | ||
+ | } | ||
+ | s->s--; | ||
+ | pthread_mutex_unlock(& | ||
+ | } | ||
+ | |||
+ | void V(SEM *s) { | ||
+ | pthread_mutex_lock(& | ||
+ | s->s++; | ||
+ | pthread_cond_broadcast(& | ||
+ | pthread_mutex_unlock(& | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | fdserver.c: | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #include " | ||
+ | |||
+ | #define NTHREAD 25 | ||
+ | |||
+ | void *serve(void *arg); | ||
+ | void handle(FILE *socket, char path[]); | ||
+ | void fhandle(FILE *socket, char path[]); | ||
+ | void dhandle(FILE *socket, char path[]); | ||
+ | |||
+ | struct bufinfo { | ||
+ | SEM *sem_full; | ||
+ | SEM *sem_free; | ||
+ | pthread_mutex_t lock; | ||
+ | |||
+ | int socks[NTHREAD]; | ||
+ | int writep; | ||
+ | int readp; | ||
+ | }; | ||
+ | |||
+ | int main(int argc, char *argv[]) { | ||
+ | struct bufinfo b; | ||
+ | pthread_t tid[NTHREAD]; | ||
+ | int i; | ||
+ | int sock; | ||
+ | struct sockaddr_in saddr; | ||
+ | | ||
+ | b.writep = 0; | ||
+ | b.readp = 0; | ||
+ | b.sem_full = sem_init(0); | ||
+ | b.sem_free = sem_init(NTHREAD); | ||
+ | pthread_mutex_init(& | ||
+ | |||
+ | for (i = 0; i < NTHREAD; i++) { | ||
+ | pthread_create(& | ||
+ | } | ||
+ | |||
+ | sock = socket(PF_INET, | ||
+ | if (sock == -1) { | ||
+ | perror(" | ||
+ | return EXIT_FAILURE; | ||
+ | } | ||
+ | | ||
+ | saddr.sin_family = AF_INET; | ||
+ | saddr.sin_port = argc > 1 ? htons(atoi(argv[1])) : htons(31337); | ||
+ | saddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
+ | |||
+ | if (bind(sock, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) { | ||
+ | perror(" | ||
+ | return EXIT_FAILURE; | ||
+ | } | ||
+ | if (listen(sock, | ||
+ | perror(" | ||
+ | return EXIT_FAILURE; | ||
+ | } | ||
+ | |||
+ | while (1) { | ||
+ | int clsock; | ||
+ | struct sockaddr clsaddr; | ||
+ | socklen_t clsaddr_len = sizeof(clsaddr); | ||
+ | |||
+ | clsock = accept(sock, | ||
+ | if (clsock == -1) { | ||
+ | perror(" | ||
+ | continue; | ||
+ | } | ||
+ | | ||
+ | P(b.sem_free); | ||
+ | b.socks[b.writep] = clsock; | ||
+ | b.writep = (b.writep + 1) % NTHREAD; | ||
+ | V(b.sem_full); | ||
+ | |||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | void *serve(void *arg) { | ||
+ | struct bufinfo *b = (struct bufinfo *) arg; | ||
+ | int s; | ||
+ | FILE *fd; | ||
+ | char buffer[1024+2]; | ||
+ | | ||
+ | P(b-> | ||
+ | pthread_mutex_lock(& | ||
+ | s = b-> | ||
+ | b->readp = (b-> | ||
+ | pthread_mutex_unlock(& | ||
+ | V(b-> | ||
+ | |||
+ | fd = fdopen(s, " | ||
+ | |||
+ | while (fgets(buffer, | ||
+ | buffer[strlen(buffer)-1] = ' | ||
+ | handle(fd, buffer); | ||
+ | } | ||
+ | |||
+ | return NULL; | ||
+ | } | ||
+ | |||
+ | void handle(FILE *socket, char path[]) { | ||
+ | struct stat statinfo; | ||
+ | |||
+ | if (stat(path, & | ||
+ | fprintf(socket, | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | if (S_ISDIR(statinfo.st_mode)) { | ||
+ | dhandle(socket, | ||
+ | } else if (S_ISREG(statinfo.st_mode)) { | ||
+ | fhandle(socket, | ||
+ | } else { | ||
+ | fprintf(socket, | ||
+ | return; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void fhandle(FILE *socket, char path[]) { | ||
+ | char buf[1024+2]; | ||
+ | FILE *fd; | ||
+ | |||
+ | fd = fopen(path, " | ||
+ | if (fd == NULL) { | ||
+ | fprintf(socket, | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | while (fgets(buf, 1024+2, fd) != NULL) { | ||
+ | if (fputs(buf, socket) == EOF) { | ||
+ | perror(" | ||
+ | return; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void dhandle(FILE *socket, char path[]) { | ||
+ | DIR *dir; | ||
+ | struct dirent entry; | ||
+ | struct dirent *result; | ||
+ | int err; | ||
+ | |||
+ | dir = opendir(path); | ||
+ | if (dir == NULL) { | ||
+ | | ||
+ | } | ||
+ | |||
+ | while(((err = readdir_r(dir, | ||
+ | fprintf(socket, | ||
+ | } | ||
+ | } | ||
</ | </ | ||