Download presentation
Presentation is loading. Please wait.
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 Review some UNIX system calls
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 < 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
11
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
12
Some Unix System Calls You may use these in your first assignment
mkstemp
13
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
14
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
15
Portability Standards 32 bit vs 64 bit Byte order
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
POSIX (IEEE STDS 1003.x and ISO/IEC 9945)
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 and
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 and
19
Internet Engineering Task Force (IETF)
Network designers, operators, vendors, researchers Deals with the Internet Issues RFCs See also
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 short 16 int 32 long 64 pointer (long long) Note: ILP32 and LP64 are not the only two models
21
Byte order Little-Endian Big-Endian
Low-order byte is stored at lowest address Big-Endian High-order byte is stored at lowest address
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.