-2

I am building a feature, where I can open and close files in vi editor. I am using execlp() system call in C for opening a file in a new terminal window in a new child process. Here is how it looks:

void openFile (char *fileName) {
    int pid=fork();
    if (pid==0){
        execlp("gnome-terminal", "gnome-terminal", "--", "vi", fileName, NULL) ;
    }
    insert(fileName, pid);
    return;
}

So for closing the file, I have the child process Id.

void closeProcess (int pid){
    printf("killing %d\n", pid);
    kill(pid, SIGKILL);
    return;
}

Now the challenge is this process id is not the same as the vi editor process id. Killing this process is not closing the vi editor.

Here are some example processes. The process id that I have in pid variable is 25803 but is now dead. To kill vi editor I need the process id 25815. list of processes

How killing vi process working. killing process

Here is my current program output, which doesn't close vi editor for that file. enter image description here

James Risner
  • 5,451
  • 11
  • 25
  • 47
Biplab Roy
  • 42
  • 7
  • 2
    Please [edit] your question and post the images as text. See: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/68587) – John Kugelman Sep 08 '22 at 04:01
  • 2
    Avoid using `SIGKILL` / `kill -9`. It doesn't give processes a chance to exit gracefully -- that could include killing their children. Use `SIGTERM` / plain `kill` instead. – John Kugelman Sep 08 '22 at 04:03
  • Seems to be a bug in gnome-terminal. vi should have gotten a SIGHUP when gnome-terminal went away; then further reads from STDIN would report EOF. – Joshua Sep 08 '22 at 04:09
  • Does this answer your question? [How to kill a child process by the parent process?](https://stackoverflow.com/questions/6501522/how-to-kill-a-child-process-by-the-parent-process) – abdo Salm Sep 08 '22 at 06:02
  • Instead of opening a `gnome-terminal` in which to run text-mode `vim`, consider directly opening `gvim`. – John Bollinger Sep 13 '22 at 22:09

1 Answers1

2

How can I kill the terminal window and vi?

Between the fork() and execlp() you will need to set up a process group id. Then when killing, use the group id.

int setpgid(0, 0);

When this is called in the child, the first 0 is shorthand for "me". The second 0 is shorthand for "use the pid". So this means "create a new process group and put me in that group."

When killing, use the PID you have been using. But it will also handle all children of the group.

How do you get the Process Group ID for a given process?

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
    int pid = 20989; // That PID has a PGID of 20988

    int pgid =  getpgid(pid);
    printf("pgid = %d\n", pgid);
}

Alternatively you can use this ps command:

ps xao pid,ppid,pgid,sid,command

Why should I avoid using SIGKILL?

The KILL signal instructs Linux to end the job immediately. Any open files are closed in place. There is no clean up of user land state by the application.

Alternatively, sending SIGTERM instructs the application to end. Permitting the application to close all files, save their state, and prepare to end.

James Risner
  • 5,451
  • 11
  • 25
  • 47