Thread: g_spawn_async_with_pipes doesn't work

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    8

    g_spawn_async_with_pipes doesn't work

    What I want to do is the following: I want to execute a ping command, but I don't want my program to be blocked while waiting for the response, so I want to execute it asynchronously and after a timeout check if the answer has arrived. This is for a program which should run on linux (debian).

    I thought of using the g_spawn_async_with_pipes command for glib, but that doesn't seem to work. Here's a simplified version of part my code:

    Code:
    void ping(char *IpAddress)
    {
      char command[100];
      gint Child_Process;
      gint Child_Out, Child_Err;
      GError *Child_Error = NULL;
      GIOChannel* iochannel;
    
      strcpy(command, "ping -c 1 "); // one packet at a time
      strcat(command, IpAddress);
      g_spawn_async_with_pipes(NULL, (gchar **)&command, NULL, (GSpawnFlags) (G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD),
    NULL, NULL, &Child_Process, NULL, &Child_Out, &Child_Err, &Child_Error);
    
      iochannel = g_io_channel_unix_new(Child_Out);
      g_io_channel_set_close_on_unref (iochannel, TRUE);
      g_io_add_watch (iochannel, (GIOCondition)(G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL), iofunc_stdout, NULL);
    }
    So I call the ping-function, but what I see is that the response-function (iofunc_stdout) is never called. Anybody an idea what I'm doing wrong. Or has an example on how to do this (I already searched the web for that)? Or anybody has a better way to do this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Most normal people use fork() and execl() to run processes, and close(), dup(), pipe() to alter the redirections of stdin/stdout/stderr.

    Many examples of this on the Linux forum.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    OK thx, that's indeed easier and it's working now.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I should have suggested using popen()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM