This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug nptl/5245] New: pthread_create returns ENOMEM instead of EAGAIN


I found a bug about the return value of pthread_create. The posix expects 
EAGAIN for lack of memory, but in fact, it returns ENOMEM.

This problem can be reproduced by the following code:
--------------------------------------------------------------------
#include <stdio.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>

void use_up_memory(void)
{
  char *c;
  int PAGESIZE = getpagesize();
  while (1) {
    c = mmap(NULL, PAGESIZE, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
    if (c == MAP_FAILED)
      break;
  }
  printf("errno %d: %s\n", errno, strerror(errno));
}

void child()
{
  sleep(1);
}

int main(int argc, char *argv[])
{
  int err;
  pthread_t tid;

  use_up_memory();

  err = pthread_create(&tid, NULL, (void *)child, NULL);
  if (err) {
    printf("pthread_cretate returns %d: %s\n", err, strerror(err));
    return 1;
  }

  err = pthread_join(tid, NULL);
  if (err){
    printf("pthread_join returns %d\n", err);
    return 1;
  }
  return 0;
}
--------------------------------------------------------------------


The result is as follows:

----------------------------------------------------------
$ ./test_pthread_create
errno 12: Cannot allocate memory
pthread_cretate returns 12: Cannot allocate memory
----------------------------------------------------------

-- 
           Summary: pthread_create returns ENOMEM instead of EAGAIN
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: normal
          Priority: P2
         Component: nptl
        AssignedTo: drepper at redhat dot com
        ReportedBy: wangf at cn dot fujitsu dot com
                CC: glibc-bugs at sources dot redhat dot com


http://sourceware.org/bugzilla/show_bug.cgi?id=5245

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]