CSC 112 Lab 1: Introduction to Unix and C++ Fall 2009

Similar documents
Unix Tutorial Haverford Astronomy 2014/2015

CS Fundamentals of Programming II Fall Very Basic UNIX

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

CS 2400 Laboratory Assignment #1: Exercises in Compilation and the UNIX Programming Environment (100 pts.)

Brief Linux Presentation. July 10th, 2006 Elan Borenstein

History. Terminology. Opening a Terminal. Introduction to the Unix command line GNOME

Using the Zoo Workstations

Unix tutorial. Thanks to Michael Wood-Vasey (UPitt) and Beth Willman (Haverford) for providing Unix tutorials on which this is based.

Lab 1 Introduction to UNIX and C

Linux at the Command Line Don Johnson of BU IS&T

Unix/Linux Operating System. Introduction to Computational Statistics STAT 598G, Fall 2011

CHEM5302 Fall 2015: Introduction to Maestro and the command line

EECS Software Tools. Lab 2 Tutorial: Introduction to UNIX/Linux. Tilemachos Pechlivanoglou

CMSC 104 Lecture 2 by S Lupoli adapted by C Grasso

CMSC 201 Spring 2017 Lab 01 Hello World

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

Some Linux (Unix) Commands that might help you in ENSC351

Session 1: Accessing MUGrid and Command Line Basics

CMSC 201 Spring 2018 Lab 01 Hello World

STA 303 / 1002 Using SAS on CQUEST

Chapter-3. Introduction to Unix: Fundamental Commands

Linux File System and Basic Commands

CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C

Linux hep.wisc.edu

CSC111 Computer Science II

Introduction of Linux

15-122: Principles of Imperative Computation

Systems Programming and Computer Architecture ( ) Exercise Session 01 Data Lab

CSC116: Introduction to Computing - Java

1 Getting Started with Linux.

Computer Architecture Lab 1 (Starting with Linux)

C++ Programming on Linux

INF322 Operating Systems

UoW HPC Quick Start. Information Technology Services University of Wollongong. ( Last updated on October 10, 2011)

Introduction to Unix: Fundamental Commands

CSC116: Introduction to Computing - Java

EE516: Embedded Software Project 1. Setting Up Environment for Projects

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Tutorial 1: Unix Basics

Running Sentaurus on the DOE Network

Introduction to Linux Spring 2014, Section 02, Lecture 3 Jason Tang

5/8/2012. Creating and Changing Directories Chapter 7

Operating Systems and Using Linux. Topics What is an Operating System? Linux Overview Frequently Used Linux Commands

Intermediate Programming, Spring Misha Kazhdan

A Gentle Introduction to CMSC311 labs and CVS Or How I learned to use CVS in CMSC311. William Arbaugh September 2, 2004

CSC116: Introduction to Computing - Java

Introduction to Cygwin Operating Environment

Unix background. COMP9021, Session 2, Using the Terminal application, open an x-term window. You type your commands in an x-term window.

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

Week Overview. Unix file system File types and file naming Basic file system commands: pwd,cd,ls,mkdir,rmdir,mv,cp,rm man pages

User Guide Version 2.0

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

Introduction to Linux for BlueBEAR. January

Lab 1 1 Due Wed., 2 Sept. 2015

Introduction to Unix - Lab Exercise 0

Files

Unit 10. Linux Operating System

Unix File System. Learning command-line navigation of the file system is essential for efficient system usage

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

CSCI 161: Introduction to Programming I Lab 1a: Programming Environment: Linux and Eclipse

Part I. Introduction to Linux

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

Refresher workshop in programming for polytechnic graduates General Java Program Compilation Guide

CS : Computer Programming, Spring 2000 Programming Project #1: Thinking of a Number Due in class Thursday 27 January 2000

Introduction to remote command line Linux. Research Computing Team University of Birmingham

BIOINFORMATICS POST-DIPLOMA PROGRAM SUBJECT OUTLINE Subject Title: OPERATING SYSTEMS AND PROJECT MANAGEMENT Subject Code: BIF713 Subject Description:

For Dr Landau s PHYS8602 course

CS4023 Week04 Lab Exercise

Introduction to Linux

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Lab 1: Introduction to Linux Networking

AMS 200: Working on Linux/Unix Machines

Introduction to the Linux Command Line

You should see something like this, called the prompt :

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

EKT332 COMPUTER NETWORK

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

Linux Survival Guide

Temple University Computer Science Programming Under the Linux Operating System January 2017

EL2310 Scientific Programming

Getting started with Hugs on Linux

THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering

Overview LEARN. History of Linux Linux Architecture Linux File System Linux Access Linux Commands File Permission Editors Conclusion and Questions

Introduction: What is Unix?

Basic Survival UNIX.

COSC UNIX. Textbook. Grading Scheme

Module 10: Unix Primer

Lab 1: Introduction to C, ASCII ART & the Linux Command Line

CSE 391 Lecture 1. introduction to Linux/Unix environment

Introduction to Linux

Lab #1 Installing a System Due Friday, September 6, 2002

Introduction to Unix and Linux. Workshop 1: Directories and Files

Linux/Cygwin Practice Computer Architecture

Short Read Sequencing Analysis Workshop

CS CS Tutorial 2 2 Winter 2018

Lab 1 Introduction to UNIX and C

Basic Shell Commands

Parallel Programming Pre-Assignment. Setting up the Software Environment

Introduction to Linux

Introduction to Linux. Fundamentals of Computer Science

Transcription:

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/