Presentation is loading. Please wait.

Presentation is loading. Please wait.

Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.

Similar presentations


Presentation on theme: "Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3."— Presentation transcript:

1 Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3

2 Circular Buffer a data structure that uses a fixed-sized area of memory as if it were connected end-to-end. good for character I/O #define CBUFSIZE 8 /* size should be power of two */ #define CBUFMASK 0x7 /* wrap around to top by anding mask */ char cbuf[CBUFSIZE]; int in, /* in points to next open location */ out; /* out points to oldest entry */ void initbuf(void){ /* in == out => empty */ in = out = 0; /* (in+1)%buf_size == out => full */ } /* we instead use (in+1)&mask */ /* when buf_size is a power of 2 */ /* and mask = buf_size -1 */

3 Circular Buffer void put(char val) { if(((in+1)&CBUFMASK)==out) /* or use synchronization primitives */ { fprintf(stderr,"full\n"); /* so calls to put() are blocked */ } else /* until buffer is no longer full */ { cbuf[in] = val; in = (in+1)&CBUFMASK; }

4 Circular Buffer char get(void) { register char val; if(in==out) /* or use synchronization primitives */ { fprintf(stderr,"empty\n"); /* so calls to get() are blocked */ return('\0'); /* until buffer is no longer empty */ } else { val = cbuf[out]; out = (out+1)&CBUFMASK; return val; }

5 Double buffering want to reduce or eliminate delay caused by I/O device timeline read data into buffer. process data in buffer P read data into buffer. process data in buffer P read data into buffer. process data in buffer P...

6 Double buffering overlap I/O into one buffer with processing of data in separate buffer timeline read data into buffer 1. read data into buffer 2 process data in buffer 1 P1 process data in buffer 2 read data into buffer 1 P2 read data into buffer 2 process data in buffer 1 P1 process data in buffer 2 read data into buffer 1 P2...

7 Double buffering code structure for N blocks of data: i = 1; read data into buffer 1 while( i < N ){ read data into buffer 1 + (i&1) process data in buffer 2 - (i&1) i++; } process data in buffer 2 - (i&1) or j = 1; k = 2; read data into buffer j for( i = 1; i < N; i++ ){ read data into buffer k process data in buffer j swap(j,k) } process data in buffer j

8 Files request operations by calls to buffered functions from C stdio library, or by calls to functions from the low-level Unix I/O library, or directly invoke the OS by intentional interrupts

9 Files buffered file operations - use file pointers – fopen - pass symbolic name, checks permissions, returns file pointer – fclose - release file (e.g., release from exclusive write access) – fgetc - read single characte – fputc - write single character – fgets - read string – fputs - write string – fscanf - formatted read – fprintf - formatted write – fread - binary read – fwrite - binary write – ftell - return file position – fseek - set file position

10 Files low-level file operations and OS calls - use integer file descriptors – open - pass symbolic name, checks permissions, returns file descriptor – close - release file – read - pass file descriptor, buffer address, and byte count – write - pass file descriptor, buffer address, and byte count standard files in Unix (already open) – stdin (file descriptor = 0) – stdout (file descriptor = 1) – stderr (file descriptor = 2)

11 Files file structure typedef struct { int _cnt; /* number of characters in buffer */ unsigned char * _ptr; /* next character from/to in buffer */ unsigned char * _base ; /* address of the buffer */ unsigned char _flag; /* state of the stream (e.g., r/w, EOF) */ unsigned char _file; /* file descriptor */ unsigned char _opts; /* other options (e.g., seekable) */ } FILE; stored as __iob[] array stdin is &__iob[0], stdout is &__iob[1], stderr is &__iob[2] fopen() returns &__iob[n] for some file descriptor value n

12 Files hierarchy of wrappers -- all leading to invoking the OS

13 Files e.g., here is a stack trace of routines being called for a scanf() (edited from pstack output) ff31f800 read ff310a18 __doscan_u ff3103b8 _doscan ff31651c vscanf ff315314 scanf 0001067c main 000104e8 _start

14 Files example of getchar excerpts from "man getchar" Standard C Library Functions fgetc(3C) SYNOPSIS int fgetc(FILE *stream); int getc(FILE *stream); int getchar(void); DESCRIPTION fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error.... getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream more than once.... getchar() is equivalent to getc(stdin).... RETURN VALUES getc(), getc() and getchar() return the character read as an unsigned char cast to an int or EOF on end of file or error. gets() and fgets() return s on success, and NULL on error or when end of occurs while no characters have been read.

15 Files directory - map filename to disk location(s) of data; also stores file protection information blocking – the transfer of multiple logical records as a single physical record. Thus, the transfer overhead, such as seek and rotational latency on a disk transfer, is only required once for the single physical record rather than for each logical record. logical record 1 logical record 2 logical record 3 transferred as single physical record

16 Files Blocking (continued) logical record is application's unit of transfer physical record is I/O system's unit of transfer multiple logical records per physical record is more efficient – (since on read/write which accesses a logical record already in a buffer there is no physical I/O needed)


Download ppt "Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3."

Similar presentations


Ads by Google