Question using glib, threads, and the daemon() function..



  While doing some work, I ran into a strange issue. I had a linux application 
using the pthreads library and the daemon function. Since moving to glib's 
threads, the daemon() function no longer works.

  Here is a sample program which reproduces the behavior. If I remove glib's 
threads and replace with generic pthread usage, it functions as desired. 
Anyone have advice for me?

  Franco

gcc -g `pkg-config --cflags glib-2.0 gmodule-2.0 gthread-2.0` -c test.c
gcc -g -o test `pkg-config --libs glib-2.0 gmodule-2.0 gthread-2.0` test.o

Uncomment the daemon() function to see the behavior.

--- BEGIN SAMPLE ---
#include <glib.h>

#define THREAD_COUNT (3)

guint shutdown = 0;

gpointer thread_main(gpointer data)
{
  guint count = 0;

  printf("Thread started.\n");

  while (shutdown == 0)
  {
    count++;
    g_usleep(100);
  }

  printf("Thread did %i work instructions.\n");
  g_thread_exit(NULL);
}

gint main(gint argc, gchar **argv)
{
  GThread *peers[THREAD_COUNT];
  guint loop;

  if (g_thread_supported() == FALSE)
    g_thread_init(NULL);

  /*
  if (daemon(1, 0) == -1)
  {
    printf("Cannot detach!\n");
    exit(-1);
  }
  */

  for (loop = 0; loop < THREAD_COUNT; loop++)
  {
    if ((peers[loop] = g_thread_create(thread_main, NULL, TRUE, NULL)) == 
NULL)
    {
      printf("Cannot create thread!\n");
      exit(-1);
    }
  }

  printf("All threads created, allowing threads to do a little BS work.\n");

  sleep(10);

  printf("Signalling shutdown..\n");

  shutdown = 1;

  for (loop = 0; loop < THREAD_COUNT; loop++)
    g_thread_join(peers[loop]);

  printf("All threads cleaned up.\n");

  exit(0);
}
--- END SAMPLE ---




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]