Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 4

advertisement
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
• Reading assignment
– APUE chapter 1
– APUE Chapter 2
• Pages 25-30, 44-46
Review of week 1 topics
• UNIX programming environment
– Editors
– C compilers and debuggers
– Makefiles
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
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
Review some features of C
•
•
•
•
Header files
Macros
Command line arguments
Utilities
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
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
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
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
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
Some Unix System Calls
• You may use these in your first
assignment
– system
– mkstemp
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
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
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
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
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
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/007904
975
IETF
• Internet Engineering Task Force (IETF)
– Network designers, operators, vendors,
researchers
– Deals with the Internet
– Issues RFCs
• See also http://www.ietf.org
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
Byte order
• Little-Endian
– Low-order byte is
stored at lowest
address
• Big-Endian
– High-order byte is
stored at lowest
address
Download