Download presentation
Presentation is loading. Please wait.
Published byVerity Sutton Modified over 9 years ago
1
Introduction to C Programming in Unix Environment - I Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2013/2014
2
Lecturer Abed Asi (abedas@cs.bgu.ac.il)abedas@cs.bgu.ac.il Lab assistants David Tolpin – tolpin@cs.bgu.ac.il – Monday 16:00 – 19:00tolpin@cs.bgu.ac.il 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
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
4
http://www.cs.bgu.ac.il/~espl141/Main http://www.cs.bgu.ac.il/~espl141/Main 4 Abed Asi – ESPL
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
6
Unix environment Data types in C C – simple programs C Arrays – A Glance Makefiles Java vs. C Abed Asi - ESPL 6
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
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
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
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
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
12
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, 2 31 -1] ▪ signed int: [-2 31, 2 31 -1] Abed Asi - ESPL 12
13
Boolean types Doesn’t exist in C ! Use char/int instead zero = false non-zero = true Abed Asi - ESPL 13 while (1) { … } (infinite loop) if (-1974) {.. } (true statement)
14
#include 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 %d\n", i, j, (i*(i+1))/2); } return 0; } Abed Asi - ESPL 14
15
> 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
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
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
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
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 programmer’s responsibility to check whether the index is out of bounds… Abed Asi - ESPL 19 int a[4]; a[-1] = 0; a[4] = 0;
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
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
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
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
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
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
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
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
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
29
Read about the explicit and implicit rules for Makefiles Abed Asi - ESPL 29
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.