about summary refs log tree commit diff
path: root/linuxthreads/Examples/ex17.c
blob: f5c99ee3fb5fdcba0ddc4b71d559024c27baa351 (plain) (blame)
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
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>

static void *
test_thread (void *v_param)
{
  return NULL;
}

#define STACKSIZE 0x100000

int
main (void)
{
  pthread_t thread;
  pthread_attr_t attr;
  int status;
  void *stack, *stack2;
  size_t stacksize;

  pthread_attr_init (&attr);
  stack = mmap (NULL, STACKSIZE,
		PROT_READ | PROT_WRITE | PROT_EXEC,
		MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

  if (stack == MAP_FAILED)
    {
      perror ("mmap failed");
      return 1;
    }

  status = pthread_attr_setstack (&attr, stack, STACKSIZE);
  if (status != 0)
    {
      printf ("pthread_attr_setstack failed: %s\n", strerror (status));
      return 1;
    }

  status = pthread_attr_getstack (&attr, &stack2, &stacksize);
  if (status != 0)
    {
      printf ("pthread_attr_getstack failed: %s\n", strerror (status));
      return 1;
    }

  if (stack2 != stack || stacksize != STACKSIZE)
    {
      printf ("first pthread_attr_getstack returned different stack (%p,%x)\n"
	      "than was set by setstack (%p,%x)\n",
	      stack2, stacksize, stack, STACKSIZE);
      return 2;
    }

  status = pthread_create (&thread, &attr, test_thread, NULL);
  if (status != 0)
    {
      printf ("pthread_create failed: %s\n", strerror (status));
      return 1;
    }

  status = pthread_getattr_np (thread, &attr);
  if (status != 0)
    {
      printf ("pthread_getattr_np failed: %s\n", strerror (status));
      return 1;
    }

  status = pthread_attr_getstack (&attr, &stack2, &stacksize);
  if (status != 0)
    {
      printf ("pthread_attr_getstack failed: %s\n", strerror (status));
      return 1;
    }

  if (stack2 != stack || stacksize != STACKSIZE)
    {
      printf ("second pthread_attr_getstack returned different stack (%p,%x)\n"
	      "than was set by setstack (%p,%x)\n",
	      stack2, stacksize, stack, STACKSIZE);
      return 3;
    }

  /* pthread_detach (thread); */
  if (pthread_join (thread, NULL) != 0)
    {
      printf ("join failed\n");
      return 1;
    }
  return 0;
}