Administration Lecturer Abed Asi abedascs bgu ac il
Administration � Lecturer Abed Asi (abedas@cs. bgu. ac. il) � Lab Instructor Abed Asi Monday 9: 00 – 12: 00 Monday 16: 00 – 19: 00 � Office hours Sunday 14: 00 – 16: 00 (-102/37) � Website www. cs. bgu. ac. il/~espl 151 Abed Asi - ESPL 2
Administration �Lectures and Labs 8 lectures, one hour each 11 Lab sessions �Quizzes 2 Quizzes NO final exam �Grading policy 40% for quizzes (20% per quiz) 60% Labs (~ 5% per lab) Abed Asi – ESPL 3
Lab Instructions �http: //www. cs. bgu. ac. il/~espl 151/Main Abed Asi – ESPL 4
Lectures schedule Lecture Topic When ? 1 Introduction to C Programming in Unix Environment October 26, 2014 2 Memory Management in C November 2, 2014 3 Introduction to Assembly November 9, 2014 4 Functions and System Calls (Assembly) November 16, 2014 5 Unix Processes December 7, 2014 6 Executable Programs and ELF December 14, 2014 7 Introduction to script languages (Python) December 28, 2014 8 Web programming January 4, 2015 Abed Asi - ESPL 5
Today �Unix environment �Data types in C �C – simple programs �C Arrays – A Glance �Makefiles �Java vs. C Abed Asi - ESPL 6
What is an Operating System? � A program that starts up when you turn on your computer and runs underneath all other programs � It is a manager. It manages all the available resources on a computer, from the CPU, to memory, to hard disk accesses � Tasks the operating system must perform Control Hardware - attempts to get everything working together Memory Management - controls the mapping between logical memory and the hard disk, managing file system and more. . Run Applications – running applications software such as: word processors, web browsers, games, etc. . . Abed Asi - ESPL 7
Unix OS: Brief History The UNIX operating system was born in the late 1960 s. � It originally began as a one man project led by Ken Thompson of Bell Labs (. . . Dennis Retchie) � � Grown to become one of the most widely used operating system � It has gone through many different generations and even mutations Some differ substantially from the original version, like Berkeley Software Distribution (BSD) or Linux. Others, still contain major portions that are based on the original source code. Abed Asi - ESPL 8
Unix OS: Overview � The Kernel - handles memory management, input and output requests, and program scheduling. Technically speaking, the kernel is the OS. � The Shell - basic UNIX shells provides a “command line” interface which allows the user to type in commands. � The Built-in System Utilities - are programs that allow a user to perform tasks which involve complex actions such as listing the content of directories, move & copy files, remove files, etc. . . � Application Software & Utilities – additional programs that are bundled with the OS distribution, or available separately. They are not part of UNIX Abed Asi - ESPL 9
Programming in Unix Environment � To develop your own program you need: Compiler: translates your programming language to assembly Text editor: to type down source code Project management tool: dependencies, linking, executable. . � In Unix: You will use the gcc compiler Any text editor that you prefer (Kate, Emacs, KWrite, …) make as the project management tool � Use Unix Manual for function/program description and more Abed Asi - ESPL 10
Data types � Char char is the basic type in C ▪ sizeof(char) = 1 byte by definition Examples: ▪ char c = ‘A’; ▪ char c = 65; Abed Asi - ESPL 11
Data types � int , short, long sizes: machine dependent! ▪ sizeof(short) <= sizeof(int) <= sizeof(long) For example: (linux on pentium) ▪ short: 16 bit, int: 32 bit, long: 32 bit All can be signed/unsigned ▪ Default: signed ▪ unsigned int: [0. . 231 -1] ▪ signed int: [-231. . 231 -1] Abed Asi - ESPL 12
Boolean types �Boolean types Doesn’t exist in C ! while (1) { … } (infinite loop) �Use char/int instead zero = false non-zero = true if (-1974) {. . } (true statement) Abed Asi - ESPL 13
Programming in C – First program #include <stdio. h> int main() { int i; // declares i as an integer int j = 0; // declares j as an integer, and initializes it to 0 } // for( initial ; test condition ; update step ) for( i = 0; i < 10; i++ ) { j += i; // shorthand for j = j + i printf("%d %d %dn", i, j, (i*(i+1))/2); } return 0; Abed Asi - ESPL 14
First Program – Compiling and running > > 0 1 2 3 4 5 6 7 8 9 Abed Asi - ESPL gcc –Wall loop. c –o loop 0 0 1 1 3 3 6 6 10 10 15 15 21 21 28 28 36 36 45 45 15
Programming in C – Second program #include <stdio. h> /* count number of words in sentence */ int main(int argc, char **argv) { int i; printf("There are %d words in phrase '", argc-1); for(i=1; i!=argc; ++i) { printf("%s", argv[i]); if(i!=argc-1) printf(" "); } printf("'. n"); return 0; } Abed Asi - ESPL 16
argv & argc: example $> prog –u danny –p 1234 argc = 5 argv[0] = “prog” argv[1] = “-u”. . . argv[4] = “ 1234” Always: argv[5] = 0 Abed Asi - ESPL 17
Arrays � C does not provide array operations int a[4]; int b[4]; . . . a = b; // illegal if( a == b ) // illegal � int arr[5][7]; 5 rows, 7 columns continuous memory (divided to 5 blocks) access: arr[row][col] = 0; Abed Asi - ESPL 18
Arrays int a[4]; a[-1] = 0; a[4] = 0; ? ? ? � C does not provide any run time checks � This will compile and run (no errors) � But can lead to unpredictable results � It is the programmer’s responsibility to check whether the index is out of bounds… Abed Asi - ESPL 19
Array initialization int arr[3] = {3, 4, 5}; // Good int arr[] = {3, 4, 5}; // Good - The same int arr[4] = {3, 4, 5}; // Bad style - The last is 0 int arr[2] = {3, 4, 5}; // Bad int arr[2][3] = {{2, 5, 7}, {4, 6, 7}}; // Good int arr[2][3] = {2, 5, 7, 4, 6, 7}; // Good - The same int arr[3][2] = {{2, 5, 7}, {4, 6, 7}}; // Bad int arr[3]; arr = {2, 5, 7}; // Bad - array assignment only in initialization Abed Asi - ESPL 20
Compilation � Takes input C-code and produces machine code (object file) gcc –c Main. c –o Main. o Main. c Main. o � The object file does not contain all external references It leaves names, such as “printf”, “area”, etc. as undefined references Abed Asi - ESPL 21
Linking � Combines several object files into an executable file No unresolved references Square. c Square. o Main. c Main. o Main libc. a Preprocessor Compiler Abed Asi - ESPL Linker 22
Linking $ gcc -Wall –c Square. c –o Square. o $ gcc -Wall –c Main. c –o Main. o $ gcc Square. o Main. o –o Main Square. c Square. o Main. c Main. o Main libc. a Preprocessor Compiler Abed Asi - ESPL Linker 23
Makefile �What is it good for ? Automatic tool for projects management Less boring work for the programmer Less errors mywc: mywc. c gcc -Wall mywc. c -o mywc Abed Asi - ESPL 24
Makefile – compilation and linkage Makefile prog 1: read. o main. o list. o gcc main. o read. o list. o –o prog 1 main. o: main. c read. h list. h gcc -c main. c read. o: read. c read. h gcc -c read. o main. o list. o: list. c list. h gcc -c list. c $ make prog 1 Abed Asi - ESPL read. c read. h main. c list. h 25
Makefile – compilation and linkage � If only one file is modified, will we have to recompile all over again? � No, the Makefile uses the dependencies tree prog 1 read. o main. o list. o read. c read. h main. c list. h list. c Abed Asi - ESPL 26
Makefile – dependencies tree � If read. h is modified, what should be done? prog 1 � We have to recreate a subset of the files read. o main. o list. o read. c read. h main. c list. h list. c Abed Asi - ESPL 27
Makefile – summary � � Aim: build only out-of-date files (use timestamps) Makefile contains: List of dependencies (no cycles) “Recovery” scenario when any file is modified main. o: main. c list. h read. h <tab> gcc -c main. c � If any of the files [main. c, list. h, read. h] was modified after main. o, the command “gcc –c main. c” will be invoked � By default, make only executes the first target in the makefile Abed Asi - ESPL 28
Makefile Read about the explicit and implicit rules for Makefiles Abed Asi - ESPL 29
Java vs. C C Java native code byte-code קומפילציה function, structure method, class מרכיבים בסיסיים foo. c, foo. h foo. java מודול malloc, calloc, free new, GC ניהול זכרון void * , מצביע Abed Asi - ESPL כתובת , גישה ישירה if(a = 1) ok if(a = 1) error מערכת טיפוסים char *s, #include <string. h> String, String. Buffer עיבוד מחזורות #include <stdlib. h> java. lang ספריה מובנת #include <stdio. h> java. io קלט ופלט #include <math. h> java. lang. Math פונקציות מתמטיות 30
- Slides: 29