Lab5 FAQ
This document contains Frequently Asked Questions, which are relevant to completing the week's lab
assignment. Any Questions may be posted to cspp51081.
Where to begin.
Get started on this assignment early. First, finish the
Grand Ole Shell. There are two parts of this project:
- Parsing the input line
- Creating processes to run commands using fork
and exec.
You will probably find the first part is the harder of the two, because it can lead to some rather
nasty C programming. (Watch out for those SIGSEGVs.
Plan to give yourself some time to debug. I will give you some hints below that will take a long
way on this part of the project. The second part is significantly easier to program, but the more
difficult and important conceptually. You need to establish a strong foundation in process control,
the rest of this course depends upon it. I will give you some pointers below to get started, and
some places to look for more information.
String Processing
Get acquainted with C's standard string library, any C book will have a thorough discussion of it.
A string is of type char * , and ALWAYS ends in the
null character '\0'. Here are some useful functions:
all require #include <string.h>
- char *strtok( char *str, const char *set );
This function is used to separate a string str
into tokens (or fields) by using the characters in set
as separators. Here is how it works: The first time you call strtok
to separate str, you call
strtok( str, set );
strtok will skip over any members of
set until it comes to the first character
not in set; it will place a pointer at this position
of str. It will then look for the next character
in set, and write a null character '\0' in place of this character.
That is the end of your first string token. Your are returned a pointer to it. Note well: Your string
has been overwritten. Every subsequent call to strtok, to obtain all other tokens must pass
NULL in place of str:
strtok( NULL, set );
and strtok will return a pointer to the next string token
in your string str. strtok will return
NULL when no more tokens are found. You may change
the argument set on subsequent calls to strtok, but you must pass NULL if you want
strtok to continue parsing str.
- int strcmp( const char *s1, const char *s2);
int strncmp( const char *s1, const char *s2, size_t n);
strcmp compares the two strings s1
and s2 by dictionary order, and returns
an integer value less than zero if s1 comes before s2,
equal to zero if they are the same string,
and greater than zero if comes after s2.
strncmp compares on no more than the first n characters of each string.
Process Control
There are three good sources for information about process control from your texts:
- Molay, Understanding Unix/Linux Programming, Chapter 8, is an excellent
discussion which is very relevant to this project.
- Stevens, Advanced Programming for the Unix Environment, Chapter 8, will
give you a thorough background in Processes.
- Stone, Basic Linux Programming, Chapter 10, a good introduction, but
not sufficient by itself for a solid understanding.
In addition to these references, if will look for a good and reasonably short tutorial to help; you
also have your manpages, section 2. The following functions are very important:
- fork
- exec family of six functions (all of them
use execve, although you will find execvp
and execlp most useful)
- wait
- exit (You probably know all
about this one, but it is in the same important family of functions as
the other three.)
Remember, it will pay you well if you spend some time learning to use these functions, and
understanding how Unix manages processes by means of them.