There seem to be a lot of question on SO that are close, but not quite what I'm looking for. I'm trying to see if there's a way to open a new terminal window (Linux), with a thread/child process from my main program, and have that thread/child process own the new window.
Just an overview of the full objective: I'm going to have a main program that I will launch and will take input via stdin
, if I select input to "start a helper" it will spawn a new terminal window which can itself interact with the user (stdin/stdout).
So what I want to do is have the main program call the thread, have the thread use/own the new terminal window, then have that window close when the thread goes away and dies.
I know this code doesn't work right, but conceptually, I'd like something like this:
void * Runit()
{
system("gnome-terminal"); //Would like to get a handle to this window
while(1)
printf("I'm the thread!!!\n"); //Would like to get this printed to the new window
}
int main()
{
pthread_t child;
pthread_create(&child, NULL, Runit, NULL);
sleep(10);
return 0; //Would like the child & its window to go away now.
}
The requirements on this are loose, it does not have to be portable, this is just a little Linux tool to make my life easier. It does have to be C code, so no shell scripting unless that script can be run from C. Any help or even other ideas is appreciated.
EDIT:
I'm aware that in linux terminals have file handles /dev/pts/x
and I have tried code like:
system("gnome-terminal");
sleep(2); //let the file handle show up in /dev/pts
fp = fopen("/dev/pts/<new file handler number>");
fprintf(fp, "echo hi");
The handle opens correctly, but nothing is displayed in the terminal.
To open a terminal, you can press Ctrl, Alt and T keys together. It's not that complicate. Press and hold Ctrl first and then press Alt key and hold on to it as well. When you are holding both Ctrl and Alt keys, press T and you'll see that a new terminal window is opened.
You can also press Alt+F2 to open the Run a Command dialog. Type gnome-terminal here and press Enter to launch a terminal window. You can run many other commands from the Alt+F2 window, too. You won't see any information as you would when running the command in a normal window, however.
I would recommend, therefore, that you write a helper program that knows how to communicate with your main program (via sockets, named pipes, or some other IPC mechanism). Your thread spawns the terminal program, passing it your helper program, which will run inside the terminal and communicate with the thread via the aforementioned IPC channel.
Option 2) – The easiest way to open a terminal window is to use the shortcut CTRL+ALT+T or press Alt+F2, type in gnome-terminal, and press enter. Entering this shortcut will instantly open the terminal window.
How To Open C File In Linux Terminal? Select the directory where your software is stored. There is a command called “cd”. This command has all the functions that allow you to list all files in a directory after changing it. By pressing the “vi” key, you can open a file.
To open a new tab in the current opened terminal you can press SHIFT + CTRL + T . Alternatively, use the top level menu, which shows the keyboard shortcut (see screenshot below) Install xdotool - a program that lets you simulate keyboard input (among other things).
Both gnome-terminal and xterm allow you to run an arbitrary command once the terminal opens.
I would recommend, therefore, that you write a helper program that knows how to communicate with your main program (via sockets, named pipes, or some other IPC mechanism). Your thread spawns the terminal program, passing it your helper program, which will run inside the terminal and communicate with the thread via the aforementioned IPC channel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With