#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    32
    Rep Power
    13

    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.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    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!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    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. im off reading now...
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    32
    Rep Power
    13
    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?

    Thanks again!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    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)

IMN logo majestic logo threadwatch logo seochat tools logo