Mike
Mike

Reputation: 49373

Is it possible to open a new Linux terminal with a thread in C?

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.

Upvotes: 7

Views: 6566

Answers (3)

Al W
Al W

Reputation: 476

As I understand it, the application must be able to print into the terminal - but do you want it also to read from it? Should the terminal be controlled by the user?

I'd manipulate with terminal's file descriptors, you should check from what file does the terminal obtain user input (it's probably NOT stdin, but some /dev/pts?), and to what file does the terminal write it's output, and you could capture it, and you could also write into it, causing the content to be displayed in the terminal. But beware: 1) The terminal itself will probably lauch shell (bash) - if you write to /dev/pts, it will be displayed in the terminal, but will not be passed to the bash process in the terminal - you can't remotely make commands in such temrinal, 2) I think that /dev/pts/x file will be created when the terminal starts, so you can't fork+dup+exec and have the terminal output captured 3) The process that will be ran in the terminal will print some output too - it will be not read from /dev/pts

Maybe you need to write application in C which will just be ran in the terminal, and that application will communicate with your application which owns the terminal?

Upvotes: 0

parsifal
parsifal

Reputation: 301

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.

Upvotes: 2

zwol
zwol

Reputation: 140540

The library libvte, specifically the vte_pty_* functions, may be capable of doing what you want.

The way you have phrased the question suggests that you do not understand how windows and terminal I/O work under Linux (or, actually, Unix in general), so I strongly recommend you read up on these things. Start with W. Richard Stevens' book Advanced Programming in the Unix Environment.

Upvotes: 1

Related Questions