【问题标题】:Function that returns exit type as string以字符串形式返回退出类型的函数
【发布时间】:2014-03-20 21:52:02
【问题描述】:

我希望我的程序输出进程如何退出,如用户提示和输入后的第二行所示。

shell> wait
shell: process has exited abnormally with signal 11: Segmentation fault.

我将在我的 printf() 中使用什么函数?我想到了 exit() ,但它返回 void。 可能是 strsignal,如果是这样,我会将什么作为 strsignal 的 int 传递?谢谢!

【问题讨论】:

  • 做系统('暂停');在你的代码末尾?
  • 普通读者是否应该假设您的程序是一个正在启动其他程序的shell?因为没有这种清晰度,这个问题就像一只鸡全力以赴。

标签: c unix exit


【解决方案1】:

你是对的,它是 strsignal()。看这个例子:https://www.cs.fsu.edu/~baker/opsys/examples/forkexec/print_child_status.c

相关部分:

#include <stdio.h>
#include <wait.h>
#include <string.h>

void print_child_status (int status) {
  if (WIFEXITED (status)) {
    fprintf (stdout, "Child exited with status %d\n", WEXITSTATUS (status));
  } else if (WIFSTOPPED (status)) {
    fprintf (stdout, "Child stopped by signal %d (%s)\n", WSTOPSIG (status), strsignal (WSTOPSIG (status)));
  } else if (WIFSIGNALED (status)) {
    fprintf (stdout, "Child killed by signal %d (%s)\n", WTERMSIG (status), strsignal (WTERMSIG (status)));
  } else {
    fprintf (stdout, "Unknown child status\n");
  }
}

【讨论】:

    【解决方案2】:

    您可以参考以下链接http://linux.die.net/man/2/waitpid末尾的代码

    程序演示:

    $ ./a.out &
    Child PID is 32360
    [1] 32359
    $ kill -STOP 32360
    stopped by signal 19
    $ kill -CONT 32360
    continued
    $ kill -TERM 32360
    killed by signal 15
    [1]+  Done                    ./a.out
    $
    

    节目来源:

    #include <sys/wait.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <stdio.h>
    
    int
    main(int argc, char *argv[])
    {
        pid_t cpid, w;
        int status;
    
       cpid = fork();
        if (cpid == -1) {
            perror("fork");
            exit(EXIT_FAILURE);
        }
    
       if (cpid == 0) {            /* Code executed by child */
            printf("Child PID is %ld\n", (long) getpid());
            if (argc == 1)
                pause();                    /* Wait for signals */
            _exit(atoi(argv[1]));
    
       } else {                    /* Code executed by parent */
            do {
                w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
                if (w == -1) {
                    perror("waitpid");
                    exit(EXIT_FAILURE);
                }
    
               if (WIFEXITED(status)) {
                    printf("exited, status=%d\n", WEXITSTATUS(status));
                } else if (WIFSIGNALED(status)) {
                    printf("killed by signal %d\n", WTERMSIG(status));
                } else if (WIFSTOPPED(status)) {
                    printf("stopped by signal %d\n", WSTOPSIG(status));
                } else if (WIFCONTINUED(status)) {
                    printf("continued\n");
                }
            } while (!WIFEXITED(status) && !WIFSIGNALED(status));
            exit(EXIT_SUCCESS);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-30
      • 1970-01-01
      • 2016-10-19
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多