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.

  • normal exec not suitable? - KoVadim
  • @KoVadim when 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 Markov
  • In linus, running any (yes yes, except init) is fork + exec. On another it is impossible. And fork does not make the program "multi-threaded." - KoVadim
  • @KoVadim just gave me a task, and there it was said to imagine that we were doing a program on some embedded system where there was no support for multithreading. That is, calls fork() and pthread() not allowed - Roman Markov
  • one
    fork and exec are not multithreading. And if it was possible to run gnome-terminal on this system, then fork exec will work there. - KoVadim

1 answer 1

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:

  1. Start the terminal (how? If you are not allowed to fork? posix_spawn , in fact, the same fork + exec inside).
  2. In the parent process you have his PID
  3. The terminal opens the PTS device (/ dev / pts / ###) which is used for IO
  4. We look in / proc / TERMINAL-PID / fd where descriptors 0, 1 and 2 refer (it is possible only 1)
  5. In our program we open these files for writing, remember the descriptors (let it be 1 - fd )
  6. Close our descriptors 1 and 2 ( close(1); close(2); )
  7. Use dup2 to put fd on descriptors 1 and 2.
  8. All our output to STDOUT and STDERR now goes to the terminal window.
  9. But why?