Back to index

glibc  2.9
Defines | Functions | Variables
tst-flock1.c File Reference
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/file.h>
#include "../test-skeleton.c"

Go to the source code of this file.

Defines

#define TEST_FUNCTION   do_test ()

Functions

static void * tf (void *arg)
static int do_test (void)

Variables

static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER
static int fd

Define Documentation

#define TEST_FUNCTION   do_test ()

Definition at line 92 of file tst-flock1.c.


Function Documentation

static int do_test ( void  ) [static]

Definition at line 48 of file tst-flock1.c.

{
  char tmp[] = "/tmp/tst-flock1-XXXXXX";

  fd = mkstemp (tmp);
  if (fd == -1)
    {
      puts ("mkstemp failed");
      exit (1);
    }

  unlink (tmp);

  write (fd, "foobar xyzzy", 12);

  if (flock (fd, LOCK_EX | LOCK_NB) != 0)
    {
      puts ("first flock failed");
      exit (1);
    }

  pthread_mutex_lock (&lock);

  pthread_t th;
  if (pthread_create (&th, NULL, tf, NULL) != 0)
    {
      puts ("pthread_create failed");
      exit (1);
    }

  pthread_mutex_lock (&lock);

  void *result;
  if (pthread_join (th, &result) != 0)
    {
      puts ("pthread_join failed");
      exit (1);
    }

  close (fd);

  return result != NULL;
}

Here is the call graph for this function:

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

Definition at line 33 of file tst-flock1.c.

{
  if (flock (fd, LOCK_SH | LOCK_NB) != 0)
    {
      puts ("second flock failed");
      exit (1);
    }

  pthread_mutex_unlock (&lock);

  return NULL;
}

Here is the call graph for this function:


Variable Documentation

int fd [static]

Definition at line 29 of file tst-flock1.c.

Definition at line 27 of file tst-flock1.c.