0

I have some program want to use Glib for cross-platform running. but I found Glib's process control g_spawn*() functions are very difficult to use. I don't know how to replace some very basic use of Unix system function fork().

#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    pid_t pid = fork();
    if (pid == 0)
    {
        printf("Here is child process\n");
    }
    else if (pid > 0)
    {
        printf("Here is parent process\n");
    }
    else
    {
        // fork failed
        printf("fork() failed!\n");
        return 1;
    }
    return 0;
}

the g_spawn*() functions description is here,It need fill a lot of arguments and even need other functions help it bind IO,I don't know how to use them replace fork(), it always try to run some scripts in "gchar **argv," argument. but I just need it work like fork(),don't want it run like exec(). Click here!

1 Answers1

0

The process spawning API in GLib is designed to replace the common pattern of using fork() and exec() together, not just fork(). It's actually very nice (and easy) for what it was designed for, but if you want an API that acts like fork() you should use fork().

nemequ
  • 16,623
  • 1
  • 43
  • 62
  • still have problem. "The process spawning API in GLib is designed to replace ... fork() and exec() together, not just fork()." then can you tell me how to replace fork() in my code and print 2 message (1 form child process 1 form parent),please ?Definitely, I don't mean totally same with fork,just similar , can replace and do it's work, just as you have said "replace ... fork()" . "you should use fork()." I have already said "use Glib for cross-platform running." You should know there is only glib , no fork() in target platforms,I only need it's function. – Terrance Chen Jun 02 '15 at 00:59
  • AFAIK glib doesn't contain a function to replace fork alone. If you're doing something that can't be solved another way (threads, a helper executable, etc.) you may have to just write multiple implementations yourself. See http://stackoverflow.com/questions/985281/what-is-the-closest-thing-windows-has-to-fork/985525#985525 for the Windows version. – nemequ Jun 04 '15 at 17:26