| 1 |
// Programmet bruker ISO C 2011 (ISO/IEC 9899:2011) og Pthreads (IEEE Std 1003.1c-1995). -*- coding: utf-8 -*- |
// Programmet bruker ISO C 2011 (ISO/IEC 9899:2011) og Pthreads (IEEE Std 1003.1c-2001). -*- coding: utf-8 -*- |
| 2 |
// Løsning programmert av Trond Endrestøl <Trond.Endrestol@ximalas.info>, 2014-11-06. |
// Løsning programmert av Trond Endrestøl <Trond.Endrestol@ximalas.info>, 2014-11-06. |
| 3 |
|
|
| 4 |
// $Ximalas$ |
// $Ximalas$ |
| 5 |
|
|
| 6 |
|
#if __STDC_VERSION__ < 199901L |
| 7 |
|
#error det er tvilsomt om du får kompilert denne kildekoden med så gammel C-kompilator |
| 8 |
|
#endif |
| 9 |
|
|
| 10 |
#include <errno.h> |
#include <errno.h> |
| 11 |
#include <pthread.h> |
#include <pthread.h> |
| 12 |
|
#include <sched.h> |
| 13 |
#include <signal.h> |
#include <signal.h> |
| 14 |
#include <stdbool.h> |
#include <stdbool.h> |
| 15 |
#include <stdio.h> |
#include <stdio.h> |
| 18 |
#include <time.h> |
#include <time.h> |
| 19 |
#include <unistd.h> |
#include <unistd.h> |
| 20 |
|
|
| 21 |
|
#if _POSIX_THREADS < 200112L |
| 22 |
|
#error her mangler det støtte for POSIX Threads anno 2001 |
| 23 |
|
#endif |
| 24 |
|
|
| 25 |
#if __STDC_VERSION__ >= 201112L |
#if __STDC_VERSION__ >= 201112L |
| 26 |
#include <stdnoreturn.h> |
#include <stdnoreturn.h> |
| 27 |
#else |
#else |
| 48 |
size_t i; |
size_t i; |
| 49 |
pthread_t tid = pthread_self(); |
pthread_t tid = pthread_self(); |
| 50 |
|
|
| 51 |
atexit(visResultater); |
if (argc >= 0) { // Bare for å få clang til å ti stille. |
| 52 |
signal(SIGINT, signalhandler); |
atexit(visResultater); |
| 53 |
|
signal(SIGINT, signalhandler); |
| 54 |
|
} // if |
| 55 |
|
|
| 56 |
#ifdef __FreeBSD__ |
#ifdef __FreeBSD__ |
| 57 |
srandomdev(); |
srandomdev(); |
| 77 |
printf("maintråden oppretter livstidsfange %2zu\n", i + 1); |
printf("maintråden oppretter livstidsfange %2zu\n", i + 1); |
| 78 |
|
|
| 79 |
if ( (errno = pthread_create(&tid, NULL, livstidsfange, (void *)i)) != 0) { |
if ( (errno = pthread_create(&tid, NULL, livstidsfange, (void *)i)) != 0) { |
| 80 |
|
fflush(stdout); |
| 81 |
fprintf(stderr, |
fprintf(stderr, |
| 82 |
"%s: pthread_create(&tid, NULL, livstidsfange, (void *)%2zu) = %s (%d)\n", |
"%s: pthread_create(&tid, NULL, livstidsfange, (void *)%2zu) = %s (%d)\n", |
| 83 |
argv[0], i, strerror(errno), errno); |
argv[0], i, strerror(errno), errno); |
| 84 |
exit(1); |
fflush(stderr); |
| 85 |
|
_exit(1); |
| 86 |
} // if |
} // if |
| 87 |
} // for |
} // for |
| 88 |
|
|
| 182 |
printf("livstidsfange %2zu låser opp mutex\n", i + 1); |
printf("livstidsfange %2zu låser opp mutex\n", i + 1); |
| 183 |
pthread_mutex_unlock(&mutex); |
pthread_mutex_unlock(&mutex); |
| 184 |
printf("livstidsfange %2zu har låst opp mutex\n", i + 1); |
printf("livstidsfange %2zu har låst opp mutex\n", i + 1); |
| 185 |
|
|
| 186 |
|
#ifdef _POSIX_PRIORITY_SCHEDULING |
| 187 |
|
sched_yield(); |
| 188 |
|
#endif |
| 189 |
} // while |
} // while |
| 190 |
} // livstidsfange() |
} // livstidsfange() |
| 191 |
|
|
| 195 |
|
|
| 196 |
fflush(stderr); |
fflush(stderr); |
| 197 |
fflush(stdout); |
fflush(stdout); |
| 198 |
fflush(stderr); |
|
|
fflush(stdout); |
|
| 199 |
puts("\nResultater:\n"); |
puts("\nResultater:\n"); |
| 200 |
|
|
| 201 |
printf("bryter %u er ved avslutning vippet %s\n", 1U, brytere[0] == true ? "opp" : "ned"); |
printf("bryter %u er ved avslutning vippet %s\n", 1U, brytere[0] == true ? "opp" : "ned"); |
| 205 |
for (i = 0; i < ANTALL_LIVSTIDSFANGER; i++) { |
for (i = 0; i < ANTALL_LIVSTIDSFANGER; i++) { |
| 206 |
printf("livstidsfange %2zu: antall besøk: %u\n", i + 1, antallBesok[i]); |
printf("livstidsfange %2zu: antall besøk: %u\n", i + 1, antallBesok[i]); |
| 207 |
} // for |
} // for |
| 208 |
|
puts(""); |
| 209 |
|
|
| 210 |
|
printf("antall livstidsfanger talt av den tellende livstidsfangen er %u\n", antallLivstidsfanger); |
| 211 |
|
|
| 212 |
fflush(stdout); |
fflush(stdout); |
| 213 |
fflush(stdout); |
fflush(stdout); |
| 214 |
} // visResultater() |
} // visResultater() |