19
1 1 CSC 2400: Computing Systems Professor Mirela Damian http://www.csc.villanova.edu/~mdamian/ 2 Introductions • Mirela Damian (professor) Room 167A in the Mendel Science Building [email protected] Programming assistants Room G85 in the Mendel Science Building (left back corner of the room)

CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

1

1

CSC 2400: Computing Systems!

Professor Mirela Damian!http://www.csc.villanova.edu/~mdamian/!

2

Introductions!• Mirela Damian (professor)!

•  Room 167A in the Mendel Science Building!•  [email protected] !!

•   Programming assistants!•  Room G85 in the Mendel Science Building! (left back corner of the room) !

Page 2: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

2

3

What do Computers Do?!

Computers Execute Instructions.!

• Do a survey on campus!•  Numerous “correct” answers!

That’s all!!

4

What Kind of Instructions?!

• Strictly speaking, computers only understand machine language (binary machine code). !

1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0

1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0

0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0

1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0

Page 3: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

3

5

•   Assembly  •   Machine  Code! Low-­‐level  languages  

•   Pascal/Fortran  •   ADA  •   C++    •   C!

High-­‐level  compiled  languages

•   Lisp  /  ML    •   Shells  /  scripts  •   Python  •   Basic  /  V-­‐Basic  •   Java  

Interpreted  languages

•   SGML  /  XML  •   HTML! Mark-­‐up  languages  

• Oracle  /  SQL  •   MS  Access! Database  languages  

•   “Click”  an  icon  •   Press  a  key  •   Verbal  command!

Human  interface  languages  

Hierarchy of Computer Languages!

6

Course Goals: “Look under the hood”!• “Help you learn what happens “under the hood” of computer systems!

• Specifically, two downward tours!•  Language levels tour!

•  High-level language (C) → assembly language → machine language (IA-32)!

•  Service levels tour!•  High-level language (C) → standard libraries →

operating system (Linux)!

• Reveal how abstractions are created!

Page 4: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

4

7

Course Goals: Why C?!• Q: Why C instead of Java?!• A: C facilitates language levels tour!

•  C is a lower-level language!•  C is closely related to assembly language!

• A: C facilitates service levels tour!•  Linux is written in C!

8

Course Goals: Why Linux?!• Q: Why Linux instead of Microsoft Windows?!• A: Linux is good for education and research!

•  Linux is open-source and well-specified!

• A: Linux is good for programming!•  Linux is a variant of Unix!•  Unix has GNU, a rich open-source programming

environment!

Page 5: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

5

9

Course Goals: Summary!• Help you to become a...!

Power Programmer!!!!

10

Resources: Books!• Textbook!

•  X86 Assembly Language and C Fundamentals! Joseph Cavanagh. CRC Press ! ISBN 9781466568242!

•  Covers both language-level and system-level tours!•  Some topics will be covered in more depth in class!

Page 6: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

6

11

Resources: Books!• Textbook is mandatory!

•  Homework assignments are based on textbook and in class lectures.!

•  All homework due at start of class!• Lectures based on textbook and online material !

• Additional materials posted on the class web site!

12

Resources: Programming Environment!

•  Linux machines felix.csc.villanova.edu, helix.csc.villanova.edu!•  Unix machines degas, tanner, rodin!

tanner csgate

degas rodin felix

NFS

Any lab, or your own !PC/Mac/Linux Computer!

SSH!

SSH

SSH

Page 7: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

7

13

Try to Log In Now!•  Locate and invoke Secure Shell Client from the Start menu!•  Click on the ‘Quick Connect’ tab !•  In the ‘Host Name’ textbox, type in!

tanner.csc.villanova.edu!•  In the ‘User Name’ textbox, type in your username (the

one the system administrator emailed you)!•  Click on ‘Connect’!•  A text window with the shell prompt will be opened for you.!•  This is a command line interface. !•  You will interact with the Unix operating system by typing in

commands at the shell prompt.Try for example !! hostname!!The name of the machine (tanner) will be displayed.!

14

Workload Distribution!•  To use our Unix resources efficiently, from tanner connect

arbitrarily to rodin or degas by typing in either!

! !ssh rodin!!! !ssh degas!!!

•  Too many students working on one same machine can slow it down considerably.!

•  To log off, simply type in !

! !exit!

Page 8: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

8

15

Grading!• Assignments (45%)!

•  Pencil and paper!•  Clean, readable, working code!•  On time (no late submission)!•  First assignment is available now!

• Exams (45%)!•  Midterm!•  Comprehensive final exam!

• Class Participation (10%)!• No makeup assignments or exams!• Lecture attendance is mandatory!

16

Programming Assignments!• Start early to allow time for debugging.!

Page 9: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

9

17

Course Schedule!•  Very generally…!

Weeks! Lectures!1-3! Introduction to C and Unix!4-7! Bits, Bytes and Data Types!7! Midterm exam!

Recess!8-12! “Under the hood” – assembly language!

Thanksgiving Recess!13-14! “Under the hood” – Unix processes!Dec. 17 / 18! Final exam, 6:00 – 8:30 pm!

•  See course web site for details!

18

Any questions before we start?!

Page 10: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

10

19

C vs. Java: Design Goals!•  Java design goals!

•  Support object-oriented programming!•  Allow same program to be executed on multiple operating systems !•  Support using computer networks !•  Execute code from remote sources securely!•  Adopt the good parts of other languages (esp. C and C++) !

•  Implications for Java!•  Good for application-level programming!•  High-level!

•  Virtual machine insulates programmer from underlying assembly language, machine language, hardware!

•  Portability over efficiency!•  Security over efficiency!•  Security over flexibility!

20

C vs. Java: Design Goals!•  C design goals!

•  Support structured programming!•  Support development of the Unix OS and Unix tools!

•  As Unix became popular, so did C!

•  Implications for C!•  Good for system-level programming!

•  But often used for application-level programming – sometimes inappropriately!

•  Low-level!•  Close to assembly language; close to machine language; close to

hardware!•  Efficiency over portability!•  Efficiency over security!•  Flexibility over security!

Page 11: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

11

21

C vs. Java: Design Goals!

•  Differences in design goals explain many differences between the languages!

•  C’s design goal explains many of its eccentricities!

•  We’ll see examples throughout the course!

22

C vs. Java: Overview!

•  Dennis Ritchie on the nature of C:!

•  “C has always appealed to systems programmers who like the terse, concise manner in which powerful expressions can be coded.” !

•  “C allowed programmers to (while sacrificing portability) have direct access to many machine-level features that would otherwise require the use of assembly language.”!

•  “C is quirky, flawed, and an enormous success. While accidents of history surely helped, it evidently satisfied a need for a system implementation language efficient enough to displace assembly language, yet sufficiently abstract and fluent to describe algorithms and interactions in a wide variety of environments.”!

Page 12: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

12

23

C vs. Java: Overview (cont.)!

•  Bad things you can do in C that you can’t do in Java!•  Shoot yourself in the foot (safety)!•  Shoot others in the foot (security)!•  Ignore wounds (error handling)!

•  Dangerous things you must do in C that you don’t in Java!•  Explicitly manage memory via malloc() and free()

•  Good things you can do in C, but (more or less) must do in Java!•  Program using the objected-oriented style!

•  Good things that you can’t do in C but can do in Java!•  Write completely portable code!

24

C vs. Java: Details!

•  Remaining slides provide some details!•  Suggestion: Use for future reference!

•  Slides covered briefly now …!

Page 13: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

13

25

C vs. Java: Details (cont.)!Java! C!

OverallProgram Structure!

Hello.java:

import java.io.*

public class Hello { public static void main(String[]s) { System.out.println("Hello 2400"); } }

hello.c:

#include <stdio.h>

int main() { printf("Hello 2400\n"); return 0; }

Building!

% javac Hello.java % ls Hello.class Hello.java %

% gcc hello.c % ls a.out hello.c %

Running!% java Hello Hello, world %

% ./a.out Hello, world %

C vs. Java Example!

26

Java! C!HelloWorld.java: helloworld.c:

import java.io.*

public class HelloWorld { public static void main(String[]args) { System.out.println("Hello world"); } }

#include <stdio.h>

int main() { printf("Hello world\n"); return 0; }

Page 14: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

14

27

C vs. Java: Details (cont.)!

Java! C!

Character type! char // 16-bit unicode char /* 8 bits */

Integral types!

byte // 8 bits short // 16 bits int // 32 bits long // 64 bits

(unsigned) char (unsigned) short (unsigned) int (unsigned) long

Floating point types!

float // 32 bits double // 64 bits

float double long double

Logical type! boolean /* no equivalent */ /* use integral type */

Generic pointer type! // no equivalent void*

Constants! final int MAX = 1000; #define MAX 1000 const int MAX = 1000; enum {MAX = 1000};

28

C vs. Java: Details (cont.)!

Java! C!

Arrays!int [] a = new int [10]; float [][] b = new float [5][20];

int a[10]; float b[5][20];

Array bound checking! // run-time check /* no run-time check */

Pointer type! // Object reference is an // implicit pointer

int *p;

Record type!

class Mine { int x; float y; }

struct Mine { int x; float y; }

Page 15: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

15

C vs. Java Example!

29

Java! C!DataTypes.java: datatypes.c:

import java.io.*

public class DataTypes { public static void main(String[]args) { go(); } public static void go() { int a = 2; float b = 3.1416; byte[] s = new byte [100];

System.in.read(s); System.out.print(Arrays.toString(s)); } }

#include <stdio.h> void go() { int a = 2; float b = 3.1416; char s[100];

scanf("%s", s); printf("%s", s); }

int main() { go(); return 0; }

30

C vs. Java: Details (cont.)!

Java! C!

Strings!String s1 = "Hello"; String s2 = new String("hello");

char *s1 = "Hello"; char s2[6]; sprintf(s2,"%s", "hello");

String concatenation!

s1 + s2 s1 += s2

#include <string.h> strcat(s1, s2);

Logical ops! &&, ||, ! &&, ||, !

Relational ops! =, !=, >, <, >=, <= =, !=, >, <, >=, <=

Arithmetic ops! +, -, *, /, %, unary - +, -, *, /, %, unary -

Bitwise ops! >>, <<, >>>, &, |, ^ >>, <<, &, |, ^

Assignment ops!

=, *=, /=, +=, -=, <<=, >>=, >>>=, =, ^=, |=, %=

=, *=, /=, +=, -=, <<=, >>=, =, ^=, |=, %=

Page 16: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

16

C vs. Java Example!

31

Java! C!StringManipulation.java: mystring.c:

import java.io.*

public class StringManipulation { public static void main(String[]args) { String s1 = "Computer Science"; int x = 2400; String s2 = "is fun!\n" String fun = s1 + " " + x + " " + s2; System.out.println(fun); } }

#include <stdio.h> #include <string.h>

int main() { char * s1 = "Computer Science"; int x = 2400; char * s2 = "is fun!\n" char fun[100]; char tmp[5]; strcpy(fun, s1); sprintf(tmp, " %d ", x); strcat(fun, tmp); strcat(fun, s2); printf("%s\n", fun); return 0; }

32

C vs. Java: Details (cont.)!

Java! C!

if stmt!if (i < 0) statement1; else statement2;

if (i < 0) statement1; else statement2;

switch stmt!

switch (i) { case 1: ... break; case 2: ... break; default: ... }

switch (i) { case 1: ... break; case 2: ... break; default: ... }

goto stmt! // no equivalent goto SomeLabel;

Page 17: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

17

C vs. Java Example!Java! C!

Exam.java: exam.c:

public class Exam { public static void main(String args[]) { char grade = (char)System.in.read(); switch(grade) { case 'A': System.out.print("Yay!"); break; case 'B': case 'C': System.out.print("Well done"); break; case 'D': System.out.print("Passed!"); break; case 'F': System.out.print("Try again"); break; default: System.out.print("Invalid"); } if ((grade == 'A') || (grade == 'B')) System.out.print("Congratulations!!!") } }

#include <stdio.h>

int main() { char grade; scanf(“%d”, &grade); switch(grade) { case 'A': printf("Yay!"); break; case 'B': case 'C': printf("Well done"); break; case 'D': printf("Passed!"); break; case 'F’: printf("Try again"); break; default: print("Invalid"); } if((grade=='A’)||(grade=='B')) printf("Congratulations!!!") return 0; }

34

C vs. Java: Details (cont.)!

Java! C!

for stmt! for (int i=0; i<10; i++) statement;

int i; for (i=0; i<10; i++) statement;

while stmt! while (i < 0) statement;

while (i < 0) statement;

do-while stmt!

do { statement; … } while (i < 0)

do { statement; … } while (i < 0)

continue stmt! continue; continue;

labeled continue stmt! continue SomeLabel; /* no equivalent */

break stmt! break; break;

labeled break stmt! break SomeLabel; /* no equivalent */

Page 18: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

18

C vs. Java Example!

35

Java! C!Factorial.java: factorial.c:

public class Factorial { public static void main(String[] args) { final int NUM_FACT = 100; for(int i = 0; i < NUM_FACT; i++) System.out.println( i + "! is ” + fact(i) ); } public static int fact(int n) { int result = 1; for(int i = 2; i <= n; i++) result *= i; return result; } }

#include <stdio.h> #include <string.h> #define NUM_FACT 100 int fact(int n) { /* identical to Java */ }

int main() { for(int i=0; i < NUM_FACT; i++) printf( “%d! is %d”, i, fact(i)); return 0; }

36

C vs. Java: Details (cont.)!

Java! C!

return stmt! return 5; return;

return 5; return;

Compound stmt (alias block)!

{ statement1; statement2; }

{ statement1; statement2; }

Exceptions! throw, try-catch-finally /* no equivalent */

Comments! /* comment */ // another kind

/* comment */

Method / function call!

f(x, y, z); someObject.f(x, y, z); SomeClass.f(x, y, z);

f(x, y, z);

Page 19: CSC 2400: Computing Systemsmdamian/Past/csc2400fa13/notes/01... · 2014. 1. 7. · • This is a command line interface. ! • You will interact with the Unix operating system by

19

37

Getting Started!

•  Check out course web site now!! !http://www.csc.villanova.edu/~mdamian/csc2400/!

!

•  Explore your computing environment!•  Assignment posted on the website!

•  Reading!•  Required: Chapter 4, Cavanagh textbook!