Back to index

glibc  2.9
Defines | Functions | Variables
ex17.c File Reference
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>

Go to the source code of this file.

Defines

#define STACKSIZE   0x100000

Functions

static void * test_thread (void *v_param)
int main (void)

Variables

static pthread_mutex_t synch = PTHREAD_MUTEX_INITIALIZER

Define Documentation

#define STACKSIZE   0x100000

Definition at line 18 of file ex17.c.


Function Documentation

int main ( void  )

Definition at line 21 of file ex17.c.

{
  pthread_t thread;
  pthread_attr_t attr;
  int status;
  void *stack, *stack2;
  size_t stacksize;

  pthread_attr_init (&attr);
  stack = mmap (NULL, STACKSIZE,
              PROT_READ | PROT_WRITE | PROT_EXEC,
              MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

  if (stack == MAP_FAILED)
    {
      perror ("mmap failed");
      return 1;
    }

  status = pthread_attr_setstack (&attr, stack, STACKSIZE);
  if (status != 0)
    {
      printf ("pthread_attr_setstack failed: %s\n", strerror (status));
      return 1;
    }

  status = pthread_attr_getstack (&attr, &stack2, &stacksize);
  if (status != 0)
    {
      printf ("pthread_attr_getstack failed: %s\n", strerror (status));
      return 1;
    }

  if (stack2 != stack || stacksize != STACKSIZE)
    {
      printf ("first pthread_attr_getstack returned different stack (%p,%zx)\n"
             "than was set by setstack (%p,%x)\n",
             stack2, stacksize, stack, STACKSIZE);
      return 2;
    }

  status = pthread_mutex_lock (&synch);
  if (status != 0)
    {
      printf ("cannot get lock: %s\n", strerror (status));
      return 1;
    }

  status = pthread_create (&thread, &attr, test_thread, NULL);
  if (status != 0)
    {
      printf ("pthread_create failed: %s\n", strerror (status));
      return 1;
    }

  status = pthread_getattr_np (thread, &attr);
  if (status != 0)
    {
      printf ("pthread_getattr_np failed: %s\n", strerror (status));
      return 1;
    }

  status = pthread_attr_getstack (&attr, &stack2, &stacksize);
  if (status != 0)
    {
      printf ("pthread_attr_getstack failed: %s\n", strerror (status));
      return 1;
    }

  if (stack2 != stack || stacksize != STACKSIZE)
    {
      printf ("second pthread_attr_getstack returned different stack (%p,%zx)\n"
             "than was set by setstack (%p,%x)\n",
             stack2, stacksize, stack, STACKSIZE);
      return 3;
    }

  status = pthread_mutex_unlock (&synch);
  if (status != 0)
    {
      printf ("cannot release lock: %s\n", strerror (status));
      return 1;
    }

  /* pthread_detach (thread); */
  if (pthread_join (thread, NULL) != 0)
    {
      printf ("join failed\n");
      return 1;
    }
  return 0;
}

Here is the call graph for this function:

static void* test_thread ( void *  v_param) [static]

Definition at line 12 of file ex17.c.

{
  pthread_mutex_lock (&synch);
  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 9 of file ex17.c.