Advanced UNIX progamming - PowerPoint PPT Presentation

About This Presentation
Title:

Advanced UNIX progamming

Description:

Acknowledgements: The syllabus and power point presentations are modified ... String1 = string2. Example. CC=gcc. CFLAG=-Wall ansi pedantic. Target rules: ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 19
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 3

Class web site http//www.cs.fsu.edu/asriniva/co
urses/aup-02
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

3
Week 1 Topics
  • Course outline and policies
  • UNIX programming environment
  • Editors
  • C compilers and debuggers
  • Makefiles
  • Review some features of C
  • Header files
  • Command line arguments
  • Utilities
  • Review some UNIX system calls
  • system, etc

4
UNIX programming environment
  • Editors
  • C compilers
  • Debugger
  • Makefiles
  • make

5
  • run ltcommand line argumentsgt to run the program

6
Make
  • make -f makefileoption target
  • A tool to update files derived from other files
  • The default files for make are ./makefile,
    ./Makefile, ./s.makefile
  • Use the f option to specify some other file
  • make f makefile1
  • The makefile has three components
  • Macros define constants
  • Target rules Specify how targets are made
  • Inference rules Specify how targets can be made,
    implicitly. make will first check if a target
    rule applies, before using inference rules.

7
make ... continued
  • Macros
  • String1 string2.
  • Example
  • CCgcc
  • CFLAG-Wall ansi pedantic
  • Target rules
  • Target prerequisite
  • lttabgt command
  • lttabgt command
  • Example
  • a.out myprog1.c myprog2.c myprog3.c
    (CC) (CFLAG) myprog1.c myprog2.c
    myprog3.c

8
make ... continued
  • Inference rules
  • Target
  • lttabgt command
  • lttabgt command
  • Target must be of the form .s1 or .s1.s2 where
    .s1 and .s2 must be prerequisites of the
    .SUFFIXES special target.
  • .s1.s2 ? make .s2 from .s1
  • .s1 ? make from .s1
  • Example
  • .c
  • (CC) o _at_ lt
  • .c.o
  • (CC) c lt

9
makefile examples
  • See the example makefiles
  • makefile, makefile1, makefile2
  • makefile1 will recompile only the modified files,
    instead of everything
  • makefile2 has inference rules
  • www.cs.fsu.edu/cgi-bin/man.cgi can be used to
    find more information

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

11
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

12
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

13
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

14
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

15
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.

16
Some Unix System Calls
  • You may use these in your first assignement
  • system
  • mkstemp

17
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

18
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
Write a Comment
User Comments (0)
About PowerShow.com