| 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-1995). -*- 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 <signal.h> |
#include <signal.h> |
| 13 |
#include <stdbool.h> |
#include <stdbool.h> |
| 14 |
#include <stdio.h> |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
#include <stdlib.h> |
| 16 |
#include <string.h> |
#include <string.h> |
| 17 |
#include <time.h> |
#include <time.h> |
| 18 |
#include <unistd.h> |
#include <unistd.h> |
| 19 |
|
|
| 20 |
#if __STDC_VERSION__ >= 201112L |
#if __STDC_VERSION__ >= 201112L |
| 21 |
#include <stdnoreturn.h> |
#include <stdnoreturn.h> |
| 22 |
#else |
#else |
| 23 |
#define noreturn |
#define noreturn |
| 24 |
#endif |
#endif |
| 31 |
static unsigned antallLivstidsfanger; |
static unsigned antallLivstidsfanger; |
| 32 |
|
|
| 33 |
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 34 |
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
| 35 |
static bool brytere[2U]; |
static bool brytere[2U]; |
| 36 |
|
|
| 37 |
noreturn void *livstidsfange(void *arg); |
noreturn void *livstidsfange(void *arg); |
| 38 |
void visResultater(void); |
void visResultater(void); |
| 39 |
void signalhandler(int sig); |
void signalhandler(int sig); |
| 40 |
|
|
| 41 |
noreturn int main(int argc, char **argv) |
noreturn int main(int argc, char **argv) |
| 42 |
{ |
{ |
| 43 |
size_t i; |
size_t i; |
| 44 |
pthread_t tid = pthread_self(); |
pthread_t tid = pthread_self(); |
| 45 |
|
|
| 46 |
atexit(visResultater); |
if (argc >= 0) { // Bare for å få clang til å ti stille. |
| 47 |
signal(SIGINT, signalhandler); |
atexit(visResultater); |
| 48 |
|
signal(SIGINT, signalhandler); |
| 49 |
|
} // if |
| 50 |
|
|
| 51 |
#ifdef __FreeBSD__ |
#ifdef __FreeBSD__ |
| 52 |
srandomdev(); |
srandomdev(); |
| 53 |
#else |
#else |
| 54 |
srandom((unsigned int)time(NULL)); |
srandom((unsigned int)time(NULL)); |
| 55 |
#endif |
#endif |
| 56 |
|
|
| 57 |
memset((void *)&antallBesok, 0, sizeof(antallBesok)); |
memset((void *)&antallBesok, 0, sizeof(antallBesok)); |
| 58 |
brytere[0] = random() & 1; |
brytere[0] = random() & 1; |
| 59 |
brytere[1] = random() & 1; |
brytere[1] = random() & 1; |
| 60 |
|
|
| 61 |
tellendeLivstidsfange = random() % ANTALL_LIVSTIDSFANGER; |
tellendeLivstidsfange = random() % ANTALL_LIVSTIDSFANGER; |
| 62 |
|
|
| 63 |
printf("bryter %u er i utgangspunktet vippet %s\n", 1U, brytere[0] == true ? "opp" : "ned"); |
printf("bryter %u er i utgangspunktet vippet %s\n", 1U, brytere[0] == true ? "opp" : "ned"); |
| 64 |
printf("bryter %u er i utgangspunktet vippet %s\n", 2U, brytere[1] == true ? "opp" : "ned"); |
printf("bryter %u er i utgangspunktet vippet %s\n", 2U, brytere[1] == true ? "opp" : "ned"); |
| 184 |
{ |
{ |
| 185 |
size_t i; |
size_t i; |
| 186 |
|
|
| 187 |
fflush(stderr); |
fflush(stderr); |
| 188 |
fflush(stdout); |
fflush(stdout); |
| 189 |
|
|
| 190 |
puts("\nResultater:\n"); |
puts("\nResultater:\n"); |
| 191 |
|
|
| 192 |
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"); |
| 193 |
printf("bryter %u er ved avslutning vippet %s\n", 2U, brytere[1] == true ? "opp" : "ned"); |
printf("bryter %u er ved avslutning vippet %s\n", 2U, brytere[1] == true ? "opp" : "ned"); |
| 194 |
printf("livstidsfange %2zu var den tellende livstidsfangen\n\n", tellendeLivstidsfange + 1); |
printf("livstidsfange %2zu var den tellende livstidsfangen\n\n", tellendeLivstidsfange + 1); |
| 195 |
|
|
| 196 |
for (i = 0; i < ANTALL_LIVSTIDSFANGER; i++) { |
for (i = 0; i < ANTALL_LIVSTIDSFANGER; i++) { |
| 197 |
printf("livstidsfange %2zu: antall besøk: %u\n", i + 1, antallBesok[i]); |
printf("livstidsfange %2zu: antall besøk: %u\n", i + 1, antallBesok[i]); |
| 198 |
} // for |
} // for |
| 199 |
|
puts(""); |
| 200 |
|
|
| 201 |
|
printf("antall livstidsfanger talt av den tellende livstidsfangen er %u\n", antallLivstidsfanger); |
| 202 |
|
|
| 203 |
fflush(stdout); |
fflush(stdout); |
| 204 |
fflush(stdout); |
fflush(stdout); |
| 205 |
} // visResultater() |
} // visResultater() |
| 206 |
|
|
| 207 |
void signalhandler(int sig) |
void signalhandler(int sig) |
| 208 |
{ |
{ |
| 209 |
if (sig == SIGINT) { |
if (sig == SIGINT) { |
| 210 |
exit(1); |
exit(1); |
| 211 |
} // if |
} // if |
| 212 |
} // signalhandler() |
} // signalhandler() |
| 213 |
|
|
| 214 |
// livstidsfanger.c |
// livstidsfanger.c |