Programming Project #1 Fork and Command Shell CS-3013 Operating

20 Slides271.23 KB

Programming Project #1 Fork and Command Shell CS-3013 Operating Systems Hugh C. Lauer (Slides include materials from Slides include materials from Modern Operating Systems, 3rd ed., by Andrew Tanenbaum and from Operating System Concepts, 7th ed., by Silbershatz, Galvin, & Gagne) CS-3013, C-Term 2012 Project #1, Fork and Command Shell 1

Purpose To practice using the fork and exec facilities of Unix/Linux To learn how to find and use on-line documentation about system calls and system facilities. man pages To become accustomed to speaking directly with the operating system CS-3013, C-Term 2012 Project #1, Fork and Command Shell 2

Programming Assignment Build a command shell capable of running commands in the background:– 1. Write a command called doit to execute another command and print out statistics 2. Adapt doit to become a basic shell that prompts for and executes commands 3. Modify your shell to become shell2 to execute commands in the background CS-3013, C-Term 2012 Project #1, Fork and Command Shell 3

Part 1: doit doit command:– Take another command line as argument Execute that command Print out statistics about command ) ( e cv xe Must compile and execute on your e virtual machine t no (), Likely to be compatible with most pmodern Linux systems v Useful system calls c e x e efunctions:– and s U fork(), execvp(), wait() getrusage(), gettimeofday() May not use system() system call CS-3013, C-Term 2012 Project #1, Fork and Command Shell 4

Part 1 (sample output) % ./doit cat /etc/motd /* print the current message of the day */ /* statistics about the cat command */ % ./doit ls –l /usr/src /* list contents of the /usr/src directory */ /* statistics of the ls command */ % ./doit cp –al /usr/src/linux-2.6.27.25-0.1 kernelSrc /* output of the cp command (if any) */ /* statistics of the cp command */ CS-3013, C-Term 2012 Project #1, Fork and Command Shell 5

doit Implementation Hint Command line is already tokenized in argv Strip off the 0th element of argv vector Pass the remainder of argv to exec() for arguments of command CS-3013, C-Term 2012 Project #1, Fork and Command Shell 6

Questions? CS-3013, C-Term 2012 Project #1, Fork and Command Shell 7

Part 2: shell Repeatedly prompt for command lines Execute each command and print out statistics, as in doit Implement special “built-in” commands exit cd dir Special conditions Input from stdin Exit on end of file Complain about illegal commands Check for line length, number of arguments CS-3013, C-Term 2012 Project #1, Fork and Command Shell 8

Part 2 output % shell cat /etc/motd /* print the current message of the day */ /* statistics about the cat command */ cd dir /* current directory is changed to dir */ ls –l /boot /* listing of files in /boot directory */ /* statistics about this ls command */ exit % /* back to your previous Linux prompt */ % shell filename.txt /* reads, echoes, and executes each command/* /* statistics about each command */ /* exits after last command is completed */ CS-3013, C-Term 2012 Project #1, Fork and Command Shell 9

shell Implementation Hints Input command line May limit to 128 characters, 32 arguments Must tokenize — use strtok() function Built-in commands are not handled by fork and exec Must be executed “in-line” in shell getrusage() returns cumulative statistics For all children of this process Must remember and subtract previous values to get statistics for most process most recently waited for CS-3013, C-Term 2012 Project #1, Fork and Command Shell 10

Questions? CS-3013, C-Term 2012 Project #1, Fork and Command Shell 11

Part 3: Background Execution (shell2) Modify shell to allow commands to execute in background, indicated by ‘&’ character Causes shell to prompt for next command before previous command is complete jobs built-in command lists background tasks in progress Modified wait() call Returns immediately if nothing has terminated Returns PID if any process has terminated Report completion of command at next opportunity – I.e., the next time user types a command, or whenever wait() returns PID of the command CS-3013, C-Term 2012 Project #1, Fork and Command Shell 12

Part 3 output % shell2 make –j4 O /kernelDst & [1] 12345 /* indicate background task #1, process id */ jobs [1] 12345 make /* print process id and command name for tasks */ ls /* listing of files in the current directory */ /* statistics about this ls command */ cat /etc/motd [1] 12345 make Completed /* statistics about background job */ /* print the current message of the day */ /* statistics about this cat command */ exit % /* back to your previous Linux prompt */ CS-3013, C-Term 2012 Project #1, Fork and Command Shell 13

shell2 Implementation Hints Use wait3() to Wait for any process Wait without blocking – WNOHANG option Get usage information directly After fork(), shell2 does while (pid wait3(WNOHANG)) { print msg & statistics of pid update list of outstanding tasks } if (latest not background && latest not done) wait3(latest pid); else get next command; CS-3013, C-Term 2012 Project #1, Fork and Command Shell 14

Work environment Program in C Even though it could easily be done in C Project can be carried out on any Linux system E.g., your own or corporate environment We will grade it on openSUSE 11.4 I.e., your virtual machine environment Note: – Linux systems are often compatible with each other – However, not all C/C compilers and Linux kernels are alike – Some may produce or ignore compiler warnings – Your virtual machine is the one that counts CS-3013, C-Term 2012 Project #1, Fork and Command Shell 15

Project submission Due Sunday, January 22 @ 11:59 PM Submit via web-based Turnin Project name Project 1 Put your name at top of every file! (You would be surprised how many students forget!) CS-3013, C-Term 2012 Project #1, Fork and Command Shell 16

Project Submission (continued) Code of your solution A single Makefile make all make doit make shell – Especially how to keep track of outstanding processes in shell2 Zip everything together make shell2 make clean Test cases for each DO NOT create .rar, .gz, .tar, etc. files part Only .zip files are acceptable CS-3013, C-Term 2012 Write-up explaining everything DO NOT – Put different parts of project in separate folders – Use different makefiles for different parts of project Project #1, Fork and Command Shell 17

Grading Ten point project Part 1 – 2 points Part 2 – 3 points Part 3 – 4 points Point allocation per part See printed project descripton Write-up explaining what you did and your results 1 point MS Word or PDF No credit for a part if it fails to build CS-3013, C-Term 2012 Project #1, Fork and Command Shell 18

Not a team project! Each student must submit own solution Encouraged to discuss general approach, meaning of system calls, etc., with friends Encouraged to seek help if puzzled by some concept, system call, etc. Distill all discussions into your own work! CS-3013, C-Term 2012 Project #1, Fork and Command Shell 19

Questions? CS-3013, C-Term 2012 Project #1, Fork and Command Shell 20

Back to top button