Advanced UNIX progamming

21
Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 4 Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan

description

Advanced UNIX progamming. Fall 2002 Instructor: Ashok Srinivasan Lecture 4. Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan. Announcements. Assignment 1 announced Due Sep 13, 2002 Quiz next week, on week 1 & 2 topics - PowerPoint PPT Presentation

Transcript of Advanced UNIX progamming

Page 1: Advanced UNIX progamming

Advanced UNIX progamming

Fall 2002

Instructor: Ashok SrinivasanLecture 4

Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan

Page 2: Advanced UNIX progamming

Announcements

• Assignment 1 announced– Due Sep 13, 2002

• Quiz next week, on week 1 & 2 topics

• Reading assignment– APUE chapter 1– APUE Chapter 2

• Pages 25-30, 44-46

Page 3: Advanced UNIX progamming

Review of week 1 topics

• UNIX programming environment– Editors– C compilers and debuggers– Makefiles

Page 4: Advanced UNIX progamming

Week 2 Topics• Review some features of CReview some features of C

– Header filesHeader files– Command line argumentsCommand line arguments– UtilitiesUtilities

• Review some UNIX system callsReview some UNIX system calls– system, etcsystem, etc

• PortabilityPortability– Standards: ANSI, POSIX, etcStandards: ANSI, POSIX, etc– 32 bit vs 64 bit32 bit vs 64 bit– Byte order: Little endian vs big endianByte order: Little endian vs big endian

Page 5: Advanced UNIX progamming

Week 2 Topics ... continued• Introduction to the UNIX API

– Environment variables– Exit status– Process ID– User ID– Signals and signal masking

• UNIX file system– File system abstraction– Directories– File descriptors

• Unix API Programming Examples and Techniques– Example with direct IO

• open, close, etc

– Variable argument list

Page 6: Advanced UNIX progamming

Review some features of C

• Header filesHeader files

• MacrosMacros

• Command line argumentsCommand line arguments

• UtilitiesUtilities

Page 7: Advanced UNIX progamming

Header files

• Usually define interfaces between separately compiled modules

• May contain macro definitions, preprocessor directives, declarations of types, and function prototypes

• Should not contain variable definitions or executable code

Page 8: Advanced UNIX progamming

Some header file errors

• Improper header file use can cause problems– Try compiling example2.c– Including a header file multiple times may

cause redefinition errors – Why does including stdio.h twice not cause

any problem?• Look at /usr/include/stdio.h

Page 9: Advanced UNIX progamming

Conditional Code in Headers

• Preprocessor directives are used to prevent the body of a header file from being used multiple times. #ifndef MYHEADER #define MYHEADER /* the body of the header file */ #endif

Page 10: Advanced UNIX progamming

Macros with and without Parameters

• #define MAX_LENGTH 256 – ... for (i = 0; i < MAX_LENGTH; i++) ...

• Macros can have parameters– #define max(a,b) (a > b) ? a : b

• What is wrong with the following? – #define sum(a, b) a + b– #define product(a, b) a*b– See example3.c, example3b.c,

example3c.c, and example3d.c

Page 11: Advanced UNIX progamming

Some useful functions

• #include <stdio.h> • int sprintf(char *s, const char

*format, ...); • int sscanf(const char *s, const char

*format, ...); • How would these be used to get all the

fields from the output of the shell command ps?– See example4.c

Page 12: Advanced UNIX progamming

Some Unix System Calls

• You may use these in your first assignment– system– mkstemp

Page 13: Advanced UNIX progamming

system

#include <stdlib.h> int system(const char *string);

– Works as if string is typed into the shell at a terminal

– Returns the exit status (see man page for waitpid)

– Usually -1 is returned if there is an error

Page 14: Advanced UNIX progamming

mkstemp

#include <stdlib.h> int mkstemp(char *template)

– template should end in XXXXXX– It replaces XXXXXX with unique file name,

and returns an open file descriptor for a file available for reading and writing

Page 15: Advanced UNIX progamming

Portability

• Standards– Source code portability: ANSI/ISO C– UNIX standards: POSIX, open group– Internet engineering task force (IETF)

• 32 bit vs 64 bit

• Byte order– Little endian vs big endian

Page 16: Advanced UNIX progamming

Source Code Portability

• Standard programming language– Example: ANSI/ISO C

• ISO C90 is in use; C99 is latest - should it be used?

• Standard libraries • Standard API to operating system

– Example: POSIX.1

• Auto-configuration mechanisms • Programmer discipline

Page 17: Advanced UNIX progamming

Unix Standards

• POSIX (IEEE STDS 1003.x and ISO/IEC 9945) – POSIX.1: System API for C language – POSIX.2: Shell and utilities – POSIX.5: System API for Ada language – POSIX.9: System API for Fortran language

• See also http://www.pasc.org and http://www.standards.ieee.org

Page 18: Advanced UNIX progamming

Unix Standards ... continued

• The Open Group – A consortium of vendors and user organizations – Consolidation of X/Open and the Open Software

Foundation – Controls the UNIX trademark – The Austin Group combined the IEEE, TOG, and

ISO standards

• See also http://www.opengroup.org and http://www.opengroup.org/onlinepubs/007904975

Page 19: Advanced UNIX progamming

IETF

• Internet Engineering Task Force (IETF) – Network designers, operators, vendors,

researchers – Deals with the Internet – Issues RFCs

• See also http://www.ietf.org

Page 20: Advanced UNIX progamming

64-bit vs. 32-bit architecture

• Pointers cannot be stored as int

• size_t cannot be stored as int

• long may not be long enough for size_t and offset_t

Datatype ILP32 LP64

char 8 8

short 16 16

int 32 32

long 32 64

pointer 32 64

(long long) 64 64

Note: ILP32 and LP64 are not the only two models

Page 21: Advanced UNIX progamming

Byte order

• Little-Endian– Low-order byte is

stored at lowest address

• Big-Endian– High-order byte is

stored at lowest address