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 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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
29 #include <sys/types.h>
42 #define GPID_FORMAT "%p"
44 #define GPID_FORMAT "%d"
55 get_a_child (gint ttl
)
61 PROCESS_INFORMATION pi
;
64 memset (&si
, 0, sizeof (si
));
66 memset (&pi
, 0, sizeof (pi
));
68 cmdline
= g_strdup_printf( "child-test -c%d", ttl
);
70 if (!CreateProcess (argv0
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
))
71 g_error ("CreateProcess failed: %s\n", g_win32_error_message (GetLastError ()));
75 CloseHandle (pi
.hThread
);
89 #endif /* G_OS_WIN32 */
93 child_watch_callback (GPid pid
, gint status
, gpointer data
)
96 gint ttl
= GPOINTER_TO_INT (data
);
98 g_print ("child " GPID_FORMAT
" (ttl %d) exited, status %d\n", pid
, ttl
, status
);
101 g_spawn_close_pid (pid
);
104 g_main_loop_quit (main_loop
);
110 quit_loop (gpointer data
)
112 GMainLoop
*main_loop
= data
;
114 g_main_loop_quit (main_loop
);
121 test_thread (gpointer data
)
123 GMainLoop
*new_main_loop
;
126 gint ttl
= GPOINTER_TO_INT (data
);
128 new_main_loop
= g_main_loop_new (NULL
, FALSE
);
130 pid
= get_a_child (ttl
);
131 source
= g_child_watch_source_new (pid
);
132 g_source_set_callback (source
, (GSourceFunc
) child_watch_callback
, data
, NULL
);
133 g_source_attach (source
, g_main_loop_get_context (new_main_loop
));
134 g_source_unref (source
);
137 g_print ("whee! created pid: " GPID_FORMAT
" (ttl %d)\n", pid
, ttl
);
140 g_main_loop_run (new_main_loop
);
147 main (int argc
, char *argv
[])
154 if (argc
> 1 && argv
[1][0] == '-' && argv
[1][1] == 'c')
156 int ttl
= atoi (argv
[1] + 2);
158 /* Exit on purpose with STILL_ACTIVE (which isn't a very common
159 * exit status) to verify that g_child_watch_check() in gmain.c
160 * doesn't believe a child still to be active if it happens to
161 * exit with that status.
166 /* Only run the test, if threads are enabled and a default thread
167 * implementation is available.
169 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
171 g_thread_init (NULL
);
173 main_loop
= g_main_loop_new (NULL
, FALSE
);
176 system ("ipconfig /all");
178 system ("/bin/true");
182 g_timeout_add (30000, quit_loop
, main_loop
);
185 g_thread_create (test_thread
, GINT_TO_POINTER (10), FALSE
, NULL
);
186 g_thread_create (test_thread
, GINT_TO_POINTER (20), FALSE
, NULL
);
188 pid
= get_a_child (10);
189 g_child_watch_add (pid
, (GChildWatchFunc
) child_watch_callback
,
190 GINT_TO_POINTER (10));
191 pid
= get_a_child (20);
192 g_child_watch_add (pid
, (GChildWatchFunc
) child_watch_callback
,
193 GINT_TO_POINTER (20));
196 g_main_loop_run (main_loop
);
200 g_warning ("%d children still alive\n", alive
);