forked from cirosantilli/linux-kernel-module-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpthread_count.c
92 lines (89 loc) · 1.73 KB
/
pthread_count.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* count to infinity in n threads sleeping one second between each count.
*
* https://cirosantilli.com/linux-kernel-module-cheat#pthreads
*
* Useful if you need to keep several threads around
* to test something.
*
* Usage:
*
* ....
* ./pthread_count.out 3
* ....
*
* Sample output:
*
* ....
* 0 0
* 1 0
* 2 0
* 1 1
* 2 1
* 0 1
* 1 2
* 0 2
* 2 2
* ....
*
* Initial motivation: confirm that:
*
* ....
* ./pthread_count.out 4 &
* cat /proc/$!/status | grep -E '^Threads:'
* kill $!
* ....
*
* shows the right thread count:
*
* ....
* Threads: 5
* ....
*
* which is 1 main thread + 4 we spawned!
*/
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
void* main_thread(void *arg) {
unsigned long i = 0;
unsigned int thread_id;
thread_id = *((unsigned int*)arg);
while (1) {
printf("%u %lu\n", thread_id, i);
i++;
sleep(1);
}
return NULL;
}
int main(int argc, char **argv) {
pthread_t *threads;
unsigned int nthreads, i, *thread_args;
if (argc > 1) {
nthreads = strtoll(argv[1], NULL, 0);
} else {
nthreads = 1;
}
threads = malloc(nthreads * sizeof(*threads));
thread_args = malloc(nthreads * sizeof(*thread_args));
for (i = 0; i < nthreads; ++i) {
thread_args[i] = i;
assert(pthread_create(
&threads[i],
NULL,
main_thread,
(void*)&thread_args[i]
) == 0);
}
for (i = 0; i < nthreads; ++i) {
pthread_join(threads[i], NULL);
}
free(thread_args);
free(threads);
return EXIT_SUCCESS;
}