System Security & Intrusion Detection

Download Report

Transcript System Security & Intrusion Detection

Streams and File Object
Buffering
Functions
Opening a stream
Reading & Writing a stream
Standard I/O Efficiency
7/16/2015
2
Streams & File Objects
When we open or create a file with the standard
I/O library
• We have associated a stream with the file,
such as
fopen() returns a pointer to a FILE object
7/16/2015
3
Streams & File Objects
FILE - A structure that contains all information
required by the standard I/O library to manage
the stream :
- The file descriptor used for actual I/O
- a pointer to a buffer for the stream
- the size of the buffer
- a count of the number of characters
currently in the buffer
- an error flag
etc.
7/16/2015
4
Streams & File Objects
Application software should never need to
examine a FILE object to reference the
stream
=> use a file pointer (type FILE*)
7/16/2015
5
Buffering
• Fully buffered
• Line buffered
• Unbuffered
7/16/2015
6
Buffering
Fully buffered
- the actual I/O takes place when the
standard I/O buffer is filled
- flush describes the writing of standard
I/O buffer
- a buffer can be automatically flushed
by the standard I/O routines (such as
when a buffer is filled), or a function
fflush() is called
7/16/2015
7
Buffering
Line buffered
- The standard I/O library performs I/O
when a new line character is encountered
an input or output
=> It is possible to perform a single
character at a time
- Typically used a terminal stream (e.g.
standard I/O)
7/16/2015
8
Buffering
Unbuffered
- The standard I/O library does not buffer
the characters
The standard error stream is normally unbuffered
--> so that any error messages are displayed
as quickly as possible
7/16/2015
9
Buffering
ANSI C
By default
- standard input and standard output are fully
buffered, if and only if they do not refer to an
interactive device
- standard error is never fully buffered
7/16/2015
10
Buffering
To buffer I/O
setbuf (FILE *fp, char *buf);
setvbuf (FILE *fp, cahr *buf, int mode, size_t
size);
where
mode is one of the following
_IOFBF
_IOLBF
-IONBF
7/16/2015
11
Buffering
Opening a Stream
FILE *fopen(char *pathname, char *type);
FILE *freopen(char *pathname, char *type,
FILE *fp);
FILE *fdopen(int filedes, char *type);
return : file pointer if OK,
NULL on error
type: r, w, a, r+, w+, a+, etc.
int fclose (FILE *fp);
7/16/2015
12
Buffering
 Standard I/O Library

handles details such as buffer allocation
 performing I/O in optimal-sized chunks
(users do not need to worry about the
correct clock size)
7/16/2015
13
Standard I/O Library
ANSI C -- by default

1. Standard input and standard output are
fully buffered, if and only if they do not
refer to an interactive device
 2. Standard error is never fully buffered
 Both SVR4 and 4.3+BSD
-- defualt
1. Standard error is always unbuffered
2. All other streams are line buffered if
they refer to a terminal device, otherwise
they are fully buffered


7/16/2015
14
Standard I/O Library
 The defaults can be altered using
void setbuf(FILE *fp, char *buf);
int setvbuf(FILE *fp, char *buf, int mode, size_t size);
mode :
_IOFBF. _IOLBF, _IONBF
7/16/2015
15
Standard I/O Library
 Opening a Stream
FILE *fopen(char *pathname, char *type);
FILE *freopen(char *pathname, char *type, FILE *fp);
FILE *fdopen(int filedes, char *type);
All three return: file pointer if ok, NULL on error
type:
r, w, a, r+, w+, a+
int fclose(FILE *fp);
7/16/2015
16
Standard I/O Library
Reading and Writing a Stream



Character-at-a-time
Line-at-a-time
Direct I/O
7/16/2015
17
Standard I/O Library
 Functions:
 Character-at-a-time:
int getc(FILE *fp);
int fgetc(FILE *fp);
int getchar(void);
int putc(FILE *fp);
int fputc(FILE *fp);
int putchar(void);
7/16/2015
18
Standard I/O Library
 Functions:
 Character-at-a-time:
char *fgets(char *buf, int n, FILE *fp);
char *gets(char *buf);
char *fputs(char *buf, int n, FILE *fp);
char *puts(char *buf);
7/16/2015
19
Standard I/O Library
 Assignment :
Modify your previous program (about buffer
variation test)
- use buffer size which yields best
performance only
- add the following functions
fgets, fputs, getc, putc, fgetc, fputc
- compare each performance (result from
running your program)
7/16/2015
20
Standard I/O Library
 Assigment (Cont.)
Time
Bytes of
Function
|User System Real
prog text
-------------------------------------------best time from
|
previous program |
fgets, fputs
|
getc, putc
|
fgets, fputc
|
single byte time from |
previous program |
7/16/2015
21