Back to index

glibc  2.9
ex17.c
Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <pthread.h>
00005 #include <unistd.h>
00006 #include <limits.h>
00007 #include <sys/mman.h>
00008 
00009 static pthread_mutex_t synch = PTHREAD_MUTEX_INITIALIZER;
00010 
00011 static void *
00012 test_thread (void *v_param)
00013 {
00014   pthread_mutex_lock (&synch);
00015   return NULL;
00016 }
00017 
00018 #define STACKSIZE 0x100000
00019 
00020 int
00021 main (void)
00022 {
00023   pthread_t thread;
00024   pthread_attr_t attr;
00025   int status;
00026   void *stack, *stack2;
00027   size_t stacksize;
00028 
00029   pthread_attr_init (&attr);
00030   stack = mmap (NULL, STACKSIZE,
00031               PROT_READ | PROT_WRITE | PROT_EXEC,
00032               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
00033 
00034   if (stack == MAP_FAILED)
00035     {
00036       perror ("mmap failed");
00037       return 1;
00038     }
00039 
00040   status = pthread_attr_setstack (&attr, stack, STACKSIZE);
00041   if (status != 0)
00042     {
00043       printf ("pthread_attr_setstack failed: %s\n", strerror (status));
00044       return 1;
00045     }
00046 
00047   status = pthread_attr_getstack (&attr, &stack2, &stacksize);
00048   if (status != 0)
00049     {
00050       printf ("pthread_attr_getstack failed: %s\n", strerror (status));
00051       return 1;
00052     }
00053 
00054   if (stack2 != stack || stacksize != STACKSIZE)
00055     {
00056       printf ("first pthread_attr_getstack returned different stack (%p,%zx)\n"
00057              "than was set by setstack (%p,%x)\n",
00058              stack2, stacksize, stack, STACKSIZE);
00059       return 2;
00060     }
00061 
00062   status = pthread_mutex_lock (&synch);
00063   if (status != 0)
00064     {
00065       printf ("cannot get lock: %s\n", strerror (status));
00066       return 1;
00067     }
00068 
00069   status = pthread_create (&thread, &attr, test_thread, NULL);
00070   if (status != 0)
00071     {
00072       printf ("pthread_create failed: %s\n", strerror (status));
00073       return 1;
00074     }
00075 
00076   status = pthread_getattr_np (thread, &attr);
00077   if (status != 0)
00078     {
00079       printf ("pthread_getattr_np failed: %s\n", strerror (status));
00080       return 1;
00081     }
00082 
00083   status = pthread_attr_getstack (&attr, &stack2, &stacksize);
00084   if (status != 0)
00085     {
00086       printf ("pthread_attr_getstack failed: %s\n", strerror (status));
00087       return 1;
00088     }
00089 
00090   if (stack2 != stack || stacksize != STACKSIZE)
00091     {
00092       printf ("second pthread_attr_getstack returned different stack (%p,%zx)\n"
00093              "than was set by setstack (%p,%x)\n",
00094              stack2, stacksize, stack, STACKSIZE);
00095       return 3;
00096     }
00097 
00098   status = pthread_mutex_unlock (&synch);
00099   if (status != 0)
00100     {
00101       printf ("cannot release lock: %s\n", strerror (status));
00102       return 1;
00103     }
00104 
00105   /* pthread_detach (thread); */
00106   if (pthread_join (thread, NULL) != 0)
00107     {
00108       printf ("join failed\n");
00109       return 1;
00110     }
00111   return 0;
00112 }