Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Assignment 1

Similar presentations


Presentation on theme: "Programming Assignment 1"— Presentation transcript:

1 Programming Assignment 1

2 Agenda Shell Requirements Phase 1, 2, 3 API and help sites

3 Shell shell is a user interface for access to an operating system's services. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer's role and particular operation. Command-line Interface the user (or client) issues commands to the program in the form of successive lines of text (command lines) Shell is just another program provided by the operating system (usually). You can install additional shells, and make your own. eg. DOS, bash, ksh, csh, Powershell etc

4 What does a shell do? designed to interpret a sequence of lines of text which may be entered by a user By interpret, we mean parse the line, execute the commands, and return results to the user Also acts as a programming language, provides its own syntax and additional operators to control command execution eg. | (pipe), > (output redirector), < (input redirector), conditional and branching statements etc.

5 How they work? Shell is just another program that we can execute.
When you enter a command, the shell will executes the command and returns the result to user. It may or may not create a child process. Design decision, and it has its own advantages and disadvantages Why create child process? You may need to run multiple commands at a time safeguard against user program crashes

6 Assignment Oh Right!!! the Assignment
We are building our own shell (not fully functional) Interpret command and execute the command We will implement some features like support (>,< , >> , || , && , | , ; ) side note : Anatomy of a Command <executable name> <list of arguments> [operator] [more commands] eg. ls -l > dirlist.out

7 Before we begin We are building a shell for a unix-based machine, so we will be working in unix environment Grading will be done on CSE server The assignment needs to work on cse server (compile and execute) Setup (option 1) If you are working on windows machine, you need to ssh to cse server, to compile and execute your program there. (option 2) If you are working with a Linux based machine, you can work on your local machine. BUT! BUT!! BUT!!! make sure that it works on cse server as well. makefile, osh

8 Approach The project is easier when we divide it to smaller phases
Phase 1 : Interpret Input Command and Recognize Invalid Command Phase 2 : Execute Command Phase 3 : Interprocess Communication and Process Control

9 Phase 1 (a) Read the input line (b) tokenize input string to command, arguments and operators (c) [optional] store it in a format to make it easy to handle input (d) Identify malformed commands [(e) logical grouping of commands] Parse.cpp does step a, b, c

10 Phase 1: Execution plan Three ways of building your program.
(i) Use parse.cpp (ii) Use comm.h (iii) Build from scratch What do they do? parse.cpp – contains functionality to parse the input string into a linked list, contains some basic definitions. (Easy mode) comm.h – header file that contains some basic structures to get you started (Hard Mode) Build from scratch – Ignore all the helper files, define your own structures (Super Saiyan mode)

11 Identifying malformed commands
(i) NULL Command eg: osh> | ls this is wrong as we have a null command in between the pair of | (ii) Missing Files for redirectors eg: osh> ls > similar to case (i), the output redirector is expecting a file, but it is null in command (iii) Multiple Redirectors eg: osh> ls > file | cat in this case, we have two output directors that can be associated to ls. this is ambiguous and thus malformed

12 Phase 2 We concentrate on executing the input command (simple, single commands) understand how to create a new child process, and execute the command pid_t fork (void); int exec*(const char *path, char *const argv[], ...); pid_t wait(int *wstatus); pid_t waitpid(pid_t pid, int *wstatus, int options);

13 Phase 2: Execution plan eg: ls ; ps -l ; cat file ; int cid, status; foreach (command) { cid = fork(); if(cid == 0) { // child process execlp(command->file, command->arguments); } else { // paren wait for exit wait(&status);

14 Phase 3 In Phase 3, we concentrate Interprocess communication and process control Challenges : (IPC) How and when do we connect output of one command to the input the other (Proc Control) Should you wait till the previous process completes, or should you run in parallel?

15 API // replace standard input with input part of pipe
int pipe(int pipefd[2]); int dup2 (int oldfd, int newfd); // replace standard input with input part of pipe dup2(pipefd[0], 0);

16 Typical workflow create pipe fork()
close appropriate end of pipe // parent and child dup2() // atach stdin or stdout to pipe exec() This is the tricky part. How to manage communication between multiple process? How to determine if all the processes exited.

17 Process Control Based on the operator, we decide if we should spawn parallel commands or we should wait || , && and ; -> we should wait | -> we need to execute in parallel When running in parallel, you can wait for the last child Or you can maintain a list of all process that you spawned, and check status for each one You figure this out, it is easy sailing from then.

18 Some Resources Advanced Programming in the Unix Environment By Richard Stevens Beej's Guide to Unix IPC : Examples online (learn from it, don’t copy paste it)

19 All the best Questions?


Download ppt "Programming Assignment 1"

Similar presentations


Ads by Google