c中的简单shell:waitpid系统调用无法正常工作

我必须创建简单的shell来读取命令并按顺序执行它们。 condition不会改变main函数的forms,而execute函数应该是递归的。 主要问题是,似乎waitpid不起作用。 但我知道,我的代码中存在很多问题。 请让我知道我应该从哪里开始..

#include  #include  #include  #include  #include  #include  #define MAX 10 char cmmd[MAX][256]; int sp; char *argv[10]; int size; void ClearLineFromReadBuffer(void){ while(getchar() != '\n'); } void printCommands(){ size = sp+1; //print by moving stack pointer while(1){ if (sp==-1) break; printf("Command line : %s\n", cmmd[sp]); sp--; } printf("print end\n"); } void readCommandLines(){ int a = 0; //return of scanf while (1){ //write commends to cmmd untill get ctrl+d printf(">"); a = (scanf("%[^\n]s", cmmd[sp])); //take input to str untill get enter(scanf returns -1) if (a==-1) {sp--; break;} if (a==1) ClearLineFromReadBuffer(); if (a==0) {printf("error"); break;} sp++; } printf("\n"); } void readACommand(char *line){ //line takes string's name. int i=0; argv[i]=strtok(line," "); //i==0 while(strtok(line," ")!=NULL){ i++; argv[i]=strtok(NULL," "); } printf("%s",argv[0]); printf("%s",argv[1]); } void executeCommands(){ //Recursive function int n = sp; n++; printf("%d",n); printf("%s",cmmd[n]); char *cmd_line = cmmd[n]; //command line which child process will execute unsigned int child_pid; //fork() returns process id of child in parents process int status; //status takes return of child's exit() child_pid=fork(); if (child_pid != 0){ // Parents process printf("parents access"); waitpid(child_pid,&status,0); printf("***Process %d Child process %d DONE with status %x\n\n",getpid(),child_pid,status); sp++; if(sp<size) executeCommands(); } else if (child_pid == 0){ //fork() returns 0 in child process printf("***Process %d Executing Command %s",getpid(),cmd_line); readACommand(cmmd[n]); execve(argv[0],argv,NULL); printf("ERROR - not executing command \"%s\"\n",argv[0]); //can be printed because exec() failed } } int main(){ readCommandLines(); printCommands(); executeCommands(); return(0); } 

这是结果。 在此处输入图像描述

你对字符串进行标记的方式是非常错误的。 有很多strtok调用,你的循环可以是一个无限循环因为你在循环中使用初始化字符串调用strtok ,而不是NULL

另外,在最后一个参数之后你没有设置为NULLexecv需要知道参数何时用完(没有传递大小)

这是一个独立的示例和一个正确的readACommand例程:

 #include  #include  char *argv[100]; void readACommand(char *line){ //line takes string's name. int i=0; argv[i]=strtok(line," "); //i==0 while(argv[i]!=NULL){ argv[++i]=strtok(NULL," "); } } int main() { char line[] = "this is a command"; char **ptr=argv; readACommand(line); while(*ptr != NULL) { printf("Arg: %s\n",*ptr); ptr++; } return 0; } 

执行(最后检测NULL指针):

 Arg: this Arg: is Arg: a Arg: command