Linux C Shell,子进程抛出的分段错误

时间:2015-10-17 15:20:01

标签: c linux shell process segmentation-fault

我一直试图拿起C来做一个写作创建C shell的作业。一个要求是所有命令都应该从子进程执行。问题似乎是我的子进程过早死亡,我从未得到实际执行命令的代码部分。我的代码:

parseCommand.h

char *parseCommand(char str[]) {
    char * token;
    //get size of the input array. divide memory amount allocated to array by the size of the 1st element (which should be representative of other elements)
    size_t n = sizeof(str) / sizeof(str[0]); 
    char *args = malloc(n);
    printf("Splitting string \"%s\" into tokens:\n", str);
    token = strtok(str, " \n");
    int i = 0;
    while (token != NULL) {
        printf(":: %s\n", token);
        args[i++] = token;
        token = strtok(NULL, " \n");
    }
    printf("after while loop");
    args[i]=(char *) 0;
    return args;

}

的main.c

//I probably don't need all these
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>

//Custom Libraries
#include "parseCommand.h"

char *parseCommand(char str[]);

int main() {

    char path[10] = "/bin/";//path to bash scripts
    int should_run = 1;
    while (should_run) {
        printf("yazan_shell>> ");
        fflush(stdout); //force the prompt to the output immediately
        char *cmdStr = (char *)malloc(40); //allocate space for array
        fgets(&cmdStr, 40, stdin); //save user input to cmdStr
        pid_t pid = fork(); //create 
        if (pid == 0) {
            printf("==> Child received: %s command. Executing...\n", &cmdStr);
            char *cmd = parseCommand(&cmdStr);//split user input by space
            printf("cmd: %s", &cmd);
            execvp(strcat(path, cmd[0]), cmd);//excecute the input cmd
        } else {
            int returnStatus;
            waitpid(pid, &returnStatus, 0); //parent waits for child process
            printf("==> Parent is silent!! PID: %d\n", pid);
            should_run = 0;
        }
        free(cmdStr); //deallocate cmdStr
    }
}

输出1

yazan_shell>> ls -l
==> Child received: ls -l
 command. Executing...
Splitting string "ls -l
" into tokens:
:: ls
:: -l
==> Parent is silent!! PID: 5500

RUN FINISHED; Segmentation fault; core dumped; real time: 3s; user: 0ms; system: 0ms

我几天前刚刚开始学习C语言,但我在C中搜索了分段错误,似乎我要么取消引用非初始化指针,要么尝试访问释放的内存。所以我试着评论

free(cmdStr);

行,然后输出如下:

yazan_shell>> ls -l
==> Child received: ls -l
 command. Executing...
Splitting string "ls -l
" into tokens:
:: ls
:: -l
==> Parent is silent!! PID: 5601

RUN FINISHED; exit value 33; real time: 1s; user: 0ms; system: 0ms

我还尝试在 parseCommand.h 中的while循环中移动print语句,但输出似乎也没有改变。我问过几位可用的C ++教授,但他们都没有能够查明错误。这里有人能给我一些关于我错误的指示(呵呵)吗?

非常感谢你!

1 个答案:

答案 0 :(得分:1)

有几个问题 -

1。main -

char *cmdStr = (char *)malloc(40);  // don't cast it
fgets(&cmdStr, 40, stdin);        // don't pass address of cmdStr it is already a char *

这很好 -

char *cmdStr =malloc(40); 
fgets(cmdStr, 40, stdin); 

2。也在这里 -

char *cmd = parseCommand(&cmdStr);//split user input by space
printf("cmd: %s", &cmd);      //cmd is already a char * don't pass its address

这样写 -

printf("cmd: %s", cmd); 

3。在计算元素数量时,在函数char *parseCommand(char str[])中 -

size_t n = sizeof(str) / sizeof(str[0]); 

这不会按预期工作。因此,在n中计算main,然后将其传递给您的函数