Fork()工作不正常,返回3次

时间:2014-11-12 19:15:09

标签: c linux bash runtime-error fork

我正在用C语言设计一个多线程,多进程的应用程序。该程序正在Ubuntu 10.04.4 LTS中使用Linux内核2.6.31和glibc版本2.11.1进行测试。

我正在尝试fork,以便我可以将bash作为子进程启动以创建虚拟shell。问题是,有时fork()返回3次表现得非常奇怪。据我所知,fork()应该返回两次:一次用于父进程,一次用于子进程。不幸的是,在我的情况下,fork()返回3次:一次用于父进程,两次用于子进程。在htop中,结果看起来像http://i59.tinypic.com/20fdjci.png

htop output

在上图中,PID 4320位于正确的位置,但PID 4316已作为孤立进程启动,并且正在消耗所有处理器时间。

以下是产生该结果的代码:

#include "v_shell.h"

/*executes bash and attaches stdin and stdout to pipes*/
int vshell_init() {
    int rc;
    FILE * rf;

    /*initialize pipes*/
    //    mknod(STDIN_PIPE, S_IFIFO | 0666, 0);
    //    mknod(STDOUT_PIPE, S_IFIFO | 0666, 0);
    mkfifo(STDIN_PIPE, 0666);
    mkfifo(STDOUT_PIPE, 0666);

    pid_t result = fork();

    if (result == 0) { //this is the child
        prctl(PR_SET_NAME, "SHELL_INPUT", 0, 0, 0); //set name of the child process

        /*set thread priority*/
        int ret;
        struct sched_param params;
        params.sched_priority = sched_get_priority_max(SCHED_RR) - 10;
        ret = pthread_setschedparam(pthread_self(), SCHED_RR, &params);
        if (ret != 0) {
            // Print the error
            record("Unsuccessful in setting thread realtime prio\n");
            return 0;
        }

        /*redirect standard input and output*/
        record("Redirecting stdin and stdout\n");

        rf = fopen(STDIN_PIPE, "r");
        if (rf == NULL) record("Error opening named pipe\n");
        rf = fopen(STDOUT_PIPE, "w");
        if (rf == NULL) record("Error opening named pipe\n");

        /*Close stdin and stdout to make sure*/
        rc = fclose(stdout);
        if (rc == EOF) record("Failed to close stdout\n");
        fclose(stdin);
        if (rc == EOF) record("Failed to close stdin\n");

        /*Copy stdin and stdout to named pipes*/
        rf = freopen(STDIN_PIPE, "r", stdin); //Redirect standard input
        if (rf == NULL) record("Failed to redirect stdin\n");
        rf = freopen(STDOUT_PIPE, "w", stdout); //Redirect standard output for new process
        if (rf == NULL) record("Failed to redirect stdout\n");
        rf = freopen(STDOUT_PIPE, "w", stderr); //Redirect standard error for new process
        if (rf == NULL) record("Failed to redirect stderr\n");

        record("Starting shell...\n");
        //Start shelld --  this one uses bash. the ./bashrc file should be used
        //        if (execlp("bash", "bash", "--noprofile", "--rcfile", "bashrc", "-i", (char *) 0) == -1) {
        if (execlp("bash", "bash", "--rcfile", "bashrc", "-i", "-s", (char *) 0) == -1) {
            record("ERROR in starting virtual shell!\n");
        }
        return -1; //shouldn't return if it worked correctly
    }

    return result;
}

有谁知道什么可以使fork()产生这个结果?

1 个答案:

答案 0 :(得分:0)

它从png看起来好像一个进程已经将自己重新定位到init(通常意味着它的父级已经死亡),但它不是僵尸。

两种可能的解释:

  1. 您不止一次致电vshell_init。我打赌你会抓住那个。

  2. (更有可能)bash shell 本身分叉。 Bash分叉比您想象的要多(例如,()中包含事物的脚本会分叉)。

  3. 调试

    strace -f -s2048 -o trace.out yourprogramnamehere
    

    然后搜索execlp,看看发生了什么。

    如果这是间歇性的,你可能想知道4316在做什么,例如:

    strace -f -s2048 -o trace.out -p 4316
    

    fork()时,请确保关闭所有未通过的FD(这是一个只关闭一个管道的示例)

    {
      int i;
      char *devnull = "/dev/null";
      ....
      for (i = getdtablesize () - 1; i >= 0; i--)
        {
          if (!pipedata || (i != pipefd[0]))
            close (i);
        }
    
      i = open (devnull, O_RDWR);
      if (i == -1)
        {
          fprintf (stderr, "Unable to open /dev/null\n");
          _exit (1);
        }
    
      if (pipedata)
        {
          dup2 (pipefd[0], 0);
        }
      else
        {
          i = open (devnull, O_RDONLY);
          if (i != 0)
            {
              dup2 (i, 0);
              close (i);
            }
        }
    
      i = open (devnull, O_WRONLY);
      if (i != 1)
        {
          dup2 (i, 1);
          close (i);
        }
    
      i = open (devnull, O_WRONLY);
      if (i != 2)
        {
          dup2 (i, 2);
          close (i);
        }
      ...
    }
    

    bash理解的方式设置信号掩码:

    {
      ...
      sigset_t set;
      struct sigaction sa;
      ...
      /* Set up the structure to specify the new action. */
      memset (&sa, 0, sizeof (struct sigaction));
      sa.sa_handler = SIG_DFL;
      sigemptyset (&sa.sa_mask);
      sa.sa_flags = 0;
      sigaction (SIGINT, &sa, NULL);
      sigaction (SIGTERM, &sa, NULL);
      sigaction (SIGPIPE, &sa, NULL);
      sigaction (SIGCHLD, &sa, NULL);
      sigaction (SIGHUP, &sa, NULL);
      sigaction (SIGUSR1, &sa, NULL);
      sigaction (SIGUSR2, &sa, NULL);
    
      /* unblock all signals */
      sigfillset (&set);
      pthread_sigmask (SIG_UNBLOCK, &set, NULL);
      ...
    }