简体   繁体   中英

How do I kill a process from c with fork() process id?

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: 打开文件

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

关闭文件

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 the processes. 25803 is the process id that I have in pid variable which is a dead process. To kill vi editor I need 25815 process id. 进程列表

How killing vi process working. 杀进程

Here is my current program output, which doesn't close vi editor for that file. 在此处输入图像描述

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 will show 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM