]> vgcfreebox.myrthtech.pt Git - ue-pp-terminationdetectionalgorithm.git/blob - util.c
hooks setup
[ue-pp-terminationdetectionalgorithm.git] / util.c
1 /*
2 Parallel Programming - 2026/2027
3
4 Utility functions
5 */
6
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <stdlib.h>
10 #include <time.h>
11
12 // controls the output of debugging messages, set to 1 to have no
13 // messages
14 int QUIET = 0;
15
16
17 // returns a random integer in [A,B]
18 int rand_ab(int a, int b)
19 {
20 return a + rand() % (b - a + 1);
21 }
22
23 // sleep between MIN and MAX ms
24 void rand_sleep(int min, int max)
25 {
26 int ms = rand_ab(min, max);
27 struct timespec ts = { ms / 1000, (ms % 1000) * 1000 };
28
29 nanosleep(&ts, NULL);
30 }
31
32 // output a debugging message to stderr
33 void trace(char *format, ...)
34 {
35 va_list args;
36
37 if (QUIET)
38 return;
39
40 va_start (args, format);
41 vfprintf(stderr, format, args);
42 va_end (args);
43 }
44
45 // unconditionally output a message to stderr
46 void warn(char *format, ...)
47 {
48 va_list args;
49
50 va_start (args, format);
51 vfprintf(stderr, format, args);
52 va_end (args);
53 }