CSC 112 Lab 1: Introduction to Unix and C++ Fall 2009 Due: Friday, September 4 th, 9:00am Introduction The operating system of a computer is the coordinator of all of the computer s activities, including memory management, process (program) scheduling, and file access. At Wake Forest University, students generally use the Windows operating system on their laptops. However, in the Computer Science professional world, a number of Operating Systems are used. In CSC 112, you will a variant of the Unix Operating System. This lab is intended to introduce you to using the Unix operating system environment for C++ programming. Specifically this lab covers: 1. An introduction to the Linux environment, namely file structures and major commands. 2. Creation of the directory structure required for this course. (graded) 3. Use of Unix commands to copy/compile/execute an existing C++ program. (graded) 4. Writing, compiling, and execution of a simple C++ program, whose description is given at the end of this assignment). (graded) A Brief History of Unix In 1969, Ken Thompson developed the Unix operating system at Bell Laboratories. Initially, Unix was only a tool for AT&T programmers; however, today many different flavors of Unix exist. These variations range from Solaris (which is on our departmental workstations) to Mac OSX (the guts behind the interface are a version of Unix) to Linux (a version of Unix for Intel based computers). The Unix operating system was designed as a multi-platform systems that is, to be able to be run on computers with different types of central processing units (CPUs). It has evolved into one of the most powerful multi-tasking operating systems in existence, meaning the Unix operating system allows multiple users to access and share time on a single CPU. More than one process can be executed at the same time and each process can communicate with others when necessary. Furthermore, computers at different locations can communicate with each other, sharing information and application programs.
The GNOME Desktop Once you are logged on to your Linux installation, the GNOME desktop environment (your graphical user interface) will appear on the screen. GNOME is a graphical user interface to the Unix operating system, which has a similar feel as Windows XP. Through GNOME, you can open windows that give you access to the Unix operating system. For example, you can open an Xterm or terminal window where you can type Unix commands that will allow you to see a listing of your files, call up a text editor, compile a C++ program, see a list of the programs that are running, send a file to the printer, open a mail program to read your mail, etc. To invoke the Main Menu (Applications), click on the Applications Menu Item on the left side of the menu bar at the top of the screen). You should now see a list of all the programs you can call up from this menu. There is also a shortcut taskbar at the bottom of the screen; however, limit your reliance on GNOME and try avoid the tool bar. Although it is tempting to use GNOME for managing your desktop (copying files, launching an editor, email, etc...), it is better to learn the appropriate Unix commands to perform the same tasks. GNOME is often slow (especially for complex commands). As a result, it is in your best interest to spend time now learning Unix commands. Unlike many GUI managers, Unix commands are standard across most variations of Unix. Furthermore, there are many situations where a GUI interface is not available (e.g. ssh sessions, where you connect, without an interface, to another computer). The investment of learning Unix commands will serve you well throughout your computer science career. Opening a Terminal Window As noted in the previous section, GNOME allows you to open working windows into which you can type Unix commands that allow you to access files and run programs. To open a terminal window, go to the Applications as before, then Accessories, then select Terminal (Applications Accessories Terminal). To type into the terminal window, click on it. Your cursor should now be positioned so that you can type at the command line. Note you can start multiple Terminal windows, which may be useful as you program. To create a shortcut to the Terminal application on your desktop, follow the same menu traversal steps as before, but instead of clicking on Terminal, right-click on Terminal and choose Add this launcher to desktop. Unix File Structure and Commands File Structure Your Linux operating system is organized as a hierarchical file structure. As seen in Figure 1, this structure appears like a tree, upside down. At the top is the root directory, which is symbolized by a forward slash / Beneath the root directory are all the other directories and files in the Unix system (this includes your files). Therefore, each file and directory has a specific place in the file
system, which is called the pathname. Pathnames indicate the way through the file structure. As will be discussed later, the forward slash serves two functions: representing the root directory, and separating names in a path name. Figure 1: Partial directory structure for your Linux system. For example using Figure 1, the path from root to your own directory path would be /home/username Given you have just logged into your machine, this path (/home/username) is returned if you issue the present working directory command at the terminal prompt. To determine the current directory, enter pwd at the prompt and hit Enter. This directory is also called your home directory. Another example is the directory that contains the include files necessary for compiling a C++ program. These files are located in /usr/include/. During the semester you will create various files and directories off of your home directory. For this reason you need to know a few simple Unix commands, discussed below.
Unix Commands The standard format for Unix commands is commandname -options path1 path2 where commandname is the command, path1 is the source (often a filename), path2 is the destination (often another filename), and options are the command options. Note how options are preceded with a dash -. Unix commands (as well as file and directory names) are case sensitive; therefore, entering PWD at the prompt will not return the current directory. The important Unix commands for you to know as we start this course are listed below: Command pwd ls mkdir directoryname rmdir directoryname cd pathname cp file1 file2 rm filename more filename man commandname Description Returns the path name from root to the current working directory (called the full path name ). Lists the files in the current directory. There are a variety of command options for ls. For example, ls -al provides detailed information about each file in the current directory (the a means describe all files [including system and hidden files] and the l means describe them in long format). Creates a directory called directoryname Removes the directory called directoryname. Note the directory needs to be empty (see rm below on how to remove individual files). Change directory to pathname. Specializations of this include: cd without a pathname takes you to your home directory and cd.. moves you back one directory Copy file file1 to file file2. Erase (remove) file filename. Once a file is erased, it can t be retrieved (this doesn t put it in a recycle bin!) View the contents of file filename, if it s in text, one page at a time Obtain help on how a command called commandname is used, including the command syntax and available options. Instead of using full path names, it is also possible (and often preferred) to use pathnames relative to the current working directory. For example using Figure 1, suppose you are in the directory /usr and want to move into the sub-directory include underneath /usr. You could enter cd include, instead of supplying the full pathname as cd /usr/include.
Creating Your CSC112 Directory Structure and Compiling an Existing C++ Program (Graded) For this portion of the lab you will create two directories, copy a C++ program into your directory, compile the program, and then execute the program. For this class, all programming assignments must reside within a certain directory structure for grading. This portion of the assignment will cover how these directories are created, using the commands in the previous section. First, move to your home directory. To do this, enter the cd command at the prompt. Issue a pwd command to be certain you are in your home directory. Now create a new directory called CSC112 off your home directory. Issue the following command to accomplish this: mkdir CSC112. Now list your home directory. You should see the directory you just created (among other files in your home directory).
All programs for this class will reside in this new directory. The program files for each lab or assignment should be contained in a separate directory. For this lab, create a directory called Lab1 in the CSC112 directory. To accomplish this, first cd to the CSC112 directory. Then create the Lab1 directory. Copy and Compile A C++ program (source) file called hello.cpp is located on the course web page. To obtain the file, use firefox for a web browser (use the shortcut for firefox on the menu bar, or type firefox & at the Terminal prompt to start). Save this program to your Lab1 directory. After you have saved the file, compile and execute the program using the instructions below. We will use the gnu C++ compiler for this course. The command to compile a program is g++ -o executablename sourcename where sourcename is the C++ program (source) file and executablename is the resulting executable file. For hello.cpp, compile the program so the resulting executable is called hello. The g++ compiler has many more options, which will be introduced in labs as they are needed. To execute the program enter the following at the prompt:./hello Note the./ which indicates to the operating system to look in the current directory [the dot means the current working directory, similar to how the double dots (..) means one directory upwards in the directory structure]. This reason this dot-slash is required is the way that executable programs are searched for in Linux operating systems. We will address this requirement in later programs.
Keep these two files (the source and executable) in your Lab1 directory, as their existence (in your Lab1 directory) will give you credit for this portion of the lab. Programming Assignment (Graded) In this portion of the assignment you will write your own C++ program. To accomplish this you will need to start a text editor. Our Unix environment has a few text editors to choose from the two most appropriate to start with in this course are: gedit - Text editor with a GUI interface. emacs - Text editor with a GUI interface. To start gedit or emacs, enter gedit & or emacs & at the Unix prompt. The & sign tells Unix to run the command in the background. Without using the &, the text editor program would start and take control of your terminal, making it so that you couldn t compile or debug your program without either a) opening another terminal or b) stopping the gedit or emacs program. Program: Light Bulb Packaging For this portion of the lab, create a new file called lab1.cpp in your Lab1 subdirectory. Then write a program that solves the following problem. Be certain you follow the programming points at the end of this section (read the points before starting on your program). The Light Bulb Company is a world leader in manufacturing one watt light bulbs. These fragile light bulbs must be packaged in special containers, ideally with each container filled to capacity to reduce breakage. The four different types of containers and their costs are listed in the following table. Container Name Capacity in Bulbs Cost in Dollars Huge 20 $5.00 Large 15 $3.50 Medium 7 $2.00 Small 1 $0.75 Bulbs costs $1.55 each. The packaging manager needs a program that calculates the minimum number of containers required for any size order (you may assume an integer number of bulbs greater than zero will be input) and total cost (bulbs and packaging). Program Description First prompt the user for the number of light bulbs in the order. Below, the user input is 59.
After reading the number of bulbs in the order, calculate and display the minimum number of containers required, the cost of the bulbs, the cost of the packaging, and the total cost of the order. For 59 bulbs, the output would be Programming Points You must turn-in printed and electronic copies of your programs. This must be done before the assignment due date. Printed copies must be given to Dr. Turkett while electronic copies will be submitted using the blackboard.wfu.edu web site. You must adhere to all of the following points to receive credit for this program. 1. Name your source (C++ program) file lab1.cpp. This file must be located in your CSC112/Lab1 directory. 2. The program must have at least four functions: main inputorder - Get the order information calcorder - Calculate order shipping schedule and cost. outputorder - Print shipment schedule and cost. 3. Selection and repetition structures are not allowed. 4. All prompts and output must appear as described in the previous section. Please note the example output carefully. 5. All code must follow the style guidelines for this course and be documented.
Electronically Submitting Your Program You will use BlackBoard to electronically turn-in programs. Go to the http://blackboard.wfu.edu web-site and check that you are enrolled in the FA2009 Fundamentals of Comp.Science CSC112 Sec A course. Please first create an archived (packaged up) and compressed version of your code by using the following commands. Please be careful in following the syntax described below so that you don t accidentally over-write your programs. Backing up your code first is also always a good idea. Go to your home directory (/home/username) cd Run the tar command, which creates an archive, as follows: tar -cvf lab1.tar CSC112/Lab1/* (This command says to create a tar file called lab1.tar and include in it all files in the CSC112/Lab1 directory. The asterisk means all.) Compress the output tar file using gzip. gzip lab1.tar Submit the lab1.tar.gz file that results under the Assignment 1 section of the Assignments Menu on Blackboard by the due date. More information about BlackBoard is available at http://www.wfu.edu/library/itc/training/blackboard/