What are the functions or system calls to open gnome terminal
? It is necessary not through the console, namely in the C program, to get the file descriptor and redirect the output of the program to the new terminal.
1 answer
There are no system calls for this. This is not a system functionality.
You can use: http://www.gnu.org/software/libc/manual/html_node/Pipe-to-a-Subprocess.html , but this allows you to send commands to the stdin terminal, i.e. it will execute each line as a command, and not just display the output of your program.
You can also make a knight's move by making fork + exec (gnome-terminal), passing it yourself as a parameter to run. Something like this should happen:
#include <cstdlib> #include <cstdio> #include <iostream> #include <vector> #include <cstring> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> using namespace std; int main(int argc, char **argv) { // Run from GUI! Restart in terminal if (!isatty(fileno(stdin))) { auto pid = fork(); if (pid == 0) { vector<char*> args = { ::strdup("gnome-terminal"), ::strdup("-e"), }; // Dup my commad line options to the terminal for (int i = 0; i < argc; ++i) { args.push_back(argv[i]); } args.push_back(nullptr); if (execvp("gnome-terminal", args.data()) < 0) exit(1); } exit(pid < 0 ? 1 : 0); } // Regular execution cout << "Hello, from the terminal!\n"; cin.get(); return 0; }
Still worth considering using VTE
Well, the last option, according to the results of the specified data:
- Start the terminal (how? If you are not allowed to fork?
posix_spawn
, in fact, the same fork + exec inside). - In the parent process you have his PID
- The terminal opens the PTS device (/ dev / pts / ###) which is used for IO
- We look in / proc / TERMINAL-PID / fd where descriptors 0, 1 and 2 refer (it is possible only 1)
- In our program we open these files for writing, remember the descriptors (let it be 1 -
fd
) - Close our descriptors 1 and 2 (
close(1); close(2);
) - Use
dup2
to putfd
on descriptors 1 and 2. - All our output to STDOUT and STDERR now goes to the terminal window.
- But why?
|
exec
called, the parent process stops its work. It will not be possible to implement fork-exec, because you need to implement a single-threaded application. - Roman Markovfork()
andpthread()
not allowed - Roman Markov