Today - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

Today

Description:

How many lines are in /etc/passwd ... Best programming style works on both ... Make sure you pick up the last item. Fall 2003 CS 325 Class Notes. Page 30 ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 47
Provided by: davidc73
Category:
Tags: best | lines | pick | today | up

less

Transcript and Presenter's Notes

Title: Today


1
Lecture 4
  • Today
  • Project comments
  • Unix
  • Iterator for output
  • Homework
  • Project One is due at 9pm, Wednesday
  • Go to CS Supplemental Instruction on Thursday
    for extension until Monday 9pm. Must turn in
    current work on Wed.

2
Class Exercises
  • Re-write the List Iterator code using templates.

3
Brief Background on Unix
  • Originated in Bell Labs (ATT)
  • Now known as Lucent
  • C also came out of these labs
  • enables groups of people to work together easily
  • UC-Berkeley
  • modified, really promoted within academic arena
  • Sun popularized w/Unix-based workstations
  • Linux, public-domain version of Unix
  • Runs on any platform
  • Source code is free

4
Our Unix environment
  • Sun Ultra Enterprise 6000
  • Solaris 7
  • Solaris 2.6 (Suns Unix)
  • 8 UltraSPARC CPUs
  • 400 MHz,
  • 8 GByte of real memory.
  • 2GByte of swap space.
  • Each CPU
  • 16KB on-board cache for instructions,
  • 16KB on-board cache for data and
  • 8MB external cache.
  • Programs can run on multiple CPUs.

5
Accessing this environment
  • Two general techniques exist
  • If at a console, X-windows is preferred
  • Windowing system similar to Mac or Windows
  • variants of windowing system developed at Xerox
    PARC
  • If on another system, telnet to log on
  • Start/Run/telnet bama.ua.edu
  • LOG ONTO BAMA NOW(telnet bama.ua.edu)

6
Learning about Unix
  • Basic commands
  • File system structure
  • Networking Mail
  • Editors
  • Compilers
  • Unix is case-sensitive !

7
Basic Commands
  • who displays users logged onto the system
  • uptime basic system load/status information
  • ls lists files in your current directory
  • -C output is in columns-F displays info on the
    type of file ( / _at_ )-a displays system
    (hidden) files-R recursive, lists from this
    directory on down
  • cp copy, cp source destination
  • mv move, mv source destination
  • rm delete, rm filename

8
Basic Commands (continued)
  • cat displays a file, cat filename
  • more displays file one screen at a time
  • wc counts lines, words, chars, wc filename
  • grep finds string in file, grep string file(s)
  • pipe, output from command sent to another
  • ls wc
  • wildcard, matches anything
  • rm
  • ls a b c

9
File System Structure
  • Tree-based directory structure
  • / is the root of the tree (forward-slash)
  • represents your home directory
  • . Represents your current working directory
  • pwd give the directory you are currently in
  • cd change directory (.. is parent directory)
  • Can give either an absolute or relative directory
  • Absolute, cd /fs/truitt (full path from root)
  • Relative, cd ../../tmp (relative to current
    location)
  • To go back, cd returns to previous directory
  • To go up, cd .. go to parent directory

10
Networking, Printing and Mail
  • host gives IP addresses, host cs.ua.edu
  • telnet logs into remote system, telnet hostname
  • finger find out info about user, finger cordes
  • ftp file transfer protocol, move files around
  • mkdir create directory
  • rmdir remove directory
  • lpr prints a file, lpr P printer filename
  • lpstat p all D list of all printers
  • pine to read/send mail while logged in

11
Class Exercises
  • Answer as many of the following questions as you
    can
  • How many users are on the system
  • How many files in your home directory
  • How many lines are in /etc/passwd
  • Is there a difference in more /etc/passwd and
    cat /etc/passwd more
  • What is the IP address for bama.ua.edu
  • What is the IP address for cs.ua.edu
  • Where is the closest printer to EE 111
  • How many system file exist in your home
    directory
  • What does the ps command do? Try ps ef
    ps ef grep userid
  • What is the difference in the date and time
    commands?
  • Look at the output of ls l. Use the man page
    for ls to explain the output
  • What are all the top-level directories in the
    Unix file system
  • Identify four Unix commands that the table next
    to you does not know

12
Editors
  • pico editor similar to the pine mail editor
  • Single-mode editor
  • Always in input mode, special commands to move
  • pico filename, CTRL-G for help
  • Easy to learn
  • vi another editor
  • Two-mode editor
  • Either in input mode or edit mode, must toggle
  • vi filename, help via man page, type man vi
  • Takes time to learn but has useful features

13
vi Basics
  • Two Modes
  • Command Mode
  • For entering commands
  • i, a, o, I, A, O, r, /ltsearchstrgt, ?ltsearchstrgt
  • yy, p, P, ., q!, w, wq
  • cw, dw, dd, D
  • And many more
  • Insert Mode
  • For entering text
  • esc returns to Command Mode

14
vi commands
  • i, a, o, I, A, O, r, R
  • Various methods of entering insert mode
  • i, a, o insert before/after/under curser
  • I, A, O insert before/after/over line
  • r, R replace char, type over
  • /ltsearchstrgt, ?ltsearchstrgt
  • Searching forward and backwards
  • Use n to repeat search (find next)

15
vi commands
  • yy
  • Yank (copy) n lines of text
  • p, P
  • Push yanked lines of text below/above
  • . (Dot)
  • Repeat last command (very useful)
  • q!, w, wq
  • Quit with saving (sometimes useful)
  • Write without exiting
  • Write and exit

16
vi commands
  • cw
  • Change N words
  • dw
  • Delete N words
  • dd
  • Delete N lines
  • D
  • Delete rest of line

17
Compilers
  • CC Sun C compiler
  • g GNU C compiler
  • We will use the GNU C compiler
  • Create a source file (pico or vi), extension of
    .cpp
  • g filename to compile the file
  • ./a.out to execute the file (executable in a.out)
  • g -o executable filename (if you dislike a.out)

18
Class Exercises
  • Write hello world using the GNU C compiler
  • Write a program (using GNU C) that declares a
    template class for a dynamic linked list and then
    adds five integers to an integer instantiation of
    the list

19
Project 2
  • Templates and Iterators for BinaryTree

20
Project Description
  • Created a templated BinaryTree class
  • Constructor
  • add()
  • Create templated iterators for BinaryTreeltTgt
  • PreorderBinaryTreeIterator
  • InorderBinaryTreeIterator
  • PostorderBinaryTreeIterator
  • LevelOrderBinaryTreeIterator

21
Iterator methods
  • Constructor
  • Initialize iterator -- set current to first
    element
  • bool hasMoreElements()
  • return false if no more elements
  • int getValue()
  • return current data (return type wont be int)
  • bool next()
  • return false if cannot advance

22
Binary Tree Traversals
23
Iterator Logic
  • Preorder (N L R)
  • Initpush root node
  • while top is node loop
  • current pop
  • push current.right
  • push current.left
  • push current.data
  • end loop
  • return pop
  • Inorder (L N R)
  • Initpush root node
  • while top is node loop
  • current pop
  • push current.right
  • push current.data
  • push current.left
  • end loop
  • return pop

24
More Iterator Logic
  • Postorder (L R N)
  • Initpush root node
  • while top is node loop
  • current pop
  • push current.data
  • push current.right
  • push current.left
  • end loop
  • return pop
  • LevelOrder
  • Initnq root node
  • while front is node loop
  • current dq
  • nq current.data
  • nq current.left
  • nq current.right
  • end loop
  • return dq

25
Files on the web
  • Project2Main.cpp
  • Sample main for non template classes
  • Project2.h
  • Compile Stubbs for non template binary tree and
    iterators

26
Project Comments
  • Must run on Unix with GNU C
  • Will deduct if runs only on PC grasp
  • May require small changes
  • Best programming style works on both
  • General format
  • Template class for BinaryTree
  • Make sure you add elements properly
  • Template classes for Iterators (4)
  • One File For Compilation
  • Project2.h
  • May include other files through Project2.h

27
Boundry Conditions
  • It new TreeIterator(bt)
  • while (it.hasMoreElements()
  • xit.getValue()
  • //Do Something
  • it.next()

It new TreeIterator(bt) while
(it.next()) xit.getValue() //Do Something
28
Boundry Conditions
It new TreeIterator(bt) it.next() it.getValue()
It new TreeIterator(bt) it.getValue()
29
Project Notes
  • The first next() precedes getValue()
  • Advance to first item
  • The first next() follows getValue()
  • Advance to second item
  • hasMoreElements() and getValue()
  • Make sure you pick up the last item

30
Useful string functions
  • The operator concatenates two strings
  • Compare strings using lt, lt, , gt, gt
  • strn references char n, so does str.at(n)
  • Str.length() returns the length of the string
  • str.find(substring) finds substring
  • str.find(char) finds character
  • find( ) returns stringnpos if not found
  • int fooa.find('z')
  • if (foo stringnpos) not found

31
Moving files to/from Unix
  • Moving files between a PC and Unix
  • Use ftp(file transfer protocol)
  • PC must be connected to the network
  • C\gtftp bama.ua.edu
  • Connected to bama.ua.edu.
  • 220-
  • 220- Use of this system (lines suppressed here)
  • 220 bama.ua.edu FTP server .
  • User (bama.ua.edu(none)) cordes
  • 331 Password required for cordes.
  • Password ltenter password heregt
  • 230 User cordes logged in.
  • ftpgt put hello.C
  • 200 PORT command successful.
  • 150 Opening ASCII mode data connection for
    hello.C.
  • 226 Transfer complete.
  • ftp 85 bytes sent in 0.00Seconds
    85000.00Kbytes/sec.
  • ftpgt quit

32
Moving files to/from Unix (cont)
  • Other ftp commands
  • dir, lists all the files on the remote system
  • cd xxx, changes directory on the remote
    system(also have lcd to change local directory)
  • get xxx, gets a file from the remote system
  • binary, toggles to binary mode (non-ASCII files)
  • mput .cpp, copies all your cpp files to remote
    sys(also have mget to retrieve files)
  • help, gives list of commands

33
Windows version
  • Most pcs have a windows version of ftp
  • Start
  • Programs
  • WS_FTP (usually)
  • WS_FPT
  • Beside host name/Address type bama.ua.edu
  • Enter your ID and password in appropriate spots
  • Click OK
  • This is a much easier way to ftp, however, be
    familiar with Unix version as well

34
Class Exercises (short)
  • Find a C file on your local PC
  • Copy it to your account on bama.ua.edu
  • Copy a file from your bama account to your local
    PC

35
Unix File Permissions
  • Each file in Unix has three levels of security
  • What the user can do to it
  • What people in the users group can do to it
  • What everyone else can do with it
  • The ls command can display security
  • /fs/cordes ls -l
  • total 10
  • drwxr-xr-x 2 cordes facstaff 512 Aug 30 1050
    cs325
  • drwx------ 2 cordes facstaff 512 Aug 30 0510
    foo
  • -rw-r--r-- 1 cordes facstaff 81 Sep 3 0820
    hello.C
  • drwx------ 2 cordes facstaff 512 Aug 30 0937
    mail
  • drwxr-xr-x 2 cordes facstaff 024 Mar 21 1999
    networks
  • ls l shows files and directories and gives extra
    information such as permissions, userid, group,
    last modified date

36
Unix File Permissions (cont)
  • /fs/cordes ls -l
  • total 10
  • drwxr-xr-x 2 cordes facstaff 512 Aug 30 1050
    cs325
  • drwx------ 2 cordes facstaff 512 Aug 30 0510
    foo
  • -rw-r--r-- 1 cordes facstaff 81 Sep 3 0820
    hello.C
  • drwx------ 2 cordes facstaff 512 Aug 30 0937
    mail
  • drwxr-xr-x 2 cordes facstaff 024 Mar 21 1999
    networks

permissions user group last-mod-date filename
37
Unix File Permissions (cont)
  • -rw-r--r-- 1 cordes facstaff 81 Sep 3 0820
    hello.C
  • - r w r - - r - -
  • indicates this is a regular file, not a
    directory
  • r w indicates the user can read write this
    file
  • r indicates the group can read this file
  • r indicates that others can read this file

38
Changing Permissions
  • chmod ltpermissionsgt filename
  • Permissions consist of who /- what
  • Who is u (user), g (group), o (others)
  • What is r (read), w (write), x (execute)
  • gives permission
  • - takes permission
  • Examples
  • chmod u-w hello.C (user cannot write)
  • chmod gor hello.C (group others can read)
  • chmod ar hello.C (a is short for u, g, o)
  • man chmod talks about changing permissions in
    terms of octal numbers

39
Class Exercises
  • Type ls l in your home directory. Explain
    what permissions are set for each file.
  • The file .plan in your home directory can tell
    information about you (what you do). In order
    for others to read this file, they need to be
    able to execute your home directory (.) and also
    read your .plan file. Set it up so that other
    people can finger userid and see your .plan.

40
Unix requirements for 325
  • Basic manipulation of your files on Unix
  • Create sub-directories for organization, etc
  • Edit and compile C files
  • Some projects are done on Unix
  • Move files between PCs and Unix

41
Unix Directories
  • mkdir
  • Creates directory
  • rmdir
  • Removes directory (must be empty)

42
Unix apropos
  • apropos keyword
  • Searches on keyword for relevant info
  • Useful when you cannot use man (looking for
    command)

43
Class Exercises
  • Teams new to Unix
  • Editors how do you
  • Copy a line
  • Delete a line
  • Give the command to
  • Rename hello.cpp to hello.C
  • Copy (to your home dir) hello.cpp from
    /st3/yessi001/cs325
  • Mail hello.cpp to yourself
  • Teams that know Unix
  • Give a one-line command (you can use pipes) to
  • Find the home directory of the user cordes
  • Print your last name in hex (and octal)
  • Print the first name of all the users on the
    system with the last name Smith
  • Convert the contents of hello.C to uppercase

44
More practice with iterators
  • Can use iterators to build output operators for a
    given data structure
  • Dont need to know anything about internal
    representation to output the data structure
  • Can define an overloaded output operator and then
    use the iterator within that function to print
    out the data structure
  • ostream operatorltlt(ostream os, const
    Stackltintgt theStack)
  • // need code here to print out the stack
  • return os

45
Output operator
  • Notes
  • This operator is not a friend of Node, Stack, or
    StackIter
  • We are simply using an instance of the StackIter
    class to traverse the list and print.
  • We will not directly access any private data
    members of any class, we will access them via
    public class methods
  • Lets look at the code using the iterator
  • template ltclass Tgt
  • ostream operatorltlt(ostream os, const StackltTgt
    theStack)
  • StackIterltTgt it (theStack)
  • while (!it.done())
  • os ltlt it.get() ltlt
  • it.next()
  • return os

46
Class Exercises
  • The file /st3/yessi001/cs325/list.cpp on
    bama.ua.edu contains a List class (using
    templates). Add an output operator for list
    using the iterator class so that you can print
    out the list.
Write a Comment
User Comments (0)
About PowerShow.com