Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Develop C language multi-process program. You are expected to extend the myshell.c program and add pipelines and I/O redirections. In particular, your shell program should

Develop C language multi-process program. You are expected to extend the myshell.c program and add pipelines and I/O redirections. In particular, your shell program should recognize the following:

a.) > - Redirect standard output from a command to a file.

Note: if the file already exist, it will be erased and overwritten without warning.

For example, COP4338$ ls > 1 COP4338$ sort myshell.c >

b) Note that you're not supposed to implement the unix commands (ls, sort, ...).

You do need to implement the shell that invoke these commands and you need to "wire" up the standard input and output so that they "chain" up as expected.

c) . >> - Append standard output from a command to a file if the file exists; if the file does not exist, create one.

For example, COP4338$ sort myshell.c >> 1 COP4338$ echo "Add A Line" >>

d) . < - Redirect the standard input to be from a file, rather than the keyboard.

For example, COP4338$ sort < myshell.c COP4338$ sort < myshell.c > 1 COP4338$ sort > 1 < myshell.c

The second and third commands are the same: the sort program reads the file named myshell.c as standard input, sorts it, and then writes to the standard output to file named 1. 4. | -

Pass the standard output of one command to another for further processing.

For example, COP4338$ ls | sort COP4338$ sort < myshell.c | grep main | cat > output

Make sure you have your parent process to fork the children and 'wire' them up using pipes accordingly Your program should be able to run with all the unix commands as expected. Don't assume your program will only be tested by the above commands. Also, there could be more than two commands chained by pipes, like the second example above. Your program needs to provide necessary sanity-check from the user input. Prompt meaningful errors accordingly as what you'd experience with your shell.

Hint: use strtok() functions to separate the tokens and use strstr() to locate sub-strings

/* This example is stolen from Dr. Raju Rangaswami's original 4338 demo and modified to fit into our lecture. */

#include #include #include #include #include

#define MAX_ARGS 20 #define BUFSIZ 1024

int get_args(char* cmdline, char* args[]) { int i = 0;

/* if no args */ if((args[0] = strtok(cmdline, " \t ")) == NULL) return 0;

while((args[++i] = strtok(NULL, " \t ")) != NULL) { if(i >= MAX_ARGS) { printf("Too many arguments! "); exit(1); } } /* the last one is always NULL */ return i; }

void execute(char* cmdline) { int pid, async; char* args[MAX_ARGS];

int nargs = get_args(cmdline, args); if(nargs <= 0) return;

if(!strcmp(args[0], "quit") || !strcmp(args[0], "exit")) { exit(0); }

/* check if async call */ if(!strcmp(args[nargs-1], "&")) { async = 1; args[--nargs] = 0; } else async = 0;

pid = fork(); if(pid == 0) { /* child process */ execvp(args[0], args); /* return only when exec fails */ perror("exec failed"); exit(-1); } else if(pid > 0) { /* parent process */ if(!async) waitpid(pid, NULL, 0); else printf("this is an async call "); } else { /* error occurred */ perror("fork failed"); exit(1); } }

int main (int argc, char* argv []) { char cmdline[BUFSIZ]; for(;;) { printf("COP4338$ "); if(fgets(cmdline, BUFSIZ, stdin) == NULL) { perror("fgets failed"); exit(1); } execute(cmdline) ; } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Databases On The Web Learn To Create Web Pages That Interface With Database Engines

Authors: Robert Papaj, Donald Burleson

11th Edition

1576100995, 978-1576100998

More Books

Students also viewed these Databases questions