/** * Simple shell interface program. * * Operating System Concepts - Ninth Edition * Copyright John Wiley & Sons - 2013 */ #include #include #include #define MAX_LINE 80 /* 80 chars per line, per command */ int main(void) { char *args[MAX_LINE/2 + 1]; /* command line (of 80) has max of 40 arguments */ int should_run = 1; while (should_run){ printf("osh>"); fflush(stdout); char line[MAX_LINE]; gets(line); char *p = NULL; int num = 0; for(p = strtok(line, " "); p != NULL; p = strtok(NULL, " ")) { args[num] = p; num++; } args[num] = NULL; pid_t pid = fork(); if(pid == 0){ execvp(args[0], args); } else{ } /** * After reading user input, the steps are: * (1) fork a child process * (2) the child process will invoke execvp() * (3) if command included &, parent will invoke wait() */ } }