Transcript Lecture 3

CS 245: Introduction to Computer
Organization, Operating Systems, and
Networks
Lecture 3: More on Computer Organization and
Data Representation
John Hurley
Cal State LA
Flip-flops
• Consider the following circuit:
Invalid!
University of Queensland
2
Memory
• A flip-flop holds a single bit of memory
– The bit “flip-flops” between the two NAND gates
• In reality, flip-flops are a bit more
complicated
– Have 5 (or so) logic gates (transistors) per flip-flop
• Consider a 1 Gb memory chip
– 1 Gb = 8,589,934,592 bits of memory
– That’s about 43 million transistors!
• In reality, those transistors are split into 9 ICs
of about 5 million transistors each
3
Memory
k x m array of stored bits; k is usually 2n, ie a
power of 2, and
0000
0001
m is usually a multiple of 8
0010
Address
0011
0100
0101
0110
– unique (n-bit) identifier of location1101
1110
– range from 0 to k
1111
Contents
– m-bit value stored in location
00101101
•
•
•
10100010
The Memory Hierarchy
Most systems have several
levels of memory
 Going down the
hierarchy:
 decreasing cost per bit
 increasing capacity
 increasing access time
 decreasing frequency of
access to the memory by the
processor
From lecture by Dr. Mary Ellen Weiskop
P
E
R
F
O
R
M
A
N
C
E
Registers
Cache
RAM
SSD (Solid State Drive)
Hard Drive
CD ROM/ DVD
E
X
P
E
N
S
E
Storage-Device Hierarchy
Silberschatz, Galvin, and Gagne, 2013
Placement of Cache in a Computer
System
• The locality principle: a recently referenced memory location is
likely to be referenced again (temporal locality); a neighbor of a
recently referenced memory location is likely to be referenced
(spatial locality).
Direct Memory Access Structure
• Used for high-speed I/O devices able
to transmit information at close to
memory speeds
• Device controller transfers blocks of
data from buffer storage directly to
main memory without CPU
intervention
• Only one interrupt is generated per
block, rather than the one interrupt
per byte
Silberschatz, Galvin, and Gagne, 2013
How a Modern Computer Works
A von Neumann architecture
Silberschatz, Galvin, and Gagne, 2013
Twos-Complement
• Computers usually use twos-complement notation for internal storage of
integer values. This system has some important benefits in implementing
arithmetic operations.
• A nonnegative number is represented in base 2 with a leading 0:
0000 decimal 0
0001 decimal 1
0010 decimal 2
0011 decimal 3
0100 decimal 4
• The twos complement of an N-bit number is defined as the complement with
respect to 2N; in other words, it is the result of subtracting the number from
2N, which in binary is one followed by N zeroes.
• The easiest way to calculate the twos complement of a number is to take the
absolute value, switch the value of each bit, and add 1
1111 decimal -1
1100 decimal -4
// note the carry in the binary addition
• To convert a negative twos-complement number to regular base 2, subtract 1,
reverse the bits, and remember the sign
10
Why Twos Complement?
Twos Complement may seem counterintuitive, but it makes arithmetic easier
for the CPU.
With signed numbers, we would have to check both signs before doing
addition or subtraction. With Twos Complement, we will get the correct
answer without doing this:
00000001
decimal 1
+11111111
decimal -1
00000000
If there are more than 8 bits, the 1s keep extending
to the left. Thus the carry is lost, no matter how
many bits we use
00000001 - (11111111) implemented as
00000001 + 00000001 = 00000010
Why Twos Complement?
• Twos Complement has only one value for zero
• Compared to Sign-And-Value, this reduces
confusion and also allows the representation to
cover one additional value, compared to just
having a sign bit.
POS + NEG → POS Answer
Take the 2’s complement of the negative number and
use regular binary addition.
9
+ (-5)
4


00001001
+ 11111011
1]00000100
8th Bit = 0: Answer is Positive
Disregard 9th Bit
http://www.aitestamford.org
00000101

11111010
+1
11111011
2’s
Complement
Process
13
POS + NEG → NEG Answer
Take the 2’s complement of the negative number and
use regular binary addition.
(-9)
+ 5
-4


11110111
+ 00000101
11111100
8th Bit = 1: Answer is Negative
To Check:
Perform 2’s
Complement
On Answer
11111100

00000011
+1
00000100
00001001

11110110
+1
11110111
2’s
Complement
Process
14
http://www.aitestamford.org
NEG + NEG → NEG Answer
Take the 2’s complement of both negative numbers and
use regular binary addition.
(-9)
+ (-5)
-14



11110111
+ 11111011
1]11110010
2’s Complement
Numbers, See
Conversion Process
In Previous Slides
8th Bit = 1: Answer is Negative
Disregard 9th Bit
To Check:
Perform 2’s
Complement
On Answer
11110010

00001101
+1
00001110
http://www.aitestamford.org
15
Fixed Point Numbers
• Using only two digits of precision for signed base 10 numbers, the
range (interval between lowest and highest numbers) is
[-99, +99] and the precision (distance between successive numbers) is
1.
• The maximum error, which is the difference between the value of a
real number and the closest representable number, is 1/2 the
precision. For this case, the maximum error is 1/2  1 = 0.5.
• If we choose a = 70, b = 40, and c = -30, then a + (b + c) = 80 (which
is correct) but (a + b) + c = -30 which is incorrect. The problem is that
(a + b) is +110 for this example, which exceeds the range of +99, and
so only the rightmost two digits (+10) are retained in the intermediate
result. This is a problem that we need to keep in mind when
representing real numbers in a finite representation.
Fractional Binary Numbers
2i
2i–1
4
2
1
•••
bi bi–1 • • •
b2 b1 b0 . b–1 b–2 b–3 • • •
1/2
1/4
1/8
•••
• Representation
b–j
2–j
– Bits to right of “binary point” represent fractional powers of 2
i
– Represents rational number:
k
 bk 2
k  j
http://www.cs.cmu.edu/afs/cs/academic/class/15213-f02/lectures/class04.ppt
Weighted Position Code
• The base, or radix of a number system defines the range of
possible values that a digit may have: 0 – 9 for decimal; 0,1 for binary.
• The general form for determining the decimal value of a number is
given by:
Example:
541.2510 = 5  102 + 4  101 + 1  100 + 2  10-1 + 5  10-2
= (500)10 + (40)10 + (1)10 + (2/10)10 + (5/100)10
= (541.25)10
Base Conversion with the Remainder Method
•Example: Convert 23.37510 to base 2. Start by
converting the integer portion:
Base Conversion with the Multiplication Method
• Now, convert the fraction:
• Putting it all together, 23.37510 = 10111.0112
Nonterminating Base 2 Fraction
• We can’t always convert a terminating base 10 fraction into an
equivalent terminating base 2 fraction:
Signed Fixed Point Numbers
•The number of values we can represent depends on how many bits
we use.
• For an 8-bit number, there are 28 = 256 possible bit patterns. These
bit patterns can represent negative numbers if we choose to assign
bit patterns to numbers in this way. We can assign half of the bit
patterns to negative numbers and half of the bit patterns to positive
numbers.
Floating-Point Representation
• Floating-point numbers allow an arbitrary number of
decimal places to the right of the decimal point.
– For example: 0.5  0.25 = 0.125
• They are often expressed in scientific notation.
– For example:
0.125 = 1.25  10-1
5,000,000 = 5.0  106
• Of course, computers use base 2 internally
oncourse.iu.edu/access/content/user/rfinkbin/C311/FP-Representation.ppt
Rounding and Errors
• No matter how many bits we use in a floating-point
representation, our model must be finite.
• The real number system is, of course, infinite, so our
models can give nothing more than an approximation
of a real value.
• At some point, every model breaks down, introducing
errors into our calculations.
• By using a greater number of bits in our model, we
can reduce these errors, but we can never totally
eliminate them.
oncourse.iu.edu/access/content/user/rfinkbin/C311/FP-Representation.ppt
Rounding and Errors
• When discussing floating-point numbers, it is
important to understand the terms range,
precision, and accuracy.
• The range of a numeric integer format is the
difference between the largest and smallest
values that can be expressed.
• Accuracy refers to how closely a numeric
representation approximates a true value.
• The precision of a number indicates how much
information we have about a value
oncourse.iu.edu/access/content/user/rfinkbin/C311/FP-Representation.ppt
Floating-Point Representation
• Internal representation of floating points types
uses a system similar to scientific notation
• These types store values with limited precision,
essentially rounding to the closest value they can
hold
• This is why double doesn’t just hold a larger
range of values than float, it also provides greater
precision
• It is also why testing floating point values using
the equality operator (==) is unsafe
26
Floating-Point Representation
• Numbers written in scientific notation have three
components:
oncourse.iu.edu/access/content/user/rfinkbin/C311/FP-Representation.ppt
Floating-Point Representation
• Computer representation of a floating-point
number consists of three fixed-size fields:
• This is the standard arrangement of these fields.
Note: Although “significand” and “mantissa” do not technically mean the same
thing, we use the term “significand” to refer to the fractional part of a floating point
number.
oncourse.iu.edu/access/content/user/rfinkbin/C311/FP-Representation.ppt
Floating-Point Representation
• The one-bit sign field is the sign of the stored value.
• The size of the exponent field determines the range
of values that can be represented.
• The size of the significand determines the precision
of the representation.
oncourse.iu.edu/access/content/user/rfinkbin/C311/FP-Representation.ppt
Floating-Point Standards
• The IEEE has established a standard for floating-point
numbers
• The IEEE-754 single precision floating point standard
uses an 8-bit exponent (with a bias of 127) and a 23-bit
significand.
• The IEEE-754 double precision standard uses an 11-bit
exponent (with a bias of 1023) and a 52-bit significand.
• These standards are implemented in the hardware in
modern computers.
oncourse.iu.edu/access/content/user/rfinkbin/C311/FP-Representation.ppt
Floating-Point Representation
• In both the IEEE single-precision and doubleprecision floating-point standard, the significand
has an implied 1 to the LEFT of the radix point.
– The format for a significand using the IEEE format
is: 1.xxx…
– For example, 1.001 x 22 = 4.5 decimal. The 1 does
not need to be listed in the significand (the
significand would include only 001). Note that
.001 binary * 22 is decimal 0.5
oncourse.iu.edu/access/content/user/rfinkbin/C311/FP-Representation.ppt
“Normalized” Numeric Values
• Condition
– exp  000…0 and exp  111…1
• Exponent coded as biased value
E = Exp – Bias
• Exp : unsigned value denoted by exp
• Bias : Bias value
–Single precision: 127 (Exp: 1…254, E: -126…127)
–Double precision: 1023 (Exp: 1…2046, E: -1022…1023)
–in general: Bias = 2e-1 - 1, where e is number of exponent bits
• The bias allows us to show fractions without using negative
exponents
http://www.cs.cmu.edu/afs/cs/academic/class/15213-f02/lectures/class04.ppt
“Normalized” Numeric Values
• Significand coded with implied leading 1
M = 1.xxx…x2
• xxx…x: bits of frac
• Minimum when 000…0 (M = 1.0)
• Maximum when 111…1
• This gets us one implied bit for “free”
http://www.cs.cmu.edu/afs/cs/academic/class/15213-f02/lectures/class04.ppt
Normalized Encoding
Example: Converting to IEEE 754 Form
Put 0.085 in single-precision format
1.The first step is to look at the sign of the number.
Because 0.085 is positive, the sign bit =0.
(-1)0 = 1.
2.Write 0.085 in base-2 scientific notation.
This means that we must factor it into a number in the range [1 <= n < 2] and a power of 2.
0.085 = (-1)0 * (1+fraction) * 2 power,
0.085 / 2power = (1+fraction).
or:
So we can divide 0.085 by a power of 2 to get the (1 + fraction).
0.085 / 2-1 = 0.17
0.085 / 2-2 = 0.34
0.085 / 2-3 = 0.68
0.085 / 2-4 = 1.36
Therefore, 0.085 = 1.36 * 2-4
Find the exponent.
The power of 2 is -4, and the bias for the single-precision format is 127. This means that the exponent = 123 ten, or
01111011bin
Dr. Arun Somani, http://class.ece.iastate.edu/arun/Cpre305/ieee754/ie5.html
Normalized Encoding
4.Write the fraction in binary form
The fraction = 0.36 . Unfortunately, this is not a "pretty" number, like those shown in the book. The best we can do is to approximate the value. Singleprecision format allows 23 bits for the fraction.
Binary fractions look like this:
0.1 = (1/2) = 2-1
0.01 = (1/4) = 2-2
0.001 = (1/8) = 2-3
To approximate 0.36, we can say:
0.36 = (0/2) + (1/4) + (0/8) + (1/16) + (1/32) +...
0.36 = 2-2 + 2-4 + 2-5+...
0.36ten ~ 0.01011100001010001111011bin .
The binary string we need is: 01011100001010001111011.
It's important to notice that you will not get 0.36 exactly. This is why floating-point numbers have error when you put them in IEEE 754 format.
5.Now put the binary strings in the correct order 1 bit for the sign, followed by 8 bits for the exponent, and 23 bits for the fraction. The answer is:
Sign
Exponent
Fraction
Decimal
0
123
0.36
Binary
0
01111011
01011100001010
001111011
Dr. Arun Somani, http://class.ece.iastate.edu/arun/Cpre305/ieee754/ie5.html
Normalized Encoding
• Example: Express -3.75 as a floating point number
using IEEE single precision.
• First, let’s normalize according to IEEE rules:
– 3.75 = -11.112 = -1.111 x 21
– The bias is 127, so we add 127 + 1 = 128 (this is our
exponent)
– The first 1 in the significand is implied, so we have:
2n in base 2 is 1 followed by n zeros
(implied)
– Since we have an implied 1 in the significand, this equates
to
-(1).1112 x 2 (128 – 127) = -1.1112 x 21 = -11.112 = -3.75.
oncourse.iu.edu/access/content/user/rfinkbin/C311/FP-Representation.ppt
Normalized Decoding
Example: Convert the following single-precision IEEE 754 number into a floating-point decimal value.
1 10000001 10110011001100110011010
•
First, put the bits in three groups.
Bit 31 (the leftmost bit) show the sign of the number.
Bits 23-30 (the next 8 bits) are the exponent.
Bits 0-22 (on the right) give the fraction
•
Now, look at the sign bit.
If this bit is a 1, the number is negative.
If it is 0, the number is positive.
This bit is 1, so the number is negative.
•
Get the exponent and the correct bias.
The exponent is simply a positive binary number.
10000001bin = 129ten
Remember that we will have to subtract a bias from this exponent to find the power of 2. Since this is a
single-precision number, the bias is 127.
Dr. Arun Somani, http://class.ece.iastate.edu/arun/Cpre305/ieee754/ie5.html
Normalized Decoding
•
Convert the fraction string into base ten.
This is the trickiest step. The binary string represents a fraction, so conversion is a little different.
Binary fractions look like this:
0.1 = (1/2) = 2-1
0.01 = (1/4) = 2-2
0.001 = (1/8) = 2-3
So, for this example, we multiply each digit by the corresponding power of 2:
0.10110011001100110011010bin = 1*2-1+ 0*2-2 + 1*2-3 + 1*2-4 + 0*2-5 + 0 * 2-6 + ...
0.10110011001100110011010bin = 1/2 + 1/8 + 1/16 + ...
Note that this number is just an approximation on some decimal number. There will most likely be some
error. In this case, the fraction is about 0.7000000476837158.
•
This is all the information we need. We can put these numbers in the expression:
(-1)sign bit * (1+fraction) * 2 exponent - bias
= (-1)1 * (1.7000000476837158) * 2 129-127
= -6.8
The answer is approximately -6.8.
Dr. Arun Somani, http://class.ece.iastate.edu/arun/Cpre305/ieee754/ie5.html
Denormalized Values
• Condition
– exp = 000…0
• Value
– Exponent value E = –Bias + 1 = -126
– Significand value M = 0.xxx…x2
• xxx…x: bits of frac
• no implied 1
• allows smaller magnitude at cost of precision
• Special Cases
– exp = 000…0, frac = 000…0
• Represents value 0
• Note that have distinct values +0 and –0
– exp = 000…0, frac  000…0
• Numbers very close to 0.0
http://www.cs.cmu.edu/afs/cs/academic/class/15213-f02/lectures/class04.ppt
Summary of Floating Point
Real Number Encodings

NaN
-Normalized
+Denorm
-Denorm
0
+Normalized
+
+0
http://www.cs.cmu.edu/afs/cs/academic/class/15213-f02/lectures/class04.ppt
NaN
FP Ranges
• For a 32 bit number
– 8 bit exponent
– +/- 2256  1.5 x 1077
• Accuracy
– The effect of changing lsb of significand
– 23 bit significand 2-23  1.2 x 10-7
– About 6 decimal places
oncourse.iu.edu/access/content/user/rfinkbin/C311/FP-Representation.ppt
Floating-Point Representation
• Using the IEEE-754 single precision floating point
standard:
– An exponent of 255 indicates a special value.
• If the significand is zero, the value is  infinity.
• If the significand is nonzero, the value is NaN, “not a
number,” often used to flag an error condition.
• Using the double precision standard:
– The “special” exponent value for a double
precision number is 2047, instead of the 255 used
by the single precision standard.
oncourse.iu.edu/access/content/user/rfinkbin/C311/FP-Representation.ppt
Character Codes
Computers can’t store character data directly; they convert it to bits. There are
several code systems that assign numeric values to characters. We usually use the
decimal values, but the compiler converts these to binary.
ASCII
•developed in the early 1960s, when memory was much more expensive
than it is now
•128 (27) possible characters
•The first 33 are control characters, originally used to control teletypes
•Unicode
•Used in Java
•Superset of ASCII
•65536 values
•First 128 are the same as ASCII
•Adds Greek, Arabic, etc. alphabets, many other characters.
Character Codes
Memory address
Memory content
.
.
.
.
.
.
2000
01001010
Encoding for character ‘J’
2001
01100001
Encoding for character ‘a’
2002
01110110
Encoding for character ‘v’
2003
01100001
Encoding for character ‘a’
2004
00000011
Encoding for number 3
ASCII Character Code
• ASCII is a 7-bit code,
commonly stored in 8bit bytes
Character Codes
public class ASCIIUnicodeDemo{
public static void main(String[] args){
for(int charNum = 0; charNum < 128; charNum++){
System.out.println("Character " + charNum + " is " +
(char) charNum);
}
}
}
Why do we get a blank line in the output after char #10 and
before char #11?
Common Sizes for Data Types
• A byte is composed of 8 bits. Two nibbles make up a byte.
• Halfwords, words, doublewords, and quadwords are composed of bytes as
shown below:
Instruction
The instruction is the fundamental unit of work.
Specifies two things:
– opcode: operation to be performed
– operands: data/locations to be used for operation
An instruction is encoded as a sequence of bits.
(Just like data!)
– Often, but not always, instructions have a fixed length,
such as 32 bits.
– Control unit interprets instruction:
generates sequence of control signals to carry out
operation.
– Operation is either executed completely, or not at all.
A computer’s instructions and their formats is known as its Instruction Set
Architecture (ISA).
The Instruction Set Architecture
• The Instruction Set Architecture (ISA) view of a machine corresponds to
the machine and assembly language levels.
• A compiler translates a high level language, which is architecture
independent, into assembly language, which is architecture dependent.
• An assembler translates assembly language programs into executable
binary codes.
• The binary codes are executed directly by the target machine
• A key aspect of the instruction set architecture is the set of opcodes, the
machine language operations that the computer can actually perform
Abstract View of a CPU
• The CPU consists of a data section containing registers and an ALU, and
a control section, which interprets instructions and effects register
transfers. The data section is also known as the datapath. Registers
store small amounts of data in the CPU, offering very fast access times.
The System Bus Model of a Computer
System, Revisited
• A compiled program is copied from a hard disk to the memory. The CPU
reads instructions and data from the memory, executes the instructions,
and stores the results back into the memory.
Clock
The clock is a signal that keeps the control unit moving.
– At each clock “tick,” control unit moves to the next
machine cycle -- may be next instruction or
next phase of current instruction.
Clock generator circuit:
– Based on crystal oscillator
– Generates regular sequence of “0” and “1” logic levels
– Clock cycle (or machine cycle) -- rising edge to rising edge
“1”
“0”
Machine
Cycle
time
The Fetch-Execute Cycle
• The steps that the control unit carries out in executing a program are:
(1) Fetch the next instruction to be executed from memory.
(2) Decode the opcode.
(3) Read operand(s) from main memory, if any.
(4) Execute the instruction and store results.
(5) Go to step 1.
This is known as the fetch-execute cycle.
An Example Datapath
• The datapath is made up of a collection of registers known as the register
file and the arithmetic and logic unit (ALU).
Virtual Machines
• You are already familiar with the Java Virtual Machine,
which takes Java bytecode as its assembly language
and interprets it at runtime.
• Virtual machines in general use resources (CPU time,
memory) supplied by a host OS. At higher levels, it
appears as though those resources were the lowerlevel components of a physical machine.
• VMs are used for many purposes
– separate software from the host system for security
purposes; intrusions or malware can be isolated within the
VM
– abstract away platform specifics so developers don’t have
to think about them, as with the JVM, Python, and .NET
– provide a uniform environment for multiple developers
who are actually using different OSs and hardware
55
Virtual Machine
App1 App2 App3
System calls
I/O instructions
Linux
Windows NT
FreeBSD
VMware
VMware
VMware
Calls to simulate I/O
“Real” I/O instructions
Linux
Bare hardware
• First widely used in VM/370 with CMS
• Available today in VMware
– Allows users to run any x86-based OS on top of Linux or NT
• “Guest” OS can crash without harming underlying OS
– Only virtual machine fails—rest of underlying OS is fine
• “Guest” OS can even use raw hardware
– Virtual machine keeps things separated
Ahmed Amer, Ethan L. Miller and Scott A. Brandt, University of Pittsburgh
Digital Ocean VM
• The rest of this course will require you to use a VM on
a Digital Ocean (www.digitalocean.com) account.
• Use the $10/month level
• The final exam for this course will be on June 6, so you
should be able to keep your expenditure to $20.
• Github offers a student package that may save you
money. I have not tried it.
• When you set up the account, you set up a "droplet"
and choose an operating system for it. Choose Fedora
23.
• Do it this way even if you have another way to run
Fedora.
57
SSH and SFTP Clients
• To work with the VM from Windows machines, you will
need to use a Secure Shell client and an SFTP client.
• If you like particular SSH and SFTP clients you have
already used, use them. Otherwise, try PUTTY and
PSFTP or SSH Secure Shell and SSH Secure FTP. These
are available on the lab computers and are easy to
install at home or on a USB drive.
• Linux and Mac OSX systems already have SSH and SFTP
clients built in.
• You can easily find instructions for using these tools
online. They are not hard to learn.
58
Basic *NIX
• UNIX was a milestone in the development of operating systems and
of programming in general.
– The original reason for creating C was to use it to develop UNIX.
– In most programming contexts, time is measured in relation to
midnight, January 1, 1970. The release of UNIX that year was so
important in the history of programming that we treat it as the
beginning of time.
• Some forms of UNIX are still widely used today
– Mac OSX
– Oracle Solaris
• Linux is fundamentally different from UNIX internally, but it
provides a "UNIX-like" interface for developers and users. This was
designed to save early users the trouble of learning new OS
commands.
– We usually call commands like ls and pwd "Unix commands" even
when we are using them with Linux
– It is almost always possible to use the same commands, and often
even the same shell programs, in Linux and Mac OSX with no changes
59
Linux
• The core of an OS is the "kernel," which does the essential work (more on
this in a later lecture). Linux uses a large, full-featured kernel where other
Oss use small kernels and load other modules as needed.
• Linux is open source, developed by a group of thousands of volunteers.
• Linux has very high market share for web and database servers. It has
small market share for desktop/laptops, mostly among technical people.
There are a few Linux phones, and the first dedicated Linux tablets have
just been introduced.
• The kernel and some of the other aspects of Linux are used by many
companies and other groups of developers to create hundreds of different
"distributions" of Linux. Each distro is aimed at people with different
priorities. The core of the OS is the same for all distros, but included
applications and user interface components differ.
• Most distros are free and open-source. Some are commercial, and some
use hybrid models in which they provide the distro for free but charge for
technical support or other services.
• For important software packages, like JDKs and Eclipse, each distro release
has an officially-supported version established at release time. Distro
sponsors develop installation packages tuned for their release.
60
Linux
• The most consumer-friendly, and probably most popular, distro is Ubuntu.
Ubuntu is open-source and provided by Canonical, a for-profit company that
charges for technical support and is currently developing commercial mobile
devices using Ubuntu. There are several other distros based on Ubuntu, including
Linux Mint.
• The nerdiest distro is called Fedora. Fedora is open source and created by a
foundation supported by Red Hat, a for-profit company that uses Fedora as the
basis of a separate distro with paid tech support.
– Linux's eponymous Linus uses Fedora exclusively, but you would not necessarily want
to be like him
– Fedora adheres rigorously to the "free as in free speech" philosophy of open source
software. It is a hassle to use anything non-"free", even including some codecs, with
Fedora. It even uses a "libre" alternative to Oracle's JDK.
– However, Fedora offers a very rapid release schedule, with a new version every six
months. This means Fedora's supported version of an application like Eclipse is often
more current than other distros'.
• I chose to use Fedora for this class because I use it on my own machines. You
might also want to use Oracle VirtualBox to try out Ubuntu, Linux Mint, and
others at home. See https://en.wikipedia.org/wiki/Linux_distribution
61
Linux
• You can easily find lists of common Unix commands on line, for example at
http://mally.stanford.edu/~sr/computing/basic-unix.html
• I will assume you are familiar with the following basic commands from CS 120
and/or cs122. If not, learn them as soon as you get your VM set up
–
–
–
–
–
–
–
–
–
–
list files in current directory: ls and its arguments -l (more details) and -a (include hidden files)
navigation: cd and relative paths
delete a file: rm,
move/rename and copy files: mv and cp
create a directory inside the current one or using a relative path: mkdir
find out where you are (present working directory): pwd
find out where an application is installed: which
change permissions: chmod and its arguments, either in the style chmod 755 or chmod a+r
upload using sftp: put
stop a foreground process: <ctrl> c
• Lab 3 will require you to compile and run a Java program on your Linux VM. The
easiest approach is to write it on your own machine using a plain text editor like
Notepad++, TextWrangler, Geany, etc., and then use sftp to put the file to your
VM. However, you may also use emacs, vi, vim, or any other tool you already
62
know to write the file directly on the VM if you choose to.
Linux
• *nix operating systems typically use package managers to automate software
installation. These tools use repositories containing software packages tuned
for the OS (and, in the case of Linux, for the distro) and usually get and install all
dependencies automatically
• Different distros use different package managers. Most of them are broadly
similar.
• Fedora uses a package manager called dnf.
• Fedora also uses a "free as in free speech" alternative jdk called openjdk, rather
than Oracle's reference JDK
• The syntax for installing packages using dnf is dnf install <package name>,
for example
dnf install java-1.8.0-openjdk
63
*nix command line editors
*nix command-line text editors include emacs, vi, vim, and others
• Some people have opinions about the relative merits of these.
• I only use them for very simple editing; for anything complex, I use a GUI
editor like Geany, Notepad++, etc. and, if the *nix machine is remote, sftp
the edited file. For this kind of use, all the command line editors are about
the same.
• vim claims to be "vi improved." You can install it with
dnf install vim
logged in as root.
• Use of these editors is not intuitive
–
–
–
–
–
–
vim myfile creates myfile if necessary, and opens it for editing
i allows you to insert text
<esc> gets you out of the text to a command mode
from the command mode, w saves the file
q quits
to quit without saving your changes, use <esc> q!
Install JDK in Fedora Linux
Use dnf to install the jdk. DON'T download the packages
from Oracle; remember Fedora uses a "free" alternative
JDK.
dnf install java-1.8.0-openjdk
dnf install java-1.8.0-openjdk-devel
dnf will determine an appropriate location to install the
packages to.
65
Create User Linux
Root has complete power to access, change, add, and delete data from the
system, change system parameters, etc. You should almost never log in as root
because of the risk that a small error can cause a disaster. You should certainly
never let anyone *else* log in as root.
For most of your work, use a regular user account, which you can create when
you log in initially to change the root password. We will soon learn how to
temporarily get root privileges when needed.
adduser <username>
then passwd <username>
for example,
adduser mothra
password mothra
66
then supply
the password at the prompt