How to open a new terminal through C program in Linux

I wrote code with client code in which I have many connections, and let each node represent a different process on the same computer. And for this I obviously use fork ().

But now the problem is that all the results are displayed on one terminal. I want to know if there is a way that after each fork() or process creation, a new terminal opens, and all the results will be displayed for this process on a specific terminal.

PS: I tried system("gnome-terminal") , but it just opens a new terminal, but all the results are displayed again on the same terminal. All new terminals simply open and remain empty without any results.

I also clicked on this link. How to call the program output of another terminal for output in C on Linux , but I do not want to run my program with parameters or something else, It should be exactly the same as ./test

Here is my code: -

 for(int i=0;i<node-1;i++) { n_number++; usleep(5000); child_pid[i]=fork(); if(!child_pid[i]) { system("gnome-terminal"); file_scan(); connection(); exit(0); } if(child_pid[i]<0) printf("Error Process %d cannot be created",i); } for(int i=0;i<node-1;i++) wait(&status); 

So basically I want for each process there should be a new terminal displaying only this process information or result.

What I definitely want:

  • After fork () I have some data related to the say 1 process, then I want its output to be on the same terminal
  • The same thing happens with every process. So if I have 3 processes, then there should be 3 terminals, and each should only display process related data.

I know that this can be done using IPC (Inter Process Communication), but is there any other way? I mean just 2-3 teams or so? Because I do not want to invest too much in coding this part.

Thanks in advance!

+4
source share
1 answer

Maybe you want something like that. This program uses the unix98 pseudo-terminal (PTS), which is a bi-directional channel between the master and slave. So, for each fork you make, you will need to create a new PTS by calling triad posix_openpt, grantpt, unlockpt on the main side and ptsname on the subordinate side. Remember to fix the original filedescriptors (stdin, stdout and sdterr) on each side.

Please note that this is just a program to prove the concept, so I do not do any error checking.

 #define _XOPEN_SOURCE 600 #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <libgen.h> #include <string.h> #include <fcntl.h> int main() { pid_t i; char buf[10]; int fds, fdm, status; fdm = posix_openpt(O_RDWR); grantpt(fdm); unlockpt(fdm); close(0); close(1); close(2); i = fork(); if ( i == 0 ) { // father dup(fdm); dup(fdm); dup(fdm); printf("Where do I pop up?\n"); sleep(2); printf("Where do I pop up - 2?\n"); waitpid(i, &status, 0); } else { // child fds = open(ptsname(fdm), O_RDWR); dup(fds); dup(fds); dup(fds); strcpy(buf, ptsname(fdm)); sprintf(buf, "xterm -S%c/2", basename(buf)); system(buf); exit(0); } } 
+5
source

All Articles