Northern Illinois University



CSCI 480 Autumn, 2019

Assignment 3 -- Microshell Simulation

(100 points)

In this assignment, you will implement a microshell in C/C++.

You will need to use several system calls under Linux including

fork(), pipe() and dup(). This assignment's logic is more complex

than in the previous assignments. Please allocate sufficient time for this assignment (suggestion: multiple days).

The name of the executable file should be "Assign3".

Your shell does the following:

--- Print a prompt "480shell>" and wait for input.

--- Read the input typed after the prompt.

--- Execute the command typed in after the prompt and print a new

prompt.

--- The shell understands the commands "quit" and "q" as the special

commands to exit.

--- The shell understands a special symbol "||", by which you can

pipe the output of one command to next command. To simplify,

this assignment only requires one pipe between two commands, such

as in:

cat ourfile || sort

Please note that the standard Unix/Linux pipe is "|", which is

different from what our microshell would understand.

---------------------------------------------------------------------

Input File

A text file is needed to produce the sample output. The file name is

"ourfile". Create a file containing 6 lines:

Computer Science Department

NIU

DeKalb

Illinois

USA

North America

Sample Output

turing%>Assign3

480shell>cat ourfile ||sort

Computer Science Department

DeKalb

Illinois

NIU

North America

USA

480shell>quit

turing%>Assign3

480shell>more ourfile || grep DeKalb

DeKalb

480shell>ls

ourfile 480sh

480shell>q

turing%>

---------------------------------------------------------------------

Background Knowledge

1. The assignment will need several system calls for process

management and IPC such as fork(), exec(), pipe(), dup(). You

need to read the Linux manual page to understand their usage.

You will use fork() to create two child processes and use pipe()

with the help of dup() or dup2() to set up the communication

between the child processes. The system call dup() is used to

duplicate the file descriptors so that you can replace the

standard input or output of a process by the file descriptors of a

pipe.

2. You need to close all the unneeded file descriptors of the pipes.

You need to close the two pipe file descriptors in the parent

process, the read end of the pipe for the first child process,

and the write end of the pipe for the second child process.

After dup() is called in each child process, you can close the

write end for the first process and the read end for the second

process since they are no longer needed.

3. You may need strtok() to parse the command line for you. Read the

manual page to understand this function. You can use other ways

to parse the line too.

4. When you use execv() or execvp(), you need to build an array of

pointers to the arguments. The last element of the array should

be (char *) NULL.

5. The parent process needs to call waitpid() to wait for the

completion of the commands.

Tackle the problem step by step:

--- Make sure that your shell is taking inputs correctly.

--- Next test the execution of commands without any pipe involved.

--- Now you can go ahead and solve the pipe problem.

--------------------------------------------------------------------

Programming Hints

How do I find the library for a system call?

Check the manual page. For example: Do "man strtok". In the

synopsis, you will see:

#include

If you are interested in the exact contents of the header file, you

can go to /usr/include and do "more strings.h".

How do I structure the program?

You can start with the following example and add logic to handle pipe(), etc. And of course, you will need to define several functions due to the not-so-trivial logic.

#include

#include

#include

int main(void)

{

char buf[1024];

pid_t pid;

int status;

printf("%% ");

while (fgets(buf,1024,stdin) != NULL)

{

buf[strlen(buf) -1] = 0; //remove the last character. Important!

if ((pid = fork()) ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download