Re: g_spawn_async_with_pipes and pipes



/* More correct and illustrative variant */

#include <glib.h>

void
setup_first (gpointer data)
{
  int *p = (int *) data;
  dup2 (p[1], 1);
  close (p[0]);
}

void
setup_second (gpointer data)
{
  int *p = (int *) data;
  dup2 (p[0], 0);
  close (p[1]);
}

int
main ()
{
  char *argv1[] = { "/bin/ls", NULL };
  char *argv2[] = { "/usr/bin/wc", "-l", NULL };
  GError *err = NULL;

  gint p[2];

  pipe (p);

  g_spawn_async_with_pipes (NULL, argv1, NULL,
			    G_SPAWN_LEAVE_DESCRIPTORS_OPEN,
			    setup_first, p,
			    NULL, NULL, NULL, NULL, &err);

  g_spawn_async_with_pipes (NULL, argv2, NULL, 
			    G_SPAWN_LEAVE_DESCRIPTORS_OPEN,
			    setup_second, p, NULL, 
			    NULL, NULL, NULL, &err);

  g_assert (err == NULL);

  sleep (1);

  close (p[0]);
  close (p[1]);

  return 0;
}





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