tst-basic7.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <errno.h>
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <limits.h>
  8. #include <sys/mman.h>
  9. #include <sys/resource.h>
  10. static void use_stack (size_t needed);
  11. void (*use_stack_ptr) (size_t) = use_stack;
  12. static void
  13. use_stack (size_t needed)
  14. {
  15. size_t sz = sysconf (_SC_PAGESIZE);
  16. char *buf = alloca (sz);
  17. memset (buf, '\0', sz);
  18. if (needed > sz)
  19. use_stack_ptr (needed - sz);
  20. }
  21. static void
  22. use_up_memory (void)
  23. {
  24. struct rlimit rl;
  25. getrlimit (RLIMIT_AS, &rl);
  26. rl.rlim_cur = 10 * 1024 * 1024;
  27. setrlimit (RLIMIT_AS, &rl);
  28. char *c;
  29. int pagesize = getpagesize ();
  30. while (1)
  31. {
  32. c = mmap (NULL, pagesize, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
  33. if (c == MAP_FAILED)
  34. break;
  35. }
  36. }
  37. static void *
  38. child (void *arg)
  39. {
  40. sleep (1);
  41. return arg;
  42. }
  43. static int
  44. do_test (void)
  45. {
  46. int err;
  47. pthread_t tid;
  48. /* Allocate the memory needed for the stack. */
  49. use_stack_ptr (PTHREAD_STACK_MIN);
  50. use_up_memory ();
  51. err = pthread_create (&tid, NULL, child, NULL);
  52. if (err != 0)
  53. {
  54. printf ("pthread_create returns %d: %s\n", err,
  55. err == EAGAIN ? "OK" : "FAIL");
  56. return err != EAGAIN;
  57. }
  58. /* We did not fail to allocate memory despite the preparation. Oh well. */
  59. return 0;
  60. }
  61. #define TEST_FUNCTION do_test ()
  62. #include "../test-skeleton.c"