Week 04: 2012-09-18 and 2012-09-20

CMSI 182
Intro to Computer Science
Week 04
Tuesday
B.J. Johnson
Doolan 222
09:25 – 10:40
This Week’s Agenda
Memory Mapping
 Resource contention / Deadlock
 Applications vs. O/S

Application types
 Threading and concurrency


Introduction to Programming
2
Where We Are:
YOU
History
ARE STILL
Lesson
STILL
HERE
HERE
Internet
I/O PeripheralsBUT
WE’RE
GOING
HERE NEXT!
Architecture
RAM/ROM
Programs
Applications
Languages
O/S
Router
HD
Algorithms
Abstractions
Database
3
Memory Mapping
Remember we talked about how
certain areas of memory are “mapped”
 Blocks of addresses are assigned to
correspond to specific functions
 Examples (addresses in hex):

040-043 – system timer
 200-20F – game controller
 320-32F – hard disk controller
 3D0-3DF – graphics controller

4
Why Is This Useful?
Input/Output!!
 Say you want to write a green dot to
the screen at a particular location
 Your program will write the code for
“green” (00ff00 hex) to the address
for the graphics controller (3D0 hex)
along with the pixel location
 Pixels are numbered on the screen
starting from the top left (0,0)
 Screen “resolution” is measured in
numbers of dots (pixels)

5
Resolutions

Some typical resolutions are:
800 x 600
 1024 x 768
 1280 x 800
 1280 x 1024


Video (TV) resolutions are different
480i and 480p
 1080i and 1080p
 Etc.


Numbers are backwards to addressing
6
So, Let’s Say …
Addressing the dot at (123,456) on
the screen
 Resolution 800 x 600
 This is 124th row, and 457th column (?)
 Which dot is this?
 Program sends the dot number
(address) with the pixel information
(data) to the display controller at 3D0
 It’s NOT all done with mirrors…

7
What IF?

Two processes want to access the hard
disk at the same time?
8
Resource Contention!
What happens when two processes
request access to the same resource
at the same time
 They are “contenders” for the resource
 First-come-first-served means that all
other processes are locked out
 If the process holding the resource
never releases it, all other processes
needing it are “stuck” or “hung up”
 This is called “STARVATION”

9
Deadlock

If two processes are trying to access
two resources
Process A holds resource X and process B
holds resource Y
 Process A is waiting for resource Y in
order to proceed
 Process B is waiting for resource X in
order to proceed
 A is stuck waiting for B to release Y
 B is stuck waiting for A to release X
 DEADLOCK!!

10
Famous Deadlock Example
Dining Philosophers
 Invented by Edsger Dijkstra

Five philosophers at a table
 Each has a plate in front of her
 One single chopstick between each plate
 Each philosopher needs two chopsticks to
eat
 While one eats, two are waiting
 How do we bound the problem so no one
goes hungry?

11
A Simulation

A working simulation is at:


This Web Page
Three ways to prevent deadlock:
Remove competition for non-sharable
resources
 Provide a way to force a process to
release a resource
 Prohibit processes from requesting
“partial” resources


Has particular importance for printers
and databases with multiple users
12
Finally ~ Threads!
Not sewing
 Method of “splitting” a process’
operations for efficiency
 Many uses:

User perception of responsiveness
 Managing remote connections
 Running program parts simultaneously

Uses the “round robin” scheduling idea
 Each thread gets a time slice
 Entire process “seems” to speed up

13
Concurrency

Threads can help with letting multiple
processes work at the same time
Called “multiprogramming” because
multiple programs can run concurrently
 NOT “multiprocessing” which means
multiple CPUs can run concurrently


Synchronization issues are important
Ensure against deadlock
 Ensure against starvation
 “semaphores” and “mutexes” can help

14
One example:

Spell checking
You are writing in your word processor
 The squiggly red line shows up
 That’s because the spell checker is
running in the background on its own
thread
 The grammar checker is running, too!

15
Applications vs. O/S
Two different levels of processes
 Two fundamentally different
approaches

O/S is the system “manager”
 Applications are the users’ “operations”


Applications won’t work without O/S
Need memory allocated to run
 Need access to peripherals
 Need protection from other processes


They DO have on thing in common:
16
Programming Languages
Both O/S and Applications are
software
 Must be written in some language
 Basic language is ones and zeros

Hard for humans to use and understand
 Easy for machines to use and understand


Next level up is “assembly language”
Uses mnemonic processor instructions
 Easier for humans to understand
 Requires “assembler” program to
translate so processor can use it

17
“Level Up”…
Next up the chain are “compiled”
languages like C, FORTRAN, Ada,
COBOL, etc.
 These languages are human-readable
 They are then translated to machine
code by a “compiler” program

May be compiled directly to binary code
 Alternatively may be compiled to
assembler code and THEN assembled into
binary code (doing the two-step)

18
Currently At the Top:
Finally there are “interpreted”
languages
 Examples include JavaScript, Perl,
Tcl/TK, Python, and venerable BASIC
 You will become intimately familiar
with JavaScript in the next few weeks!
 No “compilation” is done
 Each line is interpreted as it is
executed by the program

19
For Thursday ~
Read all of chapter 6
 Pay special attention to section 6.5

20
21
CMSI 182
Intro to Computer Science
Week 04
Thursday
B.J. Johnson
Doolan 219
09:25 – 10:40
This Week’s Agenda
Introduction to Programming
 That’s it, one bullet
 …but it’s a big bullet!

23
Where We Are:
History
Lesson
I/O Peripherals
Internet
YOU
ARE HERE
Architecture
RAM/ROM
Programs
Applications
Languages
O/S
Router
HD
Algorithms
Abstractions
Database
24
Recall Language Levels?









Machine language: 1001001010010…
Assembly:
loop: mov AX, 10h
sub AX, CX
jz
loop
“C”/”C++”: for (int i = 0; i < 10; i ++)
“Tcl/TK”:
for {set i 0} {$i < 10} {incr i}
Java:
for (int i = 0; i < 10; i ++)
JavaScript: for (int i = 0; i < 10; i ++)
FORTRAN:
DO 20 I = 1, 10
The differences are called “syntax”
The languages are called “3rd generation”
25
We’re Very Independent!

Machine independence is a good thing
Allows making the same program run on
many different computers/processors
 All programming languages rely on the
O/S to handle basic operations

• Called “low level operations”
• Also known as “system calls”
Creating programs uses “linking” which
links in libraries of system calls
 Such system calls are specific to each
system type
 Concept is also known as “portablility”

26
Language independence
This is what we’ll see more of in ch 5
 “pseudocode”

language-independent way of expressing
an algorithm
 Cross between natural language and
programming language
 Looks like:

id
ssn
sum
sum
<<<<-
23
“123-45-6789”
1+1
id + ssn
27
So, We’ll Use JavaScript





Interpreted scripting language
No compilation required
Part of every modern web browser
Nothing to download or install
Example:





Open a browser
Click in the URL line
Type:
javascript: {alert("hello world")}
Press the enter key – what happens?
Don’t forget curly braces and parens!
28
Nice and Handy Tool:






Dr. Toal and Dr. Dionisio of the LMU CS
department have set up a cool page for
running JavaScript
Go to:
http://javascript.cs.lmu.edu/scratch/
Type JavaScript into the text area
Click the “run” button
See what happens
No error checking/indicators


If there’s a syntax error, nothing happens
Must use the javascript console to see errors
29
And Away We Go!

Basic programming knowledge:

Naming and comments
• var name = value
• var name = function(input1, input2, …) {
<algorithm_body>
};
• // this is a single-line comment
• /* this comment
spans more than
one line of the code */
30
More Programming…

Basic programming knowledge:

Repetition (or iteration as it’s known)
• for (var index=0; index<end; index += 1;) {
<code_to_be_repeated>
}
• while (condition) {
<code_to_be_repeated>
}

There’s a couple more, but these are the
two biggies
31
More Programming…

Basic programming knowledge:

Conditionals
• if (condition) {
<code_if_condition_is_true>
}

OR…
• if (condition) {
<code_if_condition_is_true>
} else {
<code_if_condition_is_not_true>
}
32
More Programming…

Basic programming knowledge:

MORE Conditionals
• if (condition1) {
<code_if_condition1_is_true>
} else if (condition) {
<code_if_condition2_is_true>
} else {
<code_if_no_other_condition_is_true>
}
• switch (condition) {
case 1: <code_if_case1_is_true>
break;
case 2: <code_if_case2_is_true>
break;
case 3: <code_if_case3_is_true>
break;
}
33
More Programming…

Operations!

What you might expect:
•
•
•
•
•
•
•
•
•
Add, subtract, multiply, divide
Assignment: single equals sign
Logical equality: double or triple equals signs
Logical not equal: looks like “!==“
Less than, greater than, etc.
<, <=, >, >=
Remaindering (modulo): the “%” operator
Built-in functions
• parseInt(), Math.round(), Math.random()
• Lots more!
“dot” operator: used to access parts of “objects”
Don’t worry about that now, we’ll get there later!
34
Operator Precedence
Way of specifying what operations are
“more important” than others
 Means they get done first in any
expression by design
 Order is available here:



http://www.cs.lmu.edu/~ray/notes/program
elements/
There are “built-in” objects with their own
operators
That’s what the “dot” operator is about
35
Homework For Tuesday

Work Problems:

Chapter 3:
• Exercises: 5, 6, 25, 41

Chapter 6:
• Exercises: 1, 6, 7, 10, 15, 23, 25, 26, & 27
• Social Issues : 2, 4, & 6
36
37