Back to index

glibc  2.9
Enumerations | Functions | Variables
ex8.c File Reference
#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/wait.h>

Go to the source code of this file.

Enumerations

enum  { PREPARE_BIT = 1, PARENT_BIT = 2, CHILD_BIT = 4 }

Functions

static void prepare (void)
static void parent (void)
static void child (void)
static void * thread (void *arg)
int main (void)

Variables

static int var

Enumeration Type Documentation

anonymous enum
Enumerator:
PREPARE_BIT 
PARENT_BIT 
CHILD_BIT 

Definition at line 27 of file ex8.c.

{
  PREPARE_BIT = 1,
  PARENT_BIT = 2,
  CHILD_BIT = 4
};

Function Documentation

static void child ( void  ) [static]

Definition at line 49 of file ex8.c.

{
  var |= CHILD_BIT;
}

Here is the caller graph for this function:

int main ( void  )

Definition at line 59 of file ex8.c.

{
  pthread_t th;
  void *res;

  pthread_atfork (prepare, parent, child);

  if (pthread_create (&th, NULL, thread, NULL) != 0)
    error (EXIT_FAILURE, 0, "cannot create thread");

  pthread_join (th, &res);

  return (int) (long int) res;
}

Here is the call graph for this function:

static void parent ( void  ) [static]

Definition at line 43 of file ex8.c.

{
  var |= PARENT_BIT;
}

Here is the caller graph for this function:

static void prepare ( void  ) [static]

Definition at line 37 of file ex8.c.

{
  var |= PREPARE_BIT;
}

Here is the caller graph for this function:

static void * thread ( void *  arg) [static]

Definition at line 76 of file ex8.c.

{
  int status;
  pid_t pid;

  pid = fork ();
  if (pid == 0)
    {
      /* We check whether the `prepare' and `child' function ran.  */
      exit (var != (PREPARE_BIT | CHILD_BIT));
    }
  else if (pid == (pid_t) -1)
    error (EXIT_FAILURE, errno, "cannot fork");

  if (waitpid (pid, &status, 0) != pid)
    error (EXIT_FAILURE, errno, "wrong child");

  if (WTERMSIG (status) != 0)
    error (EXIT_FAILURE, 0, "Child terminated incorrectly");
  status = WEXITSTATUS (status);

  if (status == 0)
    status = var != (PREPARE_BIT | PARENT_BIT);

  return (void *) (long int) status;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int var [static]

Definition at line 34 of file ex8.c.