在c

时间:2015-05-20 18:45:30

标签: c shell segmentation-fault signals

所以,我在C中编写一个基本的shell,需要有信号处理程序。

终止后台进程的检测应该通过两种机制来实现,其中一种机制选择应该在编译时编译哪些机制。这些机制是通过子进程发送的信号进行普通轮询和检测。用户应该能够通过在编译时定义宏SIGDET=1来选择使用哪种机制,以便通过信号检测到终止。如果SIGDET未定义或等于零,则应通过轮询检测终止。

我需要使用gcc -pedantic -Wall -ansi -O4进行编译 当我使用cc -pedantic -Wall -ansi -O4 main.c -o shell -DSIGDET=1 && ./shell调用我的程序时,然后运行一个简短的后台进程(例如,sleep 2 &),它会给我一个段错误。我不知道为什么,有人可以帮助我吗?

执行是一个函数,它接受命令fork(),然后在子使用中execvp

编辑:有几个问题:

parser()返回放入的命令数量,例如sleep 3 &返回3. r[size-1] = NULL删除最后一个命令,但前提是&。那是因为我不想发送它来执行,我只需要知道它应该在后台运行。

编辑:打印:

在使用background输入命令后立即

&。 然后它打印出OUTSIDE LOOP消息,然后打印段错误然后退出。

编辑:使用-g标志并运行gdb

我运行调试,我得到以下内容:

>: sleep 2 &
background
>: OUTSIDE LOOP: 11661 with this status: 0
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7aa0ad9 in strtok () from /lib/x86_64-linux-gnu/libc.so.6

我没有对strtok()做任何奇怪的事情,它只在解析器中使用,它如下:

int parser(char * input, char ** r)
{
    int i = 0;
    char *tokens = strtok(input, DELIM);
    /* char r[40]; */

    while(tokens) 
    {
        r[i] = tokens;
        tokens = strtok(NULL, DELIM);
        i++;
    }
    r[i] = NULL;
    return i;
}

Heres我的代码(不包括解析和执行函数等):

#define _POSIX_SOURCE
#include <stdio.h>
#include <string.h>
#include <unistd.h> /* for getcwd */
#include <stdlib.h> /* for malloc */
#include <signal.h>
#include "functions.h"
#include "textfunc.h"
#include "textfunc.c"
#include "functions.c"

#define MAX_LENGTH 80
#define DELIM " \n\r\t"

typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);

void sig(int signal)
{
  int status;
  pid_t pid = waitpid(-1, &status, WNOHANG);
  printf(" OUTSIDE LOOP: %d with this status: %d\n", pid, status);
}

int main()
{
  char *input;
  char * r[40];
  int size;

  signal(SIGINT, SIG_IGN); /* */

  #if SIGDET==1
  signal(SIGCHLD, sig); 
  #endif

  while(1)
  {
    int bg = -1;

    #if SIGDET!=1
    int status;
    pid_t pid = waitpid(-1, &status, WNOHANG);
    if(pid > 0)
      printf("INSIDE LOOP: %d with this status: %d\n", pid, status);
    #endif

    prompt();

    input = readInput();
    size = parser(input, r);

    if(r[0] == NULL)
        continue;
    else if(strcmp("&", r[size-1]) == 0)
    {
      printf("background\n");
      r[size-1] = NULL;
      bg = 0;
    }
    if(strcmp("exit", r[0]) == 0)
        {
        kill(0, SIGKILL);
      }
    else if(strcmp("cd", r[0]) == 0)
      changedir(r[1]);
    else if(strcmp("checkEnv", r[0]) == 0)
      checkenv(r);
    else
    {
        execute(&input, r, bg);
    }
  }
  return 0;
}

1 个答案:

答案 0 :(得分:0)

您可能需要阅读'signal()'的手册页,其部分内容如下:

  

signal()的行为在UNIX版本中有所不同,并且也是如此   VAR-          历史上跨越不同版本的Linux。避免使用:          改为使用sigaction(2)。

请注意关于使用sigaction()而不是signal()

的部分