15-123 15-123 Systems Skills in C and Unix
15-123 15-123 Systems Skills in C and Unix
(5) Learning Perl, Fourth Edition by Randal L. Schwartz, Tom Phoenix, brian d foy
Fourth Edition July 2005 http://www.oreilly.com/catalog/learnperl4/
Section F
Kee Young Lee
Section G
Sylvia Han
How your time should be divided
This is how you should spend your time on any week (9
units)
Attending lecture
3 hours
Recitation
1 hour
Disclaimer
It is hard to predict how long it will take you to finish your
programming assignment
Talk to the course staff, if it is taking an unusually long time
(20 hour /week)
We will be tracking this time as part of the assignment
Important
Start assignments early – C programming can be very time
consuming
Assignments are individual, do not ask others to write code or
copy others code w/o permission
Sample code given in class can be used in any assignment
Read notes and annotated notes
Do homework
Not graded
Attend lectures and recitations
DO NOT use laptops other than to take notes in class or write
code
Any other activity is prohibited
Seek help early and often
Testing your prior knowledge
What is a function?
A mathematical notion is that a function takes a given
set of inputs and produces one and only one output
Hence for the same set of inputs it must always produce
the same output
Functions can be used in programming to
Divide and conquer
Promote modularity
Unit testing
proof of correctness of the algorithm
Functions have overhead
Change in execution path
Runtime stack use
What is the purpose of the following function?
int f(int n) {
int i = 0, k = 0;
while (k <= n) {
k += i*2 + 1;
i++;
}
return i-1;
}
Write down the assumptions you make about this
function
What is a Loop?
A programming constructs that allows one to repeat a
task
What are the types of loops you know? When do you
use them?
}
while loop syntax (new)
v
Initialize conditions
while (condition(s))
{
/* loop body */
Loop condition
v changes
}
When loops go wrong
Loop invariant
A loop invariant is a boolean variable that is true
before, during and just after execution of the loop
Example: What would be a loop invariant for
Proving the Loop invariance
Source: mathworks
Prove !(A && B) = !A || !B
Homework:
prove
!(A || B) = !A && !B
Understanding UNIX
Operating Systems
Unix Operating System
Began at AT & T in 1970’s
Free source code for certain groups
Many versions of unix
Linux version
Unix “like” system
Free and open source
Collaborative development
Small kernel
Unix system shell
Accessing unix
http://www.cmu.edu/myandrew
Download and install SSH secure shell
SSH
Provides access to unix.andrew.cmu.edu machines
Using a shell we can perform various tasks
mkdir, cp, quota, mv, …..
We develop and test our C and perl programs
We write shell scripts to make life easy
What is C?
A general purpose programming language
Developed in 1972 at AT &T for use with unix
One of most popular programming languages
High level procedural programming
Direct Access to low memory
C++ is the object oriented extension to C
Popular in industry
STL
Why learn C?
Good
Flexibility
Efficiency
Low level access to memory
Caution
Low level access to memory
Memory access violations (buffer overflows)
Hard to debug C code
Use a debugger such as gdb
Platform dependent
Life of a C program
#include <stdio.h>
int main(int argc, char* argv[]) {
printf(“hello world\n”);
return 0;
}
Life of a C program
Hello.c Hello.i Hello.s
preprocessor C compiler Assembler
Hello.o
Hello
executable Linker
How programs get executed
registers ALU
Main
Bus interface I/O bridge memory
I/O Bus