File: coredump_many_threads.c

package info (click to toggle)
valgrind 1%3A3.16.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 158,568 kB
  • sloc: ansic: 746,130; exp: 26,134; xml: 22,708; asm: 13,570; cpp: 7,691; makefile: 6,177; perl: 5,965; sh: 5,665; javascript: 929
file content (100 lines) | stat: -rw-r--r-- 2,109 bytes parent folder | download | duplicates (6)
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
93
94
95
96
97
98
99
100
/*
 * Creates 8 threads. The fifth one (counting the main thread as well)
 * causes a core dump.
 */

#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define NEVERENDING_SLEEP 10000

static pthread_barrier_t barrier;

static void *thread_func2(void *arg) {
   pthread_barrier_wait(&barrier);
   sleep(NEVERENDING_SLEEP);
   return NULL;
}

static void *thread_func3(void *arg) {
   pthread_barrier_wait(&barrier);
   sleep(NEVERENDING_SLEEP);
   return NULL;
}

static void *thread_func4(void *arg) {
   pthread_barrier_wait(&barrier);
   sleep(NEVERENDING_SLEEP);
   return NULL;
}

static void *thread_func5(void *arg) {
   pthread_barrier_wait(&barrier);
   sleep(2);

   char *x = (char *) 0x1;
   *x = 2;
   return NULL;
}

static void *thread_func6(void *arg) {
   pthread_barrier_wait(&barrier);
   sleep(NEVERENDING_SLEEP);
   return NULL;
}

static void *thread_func7(void *arg) {
   pthread_barrier_wait(&barrier);
   sleep(NEVERENDING_SLEEP);
   return NULL;
}

static void *thread_func8(void *arg) {
   pthread_barrier_wait(&barrier);
   sleep(NEVERENDING_SLEEP);
   return NULL;
}

static void *thread_func9(void *arg) {
   pthread_barrier_wait(&barrier);
   sleep(NEVERENDING_SLEEP);
   return NULL;
}

static void create_thread(void *(*thread_func)(void *))
{
   pthread_t thread;

   int ret = pthread_create(&thread, NULL, thread_func, NULL);
   if (ret != 0) {
      fprintf(stderr, "pthread_create: %s (%d)\n", strerror(ret), ret);
      exit(1);
   }
}

int main(int argc, const char *argv[])
{
   int ret = pthread_barrier_init(&barrier, NULL, 9);
   if (ret != 0) {
      fprintf(stderr, "pthread_barrier_init: %s (%d)\n",
              strerror(ret), ret);
      exit(1);
   }

   create_thread(thread_func2);
   create_thread(thread_func3);
   create_thread(thread_func4);
   create_thread(thread_func5);
   create_thread(thread_func6);
   create_thread(thread_func7);
   create_thread(thread_func8);
   create_thread(thread_func9);
   pthread_barrier_wait(&barrier);

   sleep(NEVERENDING_SLEEP);
   return 0;
}