c - 如何设置进程C的状态终止?

标签 c process signals kill-process

我的程序是一个基本的小外壳。 它允许您在 PATH 中运行程序,如 ls、cd.. 以及带参数。 要从终端“./myshell2”运行程序类型,然后它会启动,您可以插入您想要的命令数。

它启动一个子进程,运行 execvp,它返回并重新启动,以便您可以键入新命令。 当键入“Q”或“q”时,整个程序都应该终止。 问题是我不知道如何阻止它,代码如下。 我的想法是,当键入“Q”或“q”时,杀死创建的子进程并发送一个信号来通知它的错误终止(子进程)。 所以最终状态(来自父级)'将不是 1 并且函数返回。 我对代码的某些部分进行了注释,希望它更容易理解。 它的工作原理是要停止它我需要 ctrl C。 我想对子进程说,他必须以非零值结束。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>

int main(int argc, char * argv[]) {
while(1)
{
    pid_t pid = fork();

    if (pid == -1) {
        perror("fork error");
        exit(EXIT_FAILURE);
    }

    if (pid == 0) { // child process
        printf("type the command to start (and arguments if required) \n"
            "Q to quit\n");
        char *dest[10]; // allow you to insert
        char line[4096];//commands from terminal
        if (fgets(line,sizeof(line),stdin)==0) return 1;
        int i;
        line[strcspn(line, "\n")] = '\0';  
        char *st = line;
        for (i=0; i< 10 && (dest[i]=strsep(&st," "))!=NULL;i++)
            continue;//now you typed the command
        if ( ( memcmp(dest[0],"Q",1)==0 ) // if Q or q the program
             || (memcmp(dest[0],"q",1)==0) ) //must end
        {
            printf("got it!\n");
            if (kill(getpid(),SIGSEGV)==-1) printf("kill error\n");
            //in theory the process should terminates with bad status
            // and the value of the variable "status" 'll be not 0
           // I think that the problem is in this part of the code
        }


        if( strcmp(dest[0]," ")!=0 )
        {
            int res = execvp(dest[0], dest);    
         }
        else
            { int res= execvp(dest[1],dest+1);}

       perror("execvp error");
       exit(EXIT_FAILURE);
    }

    int status;
    pid_t child = wait(&status);

    if (child == -1) {
        perror("wait error");
     exit(EXIT_FAILURE);
    }
    if (status==1)
        break; //so it can exit from the loop that creates new process
    setenv("WAIT","TRUE",0);                    //dont' worry about
    //perror("setenv error\n");
    if (memcmp("TRUE",getenv("WAIT"),4) == 0 )  //these 6 lines
        printf("WAIT=TRUE\n");
    else if(memcmp("FALSE",getenv("WAIT"),4) == 0 )
        printf("WAIT=FALSE\n");                     
printf("end current process (status=%d, child=%d)\n", WEXITSTATUS(status), son);
 }
return EXIT_SUCCESS;
}

最佳答案

您正在为所有情况打印出 WEXITSTATUS(),但这是不对的。您需要使用 WIFEXITED() 检查 wait 返回的状态是否为退出状态。如果它非零,那么 child 正常退出。否则,您可以使用 WIFSIGNALED() 查看子进程是否已终止,您将从 WTERMSIG()

获得信号
if(WIFEXITED(status))
  {
  printf("end current process (status=%d, child=%d)\n", WEXITSTATUS(status), son);
  }
else if(WIFSIGNALED(status))
  {
  printf("end current process (signal=%d, child=%d)\n", WTERMSIG(status), son);
  }

您确实应该让父进程处理命令的输入,而让子进程运行它。

关于c - 如何设置进程C的状态终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50953279/

相关文章:

c - c 多线程编程中的 pthread_create 参数

c - GNU gdb 如何显示源文件名和符号行

Java Runtime.exec() 不支持 Linux 别名

java - URLClassLoader + loadClass + 在独立进程上调用 main 方法? java

c - "expected ' 字符 * ' but argument is of type ' 字符 ' "- 回文 + 反向

c - 一个数组位置的多个 OR 比较

bash - 确保一次只运行一个 shell 脚本实例的快捷方式

c - accept() 中断信号和 epoll_wait()

c++ - Qt:信号/槽设计和性能

为单个线程创建信号处理程序