I have a simple console application that creates 3 child processes:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
STARTUPINFO si[5];
PROCESS_INFORMATION pi[5];
int procIndex = 0;
void monitor_new_process();
void monitor_shutdown();
/**
*
* @see: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682512(v=vs.85).aspx
*/
void _tmain(int argc, TCHAR *argv[]) {
monitor_new_process();
monitor_new_process();
monitor_new_process();
monitor_shutdown();
}
void monitor_new_process() {
ZeroMemory(&si[procIndex], sizeof(si[procIndex]));
si[procIndex].cb = sizeof(si[procIndex]);
ZeroMemory(&pi[procIndex], sizeof(pi[procIndex]));
// Start the child process.
if (!CreateProcess(NULL, // No module name (use command line)
"consumer", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si[procIndex], // Pointer to STARTUPINFO structure
&pi[procIndex]) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
return;
}
procIndex++;
}
void monitor_shutdown() {
while (procIndex >= 0){
// Wait until child process exits.
WaitForSingleObject(pi[procIndex].hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi[procIndex].hProcess);
CloseHandle(pi[procIndex].hThread);
procIndex--;
}
}
I have also been playing around with GTK+. I've made a simple application that generates a window from a ui file:
#include <gtk/gtk.h>
GtkBuilder *builder;
GObject *window;
GObject *button;
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
/* Construct a GtkBuilder instance and load our UI description */
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "builder.ui", NULL);
/* Connect signal handlers to the constructed widgets. */
window = gtk_builder_get_object(builder, "window");
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
I'm wondering if there is a GTK+ element that I could put into the builder.ui file that could act as a "container" for child processes.
For example, I could have something like this in the builder:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">Test</property>
<property name="window_position">center</property>
<property name="default_width">440</property>
<property name="default_height">250</property>
<property name="destroy_with_parent">True</property>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuBar" id="menubar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="menuitem1">
<!- ... ->
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<!-- SOME KIND OF CHILD PROCESS CONTAINER -->
</child>
</object>
</child>
</object>
</interface>
Where <!-- SOME KIND OF CHILD PROCESS CONTAINER --> would be an object with an ID, where I could nest a GTK+ window from a child process.
A similar concept is like an iFrame in HTML -- where you can use HTML to designate a frame where an external web application will be nested into a parent container.
Another similar concept is Google Chrome. Chrome's shell is a process, and each tab is a child process. This is essentially exactly what I'm trying to mimic.
What would I need to do in GTK+ and with my child process script to do this? Is this even possible with GTK?
Yes, you can. Check out
GtkSocketandGtkPlug. They are only available for the X11 backend of GTK, though, so your application won't be cross-platform: specifically, Windows and native OSX won't support this, and neither will Wayland.