Last Lab: Writing a shell

Slides:



Advertisements
Similar presentations
Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
Advertisements

Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
CSCC69: Operating Systems
Carnegie Mellon 1 Exceptional Control Flow : Introduction to Computer Systems Recitation 9: Monday, October 21 st, 2013 Ian Hartwig Section E.
15-213, Fall 06 Outline Shell Lab Processes Signals.
Process groups, sessions, controlling terminal, and job control Process relationship: –Parent/child –Same group –Same session.
Lesson 10-Controlling User Processes. Overview Managing and processing processes. Managing jobs. Exiting/quitting when jobs have been stopped.
System Programming Project 2 Due : 4/19...?. Foreground & Background Foreground job Foreground job Only one at moment Only one at moment Having user’s.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Linux+ Guide to Linux Certification, Second Edition
CPSC 451 Editors and Systems Calls1 Minix editors Mined - (mined) is a simple screen editor. Elle - (elle) is a clone of Emacs. Elvis - (elvis, ex, vi)
Recitation 8 (Nov. 1) Outline Lab 5 hints Virtual Memory Reminder Shell Lab: Due THIS Thursday TA: Kun Gao Modified from Minglong Shao’s Recitation, Fall.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
Project 1CS-3013 A-term Programming Project #1 Forking Processes Due Tuesday, September 8, 11:59 PM.
CSSE Operating Systems
Section A (March 14) Outline Exceptions Process Signals Non-local jumps Reminders Lab4: Due Next Thursday TA: Kun Gao Shamelessly Modified from Minglong.
Processes, Signals, I/O, Shell lab
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: 10/21/2013 Tommy Klein Section B.
Project 1, Command Shell CS-502 (EMC) Fall Programming Project #1 Command Shell CS-502, Operating Systems EMC, Fall 2009 (Slides include materials.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla.
The process concept (section 3.1, 3.3 and demos)  Process: An entity capable of requesting and using computer resources (memory, CPU cycles, files, etc).
CS162B: Forking Jacob T. Chan. Fork  Forks are:  Implement with two or more prongs that is used for taking up or digging  Division into branches or.
Linux+ Guide to Linux Certification, Third Edition
Creating and Executing Processes
Linux+ Guide to Linux Certification, Second Edition Chapter 10 Managing Linux Processes.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
System calls for Process management
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Project1: Unix Shell with History Feature Goals Descriptions Methodology Submission.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: Monday, Oct. 21, 2013 Marjorie Carlson Section.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
1 Exceptional Control Flow III. 2 Outline Loading and Running Programs Multitasking, shells Suggested reading 8.4.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
Agenda Managing Processes (Jobs) Command Grouping Running jobs in background (bg) Bringing jobs to foreground (fg), Background job status (jobs) Suspending.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Implementation of a simple shell, xssh
G.Jyostna.
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Implementation of a simple shell, xssh
Unix Process Management
UNIX PROCESSES.
Programming Assignment 1
Exceptional Control Flow Part II
Fork and Exec Unix Model
LINUX System Programming with Processes (additional)
Recitation 9: Processes, Signals, TSHLab
Operation System Program 1
Programming Project #1 Command Shell
Tutorial: The Programming Interface
Programming Project #1 Fork and Command Shell
Today’s topic UNIX process relationship and job control
Exceptional Control Flow
Lab 6: Process Management
Recitation 9: Processes, Signals, TSHLab
Presentation transcript:

Last Lab: Writing a shell Due: December 11 at 11:59 p.m.

tar xvf shlab-handout.tar Getting Started On zeus.ite.gmu.edu, start by copying the file ~cs367/shlab-handout.tar to the directory in which you plan to do your work. Then do the following: Type the command tar xvf shlab-handout.tar to expand the tarfile:

Getting Started Type the command make to compile and link some test routines and some binaries. Type your team member names and user IDs in the header comment at the top of tsh.c. All of you work will be on this file tsh.c which you will extend to implement a simple shell. Each time you modify tsh.c, you must run make again

tsh.c You will be adding code into the following stubs: eval(): Main routine that parses and interprets the command line. builtin cmd() : Recognizes and interprets the built-in commands: quit, fg, bg, and jobs. waitfg() : Waits for a foreground job to complete. sigchld_handler() : Catches SIGCHILD signals. sigint_handler() : Catches SIGINT (ctrl-c) signals.

tsh.c features Your tsh shell should have the following features: The prompt should remain the string “tsh> ”. The command line typed by the user will consist of a name and zero or more arguments, all separated by one or more spaces. If name is a built-in command, then tsh should handle it immediately and wait for the next command line. Otherwise, tsh should assume that name is the path of an executable file, which it loads and runs in the context of an initial child process (In this context, the term job refers to this initial child process). If the command line ends with an ampersand &, then tsh should run the job in the background. Otherwise, it should run the job in the foreground.

tsh.c features Each job can be identified by either a process ID (PID) or a job ID (JID), which is a positive integer assigned by tsh. You will need to keep track of the jobs that are active in the system. We have provided you with all of the routines you need for manipulating the job list. tsh should support the following built-in commands: The quit command terminates the shell. The jobs command lists all background jobs. tsh should reap all of its zombie children. If any job terminates because it receives a signal that it didn’t catch, then tsh should recognize this event and print a message with the job’s PID and a description of the offending signal.

tsh.c features Typing ctrl-c should cause a SIGINT signal to be sent to the current foreground job, as well as any descendents of that job (e.g., any child processes that it forked). If there is no foreground job, then the signal should have no effect.

Helper Functions /* Here are helper routines that we've provided for you */ int parseline(const char *cmdline, char **argv); void sigquit_handler(int sig); void clearjob(struct job_t *job); void initjobs(struct job_t *jobs); int maxjid(struct job_t *jobs); int addjob(struct job_t *jobs, pid_t pid, int state, char *cmdline); int deletejob(struct job_t *jobs, pid_t pid); pid_t fgpid(struct job_t *jobs); struct job_t *getjobpid(struct job_t *jobs, pid_t pid); struct job_t *getjobjid(struct job_t *jobs, int jid); int pid2jid(pid_t pid); void listjobs(struct job_t *jobs); struct job_t { /* The job struct */ pid_t pid; /* job PID */ int jid; /* job ID [1, 2, ...] */ int state; /* UNDEF, BG, FG, or ST */ char cmdline[MAXLINE]; /* command line */ };

Checking your shell You can run your shell directly (tsh). There are trace files (trace01.txt – trace16.txt) that you can use to test your shell. To test your shell behavior on trace04 (for example), use make: zeus> make test04 There is a reference shell that will show you correct shell behavior (tshref). This can be used with the trace files as well: zeus> make rtest04

Trace Example

Hints-1 Read every word of Chapter 8 (Exceptional Control Flow) in your textbook. Use the trace files to guide the development of your shell. Starting with trace01.txt, make sure that your shell produces the identical output as the reference shell. Then move on to trace file trace02.txt, and so on. The waitpid(), kill(), fork(), execve(), setpgid(), and sigprocmask() functions will come in very handy. The WUNTRACED and WNOHANG options to waitpid() will also be useful (particularly for the extra credit).

Hints-2 One of the tricky parts of the assignment is deciding on the allocation of work between the waitfg() and sigchld_handler() functions. We recommend the following approach: In waitfg(), use a busy loop around the sleep() function. In sigchld_handler(), use exactly one call to waitpid(). While other solutions are possible, such as calling waitpid() in both waitfg() and sigchld_handler(), these can be very confusing. It is simpler to do all reaping in the handler. Programs such as more, less, vi, and emacs do strange things with the terminal settings. Don’t run these programs from your shell. Stick with simple text-based programs such as /bin/ls, /bin/ps, and /bin/echo.

Hints-3 When you implement your signal handlers, be sure to send SIGINT signals to the entire foreground process group, using ”-pid” instead of ”pid” in the argument to the kill() function. In eval(), the parent must use sigprocmask() to block SIGCHLD signals before it forks the child, and then unblock these signals, again using sigprocmask() after it adds the child to the job list by calling addjob(). Since children inherit the blocked vectors of their parents, the child must be sure to then unblock SIGCHLD signals before it execs the new program. The parent needs to block the SIGCHLD signals in this way in order to avoid the race condition where the child is reaped by sigchld_handler() (and thus removed from the job list) before the parent calls addjob().

Hints-4 sigprocmask functions: sigemptyset(&mask) - creates/initializes the sigset_t mask sigaddset(&mask,signal) - adds a signal type to the mask. For example, sigaddset(&mask,SIGCHLD) adds the SIGCHLD signal. sigprocmask(SIG_BLOCK,&mask,NULL) – used to block the signals specified by the mask sigprocmask(SIG_UNBLOCK,&mask,NULL) – used to unblock the signals specified by the mask

Hints-5 When you run your shell from the standard Unix shell, your shell is running in the foreground process group. If your shell then creates a child process, by default that child will also be a member of the foreground process group. Since typing ctrl-c sends a SIGINT to every process in the foreground group, typing ctrl-c will send a SIGINT to your shell, as well as to every process that your shell created. After the fork(), but before the execve(), the child process should call setpgid(0, 0), which puts the child in a new process group whose group ID is identical to the child’s PID. When you type ctrl-c, the shell should catch the resulting SIGINT and then forward it to the appropriate foreground job (or more precisely, the process group that contains the foreground job).

Evaluation For full credit, you must be able to correctly process traces files 01 – 08. You will get extra credit for implementing the extra functionality testing in all of the remaining traces. Your score will be computed out of a maximum of 90 points based on the following distribution: 80 Correctness: 8 trace files at 10 points each. 10 Style points. We expect you to have good comments (5 pts) and to check the return value of EVERY system call (5 pts). Your solution shell will be tested for correctness on a Linux machine, using the same shell driver and trace files that were included in your lab directory. Your shell should produce identical output on these traces as the reference shell, with only two exceptions: The PIDs can (and will) be different. The output of the /bin/ps commands in any traces will be different from run to run. However, the running states of any mysplit processes in the output of the /bin/ps command should be identical.

Submission Make sure you have included all team names and user IDs in the header comment of tsh.c. From the zeus directory where you created your solution, type: ~cs367/367bin/submit 6 tsh.c After the handin, if you discover a mistake and want to submit a revised copy, repeat the above step.