Paste: 123
Author: | zzhh |
Mode: | factor |
Date: | Sun, 21 Nov 2021 06:07:42 |
Plain Text |
/**
* Simple shell interface program.
*
* Operating System Concepts - Ninth Edition
* Copyright John Wiley & Sons - 2013
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#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()
*/
}
}
New Annotation