A Tiny Unix Shell (tsh) - Part I Study Guide - Prof. Mirela Damian-Iordache, Assignments of Operating Systems

An introduction to unix shells and a set of routines for manipulating jobs stored in a list as part of a bigger assignment on process control and signaling. It includes function definitions, global variables, and test cases for the functions. The assignment aims to expand the understanding of topics such as job control, pointers, and structures in c.

Typology: Assignments

Pre 2010

Uploaded on 08/13/2009

koofers-user-7od
koofers-user-7od 🇺🇸

9 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
A Tiny Unix Shell (tsh) Part I
Introduction
This is the first part of a bigger assignment on process control and signaling. To expand your understanding
on these topics, you will write a simple Unix shell program that supports job control.
This first assignment asks you to write a few helper routines for job manipulation. In doing so, you will also
learn how to manipulate pointers and structures in C. This assignment must be completed individually.
General Overview of Unix Shells
Ashell is an interactive command-line interpreter that runs programs on behalf of the user. A shell repeat-
edly prints a prompt, waits for a command line on stdin, and then carries out some action, as directed by
the contents of the command line.
The command line is a sequence of ASCII text words delimited by whitespace. The first word in the
command line is either the name of a built-in command or the pathname of an executable file. The remaining
words are command-line arguments. If the first word is a built-in command, the shell immediately executes
the command in the current process. Otherwise, the word is assumed to be the pathname of an executable
program. In this case, the shell forks a child process, then loads and runs the program in the context of the
child. The child processes created as a result of interpreting a single command line are known collectively
as a job.
If the command line ends with an ampersand &”, then the job runs in the background, which means that
the shell does not wait for the job to terminate before printing the prompt and awaiting the next command
line. Otherwise, the job runs in the foreground, which means that the shell waits for the job to terminate
before awaiting the next command line. Thus, at any point in time, at most one job can be running in the
foreground. However, an arbitrary number of jobs can run in the background.
For example, typing the command line
tsh> jobs
causes the shell to execute the built-in jobs command. Typing the command line
tsh> /bin/ls -l -d
runs the ls program in the foreground. By convention, the shell ensures that when the program begins
executing its main routine
int main(int argc, char *argv[])
the argc and argv arguments have the following values:
1
pf3
pf4
pf5

Partial preview of the text

Download A Tiny Unix Shell (tsh) - Part I Study Guide - Prof. Mirela Damian-Iordache and more Assignments Operating Systems in PDF only on Docsity!

A Tiny Unix Shell (tsh) – Part I

Introduction

This is the first part of a bigger assignment on process control and signaling. To expand your understanding on these topics, you will write a simple Unix shell program that supports job control.

This first assignment asks you to write a few helper routines for job manipulation. In doing so, you will also learn how to manipulate pointers and structures in C. This assignment must be completed individually.

General Overview of Unix Shells

A shell is an interactive command-line interpreter that runs programs on behalf of the user. A shell repeat- edly prints a prompt, waits for a command line on stdin, and then carries out some action, as directed by the contents of the command line.

The command line is a sequence of ASCII text words delimited by whitespace. The first word in the command line is either the name of a built-in command or the pathname of an executable file. The remaining words are command-line arguments. If the first word is a built-in command, the shell immediately executes the command in the current process. Otherwise, the word is assumed to be the pathname of an executable program. In this case, the shell forks a child process, then loads and runs the program in the context of the child. The child processes created as a result of interpreting a single command line are known collectively as a job.

If the command line ends with an ampersand ”&”, then the job runs in the background, which means that the shell does not wait for the job to terminate before printing the prompt and awaiting the next command line. Otherwise, the job runs in the foreground, which means that the shell waits for the job to terminate before awaiting the next command line. Thus, at any point in time, at most one job can be running in the foreground. However, an arbitrary number of jobs can run in the background.

For example, typing the command line

tsh> jobs

causes the shell to execute the built-in jobs command. Typing the command line

tsh> /bin/ls -l -d

runs the ls program in the foreground. By convention, the shell ensures that when the program begins executing its main routine

int main(int argc, char (^) *argv[])

the argc and argv arguments have the following values:

  • argc == 3,
  • argv[0] == ‘‘/bin/ls’’,
  • argv[1]== ‘‘-l’’,
  • argv[2]== ‘‘-d’’.

Alternatively, typing the command line

tsh> /bin/ls -l -d &

runs the ls program in the background.

Unix shells support the notion of job control, which allows users to move jobs back and forth between back- ground and foreground, and to change the process state (running, stopped, or terminated) of the processes in a job. Unix shells provide various built-in commands that support job control. For example:

  • jobs: List the running and stopped background jobs.
  • bg : Change a stopped background job to a running background job.
  • fg : Change a stopped or running background job to a running in the foreground.
  • kill : Terminate a job.

Manipulating Jobs

Each job can be identified by either a process ID (PID) or a job ID (JID), which is a positive integer assigned by tsh. JIDs should be denoted on the command line by the prefix ’%’. For example, “%5” denotes JID 5, and “ 5 ” denotes PID 5.

In this assignment you will write a set of routines for manipulating jobs stored in a list. Start with the following definitions:

  1. int maxjid(struct job t (^) *jobs); Returns the largest allocated job ID.
  2. int deletejob(struct job t (^) *jobs, pid t pid); Deletes from jobs that job whose process id equals pid. Update nextjid, if necessary (i.e, if the job removed had maximum jid, then nextjid should become maxjid(jobs)+1).
  3. pid t fgpid(struct job t (^) *jobs); Returns the identifier pid of current foreground job from the jobs list, 0 if no such job.
  4. struct job t *getjobpid(struct job t (^) *jobs, pid t pid); Finds a job (by searching for pid) in jobs.
  5. struct job t *getjobjid(struct job t (^) *jobs, int jid); Finds a job (by searching for jid) in the job list jobs.
  6. int pid2jid(pid t pid); Returns the job ID of the process with identifier pid from jobs.
  7. void listjobs(struct job t (^) *jobs); Prints out all jobs in jobs (one per line).

Thoroughly test each function by creating sample calls in the main function. Here is an example:

int main()

initjobs(jobs); listjobs(jobs); addjob(jobs, 111, FG, "ls -l"); listjobs(jobs); /* etc. (^) */

Hint: To find out the name of a header containing the declaration of a particular function, type in

man function name

at the shell prompt. For instance, typing in

man strcpy

will list information about the strcpy function.

Evaluation

Your score will be computed out of a maximum of 50 points based on the following distribution:

40 Correctness.

10 Style points. We expect you to have good comments and to check for errors (eg., deleting from an empty list, or checking the validity of an argument (jid ¿ 0, for instance), or checking for NULL pointers, etc.). Do not allow me to break your code by trying something like

addjob(jobs, 100, FG, 0); listjobs(jobs);

Hand In Instructions

Hand in a printed copy of your source code (tsh.c) and a short README file explaining any bugs and deviations from the assignment specifications. Make sure you have included your name and your Unix login name in the README file. Leave your source code in your Unix account. I will test your code on felix. Good luck!