0

I write a program, which should create new process (I use fork(), and next in child process call execl()) and communicate with it. Here is my server:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

int main(int argc, char *argv[]) {
    pid_t process;
    process = fork();
    if (process == 0) {
        printf("The program will be executed %s...\n\n", argv[0]);
        printf("Executing %s", argv[0]);
        execl("hello", "Hello, World!", NULL);

        return EXIT_SUCCESS;
    }
    else if (process < 0) {
        fprintf (stderr, "Fork failed.\n");
        return EXIT_FAILURE;
    }

    waitpid(process, NULL, NULL);

    return 0;
}

And here is my client:

#include <stdio.h>
int main(int argc, char *argv[])
{
  int i=0;
  printf("%s\n",argv[0]);
  printf("The program was executed and got a string : ");
  while(argv[++i] != NULL)
  printf("%s ",argv[i]);
  return 0;
}

The problem is the next: my client and server show output in the same terminal. I want them to show output in separate terminals. So, how can I do it?

  • Note that terminal is just an interface. They don't *execute* in the same terminal. They execute on the same machine. You mean you want to *see* the output of the client in another terminal? – Arash Mar 24 '17 at 21:11
  • @Arash Yes, I would like to see it in another terminal – Ivan Suprynovich Mar 24 '17 at 21:20
  • see this question: http://stackoverflow.com/questions/3445645/how-to-invoke-another-terminal-for-output-programmatically-in-c-in-linux – terence hill Mar 24 '17 at 21:40

1 Answers1

1

You need to have two open terminals. The idea is to run your program in the first terminal and see the output of the client in the second terminal.

First, you need to know what is the ID of the second terminal. So in the second terminal do:

$ tty
/dev/pts/1 

(note your output will be probably different because mine is a SSH connection and hence pts, yours will be /dev/tty)

And then in your child process, you tell it to use this other terminal for its output. Like this:

#include <stdio.h>
#include <fcntl.h>

int main(int argc, char *argv[]) {
  int fd = open("/dev/pts/1",O_RDWR) ;  // note that in your case you need to update this based on your terminal name   
  // duplicate the fd and overwrite the stdout value 
  if (fd < 0){
    perror("could not open fd");
    exit(0);
  }
  if (dup2(fd, 0) < 0 ){
    perror("dup2 on stdin failed");
    exit(0);
  }
  if (dup2(fd, 1) < 0 ){
    perror("dup2 on stdout failed");
    exit(0);
  }

    // from now on all your outputs are directed to the other terminal. 
    // and inputs are also come from other terminal.
}
Arash
  • 1,950
  • 14
  • 17
  • Well, I tried entering "/dev/tty" in my terminal, but it doesn't work – Ivan Suprynovich Mar 25 '17 at 09:27
  • And actually, I need not just output in the second terminal, but also write enter some information there – Ivan Suprynovich Mar 25 '17 at 09:29
  • @Ivan Suprynovich: what is the output of running `tty` in the second terminal? It should have a number... Like `/dev/ttys002`. Correct? Then you need to use this...(just entering `/dev/tty` won't work) – Arash Mar 25 '17 at 14:00
  • @IvanSuprynovich At first, try running client directly in the first terminal. like `./client` and see if you get output in the second terminal. This makes debugging easier. – Arash Mar 25 '17 at 14:05
  • But I have one problem: input is unusual, I need click on my second Terminal, if I want to enter a number. I don't know why, but it tries to write it as a command for terminal. It works on input, but not fine – Ivan Suprynovich Mar 26 '17 at 14:22
  • it sends first symbol to terminal, second - to my program, third to terminal, fourth - to program and etc. – Ivan Suprynovich Mar 26 '17 at 15:04
  • Yes. Reading is clumsy because besides your program the other terminal itself is trying to get input from the user and execute commands. Typically redirecting output is commonplace but not input. If your child process is only the interactive one, then keep it in current terminal and redirect the stdin/stdout of parent to the other terminal. – Arash Mar 26 '17 at 15:26
  • is there any flag for opening terminal like it running a program? I searched, but didn't find anything helpful. – Ivan Suprynovich Mar 26 '17 at 15:38
  • Thank you very much) It's much better, than nothing. – Ivan Suprynovich Mar 26 '17 at 16:14