Back to index

glibc  2.9
tst-basic7.c
Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <pthread.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <unistd.h>
00007 #include <sys/mman.h>
00008 #include <sys/resource.h>
00009 
00010 static void use_stack (size_t needed);
00011 
00012 void (*use_stack_ptr) (size_t) = use_stack;
00013 
00014 static void
00015 use_stack (size_t needed)
00016 {
00017   size_t sz = sysconf (_SC_PAGESIZE);
00018   char *buf = alloca (sz);
00019   memset (buf, '\0', sz);
00020 
00021   if (needed > sz)
00022     use_stack_ptr (needed  - sz);
00023 }
00024 
00025 static void
00026 use_up_memory (void)
00027 {
00028   struct rlimit rl;
00029   getrlimit (RLIMIT_AS, &rl);
00030   rl.rlim_cur = 10 * 1024 * 1024;
00031   setrlimit (RLIMIT_AS, &rl);
00032 
00033   char *c;
00034   int PAGESIZE = getpagesize ();
00035   while (1)
00036     {
00037       c = mmap (NULL, PAGESIZE, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
00038       if (c == MAP_FAILED)
00039        break;
00040     }
00041 }
00042 
00043 static void *
00044 child (void *arg)
00045 {
00046   sleep (1);
00047   return arg;
00048 }
00049 
00050 static int
00051 do_test (void)
00052 {
00053   int err;
00054   pthread_t tid;
00055 
00056   /* Allocate the memory needed for the stack.  */
00057   use_stack_ptr (PTHREAD_STACK_MIN);
00058 
00059   use_up_memory ();
00060 
00061   err = pthread_create (&tid, NULL, child, NULL);
00062   if (err != 0)
00063     {
00064       printf ("pthread_create returns %d: %s\n", err,
00065              err == EAGAIN ? "OK" : "FAIL");
00066       return err != EAGAIN;
00067     }
00068 
00069   /* We did not fail to allocate memory despite the preparation.  Oh well.  */
00070   return 0;
00071 }
00072 
00073 #define TEST_FUNCTION do_test ()
00074 #include "../test-skeleton.c"