void execCom(int in,int out,char** args){ pid_t child_id; if(strcmp(args[0],"pwd")==0){ myPwd(out,args[0]); }else if(strcmp(args[0],"cd")==0){ myCd(args[0]); }else{ child_id=fork(); if(child_id==0){ int error=child(in,out,args); if(error<0){ printf("execCom:%s failed!\n",args[0]); } } } }
void commandExecute() { struct timeval tv_beg; struct timeval tv_end; struct timezone tz_beg; struct timezone tz_end; int temp = -1; int process_pid = 0,process_pid2=0; int status = 0; int walk_clock_time = 0; int i = 0; //printf("%d\n",backFlag); while(1) { //获取进程运行前系统时间 gettimeofday(&tv_beg, &tz_beg); //获取命令 getCommand(); //判断是第几个命令 for(i=0; i<COMMAND_NUM; i++) { if(strcmp(shellCommand, commandKind[i]) == 0) { temp = i; break; } } if(backFlag) { init_daemon(); } switch(temp) { case 0: //printf("%d\n",temp); myCd(); break; case 1: //printf("%d\n",temp); getcwd(shell_args, PATH_LEN);//get the current directory myLs(shell_args); break; case 2: //printf("%d\n",temp); printf("等待后台进程.... \n"); waitpid(-1, &status, 0); printf("所有后台进程运行结束! \n"); return; case -1: //printf("%d\n",temp); process_pid=fork(); if(process_pid==0) { printf("other command\n"); externalCmd(); showStatic(); exit(0); } else waitpid(process_pid, &status, 0); break; default: continue; } temp=-1; //获取进程运行后系统时间 gettimeofday(&tv_end, &tz_end); walk_clock_time = (tv_end.tv_sec - tv_beg.tv_sec)*1000 + (tv_end.tv_usec - tv_beg.tv_usec)/1000; printf("\n"); printf("进程运行时间 (ms): %d \n", walk_clock_time); showStatic(); if(backFlag) { } } }