Advanced UNIX progamming - PowerPoint PPT Presentation

About This Presentation
Title:

Advanced UNIX progamming

Description:

Header files. Command line arguments. Utilities. Review some UNIX system calls. system, etc ... used to prevent the body of a header file from being used ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 22
Provided by: asri9
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Advanced UNIX progamming


1
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
2
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

3
Review of week 1 topics
  • UNIX programming environment
  • Editors
  • C compilers and debuggers
  • Makefiles

4
Week 2 Topics
  • Review some features of C
  • Header files
  • Command line arguments
  • Utilities
  • Review some UNIX system calls
  • system, etc
  • Portability
  • Standards ANSI, POSIX, etc
  • 32 bit vs 64 bit
  • Byte order Little endian vs big endian

5
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

6
Review some features of C
  • Header files
  • Macros
  • Command line arguments
  • Utilities

7
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

8
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

9
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

10
Macros with and without Parameters
  • define MAX_LENGTH 256
  • ... for (i 0 i lt MAX_LENGTH i) ...
  • Macros can have parameters
  • define max(a,b) (a gt b) ? a b
  • What is wrong with the following?
  • define sum(a, b) a b
  • define product(a, b) ab
  • See example3.c, example3b.c, example3c.c, and
    example3d.c

11
Some useful functions
  • include ltstdio.hgt
  • 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

12
Some Unix System Calls
  • You may use these in your first assignment
  • system
  • mkstemp

13
system
  • include ltstdlib.hgt
  • 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

14
mkstemp
  • include ltstdlib.hgt
  • 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

15
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

16
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

17
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

18
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

19
IETF
  • Internet Engineering Task Force (IETF)
  • Network designers, operators, vendors,
    researchers
  • Deals with the Internet
  • Issues RFCs
  • See also http//www.ietf.org

20
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
21
Byte order
  • Little-Endian
  • Low-order byte is stored at lowest address
  • Big-Endian
  • High-order byte is stored at lowest address
Write a Comment
User Comments (0)
About PowerShow.com