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

Similar documents
Programming Assignment #1: A Simple Shell

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

Project 2: Shell with History1

Creating a Shell or Command Interperter Program CSCI411 Lab

Processes. Processes (cont d)

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

CS 470 Operating Systems Spring 2013 Shell Project

CS167 Programming Assignment 1: Shell

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

SOFTWARE ARCHITECTURE 3. SHELL

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

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

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

Operating System Labs. Yuanbin Wu

Midterm Exam CPS 210: Operating Systems Spring 2013

The Shell, System Calls, Processes, and Basic Inter-Process Communication

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

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

CS240: Programming in C

1 The Var Shell (vsh)

Implementation of a simple shell, xssh

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.

Implementation of a simple shell, xssh

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

Fall 2015 COMP Operating Systems. Lab #3

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

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

Process Management! Goals of this Lecture!

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Review of Fundamentals

PROGRAMMING PROJECT ONE DEVELOPING A SHELL

Process Management 1

System Programming. Introduction to Unix

Project #1: Tracing, System Calls, and Processes

CS 33. Shells and Files. CS33 Intro to Computer Systems XX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Command-line interpreters

LAB 1: C PRIMER CS444/544 WENJIN HU JAN 16TH, 2009

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

Project 1: Implementing a Shell

Pipelines, Forks, and Shell

System Administration

Lab 2: Implementing a Shell COMPSCI 310: Introduction to Operating Systems

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Assignment 2. Purpose. Points. Part I : Command Line parsing. Description. The purpose of this assignment is two-fold

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Introduction to Linux

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Linux shell scripting Getting started *

Problem Set 1: Unix Commands 1

5/20/2007. Touring Essential Programs

Bash Programming. Student Workbook

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

CS240: Programming in C

CSC 1600 Unix Processes. Goals of This Lecture

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

System Programming. Process Control III

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

Operating Systems. Lecture 07. System Calls, Input/Output and Error Redirection, Inter-process Communication in Unix/Linux

Process Management! Goals of this Lecture!

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Lecture 8: Structs & File I/O

Unix Processes. What is a Process?

ENGR 3950U / CSCI 3020U Midterm Exam SOLUTIONS, Fall 2012 SOLUTIONS

Announcement (1) sys.skku.edu is now available

The Classical OS Model in Unix

CS 25200: Systems Programming. Lecture 16: Wildcards

System Calls & Signals. CS449 Spring 2016

Figure 1 Ring Structures

Processes COMPSCI 386

Process Creation in UNIX

SE350: Operating Systems

Introduction to the 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.

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

This homework is due by 11:59:59 PM on Thursday, October 12, 2017.

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

Inter-Process Communication

UNIX. Session 2. UNIX processes and forking fork system call exec system call death of process kill fork and I/O using it UNIX.

Signal Example 1. Signal Example 2

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

Operating System Labs. Yuanbin Wu

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Assignment clarifications

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Chapter 1 - Introduction. September 8, 2016

Operating systems and concurrency - B03

Shell Project Part 1 (140 points)

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

find starting-directory -name filename -user username

TCSS 422: OPERATING SYSTEMS

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

File Descriptors and Piping

Interrupts, Fork, I/O Basics

518 Lecture Notes Week 3

Transcription:

Total Points: 100 COP4342 UNIX Tools Assignment #3: A Simple Unix Shell Instructor: Dr. Robert Van Engelen Teaching Assistant: Imran Chowdhury Spring 2018 Description: The bash shell utility on UNIX and UNIX-based platforms is a command-line shell to access the resources of the operating system via built-in commands, by starting processes to run programs and utilities. Assignment: In this programming assignment, you will implement a customized simplified version of a Unix Shell called cssh. There are no built-in programming structures or variables in cssh. The cssh shell program should be able to: Execute programs that are in the current directory or on the $PATH environment variable and that are compiled executables (object files) with permission x set, and handle IO redirects for programs. Pass arguments that are separated by spaces and string arguments quoted with single -quotes, such as this is a single argument, to commands. Exit on the ``exit command. Change directory with the ``cd command. Command line options: In your cssh, it must support the following forms of basic commands:./program ARG1 ARG2 Execute program (with permission x ), arguments are passed to it ls DIR Execute /bin/ls with DIR argument (ls is on $PATH) exit Exit from cssh cd DIR Change directory to DIR < FILE and > FILE Handle IO redirects with < and/or > (space between < or > and FILE) Exit code: the cssh should exit with EXIT_SUCCESS after the exit command (its arguments can be ignored). Helpful reading: to understand the system calls that you will need to make in the C/C++ code for this assignment, which are fork(2), wait(2), execlp(3), execvp(3), signal(3), open(2), close(2), dup(2), dup2(2), please read this first: http://www.cs.fsu.edu/~engelen/courses/cop4342/unix_ch7.pdf Sample C code: To read shell input from the keyboard, we use readline(3) which is installed on Linux and most Unix systems. It is not available in Visual studio out-of-the-box, so when developing code in Visual Studio use read(2) instead. Here is some sample code on how to read input with readline(3) and tokenize the string into string tokens separated by white space using strtok(3): #include <string.h> #include <readline/readline.h> #include <readline/history.h> static const char prompt[] = "cssh> "; static const char sep[] = " \t\n\r"; // word separators

int main() while (1) char *arg, *line; int i; // prompt then read line line = readline(prompt); if (line == NULL) // tokenize the line into av[] ac = 0; for (arg = strtok(line, sep); arg && ac < 10; arg = strtok(null, sep)) av[ac++] = arg; // print the argument array, from argument 0 to ac (max 9) for (i = 0; i < ac; ++i) printf("arg[%d] = %s\n", i, av[i]); // exit? if (ac > 0 && strcmp(av[0], "exit") == 0) // line and av[] is no longer used, free the malloc-ed line free(line); To compile this sample C code, save it to readline_test.c and then: gcc -o readline_test readline_test.c -lreadline Here is a sample C code with read(2) instead, which is not so nice, because you won t have a history mechanism: #include <string.h> #include <fcntl.h> #include <unistd.h> static const char prompt[] = "cssh> "; static const char sep[] = " \t\n\r"; // word separators int main() int tty = open("/dev/tty", O_RDWR); // open tty for read/write if (tty == -1) fprintf(stderr, "can't open /dev/tty\n"); while (1) char *arg, line[256]; // buffer to hold line of input int i; // prompt then read line write(tty, prompt, sizeof(prompt) - 1); i = read(tty, line, sizeof(line));

if (i <= 0) line[i] = '\0'; // tokenize the line into av[] ac = 0; for (arg = strtok(line, sep); arg && ac < 10; arg = strtok(null, sep)) av[ac++] = arg; // print the argument array, from argument 0 to ac (max 9) for (i = 0; i < ac; ++i) printf("arg[%d] = %s\n", i, av[i]); // exit? if (ac > 0 && strcmp(av[0], "exit") == 0) Sample C code: Use the following example that is based on the helpful reading (see above), to execute a command with arguments: #include <fcntl.h> #include <unistd.h> #include <signal.h> int main() int pid; // process id int status; // child process exit status int w; void (*istat)(int), (*qstat)(int); int tty = open("/dev/tty", O_RDWR); // open tty for read/write; if (tty == -1) fprintf(stderr, "can't open /dev/tty\n"); // set up a simple example command to execute with arguments ac = 3; av[0] = "ls"; av[1] = "-a"; av[2] = "-l"; // fork this process, creating a copy of this running program if ((pid = fork()) == 0) // this is the forked child process that is a copy of the running program dup2(tty, 0); // force stdin from tty dup2(tty, 1); // force stdout to tty dup2(tty, 2); // force stderr to tty close(tty); // last argument must be NULL for execvp() av[ac] = NULL; // execute program av[0] with arguments av[0]... replacing this program execvp(av[0], av); fprintf(stderr, "can't execute %s\n", av[0]);

close(tty); // disable interrupt (^C and kill -TERM) and kill -QUIT istat = signal(sigint, SIG_IGN); qstat = signal(sigquit, SIG_IGN); // wait until forked child process terminated, get its exit status while ((w = wait(&status))!= pid && w!= -1) continue; if (w == -1) status = -1; // restore interrupt and quit signals signal(sigint, istat); signal(sigquit, qstat); // done with this example Note that at most 8 arguments can be passed to a command (not counting IO redirects), since the av[] value right after the last av[] argument must be set to NULL for execvp(3) to work, where we allocated 10 array elements av[0..9], and av[0] is set to the name of the command to execute. See the man page of execvp (man 3 execvp). Implementing your shell: we suggest the following steps to implement cssh: First of all, you are not allowed to use system(3) directly anywhere in your code. If your code contains this call, you will receive zero points for this assignment. Implement cssh using the example code provided herein. The first version of your shell can be directly based on these examples and should be able to handle command execution. Test your implementation before implementing the ``cd command and IO redirects. To implement ``cd, see the man page of chdir(2) for help. To implement ``> FILE redirect where there is always spacing between the ``> and FILE, check if the array av[] has a ``> string and if so open the file specified in av[] using open(2) for writing and use dup2(fdout, 1) with this open file descriptor fdout (see the man page of dup(2) and dup2(2)). Then modify the array av[] to remove these two entries from the array, shifting other array elements to ensure the array is not destroyed. Also reduce ac by 2. Do not forget to close(fdout) ) in the forked child and parent processes, because we dup-ed fdout. Likewise, to implement ``< FILE open the file specified for reading with open(2) and use dup2(fdin, 0) with this open file descriptor fdin. Adjust the array after extracting ``< and FILE. Do not forget to close(fdin) in the forked child and parent processes, because we dup-ed fd. You should now be able to handle IO redirects with the proper adjustment to av[] and ac, such as ``sort < somefile1.txt > somefile2.txt and in fact the IO redirects can be reordered as in a real shell such as ``sort > somefile2.txt < somefile1.txt and even ``< somefile1.txt > somefile2.txt sort. Check that your cssh can execute commands such as echo, ls and pwd. Also check that you can execute a local program with./program and arguments. Remove strtok(3) and replace it with your own tokenizer function that tokenizes arguments that are spaced apart by space (0x20) and tabs (0x09). A quoted argument with a -quote should be stored in av[] as one entry without the -quotes. A simple trick is to change the line string by replacing white space by \0 and by setting the av[] pointers to the location of each argument in the line string. Now, each array argument is a \0-terminated string. Do something similar with each -quoted argument.

Test if argument parsing works by checking you can execute echo Hello world!! Bye! which has a quoted argument, such that the wide spacing in the argument is preserved when passed to echo. Submission Requirements: A tar file named yourcsusername_asg3.tar containing the following files:- [1]. The C or C++ implementation of the above described custom basic Unix Bash Shell. Do not include object files or binaries. Grading Policy: Submitted code must compile with the -Wall -ansi -pedantic flags without any warning messages to conform to the ANSI ISO standard. Code that does not compile or generates errors (Segmentation Fault etc.) on execution will receive zero grade points. Also, when using system(3) in your code or calling a shell such as sh will result in zero grade points. Points distribution:- [1]. Code Readability (20 points) [2]. Test Cases (80 points) Individual parts of the implementation will not be graded separately for correctness. There will be several cases to test the implementation logic as a whole. Also, keep in mind that your code will be tested on linprog. Students should test their code thoroughly on the linprog server before submission Bonus to earn 2% Extra Credit for your final grade: You can earn extra credit to count towards your final grade: Implement a pipe `` between two commands using pipe(2) to create two pipe descriptors, such that the first is dup-ed as 1 for the first command that is forked+execvp-ed and the second is dup-ed as 0 for the second command that is forked+execvp-ed. Note that you should execute two fork() and two execvp() and pass the argument array av[] to both but in a way that ensures that each command receives its portion of the arguments. IO redirects may not work yet, since your code must now extract two sets of these as well. After implementing the pipe(2) call, the dup2(2) calls, and changes to split av[] up to two parts to pass to the two commands, check that cat somefile1.txt sort works as expected. Change your code to implement IO redirects for both commands. You may assume that the first command has no ``> redirect and that the second command has no ``< redirect. Check that cat < somefile1.txt sort > somefile2.txt works as expected. You can expect spacing to surround ``. Miscellaneous: Honor code policy will be strictly enforced. Write the code by yourself and protect your submission. Key Concepts: Processes, Process fork and wait, Argument passing, Interactive input, IO Redirects, Pipes, String Operations. UNIX API Calls: readline(3), read(2), write(2), chdir(), fork(), wait(), waitpid(), dup(), dup2(), open(), close(), popen(), pclose(), pipe(), signal(), execvp().