Linux课程设计进程控制 下载本文

else wait(NULL); // 父进程 exit(0); }

4.3 mini_shell.c

#include #include #include #include void pwd(); main() { int id; char cmd[80];

printf(\your-sid****\\n\ //将your-sid改为你的学号 while(1) {

printf(\ //显示命令提示符 fgets(cmd, 80, stdin); //从键盘读入命令行

cmd[strlen(cmd)-1]=0; //将行尾字符置为字符串结束符0 if ( strcmp(cmd, \ exit(0); //内部命令quit if ( strcmp(cmd, \ pwd(); //内部命令pwd else { //外部命令 id=fork();

if (id<0) { printf(\ if ( id == 0 ) {

if (execlp(cmd, cmd, NULL)<0) printf(\ exit(2); }

wait(NULL); } } }

void pwd() {

char dir[50]; getcwd(dir,50); printf(\}

4.4 mini_shell.c选作一

#include #include #include #include void pwd(); main() { int id; char cmd[80]; char* cmd1; char* cmd2; char* cmd3; char y;

printf(\ //将your-sid改为你的学号 printf(\ getchar(); scanf(\

if(y='n')

printf(\ else if(y='y') printf(\ while(1) {

printf(\ //显示命令提示符 fgets(cmd, 80, stdin); // 从键盘读入命令行

cmd[strlen(cmd)-1]=0; //将行尾字符置为字符串结束符0 cmd1=strtok(cmd,\ cmd2=strtok(NULL,\ cmd3=strtok(NULL,\

if ( strcmp(cmd1, \ exit(0); //内部命令quit if ( strcmp(cmd1, \ pwd(); //内部命令pwd else { //外部命令 id=fork();

if (id<0) { printf(\ if ( id == 0 ) { if(cmd2!=NULL) { if(cmd3==NULL) {

if (execlp(cmd1, cmd1, cmd2,NULL)<0) printf(\ exit(2); } else {

if (execlp(cmd1, cmd1, cmd2,cmd3,NULL)<0)

printf(\ exit(2); } } else {

if (execlp(cmd1, cmd1,NULL)<0)

printf(\ exit(2); }

}

wait(NULL); } } }

void pwd() {

char dir[50]; getcwd(dir,50); printf(\}

4.5 mini_shell.c选作二

#include #include #include #include void pwd(); main()