简体   繁体   English

为什么在我的 C 程序中使用 Exec 函数杀死进程不起作用?

[英]Why is the kill process not working using the Exec function in my C program?

I am creating my own shell (like bash) and I have a function that calls the execv function and passes in a specific process pid to kill.我正在创建我自己的 shell(如 bash),我有一个函数调用 execv 函数并传入一个特定的进程 pid 来杀死。 I basically run a sleep command for 10 seconds, and halfway through I want to kill it.我基本上运行了 10 秒的睡眠命令,中途我想杀死它。 But my kill command does not seem to be working.但是我的 kill 命令似乎不起作用。 Does anyone have any ideas how I can fix it?有没有人有任何想法我该如何解决?

Below are functions related to execv下面是与 execv 相关的函数

void run_execv(char *path, char *args[])
{
    int result = execv(path, args);
}

void kill_process(char *target_pid)
{
    char *bin_path = "/bin/kill";
    char *args[] = {bin_path, "-15", target_pid, NULL};
    run_execv(bin_path, args);
}

void ps()
{
    char *bin_path = "/bin/ps";
    char *args[] = {bin_path, NULL};
    run_execv(bin_path, args);
}

And the calling of the kill_process function is shown below. kill_process 函数的调用如下所示。 Basically I am calling the kill command as a child process.基本上我将 kill 命令作为子进程调用。

else if (strncmp(shellInput, "pkill", strlen("pkill")) == 0 || strncmp(shellInput, "kill", strlen("kill")) == 0)
        {
            char *target_pid = strtok(NULL, " \n");
            int childStatus;
            pid_t spawnPid = fork();
            switch (spawnPid)
            {
            case -1:
                perror("fork()\n");
                exit(1);
                break;
            case 0:
                // This is the child process where we will call the ls function
                kill_process(target_pid);
                perror("execv");
                exit(2);
                break;
            default:
                // This is the parent process that takes control back after child process finishes.
                spawnPid = waitpid(spawnPid, &childStatus, 0);
                printf("CHILD STATUS: %d\n", childStatus);
                processStatus = childStatus;
                break;
            }

Even after killing that sleep process, it still shows up when I check my current processes running with ps.即使在杀死该睡眠进程之后,当我检查当前使用 ps 运行的进程时,它仍然会出现。 Please see my screenshot below for the same execution.请参阅下面的屏幕截图以了解相同的执行情况。

~/Desktop/OSU/CS-344 (Operating Systems)/assignment3(main*) » ./a.out                                                               sampai@sams-mbp-2
: ps
  PID TTY           TIME CMD
25223 ttys003    0:06.24 /bin/zsh -l
41134 ttys003    0:00.00 ./a.out
32018 ttys004    0:01.49 -zsh
30707 ttys005    0:00.14 /bin/zsh --login -i
: sleep 30 &
background pid is 41143
: ps
  PID TTY           TIME CMD
25223 ttys003    0:06.24 /bin/zsh -l
41134 ttys003    0:00.00 ./a.out
41143 ttys003    0:00.00 sleep 30
32018 ttys004    0:01.49 -zsh
30707 ttys005    0:00.14 /bin/zsh --login -i
: kill 41143
CHILD STATUS: 0
: ps
  PID TTY           TIME CMD
25223 ttys003    0:06.24 /bin/zsh -l
41134 ttys003    0:00.00 ./a.out
41143 ttys003    0:00.00 (sleep)
32018 ttys004    0:01.49 -zsh
30707 ttys005    0:00.14 /bin/zsh --login -i
: 

尝试kill -9 <PID>它必须工作

Figured it out!弄清楚了! Basically like kaylum said in the comments, I needed to add a waitpid call outside of my switch statement.基本上就像 kaylum 在评论中所说的那样,我需要在我的 switch 语句之外添加一个 waitpid 调用。 And I needed to wait on the correct pid, which I needed to get inside of the child process.我需要等待正确的 pid,我需要它进入子进程。 Please see my updated code below.请在下面查看我更新的代码。

int process_pid;
            pid_t spawnPid = fork();
            switch (spawnPid)
            {
            case -1:
                perror("fork()\n");
                exit(1);
                break;
            case 0:
                // This is the child process where we will call the ls function
                process_pid = getpid();
                kill_process(target_pid);
                perror("execv");
                exit(2);
                break;
            default:
                // This is the parent process that takes control back after child process finishes.
                spawnPid = waitpid(process_pid, &childStatus, 0);
                processStatus = childStatus;
                break;
            }
            spawnPid = waitpid(process_pid, &childStatus, 0); // Need this to kill zombie process.
            printf("CHILD STATUS: %d\n", childStatus);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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