February 5th, 2003, 01:52 AM
fork a process with a different terminal?
Is it possible, after forking a process, to have that process run in a different terminal?
The idea is to have a client and a server terminal, each with it's own stdin, stdout and stderr.
February 5th, 2003, 01:39 PM
Do you need the process to do it programmatically, or is it ok to use the command line to redirect the output. I mean, can you do something like this instead:
nohup /path/to/program > /dev/tty2 &
This way your program does not need to fork() at all (I'm assuming you're using fork() to put the process in the background). Actually, I used the same fork() trick on some of my code before discovering that this may actually be a Bad Thing. Incidentally, one more thing I discovered is that when I fork() a process into a background, some of my threading code didn't work on FreeBSD, but worked on Linux just fine. Removing the fork() made it work on both systems!
Hope this helps!
February 5th, 2003, 02:19 PM
you can close() the three file descriptors for stdin, stdout and stderr (0,1,2) and re- open() them on a new terminal (eg. /dev/tty8).
Scorpions4ever: very interesting URL. i´m off reading now...
February 6th, 2003, 02:01 AM
Thanks for the replies guys!
Just to fix a few mistakes in my initial query. I mentioned fork()ing because I'd figured it would be similar to creating a new thread. I'm actually using an implementation of Linda (tuple-space) which uses POSIX threads for internal synchronization (sorry I didn't mention that it uses threads and not a separate process, as I wasn't thinking that it'd matter).
Basically, I'm spawning off a new thread (via the Linda implementation's spawn() functon) which would be the client thread of execution. There can be more than 1 client. And the thing is I need each client to read/write from a different 'window' or terminal from the server thread.
Scorpions4ever: I don't _think_ using nohup would work because it's actually a thread that I'm trying to attach to a terminal, not a program. Is that right?
M.Hirsch: How do I accomplish that? Would it work for a thread? I only know how to open() , dup2() fds in the case of processes but for threads it would be different wouldn't it?
February 6th, 2003, 10:11 AM
as threads run in one process, you only need to close the handles in your main() function - unless this "Linda" uses processes to emulate threads where i could not help you any further...
Anyway, afaik you should use multiple processes in linux as unix is afaik much more effective if using processes instead of threads. Threads are for windows programming. But this knowledge might be a little outdated...
But i am not sure if multiple threads can be connected to different terminals. you could just open() a new descriptor to write to another terminal though... (probably easier to use: fopen() the tty device and write to it as if it was a file)