1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 #include <sys/types.h>
39 #define GPID_FORMAT "%p"
41 #define GPID_FORMAT "%d"
52 get_a_child (gint ttl
)
58 PROCESS_INFORMATION pi
;
61 memset (&si
, 0, sizeof (si
));
63 memset (&pi
, 0, sizeof (pi
));
65 cmdline
= g_strdup_printf( "child-test -c%d", ttl
);
67 if (!CreateProcess (argv0
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
))
68 g_error ("CreateProcess failed: %s", g_win32_error_message (GetLastError ()));
72 CloseHandle (pi
.hThread
);
86 #endif /* G_OS_WIN32 */
90 child_watch_callback (GPid pid
, gint status
, gpointer data
)
93 gint ttl
= GPOINTER_TO_INT (data
);
95 g_print ("child " GPID_FORMAT
" (ttl %d) exited, status %d\n", pid
, ttl
, status
);
98 g_spawn_close_pid (pid
);
101 g_main_loop_quit (main_loop
);
107 quit_loop (gpointer data
)
109 GMainLoop
*main_loop
= data
;
111 g_main_loop_quit (main_loop
);
118 test_thread (gpointer data
)
120 GMainLoop
*new_main_loop
;
123 gint ttl
= GPOINTER_TO_INT (data
);
125 new_main_loop
= g_main_loop_new (NULL
, FALSE
);
127 pid
= get_a_child (ttl
);
128 source
= g_child_watch_source_new (pid
);
129 g_source_set_callback (source
, (GSourceFunc
) child_watch_callback
, data
, NULL
);
130 g_source_attach (source
, g_main_loop_get_context (new_main_loop
));
131 g_source_unref (source
);
134 g_print ("whee! created pid: " GPID_FORMAT
" (ttl %d)\n", pid
, ttl
);
137 g_main_loop_run (new_main_loop
);
144 main (int argc
, char *argv
[])
151 if (argc
> 1 && argv
[1][0] == '-' && argv
[1][1] == 'c')
153 int ttl
= atoi (argv
[1] + 2);
155 /* Exit on purpose with STILL_ACTIVE (which isn't a very common
156 * exit status) to verify that g_child_watch_check() in gmain.c
157 * doesn't believe a child still to be active if it happens to
158 * exit with that status.
164 main_loop
= g_main_loop_new (NULL
, FALSE
);
167 system ("ipconfig /all");
173 g_timeout_add_seconds (30, quit_loop
, main_loop
);
176 g_thread_create (test_thread
, GINT_TO_POINTER (10), FALSE
, NULL
);
177 g_thread_create (test_thread
, GINT_TO_POINTER (20), FALSE
, NULL
);
179 pid
= get_a_child (10);
180 g_child_watch_add (pid
, (GChildWatchFunc
) child_watch_callback
,
181 GINT_TO_POINTER (10));
182 pid
= get_a_child (20);
183 g_child_watch_add (pid
, (GChildWatchFunc
) child_watch_callback
,
184 GINT_TO_POINTER (20));
187 g_main_loop_run (main_loop
);
189 g_main_loop_unref (main_loop
);
193 g_warning ("%d children still alive", alive
);