introduction to c programming in unix environment - i abed asi extended system programming...

Download Introduction to C Programming in Unix Environment - I Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2013/2014

If you can't read please download the document

Upload: verity-sutton

Post on 21-Dec-2015

217 views

Category:

Documents


3 download

TRANSCRIPT

  • Slide 1
  • Introduction to C Programming in Unix Environment - I Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2013/2014
  • Slide 2
  • Lecturer Abed Asi ([email protected])[email protected] Lab assistants David Tolpin [email protected] Monday 16:00 19:[email protected] Abed Asi Wednesday 16:00 19:00 Office hours Sunday 14:00 16:00 (-102/37) Website www.cs.bgu.ac.il/~espl141 www.cs.bgu.ac.il/~espl141 2 Abed Asi - ESPL
  • Slide 3
  • Lectures and Labs One-hour lecture every 2 weeks (with two exceptions) 12 (or 11) Lab sessions Quizzes 2 Quizzes NO final exam Grading policy 40% for quizzes (20% per quiz) 60% Labs (5% per lab) 3 Abed Asi ESPL
  • Slide 4
  • http://www.cs.bgu.ac.il/~espl141/Main http://www.cs.bgu.ac.il/~espl141/Main 4 Abed Asi ESPL
  • Slide 5
  • When ?TopicLecture October 20, 2013 Introduction to C Programming in Unix Environment - I 1 October 27, 2013 Introduction to C Programming in Unix Environment - II 2 November 3, 2013Introduction to Assembly3 November 17, 2013Functions and System Calls (Assembly)4 December 8, 2013Unix Processes5 December 15, 2013Programs Execution6 December 22, 2013Introduction to script languages (Python)7 January 5, 2014Web programming8 Abed Asi - ESPL 5
  • Slide 6
  • Unix environment Data types in C C simple programs C Arrays A Glance Makefiles Java vs. C Abed Asi - ESPL 6
  • Slide 7
  • 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 Run Applications running applications software such as: word processors, web browsers, games, etc... Memory Management - controls the mapping between logical memory and the hard disk, managing file system and more.. 7 Abed Asi - ESPL
  • Slide 8
  • The UNIX operating system was born in the late 1960s. It originally began as a one man project led by Ken Thompson of Bell Labs 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
  • Slide 9
  • 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. There are not part of UNIX Abed Asi - ESPL 9
  • Slide 10
  • 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 moreUnix Manual Abed Asi - ESPL 10
  • Slide 11
  • 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
  • Slide 12
  • int, short, long sizes: machine dependent! sizeof(short)
  • > gcc Wall loop.c o loop > loop 0 0 0 1 1 1 2 3 3 3 6 6 4 10 10 5 15 15 6 21 21 7 28 28 8 36 36 9 45 45 Abed Asi - ESPL 15
  • Slide 16
  • #include /* 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
  • Slide 17
  • $> prog u danny p 1234 argc = 5 argv[0] = prog argv[1] = -u... argv[4] = 1234 Always: argv[5] = 0 Abed Asi - ESPL 17
  • Slide 18
  • 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
  • Slide 19
  • C does not provide any run time checks This will compile and run (no errors) But can lead to unpredictable results It is the programmers responsibility to check whether the index is out of bounds Abed Asi - ESPL 19 int a[4]; a[-1] = 0; a[4] = 0;
  • Slide 20
  • 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
  • Slide 21
  • 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
  • Slide 22
  • Combines several object files into an executable file No unresolved references Abed Asi - ESPL 22 Main Preprocessor Compiler Square.c Square.o Main.c Main.o Linker libc.a
  • Slide 23
  • $ gcc c Square.c o Square.o $ gcc c Main.c o Main.o $ gcc Square.o Main.o o Main Abed Asi - ESPL 23 Main Preprocessor Compiler Square.c Square.o Main.c Main.o Linker libc.a
  • Slide 24
  • What is it good for ? Automatic tool for projects management Less boring work for the programmer Less errors Abed Asi - ESPL 24 mywc: mywc.c gcc -Wall mywc.c -o mywc
  • Slide 25
  • Abed Asi - ESPL 25 prog1 read.cread.h list.clist.hmain.c read.o main.o list.o Makefile prog1: read.o main.o list.o gcc main.o read.o list.o o prog1 main.o: main.c read.h list.h gcc -c main.c read.o: read.c read.h gcc -c read.c list.o: list.c list.h gcc -c list.c $ make prog1
  • Slide 26
  • Abed Asi - ESPL 26 prog1 read.cread.h list.hlist.cmain.c read.o main.o list.o If only one file is modified, will we have to recompile all over again? No, the Makefile uses the dependencies tree
  • Slide 27
  • Abed Asi - ESPL 27 prog1 read.cread.h list.hlist.cmain.c read.o main.o list.o If read.h is modified, what should be done? We have to recreate a subset of the files
  • Slide 28
  • Aim: build only out-of-date files (use timestamps) Makefile contains: List of dependencies (no cycles) Recovery scenario when any file is modified If any of the files [main.c, list.h, read.h] wad 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 - ESPL28 main.o: main.c list.h read.h gcc -c main.c
  • Slide 29
  • Read about the explicit and implicit rules for Makefiles Abed Asi - ESPL 29
  • Slide 30
  • Abed Asi - ESPL 30 JavaC byte-code, write once read everywhere native code method, classfunction, structure foo.javafoo.c, foo.h new, GCmalloc, calloc, free , , void * if(a = 1) errorif(a = 1) ok String, StringBufferchar *s, #include java.lang#include java.io#include java.lang.Math#include