string24 string24 - 1 month ago 18
C Question

C program to run xyz.out file in another terminal

I can open a new terminal by this code,

char *argv[]={"gnome-terminal"," -x ","/home/try/",NULL};
if(execvp(argv[0], argv)==-1){
printf("Error in receiver\n");
exit(EXIT_FAILURE);
}


This will open a terminal with path /home/try

I can open another program through

system("./xyz");


I have a program say, pqr.c, and i want to open a already existing program xyz.c through it, but i want the output of pqr and xyz to be displayed on two different terminal.

How to do this?

Answer

You can make use of your terminal program's own capabilities. Most (if not all) Unix terminal emulators can take an argument which tells them what program to run, instead of the user's shell.

gnome-terminal is no different here and also it uses the most common parameter for the task: -e.

So your code can look like:

char *argv[]={
    "gnome-terminal",
    "-x", "/home/try/", // <-- note: no space before or after "-x"
    "-e", "/path/to/your/program",
    NULL};
if(execvp(argv[0], argv)==-1){
    printf("Error in receiver\n");
    exit(EXIT_FAILURE);
}

Some things to note:

  • The terminal will close as soon as its inner program (/path/to/your/program) finishes execution. To prevent that, you can make the program wait for some input before termnating.
  • As noted in the code snippet, there should be no additional spaces around program arguments. If gnome-terminal works fine with them, this only means that it strips those spaces while parsing arguments.
Comments