CS 470 Operating Systems Spring 2013 Shell Project

Similar documents
Programming Assignment #1: A Simple Shell

Project 2: Shell with History1

Operating System Structure

HW 1: Shell. Contents CS 162. Due: September 18, Getting started 2. 2 Add support for cd and pwd 2. 3 Program execution 2. 4 Path resolution 3

CS 4410, Fall 2017 Project 1: My First Shell Assigned: August 27, 2017 Due: Monday, September 11:59PM

COP4342 UNIX Tools Assignment #3: A Simple Unix Shell. Instructor: Dr. Robert Van Engelen Teaching Assistant: Imran Chowdhury Spring 2018

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

CSCi 4061: Intro to Operating Systems Spring 2017 Instructor: Jon Weissman Assignment 1: Simple Make Due: Feb. 15, 11:55 pm

Dept. Of Computer Science, Colorado State University

Lecture 4 Threads. (chapter 4)

Frequently asked questions from the previous class survey

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

3. A Periodic Alarm: intdate.c & sigsend.c

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Processes. Processes (cont d)

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

Processes. CS3026 Operating Systems Lecture 05

SE350: Operating Systems

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

1 The Var Shell (vsh)

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

W4118 Operating Systems. Junfeng Yang

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Processes. CS439: Principles of Computer Systems January 30, 2019

Process Management 1

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

Processes. CS439: Principles of Computer Systems January 24, 2018

Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday)

Creating a Shell or Command Interperter Program CSCI411 Lab

CSC374, Spring 2010 Lab Assignment 1: Writing Your Own Unix Shell

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

CS240: Programming in C

CSC209 Review. Yeah! We made it!

Workshop on Inter Process Communication Solutions

Each terminal window has a process group associated with it this defines the current foreground process group. Keyboard-generated signals are sent to

CS 213, Fall 2002 Lab Assignment L5: Writing Your Own Unix Shell Assigned: Oct. 24, Due: Thu., Oct. 31, 11:59PM

Chapter 3: Processes. Operating System Concepts Essentials 2 nd Edition

Project #1 Exceptions and Simple System Calls

Multithreaded Programming

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Final Precept: Ish. Slides Originally Prepared by: Wonho Kim

Chapter 3: Process Concept

Killing Zombies, Working, Sleeping, and Spawning Children

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Chapter 3: Processes. Chapter 3: Processes. Process in Memory. Process Concept. Process State. Diagram of Process State

Project 1: Implementing a Shell

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

fork System-Level Function

Operating Systemss and Multicore Programming (1DT089)

System Calls and Signals: Communication with the OS. System Call. strace./hello. Kernel. Context Switch

CS 213, Fall 2001 Lab Assignment L5: Writing Your Own Unix Shell Assigned: Oct. 25, Due: Fri., Nov. 2, 11:59PM

Princeton University. Computer Science 217: Introduction to Programming Systems. Process Management

Lecture 7: Signals and Events. CSC 469H1F Fall 2006 Angela Demke Brown

Fall 2015 COMP Operating Systems. Lab #3

Chapter 4 Multithreaded Programming

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

CSCI0330 Intro Computer Systems Doeppner. Project Shell 2. Due: November 8, 2017 at 11:59pm. 1 Introduction 2

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

CS 215 Fundamentals of Programming II Fall 2017 Project 7. Morse Code. 30 points. Out: November 20, 2017 Due: December 4, 2017 (Monday) a n m

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Machine Problem 1: A Simple Memory Allocator

CS 450 Operating System Week 4 Lecture Notes

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Computer Systems Assignment 2: Fork and Threads Package

Chapter 3: Processes. Operating System Concepts 8th Edition,

518 Lecture Notes Week 3

Lecture 8: Unix Pipes and Signals (Feb 10, 2005) Yap

Chapter 3: Processes

Kernel and processes

The Programming Interface

CS 326: Operating Systems. Process Execution. Lecture 5

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

CS 224 Spring 2015 Lab Assignment Shell Lab: Writing Your Own Unix Shell

Introduction. This project will focus primarily on processes.

Operating System Labs. Yuanbin Wu

CS 3013 Operating Systems WPI, A Term Assigned: Friday, August 31, 2007 Due: Monday, September 17, 2007

Operating Systems 2010/2011

Shell and Signals. Computer Organization 3/17/2015. CSC252 - Spring The World of Multiprogramming or Multitasking. Unix Process Hierarchy

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

2 Processes. 2 Processes. 2 Processes. 2.1 The Process Model. 2.1 The Process Model PROCESSES OPERATING SYSTEMS

Chapter 3: Process Concept

Chapter 3: Process Concept

CS 215 Fundamentals of Programming II Spring 2010 Project 6. Dnode<T> structure. Specifications for Document Class. 30 points

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Project 4: Implementing Malloc Introduction & Problem statement

Exceptions, Processes and Signals

Processes. Process Concept

Processes: Introduction. CS 241 February 13, 2012

last time in cs recitations. computer commands. today s topics.

Operating System Design

Operating systems and concurrency - B03

Transcription:

CS 470 Operating Systems Spring 2013 Shell Project 40 points Out: January 11, 2013 Due: January 25, 2012 (Friday) The purpose of this project is provide experience with process manipulation and signal handlers in Unix. Problem Statement 1 This project consists of completing a program to serve as a shell interface. That is, the program accepts user commands and then executes each command in a separate process. Unless otherwise specified, the parent process waits for the child to exit before continuing as shown in Figure 3.11 in the textbook. As is typical, the shell also will allow the child process to run concurrently as a background process by specifying the ampersand (&) at the end of the command. And finally, the shell will support a history mechanism that allows users to repeat commands previously entered. Project Details This project is organized into two parts: 1. Create a child process that executes the given command that the parent may or may not wait for using the fork(), exec(), and wait()/waitpid() system calls (described in Section 3.3 of the textbook) 2. Implement a history mechanism using a signal handler (notes at the end of this assignment and covered in lecture on 1/11/2013) Base shell code and the signal handler demonstration code from lecture written in C is available in the directory /home/hwang/cs470/project1. However, this project may be written in any language as long as it supports process creation and signals, and runs on csserver. Simple Shell The program in shell.c provides the basic operations of a command line shell. The program consists of two functions: main() and setup(). The setup function reads in the user's next command (up to 80 characters), and then parses it into separate tokens that are used to fill the argument vector for the command to be executed. Note that the setup function creates the args array by manipulating inputbuffer. It replaces all whitespace and any & in inputbuffer by the null terminator. The elements of args are pointers to the first letter of each word in inputbuffer. If an & is encountered, the background parameter is set to inform the main program. The program is terminated when the user enters Ctrl-d which causes the setup function to invoke the exit() system call. The main function presents the user prompt and then invokes the setup function, which waits for the user to enter a command. When the setup function returns, the args array is suitable for passing on to 1 This project is based on the UNIX Shell and History Feature project published in previous editions of the course textbook: Sibershatz, Galvin, and Gagne, Operating Systems Concepts, 7e, John Wiley & Sons, Inc., 2005. Revised: 01/11/2013, fixed typos in example run Page 1 of 5 D. Hwang

the execvp() system call. For example, if the user enters "ls -l" at the prompt, args[0] is the C style string "ls" and args[1] is the C style string "-l". Creating a Child Process The first part of the project is to modify the main function so that upon the return of the setup function, a child process is forked and executes the command specified by the user using the execvp() system call, which has the following interface: int execvp(char *command, char *params[]); where command is the command to be performed and params stores the command line. Thus for this project, the call should be execvp(args[0], args). The background variable determines whether the parent process is to wait for the child to exit or not. Some things to note: An exec call only returns if there was an error trying to start the new program, e.g. if the command does not exist. However, if an exec call fails, its process must be terminated, otherwise it will continue to run the parent (shell) code. In particular, it will print (extra) prompts to the screen. Background processes still need to be waited upon in order for them to terminate properly. Otherwise, when they try to terminate, they will become zombies for the duration of the new shell execution. The best way to handle this is to take advantage the fact that when a process that is orphaned (i.e., its parent has terminated) it is adopted by the init process that automatically issues a wait for it. By having the child process do a second fork and have the grandchild process do the exec call while the child process terminates immediately. This is called the double fork technique and often is used for long lived server type processes (e.g., web server) that fork a separate process to handle an individual client request. Creating a History Mechanism The second part of the project is to add a history mechanism that allows the user to access up to the 10 most recently entered commands. The user will be able to list these commands, with the least recent command listed first and the most recent command listed last, by pressing Ctrl-\ (control backslash), which generates the SIGQUIT signal. From the history list, the user can run any of the previous 10 commands (numbered 0 9 from oldest to newest) by entering on the command line "!n" where 'n' is the number of the command. Also, the user should be able to run the most recent command again by entering "!!". Any command executed from the history list should be echoed on the screen and placed in the history list as the next command. Note that this is the actual command executed (e.g. "ls -l", not "!3" or "!!". Here is a short example: $./shell COMMAND-> ps <ps output> COMMAND-> ls -l <ls -l output> COMMAND-> pwd <pwd output> Revised: 01/11/2013, fixed typos in example run Page 2 of 5 D. Hwang

COMMAND->!! pwd <pwd output> COMMAND->!2 ls -l <ls -l output> COMMAND->^\ 0 ps 1 ls -l 2 pwd 3 pwd 4 ls -l COMMAND->!x Error: bad history attempt COMMAND->^\ 0 ps 1 ls -l 2 pwd 3 pwd 4 ls -l COMMAND->^d $ It may be assumed that history commands will be well formed consisting of '!' followed immediately by a single character followed by a newline. I.e., you may ignore any extra characters that appear after the second character in commands starting with '!'. If the user attempts to use the history mechanism incorrectly (e.g., '!x' where x is not a digit), an error message should be displayed and the command not entered into the history list, and of course, execvp should not be called. Note this is only for history usage errors and will not prevent erroneous commands that appear to be valid from being added to the history list and attempted to be executed; handling this is beyond the scope of this project. Some things to note: As noted below, if the list is displayed from within the handler, the write system call must be used, since printf is not signal safe. After a signal handler is called, control goes back to the location of the interruption. For this project, this likely will happen someplace in the middle of the read system call, which causes the system call to fail and return a negative value. The shell program should ignore such attempts to read, and loop back to reissue the prompt and the read call in this case so that a user can ask for the history as many times as they wish. Assignment (20 points) Implementation of the shell program described above. This project is to be done individually. While there are no explicit design guidelines for this course, programs should exhibit a modular or objectoriented design. Projects exhibiting particularly poor design will not earn full credit. This project may be written in any language as long as the language supports process creation and signals, and runs on csserver. Please note that the instructor will only be able to provide assistance for projects written in C/C++/Java. Provide a makefile that will make your project if it needs to be compiled. Revised: 01/11/2013, fixed typos in example run Page 3 of 5 D. Hwang

(10 points) Provide a high level discussion of the program describing the functionality of the major components of the program and how they interact with each other, and a more detailed discussion for the data structures and algorithms used in the history feature portion of the program. If the program does not meet all of the project requirements, describe which parts are missing and what you think should be done to implement them. (10 points) Provide a test plan for your project. I.e., give a list of commands that will demonstrate the features of your shell. Annotate this list with notes regarding what result is expected. The grade for this portion of the project will depend on how thorough the test plan is. Note that the test plan should cover all of the project requirements whether or not the program actually implements them. In addition, answer the following questions: 1. What aspect of process manipulation or signal handling did you find most difficult to understand? 2. What aspect of process manipulation or signal handling did you find least difficult to understand? 3. What, if anything, would you change in your current design? 4. What, if anything, did you find interesting or surprising about process manipulation or signal handling that you did not know before doing this project? What to Submit Create a tarfile or zipfile containing the following items: The well documented code source code for your shell. If the project is not written in C/C++/Java, put instructions on how to run the program in a comment at the beginning of the main program file. A makefile to make your project, if needed The discussion of the functional design of your project, preferably in PDF format. The test plan for your project and the answers to the questions above, preferably in PDF format. Submit this tarfile or zipfile electronically by emailing it as an attachment to the instructor no later that 4:30pm on the due date. Basic Notes About Signals Unix uses signals to notify a process that a particular event has occurred. Signals may be either synchronous or asynchronous, and generated by hardware, software, or user input. Once a signal is generated, it is delivered to a process where it must be handled. A process receiving a signal may handle it in one of three ways: Ignore the signal Use the default signal handler Provide a separate signal handling function Each signal has a default action associated with it (e.g. for SIGQUIT, it is to terminate the process), and a few signals have permanent actions that cannot be changed (e.g. SIGKILL always terminates a process). Revised: 01/11/2013, fixed typos in example run Page 4 of 5 D. Hwang

A demonstration program is available in file signal.c. Signals are defined in the header file signal.h. (More detailed information on signals may be found by running "man 7 signal" on a Linux box. A signal handler simply is a function with the following interface: void handler (int signum); where x is the signal number being handled. When a signal is received, the current instruction is interrupted and control transfers to the handler function. When the handler returns, the interrupted instruction is continued. A handler may access any global variables and execute most any code. The main restriction is that I/O must be done using the read and write system calls directly as higher level I/O routines such as scanf and printf in C or << and >> in C++ are not signal safe. To attach a custom handler to a signal, a sigaction structure is created with the sa_handler field set to the handler function. The rest of the structure must be initialized, but is otherwise not used for this project. The structure is passed to the sigaction() system call, which has the interface: int sigaction (int signum, const struct sigaction *act, struct sigaction *oldact); where signum is the number of the signal (e.g. SIGQUIT), act is the the structure with the new handler, and oldact is filled with the previous structure information if is it not NULL. Revised: 01/11/2013, fixed typos in example run Page 5 of 5 D. Hwang