L03

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

1/8/23

Contents

1 A Journey of a Byte

2 Buffer Management

3 Field Structures

4 Record Structures

7 LOGO
7

A Journey of a Byte

8 LOGO
8

1
1/8/23

A Journey of a Byte
v Suppose in our program we wrote char ch=‘p’;
file << ch;
v This causes a call to the file manager (a part of O.S.
responsible for I/O operations)

v The O/S (File manager) makes sure that the byte is


written to the disk.

v Pieces of software/hardware involved in I/O:


§ Application Program
§ Operating System/ file manager
§ I/O Processor
§ Disk Controller
9 LOGO
9

A Journey of a Byte
Step 1

The program asks the operating system to write the contents of


the variable ch to the next available position in the file.

Step 2

The operating system passes the job on to the file manager.

Step 3

The file manager looks up the file in a table containing


information about it, such as whether the file is open and
available for use, what types of access are allowed, if any, and
what physical file the logical name corresponds to.

10 LOGO
10

2
1/8/23

A Journey of a Byte
Step 4

The file manager searches a file allocation table for the physical
location of the sector that is to contain the byte.

Step 5

The file manager makes sure that the last sector in the file has
been stored in a system I/O buffer in RAM, then deposits the ‘p’
into its proper position in the buffer.

Step 6

The file manager gives instructions to the I/O processor about


where the byte is stored in RAM and where it needs to be sent
on the disk.

11 LOGO
11

A Journey of a Byte
Step 7

The I/O processor, finds a time when the drive is available to


receive the data and puts the data in proper format for the disk. It
may also buffer the data to send it out in chunks of the proper
size for the disk.

Step 8

The I/O processor sends the data to the disk controller.

Step 9

The controller instructs the drive to move the read/write head to


the proper track, waits for the desired sector to come under the
read/write head, then sends the byte to the drive to be-deposited,
bit-by-bit, on the surface of the disk.

12 LOGO
12

3
1/8/23

A Journey of a Byte
v Application program
§ Requests the I/O operation

v Operating system / file manager


§ Keeps tables for all opened files
§ Brings appropriate sector to buffer.
§ Writes byte to buffer
§ Gives instruction to I/O processor to write data from this buffer into
correct place in disk.
Note: the buffer is an exact image of a cluster in disk.

13 LOGO
13

A Journey of a Byte
v I/O Processor
§ A separate chip; runs independently of CPU
§ Finds a time when drive is available to receive data and put data
in proper format for the disk
§ Sends data to disk controller

v Disk controller
§ A separate chip; instructs the drive to move R/W head
§ Sends the byte to the surface when the proper sector comes
under R/W head.

14 LOGO
14

4
1/8/23

A Journey of a Byte
v I/O Processor
§ A separate chip; runs independently of CPU
§ Finds a time when drive is available to receive data and put data
in proper format for the disk
§ Sends data to disk controller

v Disk controller
§ A separate chip; instructs the drive to move R/W head
§ Sends the byte to the surface when the proper sector comes
under R/W head.

15 LOGO
15

Buffer Management

16 LOGO
16

5
1/8/23

Buffer Management
v Buffering means working with large chunks of data in
main memory so the number of accesses to secondary
storage is reduced.

v Today, we’ll discuss the System I/O buffers. These are


beyond the control of application programs and are
manipulated by the O.S.

v Note that the application program may implement its own


“buffer” – i.e. a place in memory (variable, object) that
accumulates large chunks of data to be later written to
disk as a chunk.

17 LOGO
17

System I/O Buffer

18 LOGO
18

6
1/8/23

Buffer Bottlenecks
v Consider the following program segment:

while (1)
{
infile >> ch;
if (infile.fail())
break;
outfile << ch;
}

v What happens if the O.S. used only one I/O buffer?


§ Buffer bottleneck

v Most O.S. have an input buffer and an output buffer.

19 LOGO
19

Buffer Strategies
v Double Buffering: Two buffers can be used to allow
processing and I/O to overlap.

§ Suppose that a program is only writing to a disk.


§ CPU wants to fill a buffer at the same time that I/O is being
performed.
§ If two buffers are used and I/O-CPU overlapping is permitted,
CPU can be filling one buffer while the other buffer is being
transmitted to disk.
§ When both tasks are finished, the roles of the buffers can be
exchanged.

v The actual management is done by the O.S.

20 LOGO
20

7
1/8/23

Buffer Strategies

Double Buffering:

21 LOGO
21

Buffer Strategies
v Multiple Buffering: instead of two buffers any number of
buffers can be used to allow processing and I/O to
overlap.

v Buffer pooling:
§ There is a pool of buffers.
§ When a request for a sector is received, O.S. first looks to see that
sector is in some buffer.
§ If not there, it brings the sector to some free buffer. If no free buffer
exists, it must choose an occupied buffer. Usually, LRU (Least
Recently Used) strategy is used.

22 LOGO
22

8
1/8/23

Buffer Strategies
v Move mode (using both system buffer & program buffer)
§ moving data from one place in RAM to another before they can be
accessed
§ sometimes, unnecessary data moves

v Locate mode (using system buffer only or program buffer


only)
§ perform I/O directly between secondary storage and program
buffer (program’s data area)
§ system buffers handle all I/Os, but program uses locations through
pointer variable

23 LOGO
23

Buffer Strategies

24 LOGO
24

9
1/8/23

Fundamental File Structure Concepts

25

25

Student Example

Class Structure

class Cstudent struct student


{ {
char m_id[8]; char m_id[8];
char m_fname[10]; char m_fname[10];
char m_lname[10]; char m_lname[10];
char m_address[15]; char m_address[15];
char m_city [15]; char m_city [15];
char m_country [15]; char m_country [15];
}; };

LOGO
26

10
1/8/23

File Types
v A file can be treated as:

1. a stream of bytes

1202345 Ahmed Hassan 1 Safa street Cairo Egypt 2349890 Mona


Hussein 42 El Thawra street Ismailia Egypt 1210322 Tamer Fouad
123 Ramsis street Cairo Egypt

2. a collection of records with fields

1202345 Ahmed Hassan 1 Safa street Cairo Egypt


2349890 Mona Hussein 42 El Thawra street Ismailia Egypt
1210322 Tamer Fouad 123 Ramsis street Cairo Egypt

LOGO
27

Field & Record Organization


v Field: a data value, smallest unit of data with logical
meaning.

v Record: A group of fields that forms a logical unit.

Memory File
object record
member field

LOGO
28

11
1/8/23

Field & Record Organization


v Key: a subset of the fields in a record used to uniquely
identify the record

Primary Key Secondary Key


must identify records Does not identify records
uniquely uniquely
It is not dataless It is not dataless
Has a canonical form Has a canonical form

Ex. Student ID Ex. Student Name

LOGO
29

Field Structures

30 LOGO
30

12
1/8/23

Field structures
v Methods for organizing fields are:

1. Fix the length of fields

2. Begin each field with a length indicator

3. Separate the fields with delimiters

4. Use a “Keyword=Value” Expression to identify fields

LOGO
31

1- Fix the Length of Fields


1202345 Ahmed Hassan 1 safa street Cairo Egypt

2349890 Mona Hussein 42 El Thawra street Ismailia Egypt

1210322 Tamer Fouad 123 Ramsis street Cairo Egypt

8 10 10 15 15 15

v Advantages:
§ Easy to Read/Store

v Disadvantages:
§ Waste space with padding

LOGO
32

13
1/8/23

2- Begin Each Field with a Length Indicator

1202345 Ahmed Hassan 1 Safa street Cairo Egypt


2349890 Mona Hussein 42 El Thawra street Ismailia Egypt
1210322 Tamer Fouad 123 Ramsis street Cairo Egypt

v Advantages:
§ Easy to jump ahead to the end of the field

v Disadvantages:
§ Long fields require more than 1 byte to store length (Max is 255)

LOGO
33

3- Separate the Fields with Delimiters

1202345 Ahmed Hassan 1 Safa street Cairo Egypt


2349890 Mona Hussein 42 El Thawra street Ismailia Egypt
1210322 Tamer Fouad 123 Ramsis street Cairo Egypt

v Advantages:
§ May waste less space than with length-based

v Disadvantages:
§ Have to check every byte of field against the delimiter

LOGO
34

14
1/8/23

4- Use a “Keyword=Value” Expression to Identify Fields

1202345 Ahmed Hassan 1 Safa street


Cairo Egypt 2349890 Mona Hussein 42
El Thawra street Ismailia Egypt 1210322 Tamer
Fouad 123 Ramsis street Cairo Egypt

v Advantages:
§ Fields are self describing allows for missing fields

v Disadvantages:
§ Waste space with keywords (50% or more of the file’s space could
be taken up by the keywords )

LOGO
35

Record Structures

36 LOGO
36

15
1/8/23

Records Structures
v Methods for organizing records are:

1. Make records a predictable number of bytes ( fixed


length records)

2. Make records a predictable number of fields

3. Begin each record with a length indicator

4. Use an index to keep track of addresses

5. Place a delimiter at the end of each record


LOGO
37

1- Make records a predictable number of bytes


( Fixed Length Records)

1202345 Ahmed Hassan 1 safa street Cairo Egypt

2349890 Mona Hussein 42 El Thawra street Ismailia Egypt

1210322 Tamer Fouad 123 Ramsis street Cairo Egypt

8 10 10 15 15 15

1202345 Ahmed Hassan 1 Safa street Cairo Egypt

2349890 Mona Hussein 42 El Thawra street Ismailia Egypt

1210322 Tamer Fouad 123 Ramsis street Cairo Egypt

73 Bytes

LOGO
38

16
1/8/23

2- Make records a predictable number of fields

1202345 Ahmed Hassan 1 Safa street Cairo Egypt 2349890 Mona


Hussein 42 El Thawra street Ismailia Egypt 1210322 Tamer Fouad
123 Ramsis street Cairo Egypt

v In this example, each record consists of 6 fields.

LOGO
39

3- Begin each record with a length indicator

1202345 Ahmed Hassan 1 Safa street Cairo Egypt 2349890 Mona


Hussein 42 El Thawra street Ismailia Egypt 1210322 Tamer Fouad
123 Ramsis street Cairo Egypt

LOGO
40

17
1/8/23

4- Use an index to keep track of addresses


Data File

1202345 Ahmed Hassan 1 Safa street Cairo Egypt 2349890 Mona


Hussein 42 El Thawra street Ismailia Egypt 1210322 Tamer Fouad
123 Ramsis street Cairo Egypt

Index File

00 47 103 153 … …

LOGO
41

5- Place a delimiter at the end of each record

1202345 Ahmed Hassan 1 Safa street Cairo Egypt 2349890 Mona


Hussein 42 El Thawra street Ismailia Egypt 1210322 Tamer Fouad
123 Ramsis street Cairo Egypt

LOGO
42

18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy