1901c3d44SJérôme Duval#include <errno.h>
2901c3d44SJérôme Duval#include <stdio.h>
3901c3d44SJérôme Duval#include <stdlib.h>
4901c3d44SJérôme Duval#include <unistd.h>
5901c3d44SJérôme Duval#include <pthread.h>
6901c3d44SJérôme Duval
7901c3d44SJérôme Duval#define panic(str) if (ret != 0) { errno = ret; perror(str); return 1; }
8901c3d44SJérôme Duval
9901c3d44SJérôme Duval
10901c3d44SJérôme Duvalvoid*
11901c3d44SJérôme DuvalthreadFunction(void*)
12901c3d44SJérôme Duval{
13901c3d44SJérôme Duval	int i = 0;
14901c3d44SJérôme Duval	printf("variable addr: %p\n", &i);
15901c3d44SJérôme Duval	pthread_exit(0);
16901c3d44SJérôme Duval	return 0;
17901c3d44SJérôme Duval}
18901c3d44SJérôme Duval
19901c3d44SJérôme Duval
20901c3d44SJérôme Duvalint
21901c3d44SJérôme Duvalmain(int argc, char **argv)
22901c3d44SJérôme Duval{
23901c3d44SJérôme Duval	int ret;
24901c3d44SJérôme Duval	pthread_attr_t attr;
25901c3d44SJérôme Duval	ret = pthread_attr_init(&attr);
26901c3d44SJérôme Duval	panic("init");
27901c3d44SJérôme Duval	void* stackAddress;
28901c3d44SJérôme Duval	size_t stackSize;
29901c3d44SJérôme Duval	ret = pthread_attr_getstack(&attr, &stackAddress, &stackSize);
30901c3d44SJérôme Duval	panic("getstack");
31901c3d44SJérôme Duval	printf("stackAddress: %p, stackSize: %lu\n", stackAddress, stackSize);
32901c3d44SJérôme Duval	stackSize = PTHREAD_STACK_MIN;
33901c3d44SJérôme Duval	ret = posix_memalign(&stackAddress, sysconf(_SC_PAGE_SIZE),
34901c3d44SJérôme Duval		stackSize);
35901c3d44SJérôme Duval	panic("memalign");
36901c3d44SJérôme Duval	ret = pthread_attr_setstack(&attr, stackAddress, stackSize);
37901c3d44SJérôme Duval	panic("setstack");
38901c3d44SJérôme Duval	ret = pthread_attr_getstack(&attr, &stackAddress, &stackSize);
39901c3d44SJérôme Duval	panic("getstack");
40901c3d44SJérôme Duval	printf("stackAddress: %p, stackSize: %lu\n", stackAddress, stackSize);
41901c3d44SJérôme Duval
42901c3d44SJérôme Duval	pthread_t thread;
43901c3d44SJérôme Duval	ret = pthread_create(&thread, &attr, threadFunction, NULL);
44901c3d44SJérôme Duval	panic("create");
45901c3d44SJérôme Duval	ret = pthread_join(thread, NULL);
46901c3d44SJérôme Duval	panic("join");
47901c3d44SJérôme Duval
48901c3d44SJérôme Duval	ret = pthread_attr_destroy(&attr);
49901c3d44SJérôme Duval	panic("destroy");
50901c3d44SJérôme Duval
51901c3d44SJérôme Duval	return 0;
52901c3d44SJérôme Duval}
53