=== Released 2.3.5 ===
[platform/upstream/glib.git] / tests / child-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "config.h"
28
29 #include <sys/types.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <stdlib.h>
34
35 #include <glib.h>
36
37 #ifdef G_OS_WIN32
38 #include <windows.h>
39 #endif
40
41 GMainLoop *main_loop;
42 gint alive;
43
44 #ifdef G_OS_WIN32
45 char *argv0;
46 #endif
47
48 GPid
49 get_a_child (gint ttl)
50 {
51   GPid pid;
52
53 #ifdef G_OS_WIN32
54   STARTUPINFO si;
55   PROCESS_INFORMATION pi;
56   gchar *cmdline;
57
58   memset (&si, 0, sizeof (si));
59   si.cb = sizeof (&si);
60   memset (&pi, 0, sizeof (pi));
61
62   cmdline = g_strdup_printf( "child-test -c%d", ttl);
63
64   if (!CreateProcess (argv0, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
65     exit (1);
66
67   g_free(cmdline);
68
69   CloseHandle (pi.hThread);
70   pid = pi.hProcess;
71
72   return pid;
73 #else
74   pid = fork ();
75   if (pid < 0)
76     exit (1);
77
78   if (pid > 0)
79     return pid;
80
81   sleep (ttl);
82   _exit (0);
83 #endif /* G_OS_WIN32 */
84 }
85
86 gboolean
87 child_watch_callback (GPid pid, gint status, gpointer data)
88 {
89   gint ttl = GPOINTER_TO_INT (data);
90
91 #ifndef G_OS_WIN32
92   g_print ("child %d (ttl %d) exited, status %d\n", pid, ttl, status);
93 #else
94   g_print ("child %p (ttl %d) exited, status %d\n", pid, ttl, status);
95 #endif
96
97   if (--alive == 0)
98     g_main_loop_quit (main_loop);
99
100   return TRUE;
101 }
102
103 static gpointer
104 test_thread (gpointer data)
105 {
106   GMainLoop *new_main_loop;
107   GSource *source;
108   GPid pid;
109   gint ttl = GPOINTER_TO_INT (data);
110
111   new_main_loop = g_main_loop_new (NULL, FALSE);
112
113   pid = get_a_child (ttl);
114   source = g_child_watch_source_new (pid);
115   g_source_set_callback (source, (GSourceFunc) child_watch_callback, data, NULL);
116   g_source_attach (source, g_main_loop_get_context (new_main_loop));
117   g_source_unref (source);
118
119 #ifdef G_OS_WIN32
120   g_print ("whee! created pid: %p (ttl %d)\n", pid, ttl);
121   CloseHandle(pid);
122 #else
123   g_print ("whee! created pid: %d (ttl %d)\n", pid, ttl);
124 #endif
125
126   g_main_loop_run (new_main_loop);
127
128   return NULL;
129 }
130
131 int
132 main (int argc, char *argv[])
133 {
134 #ifdef G_OS_WIN32
135   argv0 = argv[0];
136   if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'c')
137     {
138       int ttl = atoi (argv[1] + 2);
139       Sleep (ttl * 1000);
140       exit (STILL_ACTIVE);
141     }
142 #endif
143   /* Only run the test, if threads are enabled and a default thread
144      implementation is available */
145 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
146    g_thread_init (NULL);
147   main_loop = g_main_loop_new (NULL, FALSE);
148
149   system ("/bin/true");
150
151   alive = 2;
152   g_thread_create (test_thread, GINT_TO_POINTER (10), FALSE, NULL);
153   g_thread_create (test_thread, GINT_TO_POINTER (20), FALSE, NULL);
154   
155   g_main_loop_run (main_loop);
156
157 #endif
158    return 0;
159 }