209 98 11MB
English Pages 744 Year 2022
FOUNDATIONS OF COMPUTING Fifth Edition Essential for Computing Studies, Profession and Entrance Examinations
FOUNDATIONS OF COMPUTING Fifth Edition
Essential for Computing Studies, Profession and Entrance Examinations
Pradeep K. Sinha Priti Sinha
www.bpbonline.com
FIFTH EDITION 2023 Copyright © BPB Publications, INDIA ISBN: 978-93-5551-254-3 All Rights Reserved. No part of this publication may be reproduced, distributed or transmitted in any form or by any means or stored in a database or retrieval system, without the prior written permission of the publisher with the exception to the program listings which may be entered, stored and executed in a computer system, but they can not be reproduced by the means of publication, photocopy, recording, or by any electronic and mechanical means.
LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY The information contained in this book is true to correct and the best of author’s and publisher’s knowledge. The author has made every effort to ensure the accuracy of these publications, but publisher cannot be held responsible for any loss or damage arising from any information in this book. All trademarks referred to in the book are acknowledged as properties of their respective owners but BPB Publications cannot guarantee the accuracy of this information.
Distributors: BPB PUBLICATIONS 20, Ansari Road, Darya Ganj New Delhi-110002 Ph: 23254990/23254991
DECCAN AGENCIES 4-3-329, Bank Street, Hyderabad-500195 Ph: 24756967/24756400
MICRO MEDIA Shop No. 5, Mahendra Chambers, 150 DN Rd. Next to Capital Cinema, V.T. (C.S.T.) Station, MUMBAI-400 001 Ph: 22078296/22078297
BPB BOOK CENTRE 376 Old Lajpat Rai Market, Delhi-110006 Ph: 23861747
Published by Manish Jain for BPB Publications, 20 Ansari Road, Darya Ganj, New Delhi-110002 and Printed at Akash Press, New Delhi www.bpbonline.com
LETTER TO READERS………………………………………………………………………………………………………..XV PREFACE……………………………………………………………………………………………………………………………XVI ABOUT LECTURE NOTES PRESENTATION SLIDES…..………………………………..………………....XXI ABBREVIATIONS ………………………………………………….…………….………………………………………….XXIII CHAPTER 1: CHARACTERISTICS, EVOLUTION, AND CLASSIFICATION OF COMPUTERS……1 What is a Computer? ........................................................................................... 1 Characteristics of Computers ................................................................................. 2 Evolution of Computers ........................................................................................ 2 Computer Generations ......................................................................................... 4 First Generation (1942-1955) .............................................................................. 4 Second Generation (1955-1964) ........................................................................... 4 Third Generation (1964-1975) ............................................................................. 5 Fourth Generation (1975-1989) ............................................................................ 6 Fifth Generation (1989-Present) .......................................................................... 8 Classification of Computers .................................................................................. 11 Notebook Computers (Laptops) ........................................................................... 11 Personal Computers (PCs) ................................................................................. 12 Workstations................................................................................................. 13 Mainframe Systems ......................................................................................... 13 Supercomputers ............................................................................................. 15 Client and Server Computers ............................................................................. 17 Handheld Computers ....................................................................................... 18 Points to Remember…………………………………………………………………………………………………………………………20 Questions………………………………………………………………………………………………………………………………………….21
CHAPTER 2: INTERNAL DATA REPRESENTATION IN COMPUTERS .......................... 24 Number Systems Used in Computers ........................................................................ 24 Binary Number System ..................................................................................... 25
ii
■ Foundations of Computing
Octal Number System ...................................................................................... 26 Hexadecimal Number System ............................................................................. 26 Converting from One Number System to Another ........................................................ 26 Converting from Another Base to Decimal ............................................................. 26 Converting from Decimal to Another Base (Division-Remainder Technique) ...................... 28 Converting from a Base Other Than 10 to Another Base Other Than 10 ........................... 29 Shortcut Method for Binary to Octal Conversion ...................................................... 30 Shortcut Method for Octal to Binary Conversion ...................................................... 31 Shortcut Method for Binary to Hexadecimal Conversion ............................................. 32 Shortcut Method for Hexadecimal to Binary Conversion ............................................. 33 Fractional Numbers ........................................................................................... 34 Character Coding Schemes................................................................................... 35 BCD Code .................................................................................................... 36 EBCDIC ....................................................................................................... 38 Zoned and Packed Decimal Numbers .................................................................... 38 ASCII .......................................................................................................... 40 Unicode ...................................................................................................... 44 Collating Sequence ......................................................................................... 47 Binary Arithmetic ............................................................................................. 48 Why Binary? ................................................................................................. 49 Binary Addition ............................................................................................. 49 Binary Subtraction ......................................................................................... 50 Binary Multiplication ...................................................................................... 53 Binary Division .............................................................................................. 55 Why Do Computers Use Additive Method? .............................................................. 56 Points to Remember………………………………………………………………………………………………………………………..56 Questions………………………………………………………………………………………………………………………………………...58
CHAPTER 3: DIGITAL SYSTEMS DESIGN ......................................................... 62 Boolean Algebra ............................................................................................... 62 Fundamental Concepts of Boolean Algebra ............................................................ 62 Postulates of Boolean Algebra ........................................................................... 64 The Principle of Duality ................................................................................... 64 Theorems of Boolean Algebra ............................................................................ 65 Boolean Functions ............................................................................................. 68 Minimization of Boolean Functions by Algebraic Manipulations .................................... 69 Complement of a Function ................................................................................ 71 Canonical Forms of Boolean Functions .................................................................. 72 Conversion between Canonical Forms ................................................................... 76 Minimization of Boolean Functions by Map Simplification ........................................... 77 Logic Gates ..................................................................................................... 84 Logic Circuits .................................................................................................. 87 Converting Expressions to Logic Circuits ............................................................... 88 The Universal NAND Gate ................................................................................. 88 The Universal NOR Gate ................................................................................... 90 Design of Combinational Circuits ........................................................................... 93 Design of Half-Adder....................................................................................... 93
■ Table of Contents
iii
Design of Full-Adder........................................................................................ 94 A Parallel Binary Adder .................................................................................... 96 Flip-Flops ....................................................................................................... 97 Types of Flip-flops .......................................................................................... 97 Excitation Tables ........................................................................................... 99 Edge-Triggered Flip-Flops ............................................................................... 100 Master-Slave Flip-Flop ................................................................................... 101 Uses of Flip-flops ......................................................................................... 101 Sequential Circuits .......................................................................................... 102 Flip-flop Input Equations ................................................................................ 102 State Table ................................................................................................ 102 State Diagram ............................................................................................. 104 Design of Sequential Circuits............................................................................... 104 Design of Binary Counter ................................................................................ 105 Points to Remember……………………………………………………………………………………………………………………..107 Questions……………………………………………………………………………………………………………………………………….109
CHAPTER 4 : COMPUTER ARCHITECTURE ..................................................... 113 Basic Functions of a Computer ............................................................................ Basic Computer Organization .............................................................................. Basic Functional Units .................................................................................... System Concept ........................................................................................... Basic Processor and Memory Architecture ............................................................ CPU Architectures ........................................................................................... Basic Components of CPU ................................................................................ CPU Instruction Set ....................................................................................... CPU Registers .............................................................................................. Processor Speed ........................................................................................... Types of Processors ....................................................................................... Memory Architectures....................................................................................... Storage Evaluation Criteria ............................................................................. Main Memory Organization .............................................................................. Main Memory Capacity ................................................................................... Types of Memory Chips .................................................................................. Minimizing Memory-Processor Speed Mismatch ......................................................... Cache Memory ............................................................................................. Memory Interleaving ..................................................................................... Hybrid Approach .......................................................................................... Memory and I/O Buses ...................................................................................... Memory Buses ............................................................................................. I/O Buses ................................................................................................... Interconnection Architectures ............................................................................. Unibus Architecture ...................................................................................... Dual Bus Architecture .................................................................................... DMA Architecture ......................................................................................... Multiprocessor System Architectures ..................................................................... Shared Memory Systems .................................................................................
113 113 114 115 116 116 116 117 117 119 119 123 123 124 126 126 128 128 130 131 131 131 132 134 134 134 135 136 137
iv
■ Foundations of Computing
Distributed Memory Systems ............................................................................ 139 Parallel Processing Systems ................................................................................. 142 Points to Remember………………………………………………………………………………………………………………………143 Questions……………………………………………………………………………………………………………………………………….145
CHAPTER 5: SECONDARY STORAGE ............................................................ 149 Classification of Secondary Storage ....................................................................... 149 Magnetic Tapes ............................................................................................... 150 Basic Principles of Operation ............................................................................ 151 Types of Magnetic Tapes ................................................................................. 154 Advantages and Limitations of Magnetic Tapes ...................................................... 157 Uses of Magnetic Tapes ................................................................................... 157 Magnetic Disks ................................................................................................ 158 Basic Principles of Operation ............................................................................ 158 Types of Magnetic Disks .................................................................................. 163 Advantages and Limitations of Magnetic Disks ....................................................... 164 Uses of Magnetic Disks.................................................................................... 165 Optical Disks .................................................................................................. 165 Basic Principles of Operation ............................................................................ 166 Types of Optical Disks .................................................................................... 168 Advantages and Limitations of Optical Disks.......................................................... 170 Uses of Optical Disks ...................................................................................... 171 Memory Storage Devices .................................................................................... 171 Solid State Drive (SSD).................................................................................... 171 Flash Drive (Pen Drive) ................................................................................... 173 Memory Card (SD/MMC) .................................................................................. 174 RAM Disk .................................................................................................... 175 Hybrid Secondary Storage Drives .......................................................................... 175 Mass Storage Devices ........................................................................................ 175 Disk Array................................................................................................... 176 Automated Tape Library ................................................................................. 176 CD-ROM Jukebox........................................................................................... 176 RAID Storage .................................................................................................. 177 RAID Techniques ........................................................................................... 177 RAID Levels ................................................................................................. 178 Storage Interfaces............................................................................................ 179 SCSI (Small Computer System Interface) .............................................................. 179 SAS (Serial Attached SCSI) ............................................................................... 180 SATA (Serial Advanced Technology Attachment) ..................................................... 181 Fiber Channel (FC) ........................................................................................ 181 FireWire .................................................................................................... 182 iSCSI (Internet SCSI)....................................................................................... 183 FC versus iSCSI ............................................................................................. 183 DAS, SAN, and NAS ........................................................................................... 184 DAS (Direct Attached Storage) .......................................................................... 184 SAN (Storage Area Network) ............................................................................. 185 NAS (Network Attached Storage) ....................................................................... 186 Data Backup ................................................................................................... 187 What is Data Backup? ..................................................................................... 187 Why Backup Data? ......................................................................................... 187
■ Table of Contents
v
Types of Backup ........................................................................................... Backup Policy .............................................................................................. Backup versus Archiving ................................................................................. Hierarchical Storage System (HSS) ........................................................................ On-line, Near-line, and Off-line Storage .............................................................. Hierarchical Storage Management (HSM) .............................................................
187 188 188 189 190 190
Points to Remember……………………………………………………………………………………………………………………..191 Questions………..…………………………………………………………………………………………………………………………….193
CHAPTER 6: INPUT-OUTPUT DEVICES……………………………………………………………………………197 Input Devices ................................................................................................. Keyboard Devices ......................................................................................... Point-and-Draw Devices .................................................................................. Data Scanning Devices .................................................................................... Digitizer .................................................................................................... Electronic-card Reader ................................................................................... Speech Recognition Devices ............................................................................. Vision-Input Systems ..................................................................................... Output Devices ............................................................................................... Monitors .................................................................................................... Printers ..................................................................................................... Plotters ..................................................................................................... 3D Printers ................................................................................................. Computer Output Microfilm (COM) .................................................................... Screen Image Projector .................................................................................. Voice Response Systems.................................................................................. Ergonomically Designed Devices ........................................................................ I/O Interfaces ................................................................................................ Device Controllers ........................................................................................ Serial and Parallel Ports ................................................................................. SCSI Interface.............................................................................................. USB (Universal Serial Bus) Standard ...................................................................
197 197 200 204 209 210 211 212 213 213 218 224 224 228 229 229 231 231 231 232 234 235
Points to Remember………………………………………………………………………………………………………………………236 Questions………..…………………………………………………………………………………………………………………………….238
CHAPTER 7: SOFTWARE…………………………………………………………………………………………….....242 Basic Concepts and Terminologies ........................................................................ What is Software?......................................................................................... Relationship between Hardware and Software ...................................................... Types of Software ........................................................................................ Logical System Architecture ............................................................................ Firmware ................................................................................................... Middleware ................................................................................................ Acquiring Software .......................................................................................... Buying Pre-written Software ............................................................................ Ordering Customized Software ......................................................................... Developing Customized Software ...................................................................... Downloading Public-domain Software .................................................................
242 242 242 243 243 244 244 246 246 246 247 248
vi ■ Foundations of Computing Software Development Life Cycle (SDLC) ................................................................. 248 Advantages of SDLC Model ............................................................................... 250 Limitations of SDLC Model ............................................................................... 251 Lightweight SDLC Models................................................................................. 251 Software Design Approaches................................................................................ 252 Software Implementation ................................................................................... 254 Software Testing and Debugging ........................................................................... 255 What are Testing and Debugging? ....................................................................... 255 Types of Program Errors.................................................................................. 255 Debugging a Program for Syntax Errors ................................................................ 255 Debugging a Program for Logic Errors .................................................................. 256 Software Testing Concepts............................................................................... 257 Testing Methodologies .................................................................................... 258 Test-set-generation Techniques ........................................................................ 259 Test-case-selection Techniques ......................................................................... 261 Software Deployment ........................................................................................ 262 Changeover Operations ................................................................................... 262 Changeover Methods ...................................................................................... 263 System Evaluation ......................................................................................... 265 Software Maintenance ....................................................................................... 265 Definition ................................................................................................... 265 Need for Software Maintenance ........................................................................ 266 Types of Software Maintenance ......................................................................... 266 Importance of Software Maintenance .................................................................. 266 Controlling Modifications ................................................................................ 266 Tools for Effective Software Maintenance ............................................................ 267 Software Documentation .................................................................................... 267 What is Documentation? .................................................................................. 267 Need for Documentation ................................................................................. 267 Forms of Documentation ................................................................................. 268 Documentation Standard ................................................................................. 270 Software Engineering ........................................................................................ 270 What is Software Engineering? .......................................................................... 270 Need for Software Engineering.......................................................................... 270 Goals of Software Engineering .......................................................................... 271 Principles of Software Engineering ..................................................................... 271 Some Popular Application Software Packages ........................................................... 272 Word-processing Package ................................................................................ 273 Spreadsheet Package...................................................................................... 273 Graphics Package .......................................................................................... 274 Personal-assistance Package ............................................................................. 274 Education Software ....................................................................................... 274 Entertainment Software ................................................................................. 275 Points to Remember……………………………………………………………………………………………………………………..275 Questions………………………………………………………………………….....………………………………..................277
CHAPTER 8: PLANNING THE COMPUTER PROGRAM………………………………………………………280 Purpose of Program Planning ............................................................................... 280 Algorithm ...................................................................................................... 280 What is an Algorithm? .................................................................................... 280
■ Table of Contents
vii
Sample Algorithms ........................................................................................ Quality of Algorithms .................................................................................... Representation of Algorithms........................................................................... Flowcharts .................................................................................................... What is a Flowchart? ..................................................................................... Why Use Flowcharts? ..................................................................................... Flowchart Symbols ........................................................................................ Sample Flowcharts ........................................................................................ Levels of Flowcharts ..................................................................................... Flowcharting Rules ....................................................................................... Advantages and Limitations of Flowcharts ........................................................... Decision Tables .............................................................................................. What is a Decision Table? ................................................................................ Sample Decision Table ................................................................................... Advantages and Limitations of Decision Tables ...................................................... Pseudocode ................................................................................................... What is Pseudocode? ..................................................................................... Pseudocodes for Basic Logic (Control) Structures.................................................... Sample Pseudocode ....................................................................................... Indentation ................................................................................................ Advantages and Limitations of Pseudocode........................................................... Data Structures .............................................................................................. Types of Data Structures ................................................................................ Array ........................................................................................................ Linked List ................................................................................................. Stack ........................................................................................................ Queue ....................................................................................................... Hash Table ................................................................................................. Tree ......................................................................................................... Binary Tree ................................................................................................ Binary Search Tree ....................................................................................... Graph ....................................................................................................... Heap ........................................................................................................
281 282 282 283 283 283 283 286 293 293 294 295 295 296 297 298 298 298 301 302 302 303 303 303 307 309 310 311 313 315 318 320 328
Points to Remember………………………………………………………………………………………………………………………329 Questions………………………………………………………………………………………………..…………………………………….330
CHAPTER 9: PROGRAMMING LANGUAGES……………………...…………………………………………….336 Analogy with Natural Languages ........................................................................... Machine Language ........................................................................................... Advantages and Limitations of Machine Language................................................... Assembly Language .......................................................................................... Assembler .................................................................................................. Advantages of Assembly Language over Machine Language ........................................ Limitations of Assembly Language ..................................................................... Typical Uses of Assembly Language .................................................................... Assembly Languages with Macro Instructions......................................................... High-level Language ......................................................................................... Compiler ................................................................................................... Linker ....................................................................................................... Interpreter .................................................................................................
336 337 338 339 339 341 342 343 343 344 344 347 348
viii ■ Foundations of Computing Intermediate Language Compiler and Interpreter ................................................... 348 Advantages and Limitations of High-level Languages................................................ 349 Object-Oriented Languages ................................................................................. 350 What is Object-Oriented Programming (OOP)? ....................................................... 350 Fundamental Concepts of OOP .......................................................................... 351 Key Elements of Object-Oriented Paradigm .......................................................... 353 Procedure-oriented versus Object-oriented Programming .......................................... 354 Advantages of OOP ........................................................................................ 355 Some High-level Languages ................................................................................. 356 Dealing with So Many High-level Languages .............................................................. 356 Why So Many High-level Languages? .................................................................... 356 Characteristics of a Good Programming Language ................................................... 361 Selecting a Language for Coding an Application ...................................................... 362 Other Types of Languages................................................................................... 362 Scripting Languages ....................................................................................... 362 Fourth and Fifth Generation Languages (4GLs and 5GLs) ........................................... 363 Markup Languages ......................................................................................... 364 Interface Definition Languages (IDL) ................................................................... 366 Other Related Concepts ..................................................................................... 366 Subprogram ................................................................................................ 366 Preprocessor ............................................................................................... 368 Postprocessor .............................................................................................. 369 Points to Remember………………………………………………………………………………………………………………………370 Questions……………………………………………………………………………………………………………………………………….372
CHAPTER 10: OPERATING SYSTEMS……………………………...………………………………………………375 Basic Concepts ................................................................................................ 375 What is an Operating System? ........................................................................... 375 Main Functions of an Operating System ............................................................... 376 Measuring System Performance ......................................................................... 377 Process Management......................................................................................... 377 Process Management in Early Systems ................................................................. 378 Multiprogramming ........................................................................................ 379 Multitasking ................................................................................................ 382 Multithreading ............................................................................................. 382 Multiprocessing ............................................................................................ 384 Time-sharing ............................................................................................... 385 Mutual Exclusion .......................................................................................... 387 Deadlock .................................................................................................... 390 Memory Management ........................................................................................ 392 Uniprogramming Memory Model ........................................................................ 392 Multiprogramming Memory Models ..................................................................... 393 Memory Fragmentation ................................................................................... 394 Paging ....................................................................................................... 395 Virtual Memory ............................................................................................ 397 File Management ............................................................................................. 398 File Structure .............................................................................................. 399 File Access Methods ....................................................................................... 399 File Operations ............................................................................................ 400 File Naming................................................................................................. 400
■ Table of Contents
ix
Directories ................................................................................................. Disk Space Allocation Methods .......................................................................... Device Management ......................................................................................... Controlling I/O Devices .................................................................................. Simple and Easy User Interface to I/O Devices ....................................................... Spooling .................................................................................................... Command Interpretation ................................................................................... Shell ........................................................................................................ Operating System Structure ................................................................................ Layered Structure ........................................................................................ Kernel....................................................................................................... Monolithic Kernel versus Microkernel ................................................................. Resident and Non-Resident Operating System Modules ............................................. OS Capability Enhancement Software .................................................................... Translating Programs ..................................................................................... Library Programs .......................................................................................... Utility Programs .......................................................................................... Some Popular Operating Systems.......................................................................... UNIX ......................................................................................................... MS-DOS ..................................................................................................... Microsoft Windows........................................................................................ Microsoft Windows Server (Earlier Known as Windows NT) ........................................ Linux ........................................................................................................ Mac OS ...................................................................................................... iOS .......................................................................................................... WatchOS .................................................................................................... Android OS ................................................................................................. Real-time, Network and Distributed Operating Systems ............................................... Real-time Operating Systems ........................................................................... Network and Distributed Operating Systems .........................................................
401 403 407 407 408 409 410 411 411 411 411 412 412 412 413 413 413 414 415 416 417 418 418 419 420 421 422 423 423 424
Points to Remember…………………………………………………………………………………………………………………….. 424 Questions……………………………………………………………………………………………………………………………………….427
CHAPTER 11: DATABASE AND DATA SCIENCE……………………………………………………………… 431 Data versus Information .................................................................................... Data Storage Hierarchy ..................................................................................... Standard Methods of Organizing Data .................................................................... File-oriented Approach .................................................................................. Database-oriented Approach ............................................................................ File Management System ................................................................................... File Types .................................................................................................. File Organizations ........................................................................................ File Utilities ............................................................................................... Database Management System ............................................................................. Database Models .......................................................................................... Main Components of a DBMS ............................................................................ Creating and Using a Database ......................................................................... Database Security ......................................................................................... Client-Server Database Systems ........................................................................ Distributed Database Systems ..........................................................................
431 432 433 434 434 435 435 435 439 441 441 446 447 452 453 454
x
■ Foundations of Computing
Active Database Systems ................................................................................. 455 Multimedia Database Systems .............................................................................. 455 Requirements for Multimedia Database Systems ..................................................... 455 Data Science .................................................................................................. 458 Stages in Data Science .................................................................................... 459 Data Warehousing ............................................................................................ 460 Need for a Data Warehouse .............................................................................. 461 Building a Data Warehouse .............................................................................. 461 Data Warehousing Terminologies ....................................................................... 461 Data Mining.................................................................................................... 464 Classification of Data Mining Algorithms .............................................................. 465 Data Mining Techniques .................................................................................. 467 Data Warehousing versus Data Mining ..................................................................... 469 Points to Remember………………………………………………………………………………………………………………………469 Questions……………………………………………………………………………………………………………………………………….471
CHAPTER 12: DATA COMMUNICATIONS AND COMPUTER NETWORKS………………………….474 Basic Concepts ................................................................................................ 474 Basic Elements of a Communication System .......................................................... 474 Data Transmission Modes ................................................................................ 475 Data Transmission Speed ................................................................................. 475 Data Transmission Media ................................................................................. 476 Digital and Analog Data Transmission ..................................................................... 480 Modulation Techniques ................................................................................... 480 Modems ..................................................................................................... 481 Analog versus Digital Transmission ..................................................................... 483 Data Transmission Services .............................................................................. 483 Communication Processors ................................................................................. 484 Multiplexers ................................................................................................ 484 Concentrators .............................................................................................. 486 Front-End Processors (FEP) .............................................................................. 486 Asynchronous and Synchronous Transmission ............................................................ 486 Asynchronous Transmission .............................................................................. 486 Synchronous Transmission................................................................................ 487 Switching Techniques ........................................................................................ 487 Circuit Switching .......................................................................................... 488 Message Switching......................................................................................... 489 Packet Switching .......................................................................................... 490 Routing Techniques .......................................................................................... 490 Place Where Routing Decisions are Made .............................................................. 491 Static and Dynamic Routing.............................................................................. 491 Network Topologies .......................................................................................... 491 Network Types (PAN, LAN, CAN, MAN, and WAN) ....................................................... 493 Communication Protocols ................................................................................... 495 Roles of a Communication Protocol .................................................................... 495 Concept of Layered Protocols in Network Design .................................................... 495 Network Interface Card (NIC) ........................................................................... 496 The OSI Model.............................................................................................. 496 Example of Message Transfer in the OSI Model ....................................................... 499 Internetworking Tools ....................................................................................... 500
■ Table of Contents Wireless Networks ........................................................................................... Types of Wireless Computing Systems ................................................................. Issues in Wireless Computing Systems ................................................................. Wireless Applications .................................................................................... Wireless Communication Technologies ................................................................ Wireless Technologies and Standards .................................................................. Distributed Computing Systems ............................................................................ Grid Computing .............................................................................................. Cloud Computing............................................................................................. Grid versus Cloud ......................................................................................... Service Layers of Cloud Computing .................................................................... Types of Clouds ...........................................................................................
xi 501 501 502 502 503 504 509 511 511 513 513 513
Points to Remember………………………………………………………………………………………………………………………513 Questions…………………………………………………………………………………………………………….....................515
CHAPTER 13: THE INTERNET AND INTERNET OF THINGS…………………………………………….518 Definition and History ....................................................................................... Its Basic Services ............................................................................................. Electronic Mail ............................................................................................ File Transfer Protocol (FTP)............................................................................. Telnet ...................................................................................................... Usenet News ............................................................................................... The World Wide Web (WWW) .............................................................................. WWW Browsers ............................................................................................ Web Services............................................................................................... Internet Search Engines..................................................................................... Major Elements of Internet Search Engines........................................................... Types of Internet Search Engines ....................................................................... Some Popular Internet Search Engines ................................................................ Uses of the Internet ......................................................................................... Other Related Concepts .................................................................................... Intranet .................................................................................................... Proxy Server ............................................................................................... Internet Telephony ....................................................................................... Microbrowser .............................................................................................. Next Generation Internet Protocol (IPV6)............................................................. Internet of Things (IoT) ..................................................................................... Benefits of IoT ............................................................................................ IoT Applications (Uses of IoT) ........................................................................... Challenges in Adoption of IoT...........................................................................
518 518 518 519 519 520 520 521 524 526 526 526 527 527 529 529 530 530 531 532 533 533 534 536
Points to Remember………………………………………………………………………………………………………………………537 Questions……………………………………………………………………………………………………………………………………….539
CHAPTER 14: MULTIMEDIA COMPUTING………………………………………………………………………..541 What is Multimedia? ......................................................................................... What is a Multimedia Computer System? ................................................................. Multimedia Components .................................................................................... Text .........................................................................................................
541 542 542 542
xii ■ Foundations of Computing Graphics .................................................................................................... 543 Animation .................................................................................................. 546 Audio ........................................................................................................ 547 Video ........................................................................................................ 550 Multimedia Applications ..................................................................................... 554 Applications in Education Sector........................................................................ 554 Applications in Entertainment Sector .................................................................. 555 Applications in Corporate Sector ....................................................................... 557 Applications in Science and Engineering Sector ...................................................... 558 Data Compression ............................................................................................ 559 Types of Compression Techniques ...................................................................... 559 Basic Compression Techniques .......................................................................... 560 Some Standard Multimedia Compression Techniques ................................................ 561 Multimedia Synchronization ................................................................................ 567 Temporal Specification ................................................................................... 567 Temporal Synchronization ............................................................................... 568 Media Center Computer ..................................................................................... 569 Media Center PC Features and Functionalities ....................................................... 570 Points to Remember………………………………………………………………………………………………………………………571 Questions……………………………………………………………………………………………………………………………………….573
CHAPTER 15: INFORMATION SECURITY…………………………………………………………………………577 Security Policies versus Mechanisms ...................................................................... 577 Security Goals ................................................................................................ 577 External, Internal, and Communication Security ........................................................ 578 Potential Attacks to Computer Systems .................................................................. 578 Passive Attacks ............................................................................................ 578 Active Attacks ............................................................................................. 579 Cryptography.................................................................................................. 583 Basic Concepts and Terminologies ...................................................................... 583 Basic Requirements ....................................................................................... 584 Symmetric and Asymmetric Cryptosystems ........................................................... 584 Authentication ................................................................................................ 586 What is Authentication? .................................................................................. 586 Types of Authentication Needed ........................................................................ 586 Approaches to Authentication ........................................................................... 586 User Login Authentication ............................................................................... 587 One-way Authentication of Communicating Entities ................................................ 590 Two-way Authentication of Communicating Entities ................................................ 594 Access Control ................................................................................................ 595 What is Access Control? .................................................................................. 595 Access Control Terminologies ........................................................................... 595 Protection Domains ....................................................................................... 596 Access Matrix .............................................................................................. 597 Implementation of Access Matrix as Access Control Lists ........................................... 601 Digital Signature .............................................................................................. 602 Firewall ........................................................................................................ 603 What is a Firewall? ........................................................................................ 603 Types of Firewalls ......................................................................................... 604 Advantages and Limitations of Firewalls .............................................................. 605
■ Table of Contents Software Piracy Protection ................................................................................. What is Software Piracy? ................................................................................ Mechanisms to Protect Software Piracy ............................................................... e-Mail Security ............................................................................................... Pretty Good Privacy (PGP) ............................................................................... Privacy Enhanced Mail (PEM) ............................................................................ Secure/Multipurpose Internet Mail Extensions (S/MIME) ........................................... Spam E-mail and Anti-spam Software ................................................................. IP Security (IPSec) ........................................................................................... What is IPSec? ............................................................................................. IPSec Functionality ....................................................................................... IPSec Features ............................................................................................. Secure Socket Layer (SSL) and Transport Layer Security (TLS) ....................................... SSL Connection and Session ............................................................................. SSL Architecture .......................................................................................... SSL Record Protocol ...................................................................................... SSL Change Cipher Spec Protocol ....................................................................... SSL Alert Protocol ........................................................................................ SSL Handshake Protocol .................................................................................. Transport Layer Security (TLS) ......................................................................... Secure Electronic Transaction (SET) ...................................................................... Parties Involved in an Electronic Transaction ........................................................ An Electronic Transaction Process...................................................................... Security Requirements of an Electronic Transaction Process ...................................... How SET Meets These Security Requirements? ....................................................... Dual Signature Mechanism of SET ...................................................................... Management of Public Keys ................................................................................ Certification Authority (CA) ............................................................................ Public Key Infrastructure (PKI) ......................................................................... Steganography ............................................................................................... What is Steganography? .................................................................................. Mechanism to Detect Steganographically-hidden Message ......................................... Guidelines for Secure Systems Design .................................................................... Cyber Laws, Cyber Forensics, and CERT.................................................................. Cyber Laws ................................................................................................. Cyber Forensics ........................................................................................... CERT ........................................................................................................
xiii 606 606 606 607 608 609 610 612 614 614 615 616 616 617 618 618 619 619 621 624 625 626 626 627 627 628 630 630 631 632 632 633 634 635 635 635 636
Points to Remember………………………………………………………………………………………………………………………636 Questions…………………………………………………………………………………………………………….....................637
CHAPTER 16: APPLICATION DOMAINS…………………………………………………………………………..640 Electronic Governance (E-governance) ................................................................... What is E-governance? ................................................................................... Benefits of E-governance ................................................................................ Issues and Challenges in E-governance ................................................................ E-governance Applications .............................................................................. Geo-informatics .............................................................................................. Geo-informatics Applications ........................................................................... Medical Informatics ......................................................................................... Medical Informatics Applications .......................................................................
640 640 640 642 642 643 643 644 644
xiv ■ Foundations of Computing Bioinformatics ................................................................................................ 645 What is Bioinformatics? .................................................................................. 645 Branches of Bioinformatics .............................................................................. 646 Points to Remember……………………………………………………………………………………………………………………..648 Questions…………………………………………………………………………………………………………….....................648
GLOSSARY…………………………………………………………………………………………………………………………650 INDEX……………………………………………………………………………………………………………………………….696 KNOW YOUR AUTHOR .……………………………………………………………………………………………………711
LETTER TO READERS
Dear Reader, A book that connects well with your thoughts and learning process can be written undoubtedly by an author who shares the same thoughts, understanding, and passion for learning as yours. Over three decades ago, I introduced my first book with a strong conviction to the above statement. That commitment continues even today. I am committed to writing quality books in Computer Science and associated areas for my readers. To date, I have authored six textbooks targeted towards this endeavor, and plan to author more in future. Wide popularity of my books clearly indicates the support that I have received from my readers. I express my gratitude and regards to all my readers for their support and feedback. Your support provides greater strength and inspires me to work harder towards my commitment. I look forward to your continued encouragement.
Yours truly,
January 2023
Pradeep K. Sinha
Fellow IEEE
ACM Distinguished Engineer
Fellow CSI
Audience The current edition of Foundations of Computing is based on the feedback from academia, industry, and candidates preparing for entrance examinations.
Academia Several academic institutions today offer degree/diploma/certificate programs in Computer Science (CS), Computer Applications (CA), Information Technology (IT), and other related areas. Few examples of such programs are BE (CS), BE (IT), B.Tech (CS), B.Tech (IT), MCA, MCS, MCM, M.Sc. (IT), BCA, BCS, B.Sc. (IT), DCA, PGDCA, C-DAC’s Pre-DAC and Diploma programs, and DOEACC Certificate Courses. The primary objective of these education programs is to impart knowledge of the various topics in CS, CA, and IT to their students to meet the industry’s demand for IT professionals. Hence, these programs include various foundation courses in CS, CA, and IT, such as Computer Organization, Programming Fundamentals, Digital Systems Design, Data Structures, Operating Systems, Software Engineering, Computer Networks, and Information Security. Since long time, both students and faculty members have wished for a single comprehensive book that can provide a good coverage of the essentials of these foundation courses in a consistent style. Foundations of Computing is designed to meet this requirement of academia.
Industry Different IT professionals specialize in different areas. A few such areas are System Analysis and Design, Application Programming, System Programming, Data Science, Network Management, and System Administration. A professional working in one such area often needs the understanding of other areas for effective delivery of his/her tasks. For example, a system administrator needs to refer to topics in operating systems, networking, security, and computer architecture to perform his/her job effectively. Similarly, an application programmer needs to refer to topics in software engineering, operating systems, security, networking, database management, and computer architecture to develop good applications. Often, IT professionals refer to the topics in other areas for broadening their knowledge for their professional growth. This requires them to refer to multiple books by different authors. Since long time, they have desired to have a single book that covers all these topics in sufficient depth. They expect the book to serve as a good handbook (reference material) for various topics in CS, CA, and IT to save them from the hassles of purchasing and referring to multiple books. Foundations of Computing is designed to meet this requirement of IT professionals.
■ Preface
xvii
Entrance Examination Candidates To have a bright future in any profession today, one cannot ignore having a sound foundation in IT, because IT is an integral part of all types of professions. Hence, entrance examinations for admissions to several educational institutions/programs and entrance tests for many types of jobs (such as engineering, management, banking, law, administrative services, journalism, etc.) test basic understanding of computing concepts of the candidates. For a long time, candidates preparing for such entrance tests and competitive examinations have desired for a single book that they can use to prepare for various topics in computing. Foundations of Computing is designed to meet this requirement of candidates preparing for various types of entrance examinations and tests.
Usages Although, Foundations of Computing is suitable for a wide range of readers, its four primary usages are as follows:
Textbook Foundations of Computing is suitable for use as a textbook for the various foundation courses taught in Master’s, Bachelor’s, Diploma, and Certificate programs in Computer Science (CS), Computer Applications (CA), and Information Technology (IT). Textbook style of presentation of the topics covered in the book and the Lecture Notes Presentation Slides included with the book make it suitable for use as a textbook for such courses. Hence, we recommend its use as a textbook for several foundation courses taught in CS, CA, and IT programs.
Reference Book for Students Where Foundations of Computing is not a prescribed textbook, students will still find it useful to have it as a reference book. As the book covers most of the important topics that students of CS, CA, and IT programs learn during their education programs, they will find it handy and useful as a reference book during the entire period. The book will also serve as a good reference book after they complete their education programs and enter into their professional career. Hence, we recommend every student of CS, CA, and IT programs to keep a copy of the book handy and refer to it whenever required during and even after their education programs.
Reference Book for Professionals As Foundations of Computing covers the important topics of IT profession, IT professionals will find it a useful reference book for addressing their needs. Hence, we recommend every IT professional to keep a copy of the book and refer to it whenever required during their professional career.
Preparation Book for Entrance Examination Candidates Candidates preparing for entrance examinations for admissions to several educational institutions/programs and entrance tests for many types of jobs will find the book handy and useful for preparing for various topics of CS, CA, and IT. Such candidates will find the book and its Lecture Notes Presentation Slides the most suitable material for this purpose. Once they qualify in their desired examination, they can use the book later as a good reference book while pursuing their education programs or professional career.
xviii
■ Foundations of Computing
Features Overall Features The layout, organization, and contents of the book are carefully designed to present the various topics of foundation courses in CS, CA, and IT in an interesting, logical, and informative manner. The book does not concentrate on any particular computer system or hardware. Instead, it discusses, in a general setting, the organization, functions, programming, and usage principles applicable to all types of computer systems. All concepts presented in the book are illustrated with practical examples whenever required. The book also contains numerous diagrams for better illustration. These features enable the readers to grasp the presented concepts easily and to use the book effectively for self-study. Each chapter contains a section on ‘Points to Remember’ that highlights the important points covered in the chapter. Each chapter also contains a number of review questions that test the understanding of the topics covered in the chapter. The book contains a separate list of abbreviations used in the book, and a glossary of important terms. To make the contents interesting to read and easily understandable, the style of presentation used throughout the book is motivational, non-mathematical, and explanatory in nature. The book contains a set of presentation slides for each chapter in the form of Lecture Notes Presentation Slides. It can serve as lecture aid, self-paced learning aid, and revision material. All these features make the book ideal for all types of audience mentioned earlier.
New Edition Features Readers will find this edition of the book more useful than its previous editions because: We have added new topics and classifications to various chapters, introducing the readers to the latest trends and technologies in computing. We have updated the contents to make it up-to-date. We have updated the lecture notes presentation slides. The size of the book is made more handy. Statistical data indicates that with every new edition of the book, the number of readers has soared much higher than its previous edition. This is because every new edition incorporates the suggestions received from the readers, making the book more suitable for a larger audience. We expect this edition to have a similar impact.
Contents The book begins with an introduction to computers. Chapter 1 explains the characteristics of this versatile tool, and its evolution from its early days to different types and forms in which it is available today. Chapter 2 deals with the internal data representation in computers and the manner in which computers perform various basic operations (comparison, sorting, addition, subtraction, multiplication, division, etc.) on these data. It
■ Preface
xix
begins with the fundamental concepts of number systems and examples of popular number systems that are commonly used by computer professionals. It then presents the popular coding schemes, which computers use to record data internally. Chapter 3 deals with the design of digital systems. It begins with an introduction to Boolean algebra. It then explains how Boolean algebra, logic gates and flip-flops are used to design digital circuits. It illustrates the design of digital circuits (both combinational and sequential) with suitable examples. Chapter 4 presents an overview of computer system architecture. It first introduces the basic functional units of a computer system and then deals with the architectures of processor and memory. Subsequently, it presents interconnection architectures for interconnecting the processors, memory, and I/O units. It also deals with multiprocessor system architectures. Chapter 5 presents the terminologies, principles of operation, uses, and trade-offs of different types of secondary storage devices. It also describes memory storage devices, mass storage devices, RAID storage, storage interfaces, various options available for processor-to-storage connectivity (DAS, SAN, and NAS), data backup, and Hierarchical Storage System (HSS). Chapter 6 presents the structure and functioning of various types of I/O devices. It also describes the commonly used types of I/O interfaces for connecting I/O devices to computer systems. Chapter 7 introduces the concept of software and explains the need for both hardware and software for a computer to do useful job. It describes the various types of software, and the different ways to acquire or develop software. It presents Software Development Life Cycle (SDLC) phases, software design approaches (top-down, bottom-up, and out-of-the-middle), software testing methodologies, software deployment methods, software maintenance, software documentation, and software engineering. It also introduces some popular application software packages. Chapter 8 deals with the tools that are commonly used for planning the logic of computer programs. These include flowcharts, decision tables, and pseudocodes. It also presents the various types of data structures used in programming. These include array, linked list, stack, queue, hash table, tree, binary search tree, graph, and heap. Chapter 9 deals with the programming languages that are commonly used for writing computer programs. It describes the various program development tools such as compiler, linker, interpreter, intermediate language compiler and interpreter, preprocessor, and postprocessor. It then explains how to choose a programming language for coding an application. Chapter 10 presents the need, functionalities, and major components of an operating system. It deals with the basic design principles of major components of an operating system, including process management, memory management, file management, device management, and command interpretation modules. It also introduces some popular operating systems (Unix, MS-DOS, MS-Windows, Linux, MacOS, iOS, WatchOS, Android OS), and some special types of operating systems (real-time, network, and distributed operating systems). Chapter 11 deals with database and data science. It explains the difference between data and information, and the process of converting data into information. It describes data storage hierarchy, standard methods of organizing data, difference between File Management Systems (FMS) and Database Management Systems (DBMS), and types of database systems. It also presents the basic concepts of data science and the stages involved in finding useful patterns in data to help decision makers make meaningful decisions. Finally, it introduces the concept of data warehousing and data mining, and explains the difference between them. Chapter 12 deals with data communication technologies and their use in building various types of computer networks. It describes the techniques, channels, and devices, which communication systems use to communicate data from one location to another. It then describes the various types of computer networks, communication protocols,
xx
■ Foundations of Computing
internetworking tools, wireless communication technologies, and wireless networks. It also describes a few paradigms of distributed computing systems, such as grid computing and cloud computing. Chapter 13 describes the Internet, its various uses, and tools and technologies available to the Internet users for making effective use of the resources on the Internet. It also describes the Internet of Things (IoT), the new generation of the Internet. Chapter 14 explains multimedia computing systems and their applications. It deals with the hardware and software requirements for processing various multimedia data types (text, graphics, animation, audio, and video), data compression techniques, and multimedia synchronization techniques. It also introduces media center computer, which is a popular electronic equipment used in homes. Chapter 15 deals with the security requirements of computer systems and the various policies and mechanisms, which computer systems use to deal with them. It describes potential attacks to computer systems (passive and active type attacks), cryptography (symmetric and asymmetric cryptosystems), authentication mechanisms (in stand alone and networked systems), access control mechanisms, digital signature, firewall, software piracy protection, e-mail security, IPSec., Secure Socket Layer (SSL), Transport Layer Security (TLS), Secure Electronic Transaction (SET), Public Key Infrastructure (PKI), and steganography. It also presents the guidelines for designing secure systems, and introduces cyber laws, cyber forensics, and CERT (Computer Emergency Response Team). Chapter 16 deals with a few popular application domains including e-governance, geoinformatics, medical informatics, and bioinformatics.
Acknowledgement A book of this type naturally evolves from a number of ideas from previous books dealing with the subject matter presented in this book. The authors express their thanks to all these authors, too numerous to acknowledge individually. We are indebted to all the readers and other experts who have used the earlier editions of this book and have provided valuable inputs about its contents. Their inputs have helped in gradually improving the quality of the book from one edition to another. We are thankful to Mr. Gaur Sunder for providing valuable inputs on technical contents of the book. His inputs have helped in improving the book’s quality. We are thankful to Mr. Akash Mondal and Mr. Harshit Poddar for designing the cover of this edition of the book, and to Mr. Rajeev Rudrakshi for his help in improving the quality of the Lecture Notes Presentation Slides. We are thankful to Mrs. Sushma Pawar, Mrs. Prajakta Kulkarni, Mrs. Arpita Kulkarni and Mr. Sandeep Kesavan for their support activities. We are also thankful to Mr. Manish Jain and other members of BPB Publications for their support and help in publishing the work. Finally, we would like to thank our son, Deeptanshu, for his loving support and understanding during the entire period of this long project. We dedicate the book to our parents whose sacrifices, love, and affection made us capable of carrying out such work.
Pradeep K. Sinha Priti Sinha January 2023
Foundations of Computing is widely adopted as a textbook for various courses in Computer Science, Computer Applications, Computer Engineering, Information Technology, and related areas by many universities, colleges, and training institutions. Faculty members and students have praised the book’s contents and its presentation style. They have also provided valuable feedback to increase its usefulness. Need for lecture notes in the form of presentation slides, is an important feedback received from many faculty members. On the other hand, several students have requested for a quick review material based on the book’s contents to enable them to quickly recall the knowledge gained from the book just before an examination or interview. To address these requirements, we have thoughtfully prepared and included lecture notes presentation slides with the book. It contains chapter-wise Portable Document Format (PDF) files. Each chapter’s PDF file has presentation slides covering the main points of that chapter. The lecture notes presentation slides is not a substitute for the textbook. They complement each other. Hence, real benefits can be derived when the book and lecture notes presentation slides are used as mentioned here. We believe that the book and the lecture notes presentation slides together form the best instructional package currently available to faculty members for teaching the topics covered in the book.
Use by Faculty Members Faculty members should prepare for their lectures using the book. However, to save time in drawing diagrams and writing notes on the board during a lecture, they can use the presentation slides of a chapter being taught in one of the following manner: 1.
If a projection system connected to a PC or laptop is available in the classroom, they can display the corresponding presentation slides on a board/screen while delivering the lecture.
2.
If a projection system is not available, they can take a hardcopy printout of the presentation slides and photocopy them on OHP sheets. They can then use an Overhead Projection (OHP) system in classroom to display the corresponding slides while delivering the lecture.
3.
If neither a projection system, nor an OHP system is available for use in classroom, they can print handout material containing multiple presentation slides on a single page.
Use by Students Students should primarily use the book for acquiring knowledge on the topics covered. This is because the lecture notes presentation slides cover only the main points and not full details and explanation of the subject being dealt with. Hence, students can use the lecture notes presentation slides to:
xxii
■ Foundations of Computing 1. Browse through the main points of a chapter before going to the class on the day that chapter is to be taught. This will enable them to have better understanding of the topics being taught in the class. 2. Review just before an examination or interview the main points of the topics they have already learnt through the book. Normally, students do not have sufficient time just before their examination or interview to go through the contents of the book.
How to Access the Lecture Notes Presentation Slides Follow the following steps to access the lecture notes presentation slides: 1. Scan the QR Code with your smart device 2. Google Drive will open 3. Click on Shared With Me 4. You will see the Presentation Slides 5. You can either download them or save them to your Drive Readers who do not have a smart device with the facility to scan the QR Code can type the link https://rebrand.ly/0aa3e4 in their browser and download the PDF file containing the presentation slides on their laptop/desktop/mobile phone.
1, 2, 3, 4, 5 1G 2G 3D 3G 4G 4GL 5G 5GL
First Generation Second Generation Three Dimensional Third Generation Fourth Generation Fourth Generation Language Fifth Generation Fifth Generation Language
A A/D ABC ACL ACM AHCI AI AL ALGOL ALU AM
Accumulator register Analog-to-Digital Atanasoff Berry Computer Access Control List Association for Computing Machinery Advanced Host Controller Interface Artificial Intelligence Arbitrated Loop ALGOrithmic Language Arithmetic Logic Unit Amplitude Modulation; Additive Manufacturing Advanced Micro Devices Advanced Mobile Phone System American National Standards Institute America OnLine A Programming Language Advanced Research Project Agency Network Authentication Server American Standard Code for Information Interchange Adaptive Software Development Agile Software Process Advanced Technology Attachment Automatic Teller Machine; Asynchronous Transfer Mode
A
AMD AMPS ANSI AOL APL ARPANET AS ASCII ASD ASP ATA ATM
B B2B BARC BASIC
Business to Business Bhabha Atomic Research Centre Beginners All-purpose Symbolic Instruction Code
BCD BIOS BIPS B-ISDN BLOBs BMP BNA BOT BPI BPS BSD BSNL
Binary Coded Decimal Basic Input Output System Billions of Instructions Per Second Broadband ISDN Binary Large OBjects Basic Multilingual Plan Burroughs Network Architecture Beginning-Of-Tape Bytes Per Inch Bytes Per Second Berkeley Software Distribution Bharat Sanchar Nigam Limited C
C# CA CAD CAE CAM CAN CASE CAV CBC CBQ CBR CCITT CD C-DAC CDC CDMA CDPD CD-R CD-ROM CD-RW CERT CGA CGI CHS CISC CL CLI CLR
C sharp Certification Authority Computer Aided Design Computer Aided Engineering Computer Aided Manufacturing Campus Area Network Computer Aided Software Engineering Constant Angular Velocity Cipher Block Chaining Content Based Querying Content Based Retrieval Consultative Committee for International Telephony and Telegraphy Compact Disk Centre for Development of Advanced Computing Control Data Corporation Code Division Multiple Access Cellular Digital Packet Data CD-Recordable Compact Disk Read Only Memory CD Read/Write Computer Emergency Response Team Color Graphics Adapter Common Gateway Interface Cylinder Head Sector Complex Instruction Set Computer Command Language Common Language Infrastructure; Command Line Interface Common Language Runtime
xxiv
■ Foundations of Computing
CLV CNC COBOL CODASYL COM CORBA COWs CPPM CPS CPU CRL CRT CSCW CU
Constant Linear Velocity Computer Numeric Control COmmon Business Oriented Language Conference On DAta SYstems Languages Computer Output Microfilm; Component Object Model Common Object Request Broker Architecture Cluster of Workstations Content Protection for Prerecorded Media Characters Per Second Central Processing Unit Certificate Revocation List Cathode Ray Tube Computer Supported Cooperative Working Control Unit D
D Flip-flop D/A DAB DAS DAT DBMS DBS DCBR DCE DCOM DCT DDB DDL DDS DEC DES DLP DMA DML DMLS DNA DNS DoD DOS DPI DRAM DRDO DS DSDD DSHD DSN DSS DTS DVD
Data Flip-flop Digital-to-Analog Device Address Bus Direct Attached Storage Digital Audio Tape Data Base Management System Demand Based Switching Dynamic Content Based Retrieval Distributed Computing Environment Distributed Computing Object Model Discrete Cosine Transform DNA Data Bank Data Definition Language Digital Data Storage Digital Equipment Corporation Data Encryption Standard Digital Light Processing Direct Memory Access Data Manipulation Language Direct Metal Laser Sintering Digital Network Architecture; Dexoyribonucleic Acid Domain Name System U.S. Department of Defense Distributed Operating System Dots Per Inch Dynamic RAM Defense Research and Development Organization Dual Signature Double Sided Double Density Double Sided High Density Distributed Systems Network Decision Support System Digital Theater System Digital Video/Versatile Disk
E EBCDIC
Extended Binary Coded Decimal Interchange Code ECA-rule Event Condition Action-rule ECMA European Computer Manufacturers Association EDGE Enhanced Data-rate for Global Evolution EDSAC Electronic Delay Storage Automatic Calculator EDVAC Electronic Discrete Variable Automatic Computer EEPROM Electrically EPROM EFM Eight-to-Fourteen Modulation EGA Extended Graphics Adapter E-Governance Electronic Governance EHR Electronic Health Record EISA Bus Extended Industry Standard Architecture Bus ELD Electro Luminescent Display EMBL European Molecular Biology Laboratory ENIAC Electronic Numerical Integrator And Calculator EOT End-Of-Tape EPG Electronic Programming Guide EPIC Explicitly Parallel Instruction Computing EPROM Erasable Programmable Read-Only Memory ERNET Education and Research NETwork ESDI Enhanced Small Device Interface ESMR Extended Specialized Mobile Radio ETSI European Telecommunications Standard Institute F FA FAT FC FDD FDM FEP FIFO FLOPS FM FMS FORTRAN FOSS FSK FTP
Full Adder File Allocation Table Fiber Channel Feature Driven Development Frequency Division Multiplexing Front End Processor First-In-First-Out Floating Point Operations Per Second Frequency Modulation File Management System FORmula TRANslation Free and Open Source Software Frequency Shift Keying File Transfer Protocol G
GB GEO GFLOPS GHz GIGO GIS
Giga Bytes Geosynchronous Earth Orbit Giga FLOPS Giga Hertz Garbage In Garbage Out Geographical Information System
■ Abbreviations GNU GPD GPL GPRS GPS GSM GUI
Gnu Not Unix Gas Plasma Display General Public License General Packet Radio Service Global Positioning System Global System for Mobile communication Graphical User Interface
ISAM iSCSI ISDN ISO ISP IT ITU IV
xxv
Indexed Sequential Access Method Internet SCSI Integrated Services Digital Network International Standards Organization Internet Service Provider Information Technology International Telecommunication Union Initialization Vector
H HA HDD HD-MAC HDTV HIS HP HSCSD HSM HSS HT HTML HTTP Hz
Half Adder Hard Disk Drive High Definition Multiple Analog Components High Definition Television Hospital Information System Hewlett Packard High Speed Circuit Switched Data Hierarchical Storage Management Hierarchical Storage System HyperTransport HypterText Markup Language HyperText Transport Protocol Hertz I
I I/O IaaS IBG IBM IC IDE iDEN IDL IDN IEEE IETF IIOP IMT-2000 INSAT IoT IP IPI-3 IPSec IPV4 IPV6 IQN IrDA IRG ISA ISA Bus ISAKMP
Instruction register Input and Output; I/O register Infrastructure as a Service Inter Block Gap International Business Machines Integrated Circuit Integrated Drive Electronics Integrated Digital Enhanced Network Interface Definition Language; Intermediate Definition Language Integrated Digital Networks Institute of Electrical and Electronics Engineers Internet Engineering Task Force Internet Interoperable Protocol International Mobile Telecommunications 2000 Indian National SATellite Internet of Things Internet Protocol Intelligent Peripheral Interface-Level 3 IP Security Internet Protocol Version 4 Internet Protocol Version 6 iSCSI Qualified Name Infrared Data Association Inter Record Gap Instruction Set Architecture Industry Standard Architecture Bus Internet Security Association and Key Management Protocol
J J2EE J2SE JCL JIT JK Flip-flop JPEG JRE JSP
Java 2 Enterprise Edition Java 2 Standard Edition Job Control Language Just-In-Time Jack Kilby Flip-flop Joint Photographic Experts Group Java Runtime Engine Java Server Pages
KB KDD KHz K-map
Kilo Bytes Knowledge Discovery in Databases Kilo Hertz Karnaugh map
K
L LAN LCD LED LEO LIFO LiLo LISP LMDS LPM LQ LRU LSB LSD LSI LTE
Local Area Network Liquid Crystal Display Light Emitting Diode Low Earth Orbit Last-In-First-Out Linux Loader LISt Processing Local Multipoint Distribution System Lines Per Minute Letter Quality Least Recently Used Linux Standard Base Least Significant Digit Large Scale Integration Long Term Evolution M
MAB MAC MAN MAR MB MBR MCA Bus MD MDDB MEME
Memory Address Bus Message Authentication Code Metropolitan Area Network Memory Address Register Mega Bytes Memory Buffer Register Micro Channel Architecture Bus Molecular Dynamics; Message Digest MultiDimensional DataBase Multiple Expectation Maximization for motif Elicitation
xxvi
■ Foundations of Computing
MEO MFLOPS MFT MHz MICR MIDI MIDL MIME MIMO MIPS MMC MMS MNP4 MNP5 MODEM MOLAP MP3 MPEG MPI MSCDEX MSD MS-DOS MSI MTAs MTBL MTNL MUSE MVT
Medium Earth Orbit Mega FLOPS Multiprogramming with Fixed Tasks Mega Hertz Magnetic Ink Character Recognition Musical Instrument Digital Interface Microsoft’s IDL Multiple Internet Mail Extensions Multiple Input Multiple Output Millions of Instructions Per Second Multimedia Card Multimedia Messaging Service Microcom Network Protocol Version 4 Microcom Network Protocol Version 5 MOdulator/DEModulator Multidimensional OLAP MPEG-1 Audio Layer 3 Moving Pictures Expert Group Message Passing Interface MicroSoft CD EXtension Most Significant Digit MicroSoft Disk Operating System Medium Scale Integration Mail Transfer Agents Mean Time Between Data Loss Mahanagar Telephone Nigam Limited Multiple SubNyquist Sampling Encoding Multiprogramming with Variable Tasks N
NaReGI NAS NATing NCMRWF NCQ NCSA NFC NIC NICNET NLP NLQ NOS NSF NTQ NTSC
National Research Grid Initiative Network Attached Storage Network Address Translating National Center for Medium Range Weather Forecasting Native Command Queuing National Center for Supercomputing Applications Near Field Communication National Informatics Centre; Network Interface Card National Informatics Center NETwork Natural Language Processing Near Letter Quality Network Operating System National Science Foundation Near Typeset Quality National Television Systems Committee
OMG OMR OOD OOP ORB OS OSF OSI OSS
Object Management Group Optical Mark Reader Object-Oriented Design Object-Oriented Programming Object Request Broker Operating System Open Software Foundation Open System Interconnection Open Source Software P
P2P PaaS PAL PAN PATA PB PC PCB PCI PCIe PCI-X PCS PDA PDF PDL PDP PEM PFLOPS PGP PIM PIP PKI PL/1 PM POP POS POTS PPM PPP PPU PRIMENET PROLOG PROM PSTN PVC PVM
Point to Point Platform as a Service Phase Alternating Line Personal Area Network Parallel ATA Peta Bytes Personal Computer; Program Control register Printed Circuit Board; Process Control Block Peripheral Component Interface PCI Express PCI Extended Personal Communication Services Personal Digital Assistant Portable Document Format Program Design Language Programmed Data Processor Privacy Enhanced Mail Peta FLOPS Pretty Good Privacy Personal Information Manager Peripheral Interchange Program Public Key Infrastructure Programming Language One Phase Modulation Procedure Oriented Programming Point-Of-Sale Plain Old Telephone Service Pages Per Minute Point-to-Point Protocol Peripheral Processing Unit PRIME Computers NETwork PROgramming in LOGic Programmable Read-Only Memory Public Switched Telephone Network Poly Vinyl Chloride Parallel Virtual Machine
O OCR OFDM OHA OHCI OLAP OLTP
Optical Character Recognition Orthogonal Frequency Division Multiplexing Open Handset Alliance Open Host Controller Interface On-Line Analytical Processing On-Line Transaction Processing
Q QBE QIC QoS Q-table
Query By Example Quarter Inch Cartridge Quality of Service Quantization Table
■ Abbreviations R RA RAID RAM RDD REL RGB RISC rlogin RMI RNA ROLAP ROM RPC RSA RUP
Regional Authority Redundant Array of Inexpensive Disks Random Access Memory Rights Data Dictionary Rights Expression Language Red, Green and Blue Reduced Instruction Set Computer Remote Login Remote Method Invocation Ribonucleic Acid Relational OLAP Read Only Memory Remote Procedure Call Rivest Shamir Adleman Rational Unified Process
SW
SA SaaS SAN SAS SATA SCBR SCSI SD SDK SDLC SECAM SEQUEL SERC SET SGML SHA SIMM SLIP SLS SM SMR SMS SMTP SNA SNOBOL SONET SPOOL SQL SR Flip-flop SRAM SSD SSI SSL STL
Secure/Multipurpose Internet Mail Extensions Security Association Software as a Service Storage Area Network Serial Attached SCSI Serial ATA Static Content Based Retrieval Small Computer System Interface Secure Digital Software Development Kit Software Development Life Cycle Sequential Couleur Avec Memoire Structured English QUEry Language Supercomputer Education and Research Center Secure Electronic Transaction Syntax for Generalized Markup Language Secure Hash Algorithm Single In-line Memory Module Serial Line Internet Protocol Selective Laser Sintering Subtractive Manufacturing Specialized Mobile Radio Short Message Service Simple Mail Transfer Protocol Systems Network Architecture StriNg Oriented symBOlic Language Synchronous Optical NETwork Simultaneous Peripheral Operation OnLine Structured Query Language Set-Reset Flip-flop Static RAM Solid State Drive Small Scale Integration Secure Socket Layer STereoLithography
Switched Fabric T
T Flip-flop TB TCO TCP TCP/IP TD-CDMA TDM TDMA TFLOPS TLS TQ TV
S S/MIME
xxvii
Toggle Flip-flop Tera Bytes Total Cost of Ownership Transport Control Protocol Transmission Control Protocol/Internet Protocol Time Division CDMA Time Division Multiplexing Time Division Multiple Access Tera FLOPS Transport Layer Security Typeset Quality Television U
UBE UDP ULSI UMTS UNIVAC UPC URL USB UTF UTP UVEPROM UWB
Unsolicited Bulk E-mail User Datagram Protocol Ultra Large Scale Integration Universal Mobile Telecommunication System UNIVersal Automatic Computer Universal Product Code Uniform Resource Locator Universal Serial Bus Unicode Transformation Format Unshielded Twisted Pair Ultra Violet EPROM Ultra Wide Band V
V2I V2P V2V VAN VCR VDT VGA VLSI VOD VoIP VPN VSAT VSNL VTOC
Vehicle-to-Infrastructure Vehicle-to-Person Vehicle-to-Vehicle Value Added Network Video Cassette Recorder Video Display Terminal Video Graphics Array Very Large Scale Integration Video-On-Demand Voice over Internet Protocol Virtual Private Network Very Small Aperture Terminal Videsh Sanchar Nigam Limited Volume Table Of Contents W
WAE WAN WAP W-CDMA WDP
Wireless Application Environment Wide Area Network Wireless Application Protocol Wideband Code Division Multiple Access Wireless Datagram Protocol
xxviii ■ Foundations of Computing WiMAX WLAN WLL WMA WML WORM WS WSP WTLS WTP WWN
Worldwide Interoperability for Microwave Access Wireless Local Area Network Wireless Local Loop Windows Media Audio Wireless Markup Language Write Once Read Many Web Service Wireless Session Protocol Wireless Transport Layer Security Wireless Transaction Protocol World Wide Name
WWW WYSIWYG
World Wide Web What You See Is What You Get X
X.400 X.500 XDR XHTML XML XP
Electronic Mail Protocol Directory Server Protocol eXternal Data Representation eXtensible HyperText Markup Language eXtensible Markup Language Extreme Programming
Characteristics, Evolution, and Classification of Computers
WHAT IS A COMPUTER? The original objective of inventing a computer was to create a fast calculating device. Today, we define a computer as a device that operates upon data because more than 80% of work done by today’s computers is data processing. Data can be anything like bio-data of applicants including their photographs, when computer is used for short listing candidates for recruiting; marks obtained by students in various subjects when used for preparing results; details (name, age, sex, etc.) of passengers when used for making airline or railway reservations; or number of different parameters when used for solving scientific research problems, etc. Notice from the examples that data can be numerical (only digits), alphabetic (only alphabets), alphanumeric (mixture of digits, alphabets and special characters), text, image, voice or video. A computer is often referred to as a data processor because it can store, process, and retrieve data whenever desired. The activity of processing data using a computer is called data processing. Data processing consists of three subactivities: capturing input data, manipulating the data, and managing output results. As used in data processing, information is data arranged in an order and form that is useful to people receiving it. Hence, data is raw material used as input to data processing and information is processed data obtained as output of data processing (see Figure 1.1). Data (Raw Material)
Input
Computer (Data Processor)
Output
Information (Finished Product)
Figure 1.1. A computer (also known as data processor) converts data into information.
2
Chapter 1 ■ Characteristics, Evolution, and Classification of Computers
CHARACTERISTICS OF COMPUTERS A computer’s power and usefulness are mainly due to its characteristics listed in Figure 1.2. Sr. No.
Characteristics
Description
1
Automatic
It carries out a job normally without any human intervention It can perform several billion (10 9) simple arithmetic operations per second It performs every calculation with the same accuracy
2
Speed
3
Accuracy
4
Diligence
It is free from monotony, tiredness, and lack of concentration
5
Versatility
6
Memory
7
No I. Q.
8
No Feelings
It can perform a wide variety of tasks It can store huge amount of information and can recall any piece of this information whenever required It cannot take its own decisions, and has to be instructed what to do and in what sequence It cannot make judgments based on feelings and instincts Figure 1.2. Characteristics of computers.
EVOLUTION OF COMPUTERS Researchers invented computers because of man’s search for fast and accurate calculating devices. Blaise Pascal invented the first mechanical adding machine in 1642. Later, in the year 1671, Baron Gottfried Wilhelm von Leibniz of Germany invented the first calculator for multiplication. Keyboard machines originated in the United States around 1880 and we use them even today. Around the same period, Herman Hollerith came up with the concept of punched cards that computers used extensively as input medium even in late 1970s. Business machines and calculators made their appearance in Europe and America towards the end of the nineteenth century. Charles Babbage, a nineteenth century Professor at Cambridge University, is considered the father of modern digital programmable computers. He designed a “Difference Engine” in the year 1822 that could produce reliable tables. In 1842, Babbage came out with his new idea of a completely automatic Analytical Engine for performing basic arithmetic functions for any mathematical problem at an average speed of 60 additions per minute. Unfortunately, he was unable to produce a working model of this machine because the precision engineering required to manufacture the machine was not available during that period. However, his efforts established a number of principles that are fundamental to the design of any digital programmable computer. A major drawback of the early automatic calculating machines was that their programs were wired on boards that made it difficult to change programs. In 1940s, Dr. John Von Neumann introduced the “stored program” concept that helped in overcoming hard-wired program problem. The basic idea behind this concept is that a sequence of instructions and data can be stored in memory of a computer for automatically directing flow of operations. This feature considerably influenced development of modern digital computers because of ease with which different programs can be loaded and executed on a single computer. Due to this feature, we often refer to modern digital computers as stored program digital computers. Figure 1.3 provides basic information about some of the well-known early computers.
■ Evolution of Computers
3
4
Chapter 1 ■ Characteristics, Evolution, and Classification of Computers
COMPUTER GENERATIONS “Generation” in computer talk provides a framework for the growth of computer industry based on key technologies developed. Originally, it was used to distinguish between hardware technologies but was later extended to include both hardware and software technologies. The custom of referring to computer era in terms of generations came into wide use only after 1964. There are totally five computer generations known until today. Below we describe each generation along with its identifying characteristics. In the description below, you will come across several new terminologies. Subsequent chapters will deal with these terminologies in detail. The idea here is to provide an overview of the major developments and technologies during the five generations of computers and not to explain them in detail.
First Generation (1942-1955) The early computers of Figure 1.3 and others of their time were manufactured using vacuum tubes as electronic switching device. A vacuum tube [see Figure 1.4(a)] was a fragile glass device using filaments as a source of electronics and could control and amplify electronic signals. It was the only high-speed electronic switching device available in those days. These vacuum tube computers could perform computations in milliseconds and were known as first-generation computers. Most of the first-generation computers worked on the principle of storing program instructions along with data in memory of computer (stored program concept) so that they could automatically execute a program without human intervention. Memory of these computers used electromagnetic relays, and users fed all data and instructions into the system using punched cards. Programmers wrote instructions in machine and assembly languages because of lack of high-level programming languages in those days. Since machine and assembly languages are difficult to work with, only a few specialists understood how to program these early computers.
Second Generation (1955-1964) John Bardeen, Willian Shockley, and Walter Brattain invented a new electronic switching device called transistor [see Figure 1.4(b)] at Bell Laboratories in 1947. Transistors soon proved to be a better electronic switching device than vacuum tubes. Second-generation computers were manufactured using transistors. They were more powerful, more reliable, less expensive, smaller, and cooler to operate than the first-generation computers. The second generation also experienced a change in storage technology. Memory of second-generation computers was composed of magnetic cores. Magnetic cores are small rings made of ferrite that can be magnetized in either clockwise or anti-clockwise direction. Large random access memory (having storage capacity of few tens of kilobytes) had several magnetic cores strung on a mesh of wires. In 1957, researchers introduced magnetic tape as a faster and more convenient secondary storage medium. Later magnetic disk storage was also developed, and magnetic disk and magnetic tape were the main secondary storage media used in second-generation computers. Users still used punched cards widely for preparing and feeding programs and data to a computer. On software front, high-level programming languages (like FORTRAN, COBOL, ALGOL, and SNOBOL) and batch operating system emerged during second generation. High-level programming languages made second-generation computers easier to program and use than first-generation computers. Introduction of batch operating system helped in reducing human intervention while processing multiple jobs resulting in faster processing, enhanced throughput, and easier operation of second-generation computers.
■ Computer Generations
5
In addition to scientific computations, business and industry users used second-generation computers increasingly for commercial data processing applications like payroll, inventory control, marketing, and production planning. Ease of use of second-generation computers gave birth to a new profession of programmers and systems analysts in computing that is more oriented towards usage rather than design of computers. This triggered introduction of computer science related courses in several colleges and universities.
(a) A Vacuum Tube Figure 1.4.
(b) A Transistor
(c) An Integrated Circuit (IC)
Electronics devices used for manufacturing computers of different generations.
Third Generation (1964-1975) In 1958, Jack St. Clair Kilby and Robert Noyce invented the first integrated circuit. Integrated circuits (called ICs) are circuits consisting of several electronic components like transistors, resistors, and capacitors grown on a single chip of silicon eliminating wired interconnection between components. IC technology was also known as “microelectronics” technology because it made it possible to integrate larger number of circuit components into very small (less than 5 mm square) surface of silicon, known as “chip” [see Figure 1.4(c)]. Initially the integrated circuits contained only about ten to twenty components. This technology was named small-scale integration (SSI). Later with the advancement in technology for manufacturing ICs, it became possible to integrate up to about hundred components on a single chip. This technology was known as medium scale integration (MSI). Third generation computers were manufactured using ICs. Earlier ones used SSI technology and later ones used MSI technology. ICs were smaller, less expensive to produce, more rugged and reliable, faster in operation, dissipated less heat, and consumed less power than circuits built by wiring electronic components manually. Hence, third-generation computers were more powerful, more reliable, less expensive, smaller, and cooler to operate than second-generation computers. Parallel advancements in storage technologies allowed construction of larger magnetic core based random access memory as well as larger capacity magnetic disks and tapes. Hence, third-generation computers typically had few megabytes (less than 5 Megabytes) of main memory and magnetic disks capable of storing few tens of megabytes of data per disk drive. On software front, standardization of high-level programming languages, timesharing operating systems, unbundling of software from hardware, and creation of an independent software industry happened during third generation. FORTRAN and COBOL were the most popular high-level programming languages in those days. American National Standards Institute (ANSI) standardized them in 1966 and 1968 respectively, and the standardized versions were called ANSI FORTRAN and ANSI COBOL. The idea was that as long as a programmer follows these standards in program writing, he/she could run his/her program on any computer with an ANSI FORTRAN or ANSI COBOL compiler (see Chapter 9 for details). Some more high-level programming languages were introduced during the thirdgeneration. Notable among these were PL/1, PASCAL, and BASIC. Second-generation computers used batch operating system. In those systems, users had to prepare their data and programs and then submit them to a computer centre for processing. The operator at the computer centre collected these user jobs and fed them to a computer in batches at scheduled intervals. The respective users then collected their
6
Chapter 1 ■ Characteristics, Evolution, and Classification of Computers
job’s output from the computer centre. The inevitable delay resulting from the batch processing approach was frustrating to some users, especially programmers, because often they had to wait for days to locate and correct a few program errors. To rectify this, John Kemeny and Thomas Kurtz of Dartmouth College introduced the concept of timesharing operating system, which enables multiple users to directly access and share a computer’s resources simultaneously in a manner that each user feels that no one else is using the computer. This is accomplished by using a large number of independent, relatively low-speed, on-line terminals connected to the computer simultaneously. A separate user uses each terminal to gain direct access to the computer. Timesharing operating system allocates CPU time in such a way that all user programs have a brief share (known as a time slice) of CPU time in turn. Processing speed of CPU allows it to switch from one user job to another in rapid succession and execute a small portion of each job in allocated time slice until the job is completed. Each user gets the illusion that he/she alone is using the computer. Introduction of timesharing concept helped in substantially improving the productivity of programmers and made on-line systems feasible, resulting in new on-line applications like airline reservation systems, interactive query systems, etc. Until 1965, computer manufacturers sold their hardware along with all associated software without separately charging for software. For example, buyers received language translators for all languages supported on a computer they purchased. From user’s standpoint, software was free. However, the situation changed in 1969 when IBM and other computer manufacturers began to price their hardware and software products separately. This unbundling of software from hardware gave users an opportunity to invest only in software of their need and value. For example, now buyers could purchase only the language translators they needed and not all language translators supported on the purchased computer. This led to the creation of many new software houses and the beginning of an independent software industry. Another important concept introduced during third-generation was that of backward compatible family of computers. During this period, IBM introduced its System 360 as a family of computers with backward compatibility as they were different sizes of mainframe systems based on the same machine language. This enabled businesses to upgrade their computers without incurring costs of replacing peripheral equipment and modifying programs to run on new systems. Development and introduction of minicomputers also took place during third-generation. Computers built until early 1960s were mainframe systems that only large companies could afford to purchase and use. Clearly, a need existed for low-cost smaller computers to fill the gaps left by the bigger, faster, and costlier mainframe systems. Several innovators recognized this need and formed new firms in 1960s to produce smaller computers. Digital Equipment Corporation (DEC) introduced the first commercially available minicomputer, the PDP-8 (Programmed Data Processor), in 1965. It could easily fit in the corner of a room and did not require attention of a full-time computer operator. It used timesharing operating system and a number of users could access it simultaneously from different locations in the same building. Its cost was about one-fourth the cost of a traditional mainframe system making it possible for smaller companies to afford computers. It confirmed the demand for small computers for business and scientific applications, and by 1971, there were more than 25 computer manufacturers in minicomputer market.
Fourth Generation (1975-1989) Average number of electronic components packed on a silicon chip doubled each year after 1965. This progress soon led to the era of large-scale integration (LSI) when it was possible to integrate over 30,000 electronic components on a single chip, followed by very-large-scale integration (VLSI) when it was possible to integrate about one million electronic components on a single chip. This progress led to a dramatic development – creation of a microprocessor. A microprocessor contains all circuits needed to perform arithmetic logic and control functions, the core activities of all computers, on a single chip. Hence, it became possible to build a complete computer with a microprocessor, a few additional primary storage chips, and other support circuitry. It started a new social revolution – personal computer (PC) revolution. Overnight computers became incredibly compact. They became inexpensive to make, and it became possible for many to own a computer.
■ Computer Generations
7
By 1978, Apple II from Apple Computer Inc. and the TRS-80 model from the Radio Shack Division of Tandy Corporation were dominant personal computers. By 1980, IBM realized that the personal computer market was too promising to ignore and came out with its own PC in 1981, popularly known as IBM PC. Several other manufacturers used IBM’s specification and designed their own PCs, popularly known as IBM compatible PCs or clones. The IBM PC and its clones became a popular standard for the PC industry during the fourth generation. During fourth generation, semiconductor memories replaced magnetic core memories resulting in large random access memories with fast access time. Hard disks became cheaper, smaller, and larger in capacity. In addition to magnetic tapes, floppy disks became popular as a portable medium for porting programs and data from one computer to another. Significant advancements also took place during fourth generation in the area of large-scale computer systems. In addition to improved processing and storage capabilities of mainframe systems, the fourth-generation saw the advent of supercomputers based on parallel vector processing and symmetric multiprocessing technologies. A supercomputer based on parallel vector processing technology contains a small number of custom-designed vector processors which are connected to a number of high-speed data access shared memory modules through a custom-designed, highbandwidth crossbar switch network. On the other hand, a supercomputer based on symmetric multiprocessing technology uses commodity microprocessors connected to a shared memory through a high-speed bus or a crossbar switch network. Primary builders of supercomputers of the former category included Cray Research and ETA Systems, whereas of the latter category included IBM, Silicon Graphics, and Digital Equipment Corporation. High-speed computer networking also developed during fourth-generation. This enabled interconnection of multiple computers for communication and sharing of data among them. Local area networks (LANs) became popular for connecting computers within an organization or within a campus. Similarly, wide area networks (WANs) became popular for connecting computers located at larger distances. This gave rise to network of computers and distributed systems. On software front, several new developments emerged to match the new technologies of fourth generation. For example, vendors developed several new operating systems for PCs. Notable ones among these were MS-DOS, MSWindows, and Apple’s propriety Mac OS. Since PCs were for individuals who were not computer professionals, companies developed graphical user interfaces for making computers user friendly (easier to use). A graphical user interface (GUI) provides icons (pictures) and menus (list of choices) that users can select with a mouse. PC manufacturers and application software developers developed several new PC-based applications to make PCs a powerful tool. Notable among these were powerful word processing packages that allowed easy development of documents, spreadsheet package that allowed easy manipulation and analysis of data organized in columns and rows, and graphics packages that allowed easy drawing of pictures and diagrams. Another useful concept that became popular during fourth-generation was multiple windows on a single terminal screen, which allowed users to see the status of several applications simultaneously in separate windows on the same terminal screen. In the area of software for large-scale computers, key technologies that became popular included multiprocessing operating systems and concurrent programming languages. With multiprocessing operating systems, a mainframe system could use multiple processors (a main processor and several subordinate processors) in such a manner that the subordinate processors could manage the user terminals and peripheral devices, allowing the main processor to concentrate on processing the main program, improving the overall performance. Supercomputers also used multiprocessing operating system to extract the best performance from the large number of processors used in these systems. Concurrent programming languages further helped in using the multiprocessing capabilities of these systems by allowing programmers to write their applications in such a way that different processors could execute parts of the application in parallel. The most ambitious language of this type was ADA. During fourth-generation, the UNIX operating system also became popular for use on large-scale systems. Additionally, due to proliferation of computer networks, several new features were included in existing operating systems to allow multiple computers on the same network to communicate with each other and share resources.
8
Chapter 1 ■ Characteristics, Evolution, and Classification of Computers
Some other software technologies that became popular during fourth-generation are C programming language, objectoriented software design, and object-oriented programming. C language combines features of high-level programming languages with efficiency of an assembly language. The primary objectives of object-oriented software design are to make programs generalized and to build software systems by combining reusable pieces of program codes called objects. To facilitate object-oriented software design, several object-oriented programming languages were introduced, out of which, C++ emerged as the most popular one.
Fifth Generation (1989-Present) The trend of further miniaturization of electronic components, dramatic increase in power of microprocessor chips, and increase in capacity of main memory and hard disk continued during fifth generation. VLSI technology became ULSI (Ultra-Large-Scale Integration) technology in fifth generation, resulting in production of microprocessor chips having ten million electronic components. In fact, the speed of microprocessors and the size of main memory and hard disk doubled almost every eighteen months. As a result, many features found in the CPUs of large mainframe systems of third- and fourth-generation systems became part of microprocessor architecture in fifth generation. This resulted in availability of powerful and compact computers becoming available at cheaper rates and death of traditional large mainframe systems. Subsequently, processor manufacturers started building multicore processor chips instead of increasingly powerful (faster) single-core processor chips. The multicore chips improve overall performance by handling more work in parallel. Due to this fast pace of advancement in computer technology, we see more compact and more powerful computers being introduced almost every year at more or less the same price or even cheaper. Notable among these are portable notebook computers that give the power of a PC to their users even while traveling, powerful desktop PCs and workstations, powerful servers, powerful supercomputers, and handheld computers. Storage technology also advanced making larger main memory and disk storage available in newly introduced systems. Currently, PCs having few Gigabytes (GB) of main memory and few Terabytes (TB) of hard disk capacity are common. Similarly, workstations having 4 to 64 Gigabytes of main memory and few tens of Terabytes of hard disk capacity are common. RAID (Redundant Array of Inexpensive Disks) technology enables configuration of a bunch of disks as a single large disk. It, thus, supports larger hard disk space with better in-built reliability. During fifth generation, optical disks (popularly known as Compact Disks or CDs) and Solid State Disks (SSD) emerged as popular portable mass storage media. In the area of large-scale systems, fifth-generation saw the emergence of more powerful supercomputers based on parallel processing technology. They used multiple processors and were of two types – shared memory and distributed memory parallel computers. In a shared memory parallel computer, a high-speed bus or communication network interconnects a number of processors to a common main memory, whereas in a distributed memory parallel computer, a communication network interconnects a number of processors, each with its own memory. These systems use parallel programming technique to break a problem into smaller problems and execute them in parallel on multiple processors of the system. Processors of a shared memory parallel computer use memory access mechanism for communication, whereas those of a distributed memory parallel computer use message-passing mechanism for communication. Distributed memory parallel computers have better scalability (can grow larger in capability) than shared memory parallel computers, and are now built by clustering together powerful commodity workstations by using a high-speed commodity switched network. This is known as clustering technology.
■ Computer Generations
9
During fifth generation, the Internet emerged with associated technologies and applications. It made it possible for computer users sitting across the globe to communicate with each other within minutes by use of electronic mail (known as e-mail) facility. A vast ocean of information became readily available to computer users through the World Wide Web (known as WWW). Moreover, several new types of exciting applications like electronic commerce, virtual libraries, virtual classrooms, distance education, etc. emerged during the period. The tremendous processing power and the massive storage capacity of fifth-generation computers also made them a useful and popular tool for a wide range of multimedia applications dealing with information containing text, graphics, animation, audio, and video data. In general, data size for multimedia information is much larger than plain text information because representation of graphics, animation, audio, or video media in digital form requires much larger number of bits than that required for representation of plain text. Because of this, multimedia computer systems require faster processor, larger storage devices, larger main memory, good graphics terminal, and input/output devices required to play any audio or video associated with a multimedia application program. The availability of multimedia computer systems resulted in a tremendous growth of multimedia applications during fifth-generation. In the area of operating systems, some new concepts that gained popularity during fifth-generation include microkernels, multithreading, multicore operating systems and operating systems for hand-held mobile devices. Microkernel technology enabled designers to model and design operating systems in a modular fashion. This makes operating systems easier to design and implement, easier to modify or add new services, and allows users to implement and use their own service. Multithreading technology is a popular way to improve application performance through parallelism. In traditional operating systems, the basic unit of CPU scheduling is a process, but in multithreading operating systems, the basic unit of CPU scheduling is a thread. In such operating systems, a process consists of an address space containing its instructions and data, and one or more threads sharing the same address space. Hence, these systems can create a new thread, switch CPU between threads, and share resources between threads of same process more efficiently than between processes, resulting in faster execution and better overall system performance. A multicore operating system can run multiple programs at the same time on a multicore chip with each core handling a separate program. With the advent of smart phones, tablets, smart watches and other handheld smart electronic devices, special operating systems for these devices were designed. In the area of programming languages, concepts that gained popularity during fifth generation are JAVA programming language, and parallel programming libraries like MPI (Message Passing Interface) and PVM (Parallel Virtual Machine). JAVA is used primarily on the World Wide Web. It supports Java-based applets allowing web pages to have dynamic information and more interactivity with users of web information. MPI and PVM libraries enable development of standardized parallel programs, so that a programmer can easily port and execute a parallel program developed for one parallel computer on other parallel computers. MPI is used for distributed memory parallel computers and PVM is used for shared memory parallel computers. Recently, programming languages for programming Data Analytics and Artificial Intelligence (AI) applications were developed. Python and R are two such languages. ____________________________
Figure 1.5 summarizes the key technologies and features of five computer generations. Although there is a certain amount of overlap between different generations, the approximate period shown against each are normally accepted. Description of history of computing divided into five generations shows how quickly things have changed in the last few decades. Technological progress in this area is continuing. In fact, the fastest-growth period in the history of computing may be still ahead.
10 Chapter 1 ■ Characteristics, Evolution, and Classification of Computers
■ Classification of Computers
11
CLASSIFICATION OF COMPUTERS Traditionally, computers were classified as microcomputers, minicomputers, mainframes, and supercomputers based on their size, processing speed, and cost. However, this classification is no longer relevant because with rapidly changing technology, new models of computers are introduced every few months having much higher performance and costing less than their preceding models. A recently introduced small system can outperform large models of a few years ago both in cost and performance. Hence, today computers are classified based on their mode of use. According to this classification, computers are classified as notebook computers, personal computers, workstations, mainframe systems, supercomputers, client and server computers, and handled computers.
Notebook Computers (Laptops) Notebook computers are portable computers mainly meant for use by people who need computing resource wherever they go. Their size is about the same as the size of an 8½ x 11 inch notebook or smaller, and their weight is around 2 Kg or less. They are also known as laptop PCs (laptop personal computers or simply laptop) because they are as powerful as a PC, and their size and weight are such that they can be used by placing them on ones lap. A notebook computer uses an almost full-size keyboard, a flat screen liquid crystal display, and a trackball, stick, or touchpad (instead of a mouse because notebook computers are often used without a desk) (see Figure 1.6). It also has an inbuilt hard disk and ports for connecting peripheral devices such as printer, pen drive, portable disk, etc. The lid with display screen is foldable in a manner that when not in use, a user can fold it to flush with keyboard to convert the system into notebook form. When in use, the lid with display screen is folded open as shown in the figure. Many models of laptops can be “docked” on a docking station (device with additional battery, hard disk, I/O ports, etc.) or port replicator (device with additional I/O ports) to take advantage of big monitor, storage space, and other peripherals such as a printer. Users can connect their laptop to a network and download data (read files) from other computers on the network, or access the Internet. Design of most laptops enables their users to establish wireless connectivity with other stationary computers using WiFi and Bluetooth. Bluetooth is an industrial specification for short-range wireless connectivity using globally unlicensed short-range radio frequency. It provides a way to establish wireless connectivity between devices such as PCs, laptops, printers, mobile phones, and digital cameras for exchange of information between them.
Foldable flat screen
Keyboard, trackball, hard disk, I/O ports, etc. are in this unit
Figure 1.6. A notebook computer (Laptop).
We can use laptops even at places where there is no external power source available (for example while traveling in a train or airplane) because they can operate with chargeable batteries. With a fully charged battery, a laptop can operate for a few hours. Laptops normally run MS-DOS, MS WINDOWS, Mac OS, or Linux operating system. Normally, users use them for word processing, spreadsheet computing, data entry, Internet browsing, accessing and sending e-mails, and preparing presentation materials while traveling. People also often use them to make presentations at locations away from their working places by plugging them into an LCD (liquid crystal display) projection system.
12
Chapter 1 ■ Characteristics, Evolution, and Classification of Computers
Processing capability of a laptop is normally as good as an ordinary PC (personal computer) because both use the same type of processor. However, microprocessors for laptops are designed to dynamically adjust their performance with available power to provide maximum power saving. In fact, design of a laptop is such that its each device uses little power and remains suspended if not used. A laptop generally has lesser hard disk storage than a PC to keep its total weight to around two Kg. A laptop is more expensive (2 to 3 times) than a normal PC with similar configuration.
Personal Computers (PCs) A PC is a non-portable, general-purpose computer that fits on a normal size office table (leaving some writing space to keep writing pads and other office stationary), and is used by one person at a time (single-user-oriented). As the name implies, users use PCs for their personal computing needs (such as professional work, personal work, education, and entertainment) either at their work places or at their homes. Hence, PCs can be found in offices, classrooms, homes, hospitals, shops, clinics, etc. A PC has several chips (CPU chip, RAM chips, ROM chips, I/O handling chips, etc.) neatly assembled on a main circuit board called system board or motherboard. Motherboard is what distinguishes one PC from another. Sometimes, we differentiate between two PCs based on the microprocessor chip used as CPU. Most modern PCs use microprocessor chips manufactured by Intel, AMD (Advanced Micro Devices), or Apple. System configuration of PCs varies depending on their usage. However, the most commonly used configuration consists of a system unit, a monitor (display screen), a keyboard, and a mouse (see Figure 1.7) System unit, in the form of a box, consists of main circuit board (consisting of CPU, memory, etc.), hard disk storage, any special add-on cards (such as network interface card), and ports for connecting peripheral devices such as printer, pen drive, portable disk, etc.
Monitor
Mouse
System Unit Keyboard
Figure 1.7. A personal computer (PC).
System unit of a PC has expansion slots where we can plug in a wide variety of special-function add-on boards (also called add-on cards). These add-on boards contain electronic circuitry for a wide variety of computer-related functions. Number of available expansion slots varies from computer to computer. Some popular add-on boards are: 1. Network Interface Card (NIC). It enables connecting a PC to a network. For example, a user needs an Ethernet card to connect his/her PC to an Ethernet LAN. 2. Fax modem card. It enables transferring of images of hard-copy documents from a PC to a remote computer via telephone lines just as we do using a fax (facsimile) machine. 3. Color and graphics adapter card. It enables interfacing a PC with graphics and/or color video monitor. For example, a user needs a VGA (video graphics array) card to interface his/her PC with a high-resolution monitor. 4. Motion video card. It enables integration of full-motion color video/audio with other output on monitor’s screen. Since it accepts multiple inputs, videos can be shown full-screen (one video input) or in windows (multiple video inputs). System unit of a PC also has I/O ports (both serial and parallel ports) for connecting external I/O or storage devices (such as a printer and other devices external to the system unit box) to the PC in much the same way as you plug electric equipment into an electrical socket.
■ Classification of Computers
13
Popular operating systems for PCs are MS-DOS, MS-Windows (comprising Windows XP, Vista), Linux, and UNIX. Apple’s Macintosh PCs run Apple’s propriety OS called Macintosh OS or Mac OS. These operating systems support multitasking enabling a user to switch between tasks. PCs generally cost from a few tens of thousands to about a lakh of rupees, depending on configuration. Some major PC manufacturers are Lenovo, Apple, Dell, HCL, Siemens, Toshiba, and Hewlett-Packard.
Workstations A workstation is a powerful desktop computer designed to meet computing needs of engineers, architects, and other professionals who need greater processing power, larger storage, and better graphics display facility than what normal PCs provide. For example, users use workstations commonly for computer-aided design (CAD), simulation of complex scientific and engineering problems, visualization of results of simulation, and multimedia applications such as for creating special audio-visual effects in movies or television programs. A workstation is similar to a high-end PC, and typically, only one person uses it at a time (like a PC). Characteristics used to differentiate between the two are: 1. Processing power. Processing power of a workstation is more than that of an average PC. 2. Storage capacity. Workstations have larger main memory (typically few tens of GB) as compared to PCs (typically few GB). Hard disk capacity of a workstation is also much more (typically few TB) as compared to that of PCs (typically few hundreds of GB). 3. Display facility. Most workstations have a large-screen monitor (21 inch or more) capable of displaying high-resolution graphics. PCs normally use monitors having smaller screen (19 inch or less). 4. Processor design. PCs normally use CPUs based on CISC technology, whereas workstations use CPUs based on RISC technology. 5. Operating system. Unlike PCs that run any desktop OS, all workstations generally run server version of Windows, Mac OS, Linux or UNIX operating system. Unlike most operating systems for PCs that are singleuser oriented, operating systems for workstations support multi-user environment, making them suitable for use as a server in network environment. 6. Expansion slots. Workstations usually have more expansion slots than PCs providing greater flexibility of supporting additional functionality. For example, a workstation can have two or more NICs plugged into it to enable it to communicate on two different networks (say, Ethernet and ATM). Workstations generally cost from few lakhs to few tens of lakhs of rupees depending on configuration. Some manufacturers of workstations are Apple, IBM, Dell, and Hewlett-Packard (HP).
Mainframe Systems Several organizations such as banks, insurance companies, hospitals, railways, etc., need on-line processing of large number of transactions, and require computer systems having massive data storage and processing capabilities. Moreover, often a large number of users need to share a common computing facility such as in research groups, educational institutions, engineering firms, etc. Mainframe systems are used for meeting the computing needs of such organizations. Figure 1.8 shows a configuration of a mainframe system. It consists of the following components: 1. Host, front-end, and back-end computers. A mainframe system usually consists of several subordinate computers in addition to the main or host computer. Host computer carries out most computations, and has direct control of all other computers. Other computers relieve the host computer of certain routine processing requirements. For example, a front-end computer handles all communications to/from all user terminals, thus
14
Chapter 1 ■ Characteristics, Evolution, and Classification of Computers relieving host computer of communications-related processing requirements. Similarly, a back-end computer handles all data I/O operations, thus relieving host computer of locating an I/O device and transferring data to/from it. Host and other computers are located in a system room to which entry is restricted to system administrators and maintenance staff only (see Figure 1.8). 2. Console(s). One or more console terminals (located in system room) connect directly to the host computer. System administrators use them to monitor the system’s health, or perform some system administration activities such as changing system configuration, installing new software, taking system backup, etc. 3. Storage devices. For large volume data storage, users connect several magnetic disk drives (located in system room) directly to the back-end computer. Host computer accesses data on these disks via the back-end computer. In addition, there are few tape drives and a magnetic tape library for backup/restoration of data to/from magnetic tapes. System administrators use the tape library (located in system room) to backup data from magnetic disks to magnetic tapes. Tape drives (located in users’ room) enable users to bring their input data on tape for processing, or to take their output data on tape after processing. USERS ROOM (Entry restricted to authorized users) SYSTEM ROOM (Entry restricted to system administrators and maintenance staff) Magnetic tape library Magnetic disk drives Magnetic tape drive
Back-end processor Plotter
Host processor Console
Front-end processor
User terminal
User terminal
User terminal
Figure 1.8. A mainframe system. System room is shaded.
Printer
■ Classification of Computers
15
4. User terminals. User terminals serve as access stations. Users use them to work on the system. Although, the figure shows all user terminals in users’ room, some of them may be located at geographically distributed locations. Since mainframe systems allow multiple users to use the system simultaneously (through user terminals), their operating systems support multiprogramming with timesharing. This enables all users to get good response time, and an illusion that the system is attending to their jobs. 5. Output devices. User terminals also serve the purpose of soft copy output devices. However, for hard copy outputs, there are one or more printers and one or more plotters connected to back-end computer. These output devices are also located in users’ room so that users can easily access them to collect their outputs. Configuration of a mainframe system depends a lot on the type of its usage and the kind of its users. The example of Figure 1.8 is just one possible configuration. A mainframe system looks like a row of large file cabinets, and needs a large room with closely monitored humidity and temperature. It may cost anywhere from few tens of lakhs to few crores of rupees depending on configuration. Mainframe systems having smaller configuration (slower host and subordinate computers, lesser storage space, and fewer user terminals) are known as minicomputers, but there is no well-defined boundary for differentiating between the two. Two major vendors of mainframe systems are IBM and Fujitsu.
Supercomputers Supercomputers are the most powerful and expensive computers available at any given time. Users use them primarily for processing complex scientific applications requiring enormous processing power. Scientists often build models of complex processes and simulate them on a supercomputer. For example, in nuclear fission, when a fissionable material approaches a critical mass, scientists may want to know exactly what will happen during every millisecond of a nuclear chain reaction. Such scientists use a supercomputer to model actions and reactions of literally millions of atoms as they react. A scientific application like this involves manipulation of a complex mathematical model, often requiring processing of trillions of operations in a reasonable time, necessitating use of a supercomputer. Some supercomputing applications (applications that need supercomputers for processing) are: 1. Petroleum industry uses supercomputers to analyze volumes of seismic data gathered during oil-seeking explorations to identify areas where there is possibility of getting petroleum products inside the earth. This helps in more detailed imaging of underground geological structures so that expensive resources for drilling and extraction of petroleum products from oil wells can be effectively channelized to areas where analysis results show better possibility of getting petroleum deposits. 2. Aerospace industry uses supercomputers to simulate airflow around an aircraft at different speeds and altitude. This helps in producing effective aerodynamic designs to develop aircrafts with superior performance. 3. Automobile industry uses supercomputers to do crash simulation of an automobile design before releasing it for manufacturing. Doing so is less expensive, more revealing, and safer than crashing a real model. This helps in producing better automobiles that are safer to ride. 4. Structural mechanics industry uses supercomputers to solve complex structural engineering problems that designers of various types of civil and mechanical structures need to deal with to ensure safety, reliability, and cost effectiveness. For example, designer of a large bridge has to ensure that the bridge must work in various atmospheric conditions and pressures from wind velocity, etc., and under different load conditions. Actual construction and testing of such expensive structures is prohibitive in most cases. Using mathematical modeling techniques, various combinations can be tried out on a supercomputer without going for actual construction/manufacturing of such structures and optimum design can be picked up for final implementation. 5. Meteorological centers use supercomputers for weather forecasting. They feed weather data, supplied by a worldwide network of space satellites, airplanes, and ground stations, to a supercomputer. The supercomputer analyzes the data using a series of computer programs to arrive at forecasts. This analysis involves solving of complex mathematical equations modeling atmosphere and climate processes. For example, in India, the
16
Chapter 1 ■ Characteristics, Evolution, and Classification of Computers National Center for Medium Range Weather Forecasting (NCMRWF), located in New Delhi, uses supercomputer for medium range weather forecasts that is crucial for Indian agriculture. 6. Material scientists and physicists use supercomputers to design new materials. Nanostructure technology requires microscopic investigation of mechanical, electrical, optical, and structural properties of materials having dimensions of the order of nanometer. For this, scientists need to simulate nano-scale materials like clusters of atoms, quantum dots, quantum wires, etc. using principles of molecular dynamics technique. Such simulations are compute-intensive as they combine complexity of molecular dynamics and electronic structure. Hence, they need supercomputers to complete them in a reasonable time. 7. Film and TV industries use supercomputers to create special effects for movies and TV programs. Supercomputers help in producing computer-generated images using advanced graphics features in a short time. Movies or TV programs incorporate the produced images in them to create special effects. Thus, supercomputers substantially reduce the time needed to produce a full-length feature film having several special effects.
Supercomputers use multiprocessing and parallel processing technologies to solve complex problems faster. That is, they use multiple processors, and parallel processing enables dividing a complex problem into smaller problems that different processors can process in parallel. A parallel program breaks up a problem into smaller computational modules so that different processors can work independently and cooperate to solve it faster. Hence, if the problem takes 100 hours to process on a single-processor system and if it can be broken into 100 smaller computational modules, a supercomputer having 100 processors can theoretically solve it in about one hour. Since modern supercomputers use parallel processing technology, they are also known as parallel computers or parallel processing systems. They are also known as massively parallel processors because they use thousands of processors. We measure speed of modern supercomputers in teraflops and petaflops. A teraflop is 10 12 floating-point arithmetic operations per second and a petaflop is 1015 floating-point arithmetic operations per second. A supercomputer, due to its specialized nature and huge cost, is often treated as a national resource that is shared by several academic and research organizations. For developing countries, it is not economically viable to have many such systems because of the cost involved in setting up a supercomputing facility, maintaining the facility, and fast obsolescence of technology. Major vendors of supercomputers include Cray Research Company, IBM, Silicon Graphics, Fujitsu, NEC, Hitachi, Hewlett-Packard, and Intel. Realizing the importance of supercomputers in a country’s overall progress, national effort to build indigenous supercomputers has been underway in India. Development of PARAM series of supercomputers by the Centre for Development of Advanced Computing (C-DAC), Anupam series of supercomputers
Figure 1.9. C-DAC’s PARAM Padma supercomputer [reproduced with permission from C-DAC].
■ Classification of Computers
17
by the Bhabha Atomic Research Centre (BARC), and PACE series of supercomputers by the Defence Research and Development Organization (DRDO), Hyderabad are results of this effort. Figure 1.9 shows the PARAM Padma supercomputer (one of the PARAM systems) developed by C-DAC. Cost of a supercomputer may range from few tens of lakhs to few hundreds of crores of rupees depending on configuration.
Client and Server Computers With increased popularity of computer networks, it became possible to interconnect several computers that can communicate with each other over a network. In such a computing environment, multiple users can share several resources/services for cost-effective usage. Moreover, the shared resources/services can be best managed/offered centrally. A few examples of such resources/services are: 1. File server. It provides a central storage facility to store files of several users on a network. 2. Database server. It manages a centralized database, and enables several users on a network to have shared access to the same database. 3. Print server. It manages one or more printers, and accepts and processes print requests from any user in a network. 4. Name server. It translates names into network addresses enabling different computers on a network to communicate with each other. In these cases, it is usual to have one process that “owns” a resource or service and is in charge of managing it. This process accepts requests from other processes that want to use the resource or service. The process that owns the resource and does this management is called a server process, and the computer on which the server process runs is called a server computer because it services requests for use of the resource. Other processes that send service requests to the server are called client processes, and computers on which client processes run are called client computers. Note that there may be multiple client computers sending service requests to the same server computer. Figure 1.10 shows a generic client-server computing environment. It is comprised of the following entities: 1. Client. A client is generally a single-user system (laptop, PC, workstation, or hand-held computer) that provides user-friendly interface to end users. It runs client processes that send service requests to a server. Generally, there are multiple clients in a client-server computing environment and each client has its own system-specific user interface for allowing users to interface with the system. 2. Server. A server is a relatively powerful system (workstation, mainframe, or supercomputer) that manages a shared resource and/or provides a set of shared user services to clients. It runs a server process that services client requests for use of the shared resource managed by the server. Hence, a server’s operating system has features to handle multiple client-requests simultaneously. Servers are headless machines not having monitor, keyboard, and mouse attached to them as users do not directly interact with them and use them over network. 3. Network. A network interconnects all clients and servers of a client-server computing environment. It may be a LAN, WAN, or an Internet of networks. A set of computers interconnected together to form a client-server computing environment is collectively known as a distributed computing system or distributed system. Client-server computing involves splitting an application into tasks and putting each task on a computer, which can handle it most efficiently. This usually means putting processing for presentation on user’s machine (the client) and data management and storage on a server. Depending on the application and software used, all data processing may occur on client or be split between client and server. Server software accepts a request for service from a client and returns the results of processing that request to the client. The client then manipulates the received results and presents it in proper format to the end user.
18
Chapter 1 ■ Characteristics, Evolution, and Classification of Computers
Notice that in client-server computing, the central idea is allocation of application-level tasks between clients and servers. Computers and operating systems of a client and server may be different. For example, a client may be a PC running MS-Windows OS, whereas a server may be a mainframe system running UNIX OS. In fact, a client-server computing environment may have several different types of client computers with different operating systems and several different types of server computers with different operating systems. As long as a client and server share a common communication protocol and support the same applications, these lower-level differences are irrelevant. The communication protocol enables a client and server to inter-operate even when they are different types of computers running different operating systems. One such standard communication protocol is TCP/IP. In client-server computing environment, it is common for a server to use services of another server, and hence, be both a client and a server at the same time. For example, let us assume that a client-server computing environment has clients, a file server, and a disk block server. Any client can send a file access request to the file server. On receiving such a request, the file server checks access rights, etc. of the user making the request. However, instead of reading/writing the file blocks itself; it sends a request to the disk block server for accessing requested data blocks. Disk block server returns requested data blocks to the file server, which then extracts the desired data from the data blocks and returns it to the client. In this scenario, the file server is both a server and a client. It is a server for clients, but a client for the disk block server. Hence, the concept of client and server computers is purely role-based, and may change dynamically as the role of a computer changes.
PC (Client) File server
PC (Client) LAN or WAN or an Internet of Networks PC (Client)
Database server
Workstation (Client) Workstation (Client) Figure 1.10. A generic client-server computing environment.
Handheld Computers A handheld computer, or simply handheld, is a small computing device that can be used by holding in hand (need not be kept on a table or lap to be operated). Obviously, its size, weight, and design are such that a user can use it by holding it in hand. It is also known as palmtop because it can be kept on palm and operated. Handheld computers are computing devices of choice for people requiring computing power anytime, anywhere. There are many types of handheld computers available today. Some popular ones are described here.
Tablet PC A tablet PC is a miniaturized laptop. It usually provides all features of a laptop with the following differences or enhancements:
■ Classification of Computers
19
1. Light weight. A tablet PC is much lighter than a laptop. For weight reduction, their designers remove from their basic structure a few infrequently used devices and provide them as separate add on. 2. Screen flip. A user can rotate, turn around, and flip the screen of a tablet PC over the keyboard area. When a user flips the screen over the keyboard, it hides the keyboard and leaves only the screen visible and usable. In this configuration, the user holds the tablet PC in hand and uses it as digital tablet. The display on the screen shows the content in portrait mode in this configuration (from normal landscape layout mode when using keyboard). 3. Handwriting recognition. Usually, a tablet PC comes with a specially designed pen that a user can use to write directly on the screen. Underlying OS translates the pen movements to smooth plot lines that running application can understand as handwritten input. 4. Voice recognition. Usually, tablet PCs have voice recognition feature that allows voice input capable applications to accept audio/voice commands. This feature also enables input of voice data. 5. Special design for tablet use. When a user flips the screen in tablet mode, he/she cannot use the keyboard because the screen hides it. Apart from voice and handwriting recognition, some quick access hardware keys and some on-screen programmable software keys are present. A user can use these keys to invoke predefined actions that he/she cannot make with pen, or finds it difficult to make with pen.
PDA/Pocket PC Personal Digital Assistant (PDA) was originally introduced as a Personal Information Manager (PIM) device. PIM features include contact list, calendar, task list, e-mail, pocket word-processing application, pocket spreadsheet application, presentation viewer, and host of other applications that come in handy while on the move. PDA has a decent size LCD touch screen with a pen for handwriting recognition. It usually has a PC based synchronization utility that a user can use to transfer and synchronize identified data between PC and PDA. With add-on memory cards, a user can increase its storage as required. Some PDAs also provide USB extension port, which a user can use to connect to external devices for extended features like external monitor, LCD Projector, etc. Almost all PDAs have some digital camera capability as well. Newer PDAs also pack network capability using WiFi, Bluetooth, etc. Due to network connectivity option, PDAs have applications in many domains such as medicine, teaching/training, data collection, sports, and Global Positioning System (GPS) based location and route finder. Several PDAs also provide GSM/GPRS service connectivity and, thus, their users can use them as phone for making and receiving cell phone calls, SMS, etc. PDAs come with several options of operating systems ranging from MS-Windows Mobile, iOS, PalmOS, SymbianOS, Linux, Blackberry OS, etc.
Smartphone Smartphone is a fully functional mobile phone with computing power. The major distinction between a PDA and a smartphone is that while PDA is mostly a computing platform like PC with optional phone capability, smartphone is a cell phone with PDA-like capability. In essence, smartphone is voice centric whereas, PDA is data centric. Normally, smartphones are smaller than PDAs. A user can use a smartphone with one hand, whereas PDAs usually require both hands for operation. Figure 1.11 shows samples of some handheld computers.
20
Chapter 1 ■ Characteristics, Evolution, and Classification of Computers
(a) Tablet PC
(b) PDA/Pocket PC
(c) Smartphone
Figure 1.11. Samples of handheld computers.
Points to Remember 1.
A computer is a fast calculating device. It is also known as a data processor because it can store, process, and retrieve data whenever desired.
2.
The activity of processing data using a computer is called data processing. Data is raw material used as input to data processing and information is processed data obtained as output of data processing.
3.
Key characteristics of computers are: automatic, speed, accuracy, diligence, versatility, memory, lack of intelligence, and lack of feelings.
4.
Charles Babbage is considered the father of modern digital programmable computers.
5.
Some of the well-known early computers are: MARK I (1937-44), ABC (1939-42), ENIAC (1943-46), EDVAC (1946-52), EDSAC (1947-49), UNIVAC I (1951), IBM-701 (1952), and IBM-650 (1953).
6.
Dr. John Von Neumann introduced the “stored program” concept that considerably influenced the development of modern digital computers. Due to this feature, we often refer to modern digital computers as stored program digital computers.
7.
“Generation” in computer talk provides a framework for the growth of computer industry based on key technologies developed. Originally, it was used to distinguish between hardware technologies but was later extended to include both hardware and software technologies.
8.
Till date, there are five computer generations – first, second, third, fourth, and fifth.
9.
Figure 1.5 summarizes the key hardware and software technologies and key characteristics of computers of five computer generations.
10. Traditionally, computers were classified as microcomputers, minicomputers, mainframes, and supercomputers based on their size, processing speed, and cost. However, with rapidly changing technology this classification is no longer relevant. 11. Today, computers are classified based on their mode of use. According to this classification, computers are classified as notebook computers, personal computers, workstations, mainframe systems, supercomputers, client and server computers, and handheld computers. 12. Notebook computers (or Laptops) are portable computers that are small enough to fit inside a briefcase, light enough to carry around easily, and operate with chargeable batteries so that users can use them even at places with no external power source available. They normally run MS-DOS, MS-WINDOWS, Mac OS or Linux operating system. Users use them commonly for word processing, spreadsheet computing, data entry, Internet browsing, accessing and sending e-mails, and preparing presentation materials while a person is travelling. 13. A personal computer is a non-portable, general-purpose computer that fits on a normal size office table, and only one person uses it at a time. PCs meet personal computing needs of individuals either at their work places or at their homes. They normally run MS-DOS, MS-Windows (comprising Windows XP, Vista), Mac OS, Linux, or UNIX operating system, and support multitasking enabling a user to switch between tasks. 14. A workstation is a powerful desktop computer designed to meet computing needs of engineers, architects, and other professionals, who need greater processing power, larger storage, and better graphics display facility than what normal PCs
■
Questions
21
provide. Users use workstations commonly for computer-aided design, multimedia applications, simulation of complex scientific and engineering problems, and visualization of results of simulation. Workstations generally run server version of Windows, Mac OS, Linux or UNIX operating system and support multi-user environment. 15. Mainframe systems are used for meeting the computing needs of mid to large size organizations such as banks, insurance companies, hospitals, railways, etc. They are also used in such environments where a large number of users need to share a common computing facility such as in research groups, educational institutions, engineering firms, etc. A typical configuration of a mainframe system consists of a host computer, a front-end computer, a back-end computer, one or more console terminals, several magnetic disk drives, a few tape drives, a magnetic tape library, several user terminals, several printers, and one or more plotters. Mainframe systems having smaller configuration (slower host and subordinate computers, lesser storage space, and fewer user terminals) are known as minicomputers. 16. Supercomputers are the most powerful and expensive computers available at any given time. Users use them primarily for processing complex scientific applications requiring enormous processing power. Some supercomputing applications are analysis of large volumes of seismic data, simulation of airflow around an aircraft, crash simulation of an automobile design, solving complex structural engineering problems, genome sequence analysis in bioinformatics, design of new materials, creation of special effects in movies, and weather forecasting. 17. Modern supercomputers use multiprocessing and parallel processing technologies to solve complex problems faster, and hence, they are also known as parallel computers or parallel processing systems. They are also known as massively parallel processors because they use thousands of processors. 18. A handheld computer is a device whose size, weight, and design are such that a user can use it by holding it in hand. It is also known as palmtop because it can be kept on palm and operated. Some popular handheld computers are tablet PC, PDA/Pocket PC, and smartphone. 19. In client-server computing environment, a client is generally a single-user system that provides user-friendly interface to end users. It runs client processes that send service requests to a server. A server is a relatively powerful system that manages a shared resource and/or provides a set of shared user services to the clients. It runs a server process that services client requests for use of the shared resource managed by it. 20. A set of computers interconnected together to form a client-server computing environment is collectively known as a distributed computing system or distributed system. 21. Client-server computing involves splitting an application into tasks and putting each task on a computer, which can handle it most efficiently. Computers and operating systems of a client and server may be different. As long as a client and server share a common communication protocol and support the same applications, these lower-level differences are irrelevant. 22. In a client-server computing environment, it is common for one server to use the services of another server, and hence, be both a client and a server at the same time. Hence, the concept of client and server computers is purely role-based and may change dynamically as the role of a computer changes.
Questions 1.
What is a computer? Why it is also known as a data processor?
2.
What is data processing? Differentiate between data and information. Which is more useful to people and why?
3.
List and explain some important characteristics of a computer.
4.
Who is known as the father of modern digital programmable computers and why?
5.
Who invented the concept of stored program? Why is this concept so important?
6.
What is “generation” in computer terminology? List various computer generations along with key characteristics of computers of each generation.
7.
List the key hardware technologies used in building computers of each of the five generations.
8.
List the key software technologies used in building computers of each of the five generations.
9.
What are the advantages of transistors over vacuum tubes?
10.
What is an IC? How it helps in reducing the size of computers?
22
Chapter 1 ■ Characteristics, Evolution, and Classification of Computers
11.
List the advantages of IC technology over transistor technology.
12.
Why were first- and second-generation computers more difficult and costlier to produce commercially than computers of subsequent generations?
13.
List the technologies used for constructing main memory in first-, second-, third-, and fourth-generation computers.
14.
List the secondary storage media popularly used in first-, second-, third-, fourth-, and firth-generation computers.
15.
Which key feature of third generation computers made them a highly productive tool for programmers and why?
16.
Why was it not possible to take a program written for one computer and run it on another computer in the first and secondgeneration computers?
17.
In what ways did the concept of unbundling of software from hardware affect computer manufacturers and users of computers?
18.
What new types of computer applications emerged during third, fourth, and fifth computer generations and what were the main driving technologies behind each?
19.
What is meant by backward compatible family of computers? How is this concept useful for computer users?
20.
What is a microprocessor? How it revolutionized the computer industry?
21.
Name two new types of computers, which became popular during fourth and fifth computer generations.
22.
List some applications that emerged due to emergence of computer networks.
23.
List some applications that emerged due to emergence of the Internet.
24.
List some representative computer systems of each of the five computer generations.
25.
List the parameters that were used traditionally to classify computers. What major types of computers were there based on this classification scheme? Why this classification scheme is no longer relevant?
26.
Write the basis on which computers are classified today. What major types of computers are there based on this classification scheme?
27.
What is a notebook computer? Why it is so called? What are its main uses?
28.
What is a laptop PC? Why it is so called? Describe a typical configuration of a laptop PC.
29.
What is a docking station? Why it is used?
30.
What operating systems do notebook computers normally use?
31.
What features make a notebook computer portable?
32.
What is a personal computer? What are its main uses?
33.
Describe a typical configuration of a PC.
34.
How are a microprocessor, a motherboard, and a personal computer related?
35.
What is an expansion slot? How it is useful for a buyer of a PC?
36.
What is an add-on card? List four functional enhancements possible by inserting one or more add-on cards into expansion slots of a PC.
37.
What operating systems do PCs normally use?
38.
What is a workstation? What are its common uses?
39.
Write the major differences between a PC and a workstation.
40.
What operating systems do workstations normally use?
41.
What is a mainframe system? What are its main uses?
42.
Describe a typical configuration of a mainframe system.
43.
Differentiate among host, front-end, and back-end computers of a mainframe system.
44.
What is the purpose of a console in a mainframe system?
■ 45.
What various types of storage devices are used normally in a mainframe system?
46.
What various types of input/output devices are used normally in a mainframe system?
Questions
47.
What is a minicomputer system?
48.
Describe how subordinate computers of a mainframe system help in improving overall performance of the system.
49.
What is a supercomputer? What are its main uses?
50.
Write examples of at least three supercomputing applications. Explain why these applications require a supercomputer.
51.
Explain two key technologies that modern supercomputers employ for solving complex problems faster.
52.
What is parallel processing? Explain with an example how parallel processing can help solve a complex problem faster.
53.
Why modern supercomputers are also known as massively parallel processors?
54.
What is the main difference between a supercomputer and a mainframe system?
55.
Write the main components of a client-server computing environment. Describe the roles of each component.
56.
Write three examples of resources/services that can be handled by a server in a client-server computing environment.
57.
What is a palmtop? Why it is so called?
23
58.
What is a tablet PC? How it is different from a laptop? List the features supported in a tablet PC.
59.
What is a Personal Digital Assistant (PDA)? What are its main uses? List the features supported in a PDA.
60.
What is a smartphone? What is the major distinction between a PDA and a smartphone?
61.
In a client-server computing environment, is it possible for a computer to be both a client and a server at the same time? If no, explain why. If yes, write an example of such a scenario.
62.
List the key desirable features of an operating system for each of the following types of computers: (a) (b) (c) (d)
Workstations Supercomputers Client computers Server computers
(e) Notebook computers (f) Personal computers (g) Mainframe systems
Internal Data Representation in Computers
In this chapter, you will learn how computers internally represent data, and how they perform various basic operations (comparison, sorting, addition, subtraction, multiplication, division, etc.) on these data. The chapter first explains the fundamental concepts of number system and introduces some number systems commonly used by computer professionals and relationship among them. It then presents the commonly used coding schemes to record data in computers. It finally explains how computers perform various basic operations internally.
NUMBER SYSTEMS USED IN COMPUTERS Number systems are of two types – non-positional and positional. In non-positional number system, we have symbols such as I for 1, II for 2, III for 3, IIII for 4, IIIII for 5, etc. Each symbol represents the same value regardless of its position in a number, and to find the value of a number, one has to count the number of symbols present in the number. Since it is difficult to perform arithmetic with such a number system, positional number systems were developed. In a positional number system, there are only a few symbols called digits. These symbols represent different values, depending on the position they occupy in a number. The following three considerations determine the value of each digit in such a number: 1. The digit itself, 2. The position of the digit in the number, and 3. The base of the number system. Base is defined as the total number of digits in the number system. The first digit is always zero. Hence, the maximum value of a single digit is always equal to one less than the value of the base.
■ Number Systems Used in Computers
25
As an illustration, let us consider decimal number system that we use in our day-to-day life. In this system, base is equal to 10 because there are ten symbols or digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9). Each position represents a specific power of the base (10). For example, decimal number 2586 (written as 2586 10) consists of digit 6 in unit position, 8 in tens position, 5 in hundreds position, and 2 in thousands position, and its value can be written as: (2 x 103) + (5 x 102) + (8 x 101) + (6 x 100) = 2000 + 500 + 80 + 6 = 2586 Observe that the same digit signifies different values, depending on the position it occupies in the number. For example, In 258610 the digit 6 signifies 6 x 100 = 6 In 256810 the digit 6 signifies 6 x 101 = 60 In 265810 the digit 6 signifies 6 x 102 = 600 In 625810 the digit 6 signifies 6 x 103 = 6000 Hence, we can represent any number by using the available digits and arranging them in various positions. The principles that apply to decimal number system, also apply to any other positional number system. It is important to keep track of only the base of the number system in which we are working. Below, we illustrate this for some positional number systems used in computer design and by computer professionals.
Binary Number System In binary number system, the value of base is 2. Hence, it has only two symbols or digits (0 and 1), the largest single digit being 1 (one less than the base). Each position in a binary number represents a power of the base (2). Hence, in this system, the rightmost position is units (2 0) position, the second position from the right is 2’s (2 1) position, and proceeding in this way, we have 4’s (22) position, 8’s (23) position, 16’s (24) position, and so on. Therefore, decimal equivalent of binary number 10101 (written as 101012) is: (1 x 24) + (0 x 23) + (1 x 22) + (0 x 21) + (1 x 20) = 16 + 0 + 4 + 0 + 1 = 21 To be specific about which system we are referring to, it is a common practice to indicate the base as a subscript. Hence, we write: 101012 = 2110 Bit is the short form of “binary digit”. Hence, a “bit” in computer terminology means either a 0 or 1. An n-bit number is a binary number consisting of ‘n’ bits. Any decimal number in the range 0 to 2n-1 can be represented in binary form as an n-bit number. Figure 2.1 lists all 3-bit numbers (total = 8 = 23) along with their decimal equivalent.
Binary
Decimal equivalent
000 001 010 011 100 101 110 111
0 1 2 3 4 5 6 7
Figure 2.1. 3-bit numbers with their decimal values.
Every computer stores numbers, letters, and other special characters in binary form. Computer professionals need to know raw data contents of a computer’s memory on several occasions. A commonly used way of doing this is to print/display raw memory contents, which is called memory dump. Working with memory dumps in binary form would be difficult and error prone for computer professionals. Hence, computer professionals often use octal and hexadecimal number systems, as shortcut notations for binary. They are described here.
26
Chapter 2 ■ Internal Data Representation in Computers
Octal Number System In octal number system, the base is 8. Hence, there are only eight symbols or digits: 0, 1, 2, 3, 4, 5, 6, and 7 (8 and 9 do not exist in this system). The largest single digit is 7 (one less than the base 8). Each position in an octal number represents a power of the base (8). Therefore, decimal equivalent of octal number 2057 (written as 2057 8) is: (2 x 83) + (0 x 82) + (5 x 81) + (7 x 80) = 1024 + 0 + 40 + 7 = 1071 Hence, 20578 = 107110 Since there are only 8 digits in octal number system, 3 bits (2 3 = 8) are sufficient to represent any octal number in binary (see Figure 2.1).
Hexadecimal Number System In hexadecimal number system, the base is 16. Hence, there are 16 symbols or digits. The first 10 digits are the same digits of decimal number system – 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The remaining six digits are the symbols A, B, C, D, E, and F, representing decimal values 10, 11, 12, 13, 14, and 15, respectively. Hence, the largest single digit is F or 15 (one less than the base 16). Each position in hexadecimal number system represents a power of the base (16). Therefore, decimal equivalent of hexadecimal number 1AF (written as 1AF 16) is: (1 x 162) + (A x 161) + (F x 160) = (1 x 256) + (10 x 16) + (15 x 1) = 256 +160 + 15 = 431 Hence, 1AF16 = 43110 Since there are only 16 digits in hexadecimal number system, 4 bits (2 4 = 16) are sufficient to represent any hexadecimal number in binary.
CONVERTING FROM ONE NUMBER SYSTEM TO ANOTHER Input data fed to computers and final output values are to be in decimal because decimal numbers are more meaningful to us than are numbers expressed in any other number system. Hence, computer professionals are often required to convert numbers in other number systems to decimal and vice-versa. They use various methods to convert numbers from one base to another. A method of converting from another base to decimal, and a method of converting from decimal to another base are described here.
Converting from Another Base to Decimal We use the following steps to convert a number in any other base to a base 10 (decimal) number: Step 1: Determine the column (positional) value of each position of the digit and the base of the number system).
digit
(this
depends
Step 2: Multiply the obtained column values (in Step 1) by the digits in the corresponding columns. Step 3: Sum up the products calculated in Step 2. The total is the equivalent value in decimal.
on
the
■ Converting from One Number System to Another
Example 2.1.
Example 2.2.
110012 = ?10
47068 = ?10
Solution:
Solution:
Step 1: Determine column values
Step 1: Determine column values
Column Number Column Value (from right) 20 = 1 21 = 2 22 = 4 23 = 8 24 = 16
1 2 3 4 5
Step 2: Multiply the column values by the corresponding column digits 16 x1
8 x1
16
8
Column Number Column Value (from right)
Step 2: Multiply column values by the corresponding column digits 512 x4
64 x7
8 x0
1 x6
2048 448
0
6
4 2 1 x0 x0 x1 0
0
80 = 1 81 = 8 82 = 64 83 = 512
1 2 3 4
1 Step 3: Sum up the products
Step 3: Sum up the products 16 + 8 + 0 + 0 + 1 = 25
2048 + 448 + 0 + 6 = 2502 Hence, 47068 = 250210
Hence, 110012 = 2510
Example 2.3. 1AC16 = ?10 Solution: 1AC16 = 1 x 162 + A x 161 + C x 160 = 1 x 256 + 10 x 16 + 12 x 1 = 256 + 160 + 12 = 42810
Example 2.5. 40526 = ?10 Solution: 40526 = 4 x 63 + 0 x 62 + 5 x 61 + 2 x 60 = 4 x 216 + 0 x 36 + 5 x 6 + 2 x 1 = 864 + 0 + 30 + 2 = 89610
Example 2.4. 40527 = ?10 Solution: 40527 = 4 x 73 + 0 x 72 + 5 x 71 + 2 x 70 = 4 x 343 + 0 x 49 + 5 x 7 + 2 x 1 = 1372 + 0 + 35 + 2 = 140910
Comparing this result with the result obtained in Example 2.4, we find that although the digits (4052) are same for both the numbers, their decimal equivalents are different. This is because the number in Example 2.4 is in base 7 number system, whereas the number in Example 2.5 is in base 6 number system.
27
28
Chapter 2 ■ Internal Data Representation in Computers
Converting Technique)
from
Decimal
to
Another
Base
(Division-Remainder
We use the following steps to convert a base 10 (decimal) number to a number in another base: Step 1: Divide the decimal number by the value of the new base. Step 2: Record the remainder from Step 1 as the rightmost digit (least significant digit) of the new base number. Step 3: Divide the quotient of the previous division by the new base. Step 4: Record the remainder from Step 3 as the next digit (to the left) of the new base number. Example 2.6. 2510 = ?2 Solution: Steps 1 and 2: Steps 3 and 4: Steps 3 and 4: Steps 3 and 4: Steps 3 and 4:
25/2 = 12 and remainder 1 12/2 = 6 and remainder 0 6/2 = 3 and remainder 0 3/2 = 1 and remainder 1 1/2 = 0 and remainder 1
As mentioned in Steps 2 and 4, we now arrange the remainders in reverse order, making the first remainder the least significant digit (LSD) and the last remainder the most significant digit (MSD). Hence, 2510 = 110012 Compare the result with the result obtained in Example 2.1. Repeat Steps 3 and 4, recording remainders from right to left, until the quotient becomes zero in Step 3. Note that the last remainder, thus obtained, will be the most significant digit of the new base number. Example 2.7.
Example 2.8.
4210 = ?2
95210 = ?8
Solution: 2
Solution: 42
Remainders
8
952
Remainders
21
0
119
0
10
1
14
7
5
0
1
6
2
1
0
1
1
0
0
1
Hence, 4210 = 1010102
Hence, 95210 = 16708
■ Converting from One Number System to Another Example 2.9.
Example 2.10.
42810 = ?16
10010 = ?5
Solution: 16
29
428
Remainders in hexadecimal
Solution: 5
100
Remainders
26
12 = C
20
0
1
10 = A
4
0
0
1=1
0
4
Hence, 42810 = 1AC16
Hence, 10010 = 4005
Compare the result with the result obtained in Example 2.3.
Converting from a Base Other Than 10 to Another Base Other Than 10 We use the following steps to convert a number in a base other than 10, to a number in another base other than 10: Step 1: Convert the original number to a base 10 (decimal) number. Step 2: Convert the decimal number obtained in Step 1 to the new base number. Example 2.11.
Example 2.12.
5456 = ?4
1011102 = ?8
Solution:
Solution:
Step 1: Convert from base 6 to base 10 2
1
545 = 5 x 6 + 4 x 6 + 5 x 6 = 5 x 36 + 4 x 6 + 5 x 1 = 180 + 24 + 5 = 20910
0
1011102 = 1 x 25 + 0 x 24 + 1 x 23 + 1 x 22 + 1 x 2 1 + 0 x 2 0 = 32 + 0 + 8 + 4 + 2 + 0 = 4610 Step 2: Convert 4610 to base 8
Step 2: Convert 20910 to base 4 4
Step 1: Convert 1011102 to base 10
209
Remainders
52
8
46
Remainders
1
5
6
13
0
0
5
3
1
0
3 4610 = 568
20910 = 31014
Therefore, 1011102 = 4610 = 568
Therefore, 5456 = 20910 = 31014
Hence, 1011102 = 568
Hence, 5456 = 31014
30
Chapter 2 ■ Internal Data Representation in Computers
Example 2.13. 110100112 = ?16 Solution: Step 1: Convert 110100112 to base 10 110100112 = 1 x 27 + 1 x 26 + 0 x 25 + 1 x 24 + 0 x 23 + 0 x 22 + 1 x 2 1 + 1 x 20 = 1 x 128 + 1 x 64 + 0 x 32 + 1 x 16 + 0 x 8 + 0 x 4 + 1x2+1x1 = 128 + 64 + 0 + 16 + 0 + 0 + 2 + 1 = 21110 Step 2: Convert 21110 to base 16 16
Remainders
211 13
3 = 3 in Hexadecimal
0
13 = D in Hexadecimal
Therefore, 110100112 = 21110 = D316 Hence, 110100112 = D316 Example 2.12 illustrates the method of converting a binary number to an octal number. Similarly, Example 2.13 shows how to convert a binary number to a hexadecimal number. However, these are lengthy procedures and we can use shortcut methods when we desire such conversions. These shortcut methods are described here.
Shortcut Method for Binary to Octal Conversion We use the following steps in this method: Step 1: Divide the binary digits into groups of three (starting from the right). Step 2: Convert each group of three binary digits to one octal digit. Since there are only 8 digits (0 to 7) in octal number system (refer to Figure 2.1), 3 bits (2 3 = 8) are sufficient to represent any octal number in binary. Moreover, since decimal digits 0 to 7 are equal to the octal digits 0 to 7, we use binary to decimal conversion method in this step.
■ Converting from One Number System to Another
Example 2.14. 1011102 = ?8 Solution: Step 1: Divide the binary digits into groups of 3, starting from right (LSD). 101
110
Step 2: Convert each group into one digit of octal (use binary-to-decimal conversion method). 1012 = 1 x 22 + 0 x 21 + 1 x 20 =4+0+1 = 58
1102 = 1 x 22 + 1 x 21 + 0 x 20 =4+2+0 = 68
Hence, 1011102 = 568 Compare the result with the result obtained in Example 2.12.
Example 2.15. 11010102 = ?8 Solution: 11010102 = 001
101
010 (Group of 3 digits from right)
= 1528 (Convert each group to an octal digit) Hence, 11010102 = 1528
Shortcut Method for Octal to Binary Conversion We use the following steps in this method: Step 1: Convert each octal digit to a 3 digit binary number (the octal digits may be treated as decimal numbers for this conversion). Step 2:
Combine all the resulting binary groups (of 3 digits each) into a single binary number.
31
32
Chapter 2 ■ Internal Data Representation in Computers
Example 2.16.
Example 2.17.
5628 = ?2 Solution:
67518 = ?2 Solution: 67518 = 110 111 101 001 6 7 5 1 = 1101111010012
Step 1: Convert each octal digit to 3 binary digits. 58 = 1012 68 = 1102 28 = 0102
Hence, 67518 = 1101111010012
Step 2: Combine the binary groups. 5628 = 101 110 010 5 6 2 Hence, 5628 = 1011100102
Shortcut Method for Binary to Hexadecimal Conversion We use the following steps in this method: Step 1: Divide the binary digits into groups of four (starting from the right). Step 2: Convert each group of four binary digits to one hexadecimal digit. Remember that hexadecimal digits 0 to 9 are equal to decimal digits 0 to 9 and hexadecimal digits A to F are equal to decimal values 10 to 15. Hence, for this step, we use binary to decimal conversion procedure and represent decimal values 10 to 15 as hexadecimal A to F. Example 2.18. 110100112 = ?16 Solution: Step 1: Divide the binary digits into groups of 4, starting from the right (LSD). 1101 0011 Step 2: Convert each group of 4 binary digits to 1 hexadecimal digit. 11012 = 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20 =8+4+0+1 = 1310 = D16
00112 = 0 x 23 + 0 x 22 + 1 x 21 + 1 x 20 =0+0+2+1 = 316
Hence, 110100112 = D316 Compare the result with the result of Example 2.13.
■ Converting from One Number System to Another Example 2.19. 101101011002 = ?16 Solution: 101101011002 = 0101
1010
1100 (Group 4 digits from right)
= 5AC (Convert each group to a hexadecimal digit) Hence, 101101011002 = 5AC16
Shortcut Method for Hexadecimal to Binary Conversion We use the following steps in this method: Step 1: Convert decimal equivalent of each hexadecimal digit to 4 binary digits. Step 2: Combine all resulting binary groups (of 4 digits each) into a single binary number. Example 2.20.
Example 2.21.
2AB16 = ?2
ABC16 = ?2
Solution: Step 1: Convert the decimal equivalent of each hexadecimal digit to 4 binary digits. 216 = 210 = 00102 A16 = 1010 = 10102 B16 = 1110 = 10112 Step 2: Combine the binary groups. 2AB16 = 0010 1010 1011 2 A B Hence, 2AB16 = 0010101010112
Solution: ABC16 = 1010 1011 1100 A B C = 1010101111002 Hence, ABC16 = 1010101111002
33
34
Chapter 2 ■ Internal Data Representation in Computers
Figure 2.2 summarizes the relationship among decimal, hexadecimal, binary, and octal number systems. Note that the maximum value of a single digit of octal (7) is equal to the maximum value of three digits of binary. Hence, the value range of one digit of octal is equivalent to the value range of three digits of binary. Therefore, octal shortcut notation is a one-to-three reduction in space and time required for memory dump. Similarly, hexadecimal shortcut notation is a one-to-four reduction in space and time required for memory dump because the maximum value of one digit in hexadecimal (F) is equal to the maximum value of four digits in binary.
Decimal
Hexadecimal
Binary
Octal
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 2 3 4 5 6 7 8 9 A B C D E F 10
0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000
0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20
Figure 2.2. Relationship among Decimal, Hexadecimal, Binary, and Octal number systems.
FRACTIONAL NUMBERS In binary number system, fractional numbers are formed in the same way as in decimal number system. For example, in decimal number system, 0.235 = (2 x 10-1) + (3 x 10-2) + (5 x 10-3)
and
68.53 = (6 x 101) + (8 x 100) + (5 x 10-1) + (3 x 10-2)
and
10.01
Similarly, in binary number system, 0.101 = (1 x 2-1) + (0 x 2-2) + (1 x 2-3)
2 1)
= (1 x (1 x 2-2)
+
(0
x
20)
+
(0
x
2-1)
+
Hence, binary point serves the same purpose as decimal point. Some positional values in binary number system are given here. Binary Point Position
4
3
2
Position Value
4
3
2
2
2
Quantity Represented
16
8
4
1 2
2
0 1
2
2
. 0
1
In general, a number in a number system with base b is written as: an an-1… a0 . a-1 a-2 … a-m and is interpreted to mean
-1
-2
-1
-2
2
2
½
1
/4
-3
-4
-3
2
2-4
1
/8
1
/16
■ Character Coding Schemes
35
an x bn + an-1 x bn-1 + … + a0 x b0 + a-1 x b-1 + a-2 x b-2 + … + a-m x b-m The symbols an, an-1, …, a-m used in the above representation should be one of the b symbols allowed in the number system. Hence, as per the above mentioned general rule, 46.328 = (4 x 81) + (6 x 80) + (3 x 8-1) + (2 x 8-2)
and
5A.3C16 = (5 x 161) + (A x 160) + (3 x 16-1) + (C x 16-2) Example 2.22.
Example 2.23.
Find the decimal equivalent of the binary number 110.101 Solution:
Find the decimal equivalent of the octal number 127.54 Solution:
110.1012 = 1 x 22 + 1 x 21 + 0 x 20 + 1 x 2-1 + 0 x 2-2 + 1 x 2-3 = 4 + 2 + 0 + 0.5 + 0 + 0.125 = 6 + 0.5 + 0.125 = 6.62510
127.548 = 1 x 82 + 2 x 81 + 7 x 80 + 5 x 8-1 + 4 x 8-2 = 64 + 16 + 7 + 5/8 + 4/64 = 87 + 0.625 + 0.0625 = 87.687510
Example 2.24. Find the decimal equivalent of the hexadecimal number 2B.C4 Solution: 2B.C416 = 2 x 161 + B x 160 + C x 16-1 + 4 x 16-2 = 32 + B + C/16 + 4/256 = 32 + 11 + 12/16 + 4/256 = 43 + 0.75 + 0.015625 = 43.76565210
CHARACTER CODING SCHEMES Character coding schemes (also known as computer codes) are standards for representing characters in computers. They enable easy transfer/exchange of data between computers, and between computers and peripheral devices connected to computers. They also enable independent development of several hardware and software components for a variety of computers. Although many coding schemes have evolved over the years, we will discuss here only the most commonly used. Numeric data is not the only form of data handled by a computer. We often require to process alphanumeric data also. An alphanumeric data is a string of symbols, where a symbol may be one of the letters A, B, C, …, Z, or one of the digits 0, 1, 2, …, 9, or a special character, such as + - * / , . ( ) = (space or blank) etc. An alphabetic data consists of only the letters A, B, C, …, Z, and blank character. Similarly, numeric data consists of only the digits 0, 1, 2, …, and 9. However, the bits 0 and 1 must represent any data internally. Hence, computers use binary coding schemes to represent data internally. In binary coding, a group of bits represents every symbol that appears in the data. The group of bits used to represent a symbol is called a byte. To indicate the number of bits in a group, sometimes, a byte is
36
Chapter 2 ■ Internal Data Representation in Computers
referred to as “n-bit byte”, where the group contains n bits. However, the term “byte” commonly means an 8-bit byte (a group of 8 bits) because most modern computers use 8 bits to represent a symbol.
BCD Code In Binary Coded Decimal (BCD) code, each digit of a decimal number is represented by its binary equivalent instead of converting the entire decimal value to a binary number. This makes the conversion process easier. Figure 2.3 shows BCD equivalent of each decimal digit. Since 8 and 9 require 4 bits, all decimal digits are represented in BCD by 4 bits. In Example 2.7, we saw that 42 10 is equal to 1010102. Converting 4210 into BCD, however, produces the following result: 4210 = 0100 0010 4 2
or
01000010 in BCD
Note that each decimal digit is converted independently to a 4-bit binary number, and hence, the conversion process is easy. Also note that when four bits are used, altogether 16 (24) combinations are possible (refer to hexadecimal number system). However, from Figure 2.3 you can see that only the first 10 of these combinations are used to represent decimal digits. The remaining six arrangements (1010, 1011, 1100, 1101, 1110, and 1111) have decimal values from 10 to 15. BCD coding does not use these arrangements. That is, 1010 does not represent 1010 in BCD. Instead, 1010 = 0001 0000 1 0
or
00010000 in BCD
Decimal digits
BCD equivalent
0 1 2 3 4 5 6 7 8 9
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
Figure 2.3. BCD equivalent of decimal digits.
Similarly, 1510 = 0001 0101 or 1 5
00010101 in BCD
In the discussion above, we have used a group of four bits to represent a digit (character) in BCD. 4-bit BCD coding system can represent only decimal numbers because four bits are insufficient to represent the various characters used by a computer. Hence, instead of using four bits with only 16 possible characters, computer designers commonly use six bits to represent characters in BCD code. 6-bit BCD code retains the four BCD numeric place positions, but adds two additional zone positions. With six bits, it is possible to represent 64 (2 6) different characters. This is sufficient to code the decimal digits (10), alphabetic letters (26), and other special characters (28). Figure 2.4 illustrates coding of alphabetic and numeric characters in BCD.
■ Character Coding Schemes
Character
BCD code
A B C D E F G H I
Zone 11 11 11 11 11 11 11 11 11
Digit 0001 0010 0011 0100 0101 0110 0111 1000 1001
J K L M N O P Q R
10 10 10 10 10 10 10 10 10
0001 0010 0011 0100 0101 0110 0111 1000 1001
Octal equivalent 61 62 63 64 65 66 67 70 71 41 42 43 44 45 46 47 50 51
Character
BCD code
S T U V W X Y Z
Zone 01 01 01 01 01 01 01 01
Digit 0010 0011 0100 0101 0110 0111 1000 1001
0 1 2 3 4 5 6 7 8 9
00 00 00 00 00 00 00 00 00 00
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
37
Octal equivalent 22 23 24 25 26 27 30 31 00 01 02 03 04 05 06 07 10 11
Figure 2.4. Alphabetic and numeric characters in BCD along with their Octal equivalent.
In the previous section, we saw the use of octal and hexadecimal number systems as shortcut notations for binary. Since BCD is a 6-bit code, we can divide it into two 3-bit groups, and use one octal digit for representing each of these 3-bit groups. Hence, computer professionals use octal number system as shortcut notation for memory dump in computers that use BCD code for internal representation of characters. This results in a one-to-three reduction in volume of memory dump. Figure 2.4 also shows the octal equivalent of alphabetic and numeric characters coded in BCD. Example 2.25.
Example 2.26.
Write the binary digits used to record the word BASE in BCD.
Solution:
Solution: In BCD binary notation (refer to Figure 2.4): B = 110010 A = 110001
Using octal notation, write the BCD coding for the word DIGIT.
S = 010010 E = 110101
Hence, the binary digits 110010 110001 010010 110101 B A S E will record the word BASE in BCD.
In BCD octal notation (refer to Figure 2.4): D = 64 I = 71 G = 67
I = 71 T = 23
Hence, the BCD coding for the word DIGIT in octal notation will be 64 71 67 71 23 D I G I T
38
Chapter 2 ■ Internal Data Representation in Computers
EBCDIC BCD code can represent only 64 (26) different characters. This is not sufficient for providing decimal numbers (10), lowercase (small) letters (26), uppercase (capital) letters (26), and a number of other special characters (28+). Hence, researchers extended BCD code from a 6-bit code to an 8-bit code. The added two bits are used as additional zone bits, expanding the zone to four bits. The resulting code is called the Extended Binary-Coded Decimal Interchange Code (EBCDIC). In this code, it is possible to represent 256 (2 8) different characters. In addition to the character requirements mentioned above, this also allows a variety of printable characters and several non-printable control characters. System designers enable use of control characters to control such activities as printer vertical spacing, movement of cursor on terminal screen, etc. Out of 256 possible combinations, several are still available for new characters assignment. Hence, the code can still grow, as new requirements develop. Since EBCDIC is an 8-bit code, we can divide it into two 4-bit groups and use one hexadecimal digit for representing each of these. Hence, computer professionals use hexadecimal number system as shortcut notation for memory dump in computers that use EBCDIC for internal representation of characters. This results in a one-to-four reduction in volume of memory dump. Figure 2.5 shows the alphabetic and numeric characters in EBCDIC along with their hexadecimal equivalent. Developed by IBM, most IBM models use EBCDIC code. Many other computers also use EBCDIC code.
A B C D E F G H I
EBCDIC code Zone Digit 1100 0001 1100 0010 1100 0011 1100 0100 1100 0101 1100 0110 1100 0111 1100 1000 1100 1001
J K L M N O P Q R
1101 1101 1101 1101 1101 1101 1101 1101 1101
Character
0001 0010 0011 0100 0101 0110 0111 1000 1001
Hexadecimal equivalent
S T U V W X Y Z
EBCDIC code Zone Digit 1110 0010 1110 0011 1110 0100 1110 0101 1110 0110 1110 0111 1110 1000 1110 1001
0 1 2 3 4 5 6 7 8 9
1111 1111 1111 1111 1111 1111 1111 1111 1111 1111
Character
C1 C2 C3 C4 C5 C6 C7 C8 C9 D1 D2 D3 D4 D5 D6 D7 D8 D9
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
Hexadecimal equivalent E2 E3 E4 E5 E6 E7 E8 E9 F0 F1 F2 F3 F4 F5 F6 F7 F8 F9
Figure 2.5. Alphabetic and numeric characters in EBCDIC along with their Hexadecimal equivalent.
Zoned and Packed Decimal Numbers Observe from Figure 2.5 that in the EBCDIC code, digit values are the same as numeric characters – 0 through 9 (0000 – 1001). However, numeric values need some special consideration because we must have a way to indicate whether a number is positive, negative, or unsigned (implies positive). Hence, when a numeric value is represented in EBCDIC, a sign indicator is used in the zone position of the rightmost digit. A sign indicator of hexadecimal C is a plus sign, hexadecimal D is a minus sign, and hexadecimal F means the number is unsigned. Figure 2.6 illustrates the representation of numeric values in EBCDIC. Note that the only zone affected by the sign is the zone of the rightmost
■ Character Coding Schemes
39
digit. All other zones remain as F, the zone value for numeric characters in EBCDIC. Since each decimal digit has a zone with it, numbers coded in EBCDIC are called zoned decimal numbers. Numeric data input to a computer are usually zoned decimal numbers. Printers can print only those numeric characters that are in a zoned-decimal format. Most computers cannot perform arithmetic operations on zoned-decimal data. Hence, before a computer can perform any arithmetic operation on the data, it must convert it to a format on which arithmetic operations are possible. One such acceptable format is the packed decimal format. A system uses the following steps to convert a zoned decimal number to a packed decimal number: Step 1: The zone half and the digit half of moves the sign to the extreme right of the number.
the
rightmost
byte
are
reversed.
This
Step 2: All remaining zones are dropped out. Figure 2.7 illustrates the conversion process of zoned decimal data to packed data. Observe that packed data requires less number of bytes (group of 8 bits) as compared to zoned data. In zoned format, there is only one digit per byte (each digit along with the zone requires 8 bits). However, there are two digits in each byte in packed format (each digit requires 4 bits). If the packing process does not completely fill a byte, it fills it with a zero. For example, zoned data F3F4F5F6 will convert to packed data 03456F. Observe that in this example, zoned data requires four bytes and packed data requires only three bytes. Numeric value
EBCDIC
Sign indicator
345 +345 -345
F3F4F5 F3F4C5 F3F4D5
F for unsigned C for positive D for negative
Figure 2.6. Numeric values in EBCDIC in Hexadecimal notation.
Numeric value
Zoned format
Packed format
345 +345 -345 3456
F3F4F5 F3F4C5 F3F4D5 F3F4F5F6
345F 345C 345D 03456F
Figure 2.7. Zoned and packed decimal numbers.
Example 2.27. Write the EBCDIC coding for the word “BIT” in both binary and hexadecimal notations. How many bytes are required to store this word using this coding? Solution: In EBCDIC (refer to Figure 2.5): B = 1100 0010 in binary and C2 in hexadecimal I = 1100 1001 in binary and C9 in hexadecimal T = 1110 0011 in binary and E3 in hexadecimal Hence, the EBCDIC coding for the word “BIT” will be 11000010 11001001 B I C2 B
C9 I
11100011 in binary and T E3 T
in hexadecimal
Since each character in EBCDIC requires one byte for its representation and there are three characters in the word “BIT”, three bytes will be required to store this word using this coding.
40
Chapter 2 ■ Internal Data Representation in Computers
Example 2.28. Write the EBCDIC zoned-decimal coding for the value +256 (use hexadecimal). How many bytes are required for this representation? Solution: +256 = F2F5C6 in EBCDIC Each hexadecimal digit requires 4 bits and there are altogether 6 hexadecimal digits. Therefore, 6 x 4 = 24 bits or 3 bytes (8 bits = 1 byte) will be required for this representation. We may also write that since each digit requires 1 byte for its representation in the EBCDIC zoned decimal coding and there are 3 digits in the given number, 3 bytes will be required for this representation.
Example 2.29. Write -128 as packed decimal number (use hexadecimal). How many bytes are required for this representation? Solution: -128 = F1F2D8 in EBCDIC = 128D in packed format Each hexadecimal digit requires 4 bits and there are altogether 4 hexadecimal digits. Therefore, 4 x 4 = 16 bits or 2 bytes (1 byte = 8 bits) will be required for this representation.
ASCII Another widely used computer code is the American Standard Code for Information Interchange (ASCII). Several American computer manufacturers have adopted ASCII as their computers’ internal code. This code is popular in data communications, is used almost exclusively to represent data internally in microcomputers, and is frequently found in larger computers produced by some vendors. American National Standards Institute (ANSI) published ASCII standard in 1963. However, the standard lacked lowercase letters and ANSI subsequently revised ASCII in 1967. Subsequent revisions brought it to its present form. Today, ASCII is one of the most popular and widely supported character-encoding standards. ASCII is of two types – ASCII-7 and ASCII-8. ASCII-7 is a 7-bit code that can represent 128 (2 7) different characters. Computers using 8-bit byte (group of 8 bits for 1 byte) and the 7-bit ASCII either set the 8 th bit (leftmost bit) of each byte as zero or use it as a parity bit. ASCII-8 is an extended version of ASCII-7. It is an 8-bit code that can represent 256 (2 8) different characters. It adds the additional bit to the left of the 7th bit (leftmost bit) of ASCII-7 codes. Figure 2.8 shows coding of characters in ASCII-7 and ASCII-8. ASCII-7 uses only 7 bits (the leftmost bit shown shaded is not used) whereas all the 8 bits are used in ASCII-8 (including the leftmost bit shown shaded). Hence, the codes of first 128 characters (symbols) are identical in ASCII-7 and ASCII-8. Notice from the figure that ASCII-8 codes for digits 0 to 9 are 0011 0000 (31 in hexadecimal) to 0011 1001 (39 in hexadecimal), for uppercase alphabets A to Z are 0100 0001 (41 in hexadecimal) to 0101 1010 (5A hexadecimal), and for lowercase alphabets a to z are 0110 0001 (61 in hexadecimal) to 0111 1010 (7A in hexadecimal). The figure also shows ASCII codes for special characters (such as +, -, *, /, $, %, >, =, ?
0011 0000 0011 0001 0011 0010 0011 0011 0011 0100 0011 0101 0011 0110 0011 0111 0011 1000 0011 1001 0011 1010 0011 1011 0011 1100 0011 1101 0011 1110 0011 1111
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F
Character
Figure 2.8. ASCII-7 and ASCII-8 codes along with their Hexadecimal equivalent for alphanumeric, special, and non-printable characters. The leftmost bit (shown shaded) is used only in ASCII-8 (not in ASCII-7). Codes from 0000 0000 (0 in hexadecimal) to 0010 0000 (20 in hexadecimal) and 0111 1111 (7F in hexadecimal) are for non-printable characters (their abbreviations are given). (Continued on next page…)
■ Character Coding Schemes
@ A B C D E F G H I J K L M N O
ASCIIcode 0100 0000 0100 0001 0100 0010 0100 0011 0100 0100 0100 0101 0100 0110 0100 0111 0100 1000 0100 1001 0100 1010 0100 1011 0100 1100 0100 1101 0100 1110 0100 1111
(Hexadecimal) 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F
P Q R S T U V W X Y Z [ \ ] ^ _
0101 0000 0101 0001 0101 0010 0101 0011 0101 0100 0101 0101 0101 0110 0101 0111 0101 1000 0101 1001 0101 1010 0101 1011 0101 1100 0101 1101 0101 1110 0101 1111
50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F
Character
Character ` a b c d e f g h i j k l m n 0 p q r s t u v w x y z { | } ~ DEL
ASCIIcode 0110 0000 0110 0001 0110 0010 0110 0011 0110 0100 0110 0101 0110 0110 0110 0111 0110 1000 0110 1001 0110 1010 0110 1011 0110 1100 0110 1101 0110 1110 0110 1111
Hexadecimal) 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F
0111 0000 0111 0001 0111 0010 0111 0011 0111 0100 0111 0101 0111 0110 0111 0111 0111 1000 0111 1001 0111 1010 0111 1011 0111 1100 0111 1101 0111 1110 0111 1111
70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F
Figure 2.8. ASCII-7 and ASCII-8 codes along with their Hexadecimal equivalent for alphanumeric, special, and non-printable characters. The leftmost bit (shown shaded) is used only in ASCII-8 (not in ASCII-7). Codes from 0000 0000 (0 in hexadecimal) to 0010 0000 (20 in hexadecimal) and 0111 1111 (7F in hexadecimal) are for non-printable characters (their abbreviations are given).
43
44
Chapter 2 ■ Internal Data Representation in Computers
Unicode Need for Unicode As computers became popular across the world, their usage could not be limited to English language users only. Hence, people started developing computer systems that could allow interaction and processing of data in local languages of users (e.g., Hindi, Japanese, Chinese, Korean, etc.) This required support of local language characters and other language-specific symbols on these computer systems. ASCII or EBCDIC did not have enough number of bits to accommodate all the characters and language-specific symbols of a local language, in addition to English alphabet characters and special characters. Hence, different groups designed encoding systems to cater to this requirement of different local languages. In the process, hundreds of different encoding systems came into existence. Although, this looked fine initially, it later led to a chaotic state of affairs due to the following reasons: 1. No single encoding system had enough bits and an adequate mechanism to support characters of all types of languages used in the world. Hence, supporting of characters from multiple languages on a single computer system became tedious since it required supporting of multiple encoding systems on the computer. With hundreds of different encoding systems in use across the world, it became impossible to support all of them on a single system. 2. Different encoding systems developed independently conflicted with one another. That is, two encoding systems often used the same code for two different characters or used different codes for the same character. Due to this, whenever data transfer took place between computer systems or software using different encoding systems, the data was at the risk of corruption. As a result, it became difficult to exchange text files internationally. The Unicode standard was designed to overcome these problems. It is a universal character-encoding standard used for representation of text for computer processing. The official Unicode web site (www.unicode.org/standard/WhatIsUnicode.html) states that it is an encoding system that “provides a unique number for every character, no matter what the platform, no matter what the program, no matter what the language”.
Unicode Features Today, Unicode is a universally accepted character-encoding standard because: 1. It provides a consistent way of encoding multilingual plain text. This enables data transfer through different systems without the risk of corruption. 2. It defines codes for characters used in all major languages of the world used for written communication. This enables a single software product to target multiple platforms, languages, and countries without reengineering. 3. It also defines codes for special characters (such as various types of punctuation marks), mathematical symbols, technical symbols, and diacritics. Diacritics are modifying character marks such as tilde (~), that are used in conjunction with base characters to represent accented letters (indicating different sound – for example, ñ). 4. It has the capacity to encode as many as a million characters. This is large enough for encoding all known characters including all historic scripts of the world as well as common notational systems. 5. It assigns each character a unique numeric value and name, keeping character coding simple and efficient. 6. It reserves a part of the code space for private use to enable users to assign codes for their own characters and symbols. 7. It affords simplicity and consistency of ASCII. Unicode characters that correspond to the familiar ASCII character set have the same byte values as that of ASCII. This enables use of Unicode in a convenient and
■ Character Coding Schemes
45
backward compatible manner in environments designed around ASCII, like UNIX. Hence, Unicode is usable with existing software without extensive software rewrites. 8. It specifies an algorithm for presentation of text with bi-directional behavior. For example, it can deal with a text containing a mixture of English (which uses left-to-right scripts) and Arabic (which uses right-to-left scripts). For this, it includes special characters to specify changes in direction when scripts of different directions are mixed. For all scripts, Unicode stores a text in logical order within the memory representation corresponding to the order of typing on the keyboard. Unicode has a lot of room to accommodate new characters. Moreover, its growth process is strictly additive in the sense that new characters can be added easily, but existing characters cannot be removed. This feature ensures that interpretation of data once encoded in Unicode standard will remain in the same way by all future implementations that conform to original or later versions of the Unicode standard.
Unicode Encoding Forms In addition to defining the identity of each character and its numeric value (also known as code point), characterencoding standards also define internal representations (also known as encoding forms) of each character (how its value is represented in bits). Unicode standard defines the following three encoding forms for each character: 1. UTF-8 (Unicode Transformation Format-8). This is a byte-oriented format having all Unicode characters represented as a variable length encoding of one, two, three or four bytes (remember, 1 byte = 8 bits). This form is useful for dealing with environments designed around ASCII because the Unicode characters that correspond to the familiar ASCII character set have the same byte values as that of ASCII. This form is also popular for HTML and similar protocols. 2. UTF-16 (Unicode Transformation Format-16). This is a word-oriented format having all Unicode characters represented as a variable length encoding of one or two words (remember, 1 word = 16 bits). This form is useful for environments that need to balance efficient access to characters with economical use of storage. This is because all the heavily used characters can be represented by and accessible via a single word (16-bit code unit), while all other characters are represented by and accessible via a pair of words. Hence, this encoding form is compact and efficient, yet provides support for larger number of characters. 3. UTF-32 (Unicode Transformation Format-32). This is a double-word oriented format having all Unicode characters represented as a fixed length encoding of two words (remember, 1 word = 16 bits). That is, a double word (32-bit code unit) encodes each character. This form is useful for environments where memory space is not a concern, but users desire fixed width (single code unit) access to characters. Notice that at most 4 bytes (32 bits) are required for each character in all three forms of encoding. With the three forms of encoding supported, transmission of the same data in a byte, word, or double-word format (i.e., in 8, 16, or 32-bits per code unit) is possible, depending on its usage environment. Unicode standard also provides the facility to transform Unicode encoded data from one form to another without loss of data.
Handling of Text Elements by Unicode A text consists of words and sentences, which are created using text elements. The concept of text element may be different for different languages used in the world. For example, in English, we use letters (A, B, C…a, b, c…) as text elements; and in Chinese language, we use ideographs (which represents full word or concepts) as text elements. Moreover, the definition of text elements also depends on the process handling the text. For example, when sorting is done for historic Spanish language, “II” is treated as a single text element, whereas, it is treated as two separate text elements – “I” and “I”, when Spanish words are typed. To avoid deciding what is (and what is not) a text element in different processes used for handling text, the Unicode standard defines code elements (also known as characters) instead of text elements. For most languages, code
46
Chapter 2 ■ Internal Data Representation in Computers
elements correspond to the commonly used text elements. Moreover, the Unicode standard defines each “I” of the Spanish “II” as a separate code element and leaves the task of combining two “I” together for alphabetic sorting to the text processing software.
Handling of Various Types of Characters by Unicode Since Unicode is an encoding system for supporting all types of languages used in the world, it has to deal with many different types of characters and character sequences used in different languages. Some of the commonly encountered types of characters and character sequences along with the manner in which Unicode handles them are given here. 1. Simple characters. Unicode treats a simple character (like the letter “A”) as a code element. It assigns a single member to each of these characters. Each of these numbers is called a code point. When referred to in text, a code point is listed in hexadecimal form following the prefix “U”. For example, the code point for character “A” is U+0041, where 0041 is the hexadecimal number (equal to the decimal number 65). 2. Unicode also assigns a unique name to each character, which it uses to identify it uniquely. For example, it assigns the unique name “LATIN CAPITAL LETTER A” to “A”. Other examples of unique names assigned to characters by Unicode are “BENGASLI DIGIT 5” and “GURUMUKHI LETTER CHA”. 3. Composite characters. Composite characters (like the characters “ü” and “ n ”) are often used in Latin. They are also known as precomposed characters. The Unicode standard encodes composite characters as single character (encoded as a single code point) and defines the decompositions for all composite characters as the base character (encoded as a code point) followed by the non-spacing character (encoded as another code point). For example, the composite character “ü” is encoded as the single code point U+00FC, and can be decomposed as the base character “u” (having the code point U+0075) followed by the non-spacing character “..” (having the code point U+0308). 4. Unicode defines decomposition of composite characters for consistency and analysis. For example, while alphabetical sorting of a list of names, the character “ü “ may be decomposed into a “ü” followed by the nonspacing character “..”. Once the character has been decomposed, it may be easier for the sorting routine to work with the character because it can be processed as a “ü” with modifications. This allows easier alphabetical sorting for languages where character modifiers do not affect alphabetical order. 5. Duplicate characters in multiple languages. Different languages, which have a common origin, often use many common characters. For example, Chinese, Japanese, and Korean languages share many thousands of identical characters because their ideograph sets evolved from the same source. To avoid duplicate encoding of such characters, the Unicode Standard unifies characters within scripts across languages and assigns a single code to each such character. That is, it assigns a single code point to characters, which are equivalent in form across multiple languages. 6. Glyphs. A glyph is a visual representation of a character, when the character is displayed on screen or printed on paper. We often come across the same character displayed or printed in different sizes, shapes and styles in printed documents or displayed text. For example, capital “A” appears as “A” in Helvetica font and as “A” in Courier font. The Unicode standard does not define glyphs (the size, shape, or style of displayed or printed forms of characters). It only defines how characters are interpreted, not how glyphs are rendered. It is the responsibility of the software or hardware-rendering engine of a computer to deal with the appearance of characters, when the computer displays or prints them.
■ Character Coding Schemes
47
Partitions of Unicode Codespace The Unicode standard (Version 4.0) defines codes (code points) for about 96,500 characters from the world’s alphabets, ideograph sets, and symbol collections. The entire range of already defined code points along with the space reserved for code points for private use as well as those for future expansions form the Unicode codespace. In Unicode codespace, characters are grouped together by scripts in code blocks. A script is a set of related characters. For example, all the characters used in Devnagari form the Devnagari script. Similarly, there are Arabic, Greek, Hebrew, and other scripts. If a script defines an order of the characters (for example, A, B, C, ... is the alphabetic ordering of the characters A…Z), the Unicode Standard maintains the same order whenever possible in its code space. That is, the code points assigned to the set of characters in the script are in the same order (for example, U+0041 for A, U+0042 for B, U+0043 for C, and so on). All the code points assigned to the characters of a script form a code block. Since the number of characters in different scripts varies greatly, the code blocks also vary greatly is size. For example, the code block for the Cyrillic script has 256 code points, while the code block for the combined ideographs of Chinese, Japanese and Korean languages (to avoid duplication) contains thousands of code points. In Unicode codespace, coding starts at U+0000 with the standard ASCII characters, and continues with Greek, Cyrillic, Hebrew, Arabic, Indic, and other scripts. Code point for symbols and punctuations follow these. Hiragana, Katakana, Bopomofo, Hangul and the unified ideographs of Chinese, Japanese, and Korean languages form the subsequent code blocks of codespace. Basic Multilingual Plan (BMP in short) is an area of codespace that contains code points for the commonly used characters (about 64K code points), and space for about 6,300 unused code points for future expansion. Towards the end of the BMP, there is space for 6,400 code points, reserved for private use. A range of compatibility characters follows this. These encoded character variants enable transcoding to earlier standards and old implementations that made use of them. Private use area of the codespace is for characters specific to a program or a group of users. Vendors or end users can assign these internally for their own characters and symbols, or use them with specialized fonts. For example, a group of choreographers may design a set of characters for dance notation and encode the characters using code points in the private use area. Similarly, a set of page-layout programs may use the same code points as control codes to position text on the page. These code points have no universal meaning, as the Unicode standard does not assign any meaning to them. There are 6,400 code points, on the BMP and another 131,068 supplementary private use code points, should 6,400 be insufficient for some applications.
Collating Sequence Data processing operations often use alphanumeric and alphabetic data elements. Obviously, we do not intend to perform any arithmetic operation on such data, but we frequently perform comparison operations on them, like arranging them in some desired sequence. Now, if we want a computer to compare alphabetic values A and B, which one will the computer treat as greater. Answering such questions necessitates having some assigned ordering among the characters used by a computer. This ordering is known as collating sequence. Data processing operations mostly adopt case sensitive dictionary collating sequence. Collating sequence may vary from one computer system to another depending on the type of character coding scheme (computer code) used by a computer. To illustrate this, let us consider the computer codes already discussed in this chapter. Observe from Figures 2.4 and 2.5 that the zone values of characters A through 9 decrease in BCD code from the equivalent of decimal 3 down to 0, while the zone values of characters A through 9 increase in EBCDIC from the equivalent of decimal 12 to 15. Hence, a computer that uses BCD code will treat alphabetic characters (A, B, …, Z) to be greater than numeric characters (0, 1, …9), while a computer that uses EBCDIC will treat numeric characters to be greater than alphabetic characters. Similarly, observe from Figures 2.8 that during a sort (ascending), a computer using ASCII will place numbers ahead of letters because numbers have values less than that for letters. Due to similar reason, it will place uppercase letters ahead of lowercase letters.
48
Chapter 2 ■ Internal Data Representation in Computers
However, whatever may be the type of computer code used, in most collating sequences, the following rules are observed: 1. Letters are considered in alphabetic order (A < B < C < … < Z) and (a < b < c < … < z) 2. Digits are considered in numeric order (0 < 1 < 2 < … < 9) Example 2.32. A computer uses EBCDIC as its internal representation of characters. In which order will this computer sort the strings 23, A1, and 1A? Solution: In EBCDIC, numeric characters have values greater than alphabetic characters (refer to Figure 2.5). Hence, in the said computer, numeric characters will be placed after alphabetic characters and the given strings will be sorted as: A1 < 1A < 23 Therefore, the sorted sequence will be: A1, 1A, and 23
Example 2.33. A computer uses ASCII for its internal representation of characters. In which order will this computer sort the strings 23, A1, 1A, a2, 2a, aA, and Aa? Solution: In ASCII, alphabetic characters have values greater than numeric characters and lower-case letters have values greater than upper-case letters (refer to Figure 2.8). Hence, the said computer will place numeric characters before upper-case letters, and it will place lower-case letters after upper-case letters, causing sorting of the given strings as: 1A < 23 < 2a < A1 < Aa < a2 < aA Therefore, the sorted sequence will be: 1A, 23, 2a, A1, Aa, a2, and aA
BINARY ARITHMETIC In the previous section, we saw that computers store numbers, letters, and other characters in coded form in binary number system. In this section, you will learn why computers use binary numbers instead of decimal numbers, and how they perform basic arithmetic operations internally with binary numbers. Actually, binary arithmetic is simpler to learn because binary number system deals with only two digits – 0 and 1. Since all binary numbers are made up of only 0s and 1s, when arithmetic operations are performed on these numbers, the results are also in 0s and 1s.
■ Binary Arithmetic
49
Why Binary? We saw that use of a number system with smaller base often requires more positions to represent a given value (recall the reason for using octal and hexadecimal notations). For example, 9 10 = 10012. Here, four positions are required instead of one to represent decimal number 9 in binary form. In spite of this, almost all computers use binary numbers. Hence, the obvious question is ‘Why are binary numbers used instead of decimal numbers?’ The reasons are as follows: 1. Information is handled by computer’s electronic/electrical components such as transistors, semiconductors, wires, etc., and all these can indicate only two states or conditions – ON (state 1) or OFF (state 0). Transistors are either conducting (state 1) or non-conducting (state 0), magnetic materials are either magnetized (state 1) in one or opposite direction or non-magnetized (state 0), a pulse or voltage is present (state 1) or absent (state 0) in wire. Information is represented within a computer by the presence or absence of these types of signals. Binary number system having only two digits (0 and 1) is most suitable for expressing the two possible states. Figure 2.9 illustrates the concept of binary components.
Binary state
ON (1)
OFF (0)
Bulb
Switch
Circuit Pulse Figure 2.9. Examples of devices that work in binary mode. These devices can only represent two states – ON (state 1) or OFF (state 0).
2. By using binary numbers, computer circuits only have to handle two binary digits rather than ten decimal digits. This greatly simplifies the internal circuit design of computers resulting in less expensive and more reliable circuits. 3. Finally, all possible operations in decimal number system are also possible in binary number system.
Binary Addition Since binary number system has only two digits, the addition table for binary arithmetic consists of only four entries: 0+0=0 0+1=1 1+0=1 1 + 1 = 0 plus a carry of 1 to next higher column
50
Chapter 2 ■ Internal Data Representation in Computers
Example 2.34.
Since 1 is the largest digit in binary number system, any sum greater than 1 requires a digit to be carried over. For instance, 10 plus 10 binary requires addition of two 1s in the second position. Since 1 + 1 = 0 plus a carryover of 1, the sum of 10 + 10 is 100 in binary.
Add binary numbers 10011 and 1001 in both decimal and binary forms. Solution: Binary carry
By repeated use of the above rules, we can add any two binary numbers by adding two bits at a time. The examples given here illustrate the procedure.
Decimal
11 10011 +1001
carry 1 19 +9
11100
28
In this example, a carry is generated for the first and second columns.
Example 2.35. Add binary numbers 100111 and 11011 in both decimal and binary forms. Solution: Binary carry
11111 100111 +11011
Decimal carry
1000010
1 39 +27 66
In this example, we face a new situation (1 + 1 + 1) brought about by the carryover of 1 in the second column. We can handle this by using the same four rules for binary addition. The addition of three 1s can be broken up into two steps. First, we add only two 1s giving 10 (1 + 1 = 10). The third 1 is now added to this result to obtain 11 (a 1 sum with a 1 carry). Hence, 1 + 1 + 1 = 1, plus a carry of 1 to next higher column.
Binary Subtraction The principles of decimal subtraction hold good for subtraction of numbers in other number systems. It consists of two steps, repeated for each column of the numbers. The first step is to determine, if it is necessary to borrow. If the subtrahend (the lower digit) is larger than the minuend (the upper digit), it is necessary to borrow from the column to the left. Note that the value borrowed depends upon the base of the number system and is always the decimal equivalent of the base. Hence, in decimal 10 is borrowed, in binary 2 is borrowed, in octal 8 is borrowed, and in hexadecimal 16 is borrowed. The second step is to subtract the lower value from the upper value. The complete table for binary subtraction is as follows: 0-0=0 0 - 1 = 1 with a borrow from the next column 1-0=1 1-1=0 The only case in which it is necessary to borrow is when we subtract 1 from 0. The examples given here illustrate the procedure.
■ Binary Arithmetic
51
Example 2.36. Subtract 011102 from 101012. Solution: Borrow
12 0202 10101 - 01110 00111
In the first column (from right to left), 0 is subtracted from 1. No borrow is required in this case and the result is 1. In the second column, we have to subtract 1 from 0. A borrow is necessary to perform this subtraction. Hence, a 1 is borrowed from the third column that becomes 2 (binary 10) in the second column because the base is 2. The third column now becomes 0. Now in the second column, we subtract 1 from 2 giving 1. Since the third column is 0 due to earlier borrow, we have to subtract 1 from 0 for which borrow is required. The fourth column contains a 0 and hence, has nothing to borrow.
Therefore, we have to borrow from the fifth column. Borrowing 1 from the fifth column gives 2 in the fourth column and the fifth column becomes 0. Now the fourth column has something to borrow. When 1 of the 2 in the fourth column is borrowed, it becomes 2 in the third column and 1 remains in the fourth column. Now in the third column, we subtract 1 from 2, giving 1. Subtraction of the fourth column is now 1 from 1, giving 0 and in the fifth column, subtraction is 0 from 0, giving 0. Hence, the result of subtraction is 00111 2. The result may be verified by subtracting 1410 (= 011102) from 2110 (=101012), which gives 710 (= 001112). Example 2.37. Subtract 01110002 from 10111002. Solution: Borrow
2 1011100 -0111000 0100100
Verify the result by subtracting 5610 (= 01110002) from 9210 (= 10111002), which gives 3610 (= 01001002).
Additive Method of Binary Subtraction The method of subtraction by using the borrow concept seems to be easiest when we perform subtraction with paper and pencil. However, when we implement subtraction by means of digital components, this method is less efficient than additive method of subtraction. It may sound surprising to perform even subtraction by using an additive method. Additive method of subtraction is known as complementary subtraction. To understand complementary subtraction, it is necessary to know what complement of a number is. For a number having n digits, its complement is the difference between the number and the base raised to the n th power minus one. The following examples illustrate this: Example 2.38. Find the complement of 3710. Solution:
Example 2.39. Find the complement of 68. Solution:
Since the number has 2 digits and the value of base is 10, (Base)n - 1 = 102 - 1 = 99 Now, 99 - 37 = 62
Since the number has 1 digit and the value of base is 8, (Base)n - 1 = 81 - 1 = 710 = 78 Now, 78 - 68 = 18
Hence, the complement of 3710 = 6210.
Hence, the complement of 68 = 18.
52
Chapter 2 ■ Internal Data Representation in Computers
Example 2.40. Find the complement of 101012. Solution: Since the number has 5 digits and the value of base is 2, (Base)n - 1 = 25 - 1 = 3110 = 111112 Now, 111112 - 101012 = 010102 Hence, the complement of 101012 = 010102.
In case of binary numbers, it is not necessary to go through the usual process of obtaining complement. A quicker way to obtain the complement of a binary number is to transform all its 0s to 1s and all its 1s to 0s. For example, the complement of 1011010 is 0100101. Observe this in case of Example 2.40. Hence, we can easily design the circuit for obtaining the complement of a number in binary number system at less expense.
Performing subtraction by using complementary method involves the following steps: Step 1: Find the complement of the number you are subtracting (subtrahend) Step 2: Add this to the number from which you are taking away (minuend) Step 3: If there is a carry of 1, add it to obtain the result; if there is no carry, recomplement the sum and attach a negative sign to obtain the result. To illustrate the procedure, let us first consider a few examples for decimal numbers. Example 2.42.
Example 2.41. Subtract 5610 from 9210 complementary method.
using
Subtract 3510 from 1810 complementary method.
using
Solution:
Solution: Step 1: Complement of 5610 = 102 - 1 - 56 = 99 - 56 = 4310
Step 1: Complement of 3510 = 102 - 1 - 35 = 99 - 35 = 6410
Step 2:
92 +43 (complement of 56) 135
Step 2:
Step 3:
1 (add the carry of 1)
Result = 36 Verify the result by using the method of normal subtraction: 92 - 56 = 36.
18 +64 (complement of 35) 82
Step 3: Since there is no carry, recomplement the sum and attach a negative sign to obtain the result. Result = -(99 - 82) = -17 Verify the result by using the method of normal subtraction: 18 - 35 = -17
Let us re-work these examples by using binary numbers.
■ Binary Arithmetic
53
Example 2.44.
Example 2.43. Subtract 01110002 (5610) from 10111002 (9210) using complementary method.
Subtract 1000112 (3510) from 0100102 (1810) using complementary method. Solution:
Solution: 1011100 +1000111 (complement of 0111000) 10100011
010010 +011100 (complement of 100011) 101110
1 (add the carry of 1)
Since there is no carry, we have to complement the sum and attach a negative sign to it. Hence,
0100100 Result = 01001002 = 3610 Verify the result with the results of Example 2.37 and Example 2.41.
Result = -0100012 (complement of 1011102) = -1710 Verify the result with the result of Example 2.42.
Binary Multiplication The table for binary multiplication has only four entries, instead of 100 entries necessary for decimal multiplication. It is as follows: 0x0=0 0x1=0 1x0=0 1x1=1 The example given here illustrates the method of binary multiplication. It is only necessary to copy the multiplicand, if the digit in the multiplier is 1 and to copy all 0s, if the digit in the multiplier is 0. The ease with which a system can perform each step of the operation is apparent. Example 2.45. Multiply binary numbers 1010 and 1001. Solution: 1010 Multiplicand x1001 Multiplier 1010 0000 0000 1010
Partial Product Partial Product Partial Product Partial Product
1011010
Final Product
Note that a partial product is obtained by copying the multiplicand when multiplier digit is 1 and by copying all 0s when the multiplier digit is 0. As in decimal multiplication, each partial product is shifted one place to the left from the previous partial product. The final product is obtained by adding all the partial products according to binary addition rules.
54
Chapter 2 ■ Internal Data Representation in Computers
In actual practice, whenever a 0 appears in the multiplier a separate partial product consisting of a string of 0s need not be generated. Instead, only a left shift will do. As a result, Example 2.45 reduces to 1010 x1001 1010 1010SS (S = left shift)
A computer would also follow this procedure in performing multiplication. Verify the result of this multiplication by multiplying 1010 (10102) by 910 (10012), which produces a result of 9010 (10110102).
1011010
It may not be obvious how to handle the addition, if the result of multiplication gives columns with more than two 1s. We can handle them as pairs or by adjusting the column to which the carry is placed (see Example 2.46). Example 2.46. Multiply binary numbers 1111 and 111. Solution:
1
1 x
1 1
1 1
1 1
1 1 1
1 1
1
1 1 1
1
1 1
1
0
1
0
0
1
We can handle the addition in column 3 above in any one of the following two ways: Addition handled as pairs (Column 3)
Addition handled as single carry (Column 3)
1
1
1 1
Sum = 4
1 1
1
1
From problem
Carry from column 2
0 Add two carries to column 4
From problem
1 1
Carry from column 2
100 Carry to column 4 Carry to column 5
■ Binary Arithmetic
55
Additive Method of Binary Multiplication Most computers perform multiplication operation by the way of addition only. For example, we can obtain the result of 4 x 8 by adding the digit 8 four times (8 + 8 + 8 + 8). We may use the same method to perform multiplication operations in binary number system.
Binary Division The table for binary division is as follows: 0÷0 0÷1 1÷0 1÷1
= Divide by zero error = 0 = Divide by zero error =1
Note that since division by zero in any number system is meaningless, computers deal with this problem by raising an error condition called ‘Divide by zero’ error. As in decimal division, the rules for binary division are: 1. Start from the left of the dividend. 2. Perform a series of subtractions, in which the divisor is subtracted from the dividend. 3. If subtraction is possible, put a 1 in the quotient and subtract the divisor from the corresponding digits of dividend. 4. If subtraction is not possible (divisor greater than remainder), record a 0 in the quotient. 5. Bring down the next digit to add to the remainder digits. Proceed as before in a manner similar to long division. The example here illustrates this method. Example 2.47. Divide 1000012 by 1102. Solution:
110
0101
(Quotient)
100001 110
(Dividend) 1
Divisor greater than 100, hence, put 0 in quotient
1000 110
2 3
Add digit from dividend to group used above Subtraction possible, hence, put 1 in quotient
100 110
4 5
Remainder from subtraction plus digit from dividend Divisor greater, hence, put 0 in quotient
1001 110
6 7
Add digit from dividend to group used above Subtraction possible, hence, put 1 in quotient
11
(Remainder)
Verify the result by dividing 3310 (1000012) by 610 (1102), which gives a quotient of 510 (1012) and a remainder of 310 (112).
56
Chapter 2 ■ Internal Data Representation in Computers
Additive Method of Binary Division Most computers perform division operation by repeating complementary subtraction method (additive method). For example, we can obtain the result of 35 ÷ 5 as follows: 35 - 5 = 30 30 - 5 = 25 25 - 5 = 20 20 - 5 = 15 15 - 5 = 10 10 - 5 = 5 5-5= 0
That is, the divisor is subtracted repeatedly from the dividend until the result of subtraction becomes less than or equal to zero. The total number of times we perform subtraction, gives the value of the quotient. In this example, the value of quotient is 7 because the divisor (5) was subtracted 7 times from the dividend (35) until the result of subtraction became zero. If the result of last subtraction is zero, then there is no remainder for the division. However, if it is less than zero, then we ignore the last subtraction and take the result of the previous subtraction as the value of remainder. In this case, we do not count the last subtraction operation for evaluating the value of quotient. The example here illustrates the process.
Example 2.48. Divide 3310 by 610 using the method of addition. Solution: 33 - 6 = 27 27 - 6 = 21 21 - 6 = 15 15 - 6 = 9 9-6= 3 3 - 6 = -3
Total number of subtractions = 6. Since the result of the last subtraction is less than zero, Quotient = 6 - 1 (ignore last subtraction) = 5 Remainder = 3 (result of previous subtraction) Hence, 33 ÷ 6 = 5 with a remainder 3.
Why Do Computers Use Additive Method? We have demonstrated how we can carry out all arithmetic operations by additive method. How this simplifies matter can only be understood in the context of binary (not in decimal). The number of individual steps may indeed increase, if all computer arithmetic is reduced to addition and complementation operations. However, a computer can carry out binary addition and complementation operations at such great speed that this is not a disadvantage. This also saves the labour and expense of designing complicated circuits.
Points to Remember 1.
Number systems are of two types – non-positional and positional.
2.
In a non-positional number system, each symbol represents the same value regardless of its position in the number and to find the value of a number, one has to count the number of symbols present in the number. It is difficult to perform arithmetic with such a number system.
3.
In a positional number system, there are only a few symbols called digits. These symbols represent different values depending on the position they occupy in a number. The value of each digit in such a number is determined by three considerations: The digit itself, The position of the digit in the number, and The base of the number system (where base is the total number of digits in the number system).
■ Points to Remember
57
4.
In our day-to-day life, we use the decimal number system. In this system, base is equal to 10 because there are ten symbols or digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9).
5.
Some positional number systems that are used in computer design and by computer professionals are binary (having base 2), octal (having base 8), and hexadecimal (having base 16).
6.
Figure 2.2 shows the relationship among decimal, hexadecimal, binary, and octal number systems.
7.
Readers must know the techniques for the following types of conversions to convert numbers from one base to another: Converting from another base to decimal Converting from decimal to another base Converting from a base other than 10 to another base other than 10 Shortcut method for binary to octal conversion Shortcut method for octal to binary conversion Shortcut method for binary to hexadecimal conversion Shortcut method for hexadecimal to binary conversion
8.
Fractional numbers are formed in the same way as in decimal number system. In general, a number in a number system with base b is written as: an an-1… a0 . a-1 a-2 … a-m and is interpreted to mean an x bn + an-1 x bn-1 + … + a0 x b0 + a-1 x b-1 + a-2 x b-2 + … + a-m x b-m The symbols an, an-1, …, a-m b symbols allowed in the number system.
9.
used
in
the
above
representation
should
be
one
of
the
Computers use binary coding schemes to represent data internally. In binary coding, a group of bits represents every symbol that appears in the data. The group of bits used to represent a symbol is called a byte. Most modern computers use 8 bits to represent a symbol. Hence, the term byte commonly means a group of 8 bits.
10. BCD, EBCDIC, ASCII, and Unicode are the commonly used computer codes for representing characters internally in computers. 11. BCD (Binary Coded Decimal) is a 6-bit code that can represent 64 different characters. 12. EBCDIC (Extended Binary-Coded Decimal Interchange Code) is an 8-bit code that can represent 256 different characters. 13. ASCII (American Standard Code for Information Interchange) is one of the most popular and widely supported computer codes. It is of two types – ASCII-7 and ASCII-8. ASCII-7 is a 7-bit code that can represent 128 different characters and ASCII-8 is an 8-bit code that can represent 256 different characters. 14. Unicode provides a consistent way of encoding multilingual plain text. It has the capacity to encode as many as a million characters. Unicode has three types of encoding forms – UTF-8, UTF-16, and UTF-32. 15. Collating sequence is ordering of symbols in an encoding standard. 16. Almost all computers use binary numbers for internal computations because we can design and implement electronic circuits for performing arithmetic operations in binary mode more easily, reliably, and inexpensively than those required for performing arithmetic operations in decimal mode. 17. The rules for binary addition are as follows: 0+0=0 0+1=1 1+0=1 1 + 1 = 0 plus a carry of 1 to next higher column 18. The rules for binary subtraction are as follows: 0-0=0 0 - 1 = 1 with a borrow from the next column 1-0=1 1-1=0
58
Chapter 2 ■ Internal Data Representation in Computers 19. For a number having n digits, its complement is the difference between the number and the base raised to the n th power minus one. 20. A quick way to obtain the complement of a binary number is to transform all its 0s to 1s, and all its 1s to 0s. 21. Complementary subtraction is an additive approach of subtraction. 22. The rules for binary multiplication are as follows: 0x0=0 0x1=0 1x0=0 1x1=1 23. The rules for binary division are as follows: 0 ÷ 0 = Divide by zero error 0÷1=0 1 ÷ 0 = Divide by zero error 1÷1=1 24. ‘Divide by zero’ is an error condition raised by the computer when it divides any number by zero. 25. Most computers use the additive approach for performing subtraction, multiplication, and division operations.
Questions 1.
Differentiate between positional and non-positional number systems. Write examples of both types of number systems.
2.
What is base of a number system? Write examples to illustrate the role of base in positional number systems.
3.
What is the value of base for decimal, hexadecimal, binary, and octal number systems?
4.
Write an example for octal number system to show that the same digit may signify different values depending on the position it occupies in the number.
5.
What will be the total number of different symbols or digits and the maximum value of a single digit for the following number systems: (a) Number system with base 5 (c) Number system with base 9 (b) Number system with base 20 (d) Number system with base 12
6.
What is a ‘bit’ in computer terminology? How many different patterns of bits are possible with (a) 6 bits (b) 7 bits (c) 8 bits
7.
Explain the meaning of the term “memory dump”.
8.
Why do we use octal and/or hexadecimal number systems as shortcut notations?
9.
Write the decimal equivalent of the following binary numbers: (e) 1110101 (a) 1101011 (f) 1000 (b) 11010 (c) 10110011 (g) 10110001100 (h) 110001 (d) 11011101
(i) 1010101100 (j) 111
10. Write the octal equivalent of the binary numbers of Question 9. 11. Write the hexadecimal equivalent of the binary numbers of Question 9. 12. Convert the following numbers to decimal numbers: (c) 2A3B16 (a) 1101102 (b) 25736 (d) 12349 13. Convert the following decimal numbers to binary numbers: (a) 43510 (c) 3210 (b) 169410 (d) 13510 14. Convert the decimal numbers of Question 13 to octal numbers.
Questions
59
15. Convert the decimal numbers of Question 13 to hexadecimal numbers. 16. Carry out the following conversions: (a) 1256 = ?4 (b) 249 = ?3
(c)
ABC16 = ?8
17. Convert the following numbers to their binary equivalent: (a) 2AC16 (c) 26148 (b) FAB16 (d) 5628 18. Write the decimal equivalent of the following numbers: (a) 111.012 (c) 247.658 (b) 1001.0112 (d) 2B.D416 19. Define the term ‘byte’. What is the difference between a bit and a byte? 20. Write 4-bit BCD code for the following numbers: (a) 2510 (c) 12810 (b) 6410 (d) 102410 21. Write BCD code for the following words using binary notation: (a) BIT (c) CODE (b) BYTE (d) ZERO 22. Write BCD code for the following words using octal notation: (a) COMPUTER (c) VIDEO (b) INPUT (d) OUTPUT 23. Why BCD code was extended to EBCDIC? 24. How many different characters are possible in the following encoding schemes: (a) BCD (c) ASCII-7 (b) EBCDIC (d) ASCII-8 25. Why octal and hexadecimal shortcut notations are used? Identify shortcut notations used for each of the following computer codes: (a) BCD (c) ASCII-7 (b) EBCDIC (d) ASCII-8 26. Why do we have a packed decimal format? How does it differ from a zoned decimal format? 27. A newly designed computer code uses 9 bits. How many different characters are possible in this code? 28. Write EBCDIC code for the following words in both binary and hexadecimal notations: (a) SUN (c) CAT (b) MOON (d) DOG How many bytes are required to store each of these words using this coding? 29. Write EBCDIC code for the following words in hexadecimal notation: (a) PROGRAM (c) BYTE (b) OUTPUT (d) OCTAL How many bytes are required to store each of these words using this coding? 30. Write EBCDIC code for the following words in binary notation: (a) ZONE (c) SKY (b) BOY (d) TAPE How many bytes are required to store each of these words using this coding? 31. Using hexadecimal notation, write the zoned-decimal coding for the following numbers: (a) 1256 (c) -63 (b) +439 (d) -786 How many bytes are required for each of these representations? 32. Using hexadecimal notation, write the packed-decimal coding for the following numbers: (a) 12915 (c) 872 (b) +9876 (d) -256 How many bytes are required for each of these representations?
60
Chapter 2 ■ Internal Data Representation in Computers 33. List the similarities and differences between 7-bit and 8-bit ASCII. 34. Write ASCII-7 and ASCII-8 codes for the following words using binary and hexadecimal notations: (a) DRY (c) DAMP (b) WET (d) TERM How many bytes are required to store each of these words in ASCII? 35. Write ASCII-7 and ASCII-8 codes for the following words using binary notation: (a) PRINT (c) RUB (b) TYPE (d) GIVE How many bytes are required to store each of these words in ASCII? 36. What is Unicode? What motivated its development? 37. Why it was difficult to exchange text files internationally before development of Unicode? How did Unicode help in overcoming this problem? 38. Why Unicode was developed? 39. List some key features of Unicode. 40. Describe the three encoding forms defined by Unicode standard and the environment for which each of them is most suitable. 41. Explain the meaning of the term “collating sequence”. 42. A computer uses EBCDIC as its internal representation of characters. In which order will this computer sort the following strings? (a) ABC (c) 245 (b) 123 (d) ADD 43. A computer uses ASCII. In which order will this computer sort the following strings? (a) BED (c) ADD (e) BaD (b) 512 (d) 2good (f) a1B2 44. Write full form of the following abbreviations: (a) BCD (c) ASCII (b) EBCDIC (d) UTF 45. Why computers use the binary number system? 46. Add binary numbers 1011 and 101 in both decimal and binary forms. 47. Add binary numbers 1010110 and 1011010. 48. Add binary numbers 10111 and 1011. 49. Find the complement of the following numbers: (a) 49510 (c) 48 (b) 2910 (d) C16 50. Find the complement of the following binary numbers: (d) 011011 (a) 10 (e) 10110001 (b) 101 (f) 001101001110 (c) 101101 51. Subtract 01101112 from 11011102. 52. Subtract 010102 from 100002. 53. Subtract 0110112 from 1101112. 54. Subtract 2510 from 5010 using complementary method. 55. Subtract 2510 from 2010 using complementary method. 56. Subtract 23410 from 58810 using complementary method. 57. Subtract 21610 from 17210 using complementary method. 58. Subtract 010102 from 100002 using complementary method.
(e) 25 (f) 324
■ Questions
61
59. Subtract 1101112 from 1011102 using complementary method. 60. Subtract 0110112 from 1101112 using complementary method. 61. Subtract 11112 from 11002 using complementary method. 62. Multiply binary numbers 1100 and 1010. 63. Multiply binary numbers 01101 and 1001. 64. Multiply binary numbers 101111 and 111. 65. Divide 110012 by 1012. 66. Divide 01101112 by 01112. 67. What happens when a computer divides a number by zero? 68. Explain how a computer performs multiplication and division operations in a computer by using additive approach. 69. What is the primary advantage of performing subtraction by complementary method in digital computers? 70. Discuss the advantages and disadvantages of performing various arithmetic operations by additive method in a digital computer.
Digital Systems Design
Digital computers use several interconnected digital components, which in turn use digital circuits in the design. Logic gates and flip-flops are the building blocks of all digital circuits in a computer. As digital circuits conform to the arithmetic and logic operations of binary numbers, the algebra that deals with binary number system is used in the design of digital circuits. This algebra is called Boolean algebra. This chapter deals with the design of digital systems. It introduces Boolean algebra, logic gates, and flip-flops. It then explains and illustrates with suitable examples how to use Boolean algebra, logic gates, and flip-flops to design digital circuits (both combinational and sequential).
BOOLEAN ALGEBRA In mid 1800, George Boole (1815-1864), an English mathematician, developed an algebra known as Boolean algebra for simplifying the representation and manipulation of propositional logic. Later, in the year 1938, Claude E. Shannon proposed the use of Boolean algebra in the design of relay switching circuits. The basic techniques described by Shannon were adopted universally for the design and analysis of switching circuits. Owing to analogous relationship between the action of relays and modern electronic circuits, computer designers still use the same techniques. Boolean algebra provides an economical and straightforward approach to the design of relay and other types of switching circuits. Like the basic theorems of algebra help in simplifying an ordinary algebraic expression, Boolean algebra helps in simplifying an expression describing a switching circuit. Today, computer designers use Boolean algebra for designing the electronic circuits of computers.
Fundamental Concepts of Boolean Algebra Fundamental concepts of Boolean algebra are described here.
■ Boolean Algebra
63
Use of Binary Digits In a normal algebraic expression, a variable can take any numerical value. For example, in the expression 3A 7B = C, each of the variables A, B, and C may have any value from the entire field of real numbers. Since, Boolean algebra deals with binary number system, the variables used in Boolean equations may have only two possible values (0 or 1). For example, in the Boolean equation A B = C, the variables A, B, and C may have only the values 0 or 1.
Logical Addition (OR Operator) The symbol ‘’ is used for logical addition (OR) operator. We can define the symbol (OR operator) by listing all possible combinations of A and B with the resulting value of C for each combination in the equation A B = C. Since the variables A and B can have only two possible values (0 or 1), only four (22) combinations of inputs are possible (see the table in Figure 3.1). The table gives resulting output values for each of the four input combinations. Such a table is known as a truth table. Hence, Figure 3.1 is the truth table for logical OR operator.
Inputs A B 0 0 0 1 1 0 1 1
Output = C 0 1 1 1
Figure 3.1. Truth table for logical OR () operator.
The result is 0, only when both input variables are 0. It is 1, when any one or both of the input variables are 1. This is the reason why the symbol ‘’ does not mean normal arithmetic addition, but means logical addition. Boolean algebra extends this concept of logical addition to multiple variables. For example, in the equation A B C D = E, even if A, B, C, and D, all had the value of 1, the sum of the values (the result E) would be 1 only. The equation A B = C is normally read as “A or B equals C”.
Logical Multiplication (AND Operator) The symbol ‘’ is used for logical multiplication (AND) operator. Figure 3.2 shows the truth table for logical AND operator. The result C is equal to 1 only when both input variables A and B are 1, otherwise it is 0. The equation A B = C is normally read as “A and B equals C”.
Inputs A 0 0 1 1
B 0 1 0 1
Output =
C 0 0 0 1
Figure 3.2. Truth table for logical AND ( ) operator.
Complementation (NOT Operator) OR and AND are binary operations because they operate on two variables. Complementation is a unary operation defined on a single variable. The symbol ‘’ is used for complementation operator. Hence, we write A meaning, “complement of A”, or
Input
Output
AB.”
A
A
Complementation of a variable is the reverse of its value. Hence, if A = 0, then A = 1 and if A = 1, then A = 0. Figure 3.3 shows the truth table for logical NOT () operator. A is read as “complement of A” or “not of A”.
0 1
1 0
A B
meaning,
“complement
of
Figure 3.3. Truth table for logical NOT () operator.
64
Chapter 3 ■ Digital Systems Design
Operator Precedence Does A B C mean (A B) C or A (B C)? The two generate different values for A = 1, B = 0, and C = 0 because we have (1 0) 0 = 0 and 1 (0 0) = 1. Hence, we need operator precedence rules to evaluate Boolean expressions correctly. The precedence rules for Boolean operators are as follows: 1. The expression is scanned from left to right. 2. Expressions enclosed within parentheses are evaluated first. 3. All complement (NOT) operations are performed next. 4. All ‘’ (AND) operations are performed after that. 5. Finally, all ‘’ (OR) operations are performed in the end. According to these rules, A + B C should be evaluated as A + (B C). Similarly, for the expression A B , complement of A and B are both evaluated first and the results are then ANDed. Again, for the expression A B , the expression inside the parenthesis (A + B) is evaluated first and the result is then complemented.
Postulates of Boolean Algebra Postulate 1:
Postulate 4: Associative Law
(a) A = 0, if and only if, A is not equal to 1 (b) A = 1, if and only if, A is not equal to 0 Postulate 2:
(a) x (y z) = (x y) z (b) x (y z) = (x y) z Postulate 5: Distributive Law
(a) x 0 = x (b) x 1 = x
(a) x (y z) = x y x z (b) x (y z) = (x y) (x z)
Postulate 3: Commutative Law
Postulate 6:
(a) x y = y x (b) x y = y x
(a) x x = 1 (b) x x = 0
These postulates are the basic axioms of the algebraic structure that need no proof. They are used to prove the theorems of Boolean algebra.
The Principle of Duality In Boolean algebra, there is a precise duality between the operators (AND) and (OR) and the digits 0 and 1. For instance in Figure 3.4, the second row is obtainable from the first row and vice-versa by interchanging ‘’ with ‘’ and ‘0’ with ‘1’. This property is known as the principle of duality in Boolean algebra.
Row 1 Row 2
Column 1
Column 2
Column 3
11=1 00=0
1+0=01=1 01=10=0
00=0 11=1
Figure 3.4. Illustrating the principle of duality in Boolean algebra.
■ Boolean Algebra
65
The implication of this principle is that any theorem in Boolean algebra has its dual obtainable by interchanging ‘’ with ‘’ and ‘0’ with ‘1’. Hence, if we prove a theorem, its dual theorem also holds, which we need not prove separately.
Theorems of Boolean Algebra Some important theorems of Boolean algebra are stated below along with their proof. Theorem 1 (Idempotent law) (a) x x = x (b) x x = x
Proof of (a) L.H.S. =xx = (x x) 1 = (x x) (x + x ) =xx x =x0 =x = R.H.S.
Proof of (b)
by postulate 2(b) by postulate 6(a) by postulate 5(b) by postulate 6(b) by postulate 2(a)
L.H.S. =xx =xx0 =xxx x = x (x + x ) =x1 =x = R.H.S.
by postulate 2(a) by postulate 6(b) by postulate 5(a) by postulate 6(a) by postulate 2(b)
Note that Theorem 1(b) is the dual of Theorem 1(a) and each step of the proof in part (b) is the dual of part (a). We can derive any dual theorem from the proof of its corresponding pair in a similar manner. Hence, from now onwards, the proof of only part (a) will be given. Interested readers can apply the principle of duality to various steps of the proof of part (a) to obtain the proof of part (b) for any theorem. Theorem 2 (a) x 1 = 1 (b) x 0 = 0
Proof of (a) L.H.S. =x1 = (x 1) 1 by postulate 2(b) = (x + 1) (x + x ) by postulate 6(a) =x1 x by postulate 5(b) by postulate 3(b) =x x 1 by postulate 2(b) =x+ x =1 by postulate 6(a) = R.H.S.
Theorem 3 (Absorption law) (a) x x y x (b) x . (x + y) x
Proof of (a) L.H.S. =xxy =x1xy = x (1 y) = x (y 1) =x1 =x = R.H.S.
by postulate 2(b) by postulate 5(a) by postulate 3(a) by theorem 2(a) by postulate 2(b)
Proof of (b) holds by duality.
Proof of (b) holds by duality.
Proof by the Method of Perfect Induction We can also prove the theorems of Boolean algebra by means of truth tables. In a truth table, we need to check if both sides of the relation yield identical results for all possible combinations of variables involved. In principle, it is
66
Chapter 3 ■ Digital Systems Design
possible to enumerate all possible combinations of variables involved because Boolean algebra deals with variables having only two values. This method of proving theorems is called exhaustive enumeration or perfect induction. For example, Figure 3.5 is a truth table for proving Theorem 3(a) by perfect induction method. Similarly, Figure 3.6 proves Theorem 3(b) by perfect induction method. =
= x
y
xy
xxy
x
y
xy
x (x y)
0 0 1 1
0 1 0 1
0 0 0 1
0 0 1 1
0 0 1 1
0 1 0 1
0 1 1 1
0 0 1 1
Figure 3.5. Truth table for proving Theorem 3(a) by the method of perfect induction.
Figure 3.6. Truth table for proving Theorem 3(b) by the method of perfect induction.
Theorem 4 (Involution Law)
x =x Proof The truth table below proves this theorem by the method of perfect induction. = x
x
x
0 1
1 0
0 1
Note that Theorem 4 has no dual because it deals with a unary operator (NOT).
Theorem 5 (a) x ( x + y ) = x y (b) x + x y = x y
Proof of (a)
Proof of (b)
The truth table below proves this theorem by the method of perfect induction.
The truth table below proves this theorem by the method of perfect induction.
x
y
x
x+y
x ( x y)
xy
x
y
x
x y
x+ x y
x+y
0 0 1 1
0 1 0 1
1 1 0 0
1 1 0 1
0 0 0 1
0 0 0 1
0 0 1 1
0 1 0 1
1 1 0 0
0 1 0 0
0 1 1 1
0 1 1 1
■ Boolean Algebra
67
Theorem 6 (De Morgan’s Law) (a)
xy = x y
(b) x y = x + y
Proof of (a) The truth table below proves this theorem by the method of perfect induction. x
y
x+y
xy
0 0 1 1
0 1 0 1
0 1 1 1
1 0 0 0
x y
x
y
1 1 0 0
1 0 1 0
1 0 0 0
Proof of (b) The truth table below proves this theorem by the method of perfect induction.
x
y
xy
xy
0 0 1 1
0 1 0 1
0 0 0 1
1 1 1 0
x
y
1 1 0 0
1 0 1 0
x y
1 1 1 0
Theorems 6(a) and 6(b) are known as De Morgan’s law. They can be extended to n variables as given below:
X1 X 2 X 3 ... X n X1 X 2 X 3 … X n X1 X2 X3 ... Xn X1 + X 2 + X 3 + … + X n Figure 3.7 summarizes the basic Boolean identities. Readers should become conversant with the identities given in this table to use the algebra effectively.
68
Chapter 3 ■ Digital Systems Design
Sr. No.
Identities
Dual identities
1
A+0=A
A1=A
2
A+1=1
A0=0
3
A+A=A
AA=A
4
A+A =1
A A =0
5 6
A = A A+B=B+A
AB=BA
7
(A + B) + C = A + (B + C)
(A B) C = A (B C)
8
A (B + C) = A B + A C
A + B C = (A + B) (A + C)
9
A+AB=A
A (A + B) = A
10
A+ A B=A+B
A ( A B) = A B
11
AB= A B
AB= A B
Figure 3.7. Summary of basic Boolean identities.
BOOLEAN FUNCTIONS A Boolean function is an expression formed with binary variables, Boolean operators (OR, AND, and NOT), parentheses, and equal to sign. Each variable can be either 0 or 1, and for a set of values of the variables the value of the function can also be either 0 or 1. For example, consider the equation W=X+ Y Z Here W is a function of the variables X, Y, and Z, written as W = f(X, Y, Z), and the right hand side of the equation is called an expression. X, Y, and Z are referred to as literals of this function. This Boolean function is in the form of an algebraic expression. A truth table with 2n rows can also represent a Boolean function, where n is the number of literals (binary variables) in the function. The combinations of 0s and 1s for the rows of this table are obtained from the binary numbers by counting from 0 to 2n - 1. The value of the function corresponding to each row of the table is listed in a separate column of the table. Figure 3.8 shows such a truth table for the function W = X + Y Z.
X
Y
Z
W
0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1
0 1 0 0 1 1 1 1
Figure 3.8. Truth table for the Boolean function W = X +
Y Z.
Observe that there are (23) possible combinations for assigning bits to three variables. The column labeled W is either a 0 or 1 for each of these combinations. Out of eight, there are five combinations for which W = 1. A usual question is – “Is an algebraic expression for a Boolean function unique?” In other words, “Is it possible to find two algebraic expressions specifying the same Boolean function?” The answer is yes. In Boolean algebra we often deal with the problem of finding simpler expressions for a given expression. For example, Figure 3.9 shows the truth table for two equal Boolean functions:
■ Boolean Functions
F1 x y z x y z x y
69
and
F2 x y x z Function F2 is the same as function F1 since both have identical 0s and 1s for each combination of values of the three binary variables x, y, and z. In general, two functions of n binary variables are equal, if they have the same value for all possible 2 n combinations of the n literals.
x
y
z
F1
F2
0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1
0 1 0 1 1 1 0 0
0 1 0 1 1 1 0 0
Figure 3.9. Truth table for the Boolean functions:
F1 x y z x y z x y F2 x y x z
and
Minimization of Boolean Functions by Algebraic Manipulations Logic gates (discussed later in this chapter) are used to implement Boolean functions. In such implementations, each term of a function is implemented with a gate and each literal of the term forms an input to the gate. Hence, for a given Boolean function, minimization of the number of literals and the number of terms results in a circuit with fewer components. For example, since functions F1 and F2 of Figure 3.9 are equal Boolean functions and F2 has fewer terms, it is more economical to implement the F 2 form than the F1 form. One must know how to simplify Boolean expressions to obtain equal and simpler expressions. What constitutes the best form of a Boolean function depends on the application, but here we will consider only the criterion of component minimization achieved by literal minimization. Two commonly used methods to minimize the number of literals in a Boolean function are algebraic manipulations and map simplification. The algebraic manipulations method is described here and the map simplification method is described in a later sub-section. The algebraic manipulations method uses cut-and-try procedure by repeated application of the postulates and basic theorems of Boolean algebra. There is no specific rules or guidelines to be followed guaranteeing the final answer. Following examples illustrate this method. Note that in Example 3.1, functions Fa and Fb are the dual of each other and use dual expressions in corresponding minimization steps. Function Fc shows the equality of functions F1 and F2 of Figure 3.9. Function Fd illustrates the fact that an increase in the number of literals sometimes leads to a final simpler expression. Observe that function F e is the dual of function Fd. Hence, its minimized expression can be derived directly from the dual of the steps used to derive function Fd.
70
Chapter 3 ■ Digital Systems Design
Example 3.1. Simplify the following Boolean functions to a minimum number of literals. (a) Fa = x + x y (b) Fb = x ( x + y) (c) Fc = x y z + x y z + x y (d) Fd = x y + x z + y z (e) Fe = (x + y) ( x + z) (y + z)
Solution: (a) Fa = x + x . y = (x + x ) (x + y)
by postulate 5(b)
= 1 (x + y)
by postulate 6(a)
= (x + y) 1
by postulate 3(b)
=x+y
by postulate 2(b)
(b) Fb = x ( x + y) = xx + x y
by postulate 5(a)
=0+xy
by postulate 6(b)
=xy+0
by postulate 3(a)
=xy
by postulate 2(a)
(c) Fc = x y z + x y z + x y = x z ( y + y) + x y
by postulate 5(a)
= x z (y + y ) + x y
by postulate 3(a)
= x z1+x y
by postulate 6(a)
= x z+x y
by postulate 2(b)
(d) Fd = x y + x z + y z =xy+ x z+yz1
by postulate 2(b)
= x y + x z + y z (x + x )
by postulate 6(a)
=xy+ x z+yzx+yz x
by postulate 5(a)
=xy+ x z+xyz+ x yz
by postulate 3(b)
=xy1+ x z+xyz+ x yz
by postulate 2(b)
=xy1+xyz+ x z+ x yz = x y (1 + z) + x z (1 + y) = x y (z + 1) + x z (y + 1) =xy1+ x z1 =xy+ x z (e) Fe = (x + y) ( x + z) (y + z) = (x + y) ( x + z )
by postulate 3(a) by postulate 5(a) by postulate 3(a) by theorem 2(a) by postulate 2(b) by duality from (d)
■ Boolean Functions
71
Complement of a Function Complement of a Boolean function F is F . It is obtained by interchanging 0s for 1s and 1s for 0s in the truth table defining the function. For example, Figure 3.10 defines the function F = x y + x z and its complement F . Algebraically, we can derive the complement of a function from De Morgan’s theorems whose generalized forms are as follows:
A1 A2 A3 ... An A1 A 2 A 3 … A n
A1 A2 A3 ... An A1 + A 2 + A 3 + … + A n
x
y
z
F
F
0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1
0 1 0 1 1 1 0 0
1 0 1 0 0 0 1 1
Figure 3.10. Truth table for the function F = x y +
x
z and its complement F .
These theorems state that the complement of a function is obtained by interchanging OR and AND operators and complementing each literal. Example 3.2 illustrates this method. A simpler method to derive the complement of a function is to take the dual of the function and then complement each literal. This method follows from the generalized De Morgan’s theorems. Recall that the dual of a function is obtained by interchanging OR and AND operators and 0s and 1s. Example 3.3 illustrates this method. Example 3.2. Find the complement of following functions: (a)
F1 x y z x y z
(b)
F2 x ( y z y z )
Solution: Applying De Morgan’s theorems, as many times as necessary, the complements are obtained as follows: (a) F1 x y z x y z
x y z x y z
=
x y z x y z
=
x y z x y z
(b) F2 = x y z y z
= x y z y z = x yz yz
=
x y z y z
=
x y z y z
72
Chapter 3 ■ Digital Systems Design
Example 3.3. Find the complement of the functions F1 and F2 of Example 3.2 by taking their dual and complementing each literal. Solution: (a) F1 x y z x y z The dual of
(b)
F1 is: x y z x y z
Complementing each literal, we get
F2 x y z y z The dual of F2 is:
x y z y z
Complementing each literal, we get F2 = x y z y z
F1 x y z x y z
Canonical Forms of Boolean Functions Minterms and Maxterms A binary variable may appear either in its normal form (x) or in its complement form x . Now, consider two binary variables x and y combined with an AND operator. Since each variable may appear in either of the two forms, there are four possible combinations:
x y,
x y,
x y,
Each of these four AND terms is called a minterm or a standard product.
xy
Similarly, we can combine n variables to form 2n minterms. We can determine the 2n different minterms by a method similar to the one shown in Figure 3.11 for three variables. The binary numbers from 0 to 2 n - 1 are listed under the n variables. Each minterm is obtained from an AND term of the n variables with each variable being primed, if the corresponding bit of the binary number is 0, and unprimed, if it is a 1. Variables
Minterms
Maxterms
x
y
z
Term
Designation
Term
Designation
0
0
0
x yz
m0
xyz
M0
0
0
1
0
1
0
x yz x yz
m1 m2 m3
x yz x yz x yz
M4
x yz
M5
x yz
M1 M2
0
1
1
x yz
1
0
0
x yz
1
0
1
x yz
m4 m5
1
1
0
x yz
M6
1
1
x yz x y z
m6
1
m7
x yz
M7
M3
Figure 3.11. Minterms and Maxterms for three variables.
■ Boolean Functions
73
The figure also shows a symbol for each minterm. It is of the form mj, where j denotes the decimal equivalent of the binary number of the minterm designated. Similarly, n variables forming an OR term, with each variable being primed or unprimed, provide 2 n possible combinations called maxterms or standard sums. Figure 3.11 shows the eight maxterms for three variables and their symbolic designations. Any 2 n maxterms for n variables may be determined similarly. Each maxterm is obtained from an OR term of the n variables, with each variable being unprimed, if the corresponding bit is a 0, and primed, if it is a 1. Note that each maxterm is the complement of its corresponding minterm and vice-versa.
Sum-of-Products A sum-of-products expression is a single product term (minterm) or several product terms (minterms) logically added (ORed) together. For example, expression x y x y is a sum-of-products expression. Following are all sum-ofproducts expressions: Following steps are followed to express a Boolean function in its sum-of-products form:
x x+y x+yz
1. Construct a truth table for the given Boolean function.
xy+z
2. Form a minterm for each combination of the variables that produces a 1 in the function.
x y x yz
3. The desired expression is the sum (OR) of all minterms obtained in Step 2. For example, in case of function F1 of Figure 3.12, following three combinations of the variables produce a 1: 001,
100,
and
111
Their corresponding minterms are
x y z,
x yz ,
and
x yz
Taking the sum (OR) of these minterms, we can express function F1 in its sum-of-products form as:
F1 x y z x y z x y z or F2 m1 m4 m7
x
y
z
F1
F2
0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1
0 1 0 0 1 0 0 1
0 0 0 1 0 1 1 1
Figure 3.12. Truth table for functions F 1 and F2.
Similarly, we can express function F2 of Figure 3.12 in its sum-of-products form as:
F2 x y z x y z x y z x y z F2 m3 m5 m6 m7
or
It is sometimes convenient to express a Boolean function in its sum-of-products form. If it is not in this form, we can convert it into this form by first expanding the expression into a sum of AND terms. We then inspect each term to see if it contains all the variables. If it misses one or more variables, we AND it with an expression of the form (x + x ), where x is one of the missing variables. Following example illustrates this procedure.
74
Chapter 3 ■ Digital Systems Design
Example 3.4. Express the Boolean function F = A + B C in sum-of-minterms (products) form. Solution: The function has three variables A, B, and C. The first term A is missing two variables (B and C), therefore, A = A (B + B ) = A B + A B This is still missing one variable (C), therefore, A = A B (C + C ) + A B (C + C ) = A B C + A B C + A B C + A B C The second term B C is missing one variable (A), therefore,
B C = B C (A + A ) =A BC+ A BC Now, by combining all the terms we get F = A B C + A B C + A B C + A B C + A B C +A B C However, the term A B C appears twice in this expression and according to Theorem 1(a) we have x + x = x. Hence, removing one of them and rearranging the minterms in ascending order we obtain:
F AB C A B C A B C A BC A B C = m1 m4 m5 m6 m7 We often use a shortcut notation to express a Boolean function in its sum-of-minterms form. We can represent the result of Example 3.4 in this notation as: F (A, B, C) = (1, 4, 5, 6, 7)
Product-of-Sums A product-of-sums expression is a single sum term (maxterm) or several sum terms (maxterms) logically multiplied (ANDed) together. For example, expression ( x + y) (x + y ) is a product-of-sums expression. Following are all product-of-sums expressions:
■ Boolean Functions x ( x + y) (x + y)z (x + y ) ( x + y ) ( x + y ) (x + y) ( x + y + z )
75
Following steps are followed to express a Boolean function in its product-of-sums form: 1. Construct a truth table for the given Boolean function. 2. Form a maxterm for each combination of the variables that produce a 0 in the function. 3. The desired expression is the product (AND) of all maxterms obtained in Step 2.
For example, in case of function F1 of Figure 3.12, following five combinations of the variables produce a 0: 000, 010, 011, 101, and 110 Their corresponding maxterms are: (x + y + z), (x + y + z), (x + y + z ), ( x + y + z ), and ( x + y + z) Taking the product (AND) of all these maxterms, we can express function F1 in its product-of-sums form as:
F1 x y z x y z x y z x y z x y z
or
F2 M0 M2 M3 M5 M6 Similarly, we can express function F2 of Figure 3.12 in its product-of-sums form as: F2 = (x + y + z) (x + y + z ) (x + y + z) ( x + y + z)
or
F2 = M 0 M1 M 2 M 4
To express a Boolean function in its product-of-sums form, we must first express it as a product of OR terms. We do this by using the distributive law. For example, x + y z = (x + y) (x + z) Then we OR any missing variable (say x) in each OR term with the form x x . Following example illustrates the procedure. The sum-of-products and product-of-sums forms of Boolean expressions are known as standard forms. They are preferred for logic circuit design because of their straightforward conversion into simpler logic gate networks. In their simplest form, they go into two-level networks in which the longest path for any signal to pass from input to output is through two gates.
76
Chapter 3 ■ Digital Systems Design
Example 3.5. Express the Boolean function F = x y + x z in product-of-maxterms (sums) form. Solution: At first, we convert the function into product of OR terms by using distributive law: F=xy+ x z = (x y + x ) (x y + z) = (x + x ) (y + x ) (x + z) (y + z) = ( x + y) (x + z) (y + z) The function has three variables (x, y, and z) and each OR term is missing one variable. Therefore,
x + y = x + y + z z = ( x + y + z) ( x + y + z ) x + z = x + z + y y = (x + z + y) (x + z + y ) = ( x + y + z) ( x + y + z ) y + z = x x + y + z = (x + y + z) ( x + y + z) Now by combining all the terms and retaining only one instance of those that appear more than once, we obtain: F = (x + y + z) (x + y + z) ( x + y + z) ( x + y + z ) = M0 M2 M4 M5 We may represent this in shortcut notation as: F (x, y, z) = (0, 2, 4, 5) The product symbol represents ANDing of the maxterms. The numbers following it are the maxterms of the function.
Conversion between Canonical Forms Complement of a function, expressed as sum-of-minterms, equals the sum-of-minterms missing from the original function. This is because the original function is expressed by those minterms that make the function equal to 1, while its complement is a 1 for those minterms for which the function is a 0. For example, the function F (A, B, C) = (1, 4, 5, 6, 7) = m1 m 4 m 5 m 6 m 7
has its complement expressed as: F (A, B, C) = (0, 2, 3) = m 0 m 2 m 3
Taking the complement ofF by De Morgan’s theorem, we obtain F back in a different form: F=
m0 m 2 m3
= m0 m 2 m3 = M 0 M 2 M3
■ Boolean Functions
77
= (0, 2, 3)
The last conversion follows from the definition of minterms and maxterms shown in Figure 3.11. From the figure, it is clear that the following relation holds true:
mj Mj That is, the maxterm with subscript j is a complement of the minterm with the same subscript j and vice-versa. The last example has demonstrated the conversion of a function, expressed as sum-of-minterms, into its equivalent function in product-of-maxterms form. The conversion from product-of-maxterms form to sum-of-minterms form is similar. We now state a general conversion procedure: “To convert from one canonical form to another, interchange the symbol ( with or with ) and list those numbers (from all possible numbers in the range 0 to 2 n-1, where n is the number of binary variables in the function) missing from the original form.” For example, the function F (x, y, z) = (0, 2, 4, 5) is expressed in product-of-maxterms form. Its conversion to sum-of-minterms form is: F (x, y, z) = (1, 3, 6, 7) In this case, there are three variables (x, y, and z). Hence, n = 3 and the total number of minterms or maxterms are 2 3 = 8 (0 to 7). The missing terms in product-of-maxterms form (1, 3, 6, and 7), therefore, become part of sum-ofminterms form.
Minimization of Boolean Functions by Map Simplification Map simplification method (also known as Karnaugh map or K-map) of minimizing Boolean functions requires us to represent a Boolean function in the form of a K-map. We have already seen how to represent a Boolean function in the form of a truth table or in canonical forms – sum-of-products and product-of-sums. We will now see how to represent a Boolean function in the form of a K-map.
What is K-map? K-map is a pictorial representation of a Boolean function. It consists of squares, with each square representing one minterm. Hence, the number of squares in a map of n variables is 2n. Figure 3.13 shows K-maps for Boolean functions of two, three, and four variables. Each square is assigned a minterm number as shown in the figure. Notice that minterm numbers are assigned in such a manner that the binary equivalent of minterm numbers of adjacent squares differ by one bit only. For example, notice that in case of four-variable map, the squares in the first column are assigned the minterm numbers 0, 1, 3, 2 from top to bottom instead of 0, 1, 2, 3. This is because binary equivalent of numbers 1 (0001) and 2 (0010) differ by two bits, but binary equivalent of numbers 1 (0001) and 3 (0011) as well as 3 (0011) and 2 (0010) differ by one bit only. The variable names (A, B, C, D) of the Boolean function are listed across both sides of the diagonal line in the upper left hand corner of the map. The 0s and 1s marked along each row and each column designate the value of the corresponding variables for the squares in that row or column. Additionally, the rows and columns are divided into sets (non-disjoint) marked by curly brackets and each set is designated by one of the variables of the Boolean function. Notice that each variable associated with such a set of squares (written on the curly brackets) contains half of the squares in the map where that variable appears uncomplemented. The variable appears complemented in the remaining half of the squares.
78
Chapter 3 ■ Digital Systems Design
Minterm numbers assigned to squares Binary Decimal equivalent 0 00 1 01 2 10 3 11
A A B 0 B
1
0
1
0
2
1
3
(a) Two-variable map
Minterm numbers assigned to squares Binary Decimal equivalent 0 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111
AB
C
Term
Designation
AB
m0 m1 m2 m3
AB AB AB
(a) Corresponding minterm table
A C
Corresponding minterms
00
01
11
10
0
0
2
6
4
1
1
3
7
5
B (b) Three-variable map
Corresponding minterms Term
Designation
ABC
m0 m1 m2 m3 m4 m5 m6 m7
ABC ABC ABC ABC
ABC ABC ABC
(b) Corresponding minterm table
A AB 00
01
11
10
0
4
12
8
01
1
5
13
9
11
3
7
15
11
10
2
6
14
10
CD 00
D
(c) Corresponding minterm table can be drawn in a similar manner as those for two-variable and threevariable maps.
C
B (c) Four-variable map Figure 3.13. Maps for two-, three-, and four-variable Boolean functions. Corresponding minterm tables are shown here only for the purpose of understanding. They need not be drawn during simplification of Boolean functions. Only the map should be drawn.
Notice from the tables by the side of the maps in the figure that the minterm represented by a square is determined from the binary values of the variables in the corresponding row and column. For example, in the three-variable map, the binary values of the variables corresponding to the last square (last row, last column) are A = 1, B = 0 and C = 1. Hence, the minterm represented by the square is ABC , and this is the reason why the square is assigned the minterm number 5 (decimal equivalent of binary 101). Similarly, in the four-variable map, the minterm represented by the square having number 13 is ABCD because the binary values of the variables corresponding to that square are A = 1, B = 1, C = 0 and D = 1.
■ Boolean Functions
79
Adjacent Squares in K-map Minimization of Boolean functions by map simplification deals with grouping adjacent squares that contain 1s. For doing this, it is important to understand the definition of adjacency of squares in K-maps, which is as follows: “Any two squares whose binary values of minterms differ by a single bit are adjacent.” According to this definition, the following squares are adjacent: 1. The square to the immediate left of a square (for example, square 3 is adjacent to 7) 2. The square to the immediate right of a square (for example, square 6 is adjacent to 2) 3. The square immediately above a square (for example, square 4 is adjacent to 5) 4. The square immediately below a square (for example, square 1 is adjacent to 0) 5. The squares at the extreme ends of the same horizontal row (for example, in three-variable map, square 0 is adjacent to 4 and square 1 is adjacent to 5) 6. The squares at the top and bottom of the same column (for example, in four-variable map, 0 and 2, 4 and 6, 12 and 14, and 8 and 10 are adjacent) 7. The four corner squares (for example, in three-variable map, 0-1-4-5 are adjacent, and in four-variable map, 0-2-8-10 are adjacent)
Obtaining Simplified Boolean Function with a K-map The following procedure is used to obtain a simplified Boolean function with a K-map: 1. Draw an n-variable K-map depending on the number of variables (n) in the given Boolean function. 2. From the truth table of the given Boolean function, insert 1s in those squares of the map whose corresponding minterms are 1. 3. Combine the squares containing 1s in groups of adjacent squares. These groups must contain a number of squares that is an integral power of 2. The groups are not disjoint sets of squares in the sense that one or more squares of a group may also belong to another group. 4. Each group of squares represents an algebraic term. All these terms are ORed to obtain the simplified algebraic expression of the given Boolean function. The following examples illustrate the procedure. Example 3.6 Simplify the Boolean function F x y z x y z x y Solution: At first, we must convert the function into sum-of-minterms. Hence,
F x yz x yz x y x y z x y z x y (z z) x yz x yz x yzx y z = m1 + m3 + m5 + m4
(Continued on next page …)
80
Chapter 3 ■ Digital Systems Design Hence, F (x, y, z) = ∑ (1, 3, 4, 5) Now we plot the minterms on a three-variable map and combine the squares containing 1s in groups of adjacent squares. x xy z
00
01
11
10
0 z
1
1 1
1
1
xy (set of two adjacent squares 4 and 5)
y xz (set of two adjacent squares 1 and 3)
We then represent each group as an algebraic term and take the OR of those terms. Hence, F x y x z is the simplified form of the given Boolean function. Verify the result with the result of function Fc of Example 3.1. Example 3.7 Simplify the Boolean function
F x y x z yz
Solution: At first, we must convert the function into sum-of-minterms. Hence,
F x y xz yz x y (z z) x z (y y) y z (x x) x yz x y z x yz x yzx yz x yz (x y z x y z) (x y z x y z) x y z x y z x yzx yzx y z x yz = m7 + m3 + m6 + m1 Hence, F (x, y, z) = ∑ (1, 3, 6, 7) Now we plot the minterms on a three-variable map and combine the squares containing 1s in groups of adjacent squares. x xy 00
z
01
11
0 z
1
1 1
1
10
xy (set of two adjacent squares 6 and 7)
1 y
xz (set of two adjacent squares 1 and 3)
We then represent each group as an algebraic term and take the OR of those terms. Hence, F x y x z is the simplified form of the given Boolean function. Verify the result with the result of function Fd of Example 3.1.
■ Boolean Functions Example 3.8 Simplify the Boolean function F (A, B, C) = ∑ (1, 5, 6, 7) Solution: Plot the minterms on a three-variable map and combine the squares containing 1s in groups of adjacent squares. A AB C
00
01
11
0 C
10
AB (set of two adjacent squares 6 and 7)
1
1
1
1
1
BC (set of two adjacent squares 1 and 5)
B
Represent each group as an algebraic term and take the OR of those terms. Hence, F A B BC is the simplified form of the given Boolean function.
Example 3.9 Simplify the Boolean function F (A, B, C) = ∑ (0, 1, 3, 4, 5) Solution: Plot the minterms on a three-variable map and combine the squares containing 1s in groups of adjacent squares. A AB C
C
00 0
1
1
1
01
11
10 1
1
B (set of four adjacent squares 0, 1, 4, and 5)
1 B
AC (set of two adjacent squares 1 and 3)
Represent each group as an algebraic term and take the OR of those terms. Hence, F B A C is the simplified form of the given Boolean function.
81
82
Chapter 3 ■ Digital Systems Design
Example 3.10 Simplify the Boolean function F (A, B, C, D) = ∑ (0, 2, 4, 6, 8, 9 10) Solution: Plot the minterms on a four-variable map and combine the squares containing 1s in groups of adjacent squares. A AB CD 00
00
01
1
1
11
10 1
01
ABC (set of two adjacent squares 8 and 9)
1 D
11 C 10
1
1
1
BD (set of four adjacent squares 0, 2, 8 and 10)
B
AD (set of four adjacent squares 0, 2, 4 and 6)
Represent each group as an algebraic term and take the OR of those terms. Hence, F A D BD A BC is the simplified form of the given Boolean function.
Product-of-Sums Simplification We have seen how to simplify Boolean functions expressed in sum-of-products form by using K-maps. We will now see how to simplify functions expressed in product-of-sums form by using K-maps. The following procedure is used: 1. Draw an n-variable K-map depending on the number of variables (n) in the given Boolean function. 2. From the truth table of the given Boolean function, insert 0s in those squares of the map whose corresponding maxterms are 1 (which is the same as corresponding minterms being 0). 3. Combine the squares containing 0s in groups of adjacent squares. These groups must contain a number of squares that is an integral power of 2. The groups are not disjoint sets of squares in the sense that one or more squares of a group may also belong to another group. 4. Each group of squares represents an algebraic term, which are ORed to obtain the simplified complemented function. 5. Take the complement of the function obtained in Step 4 to obtain the simplified function in product-of-sums form. Example 3.11 illustrates the procedure.
■ Boolean Functions
83
Example 3.11 Simplify the Boolean function F (A, B, C, D) = ∏ (1, 3, 7, 9, 11, 12, 13, 14, 15) Solution: Plot the maxterms with 0s on a four-variable map and combine the squares containing 0s in groups of adjacent squares. A AB CD
00
01
11
00 01
10
AB (set of four adjacent squares 12, 13, 14 and 15)
0 0
0
BD (set of four adjacent squares 1, 3, 9 and 11)
0 D
11
0
0
0
0
CD (set of four adjacent squares 3, 7, 11 and 15)
C 10
0 B
Represent each group as an algebraic term and take the OR of those terms. Hence, F AB BD CD
Hence, F AB B D CD AB BD CD (A B) (B D) (C D) (A B) (B D) (C D) is the simplified Boolean function in product-of-sums form.
Don’t Care Conditions When representing a Boolean function as a truth table, we have seen until now that the output values for each of the 2 n combinations (minterms) of the n variables are either 0 or 1. There are occasions when it does not matter if the function produces an output of 0 or 1 for a given minterm. Such minterms are called don’t-care conditions because we do not care whether the function’s output is 0 or 1. Squares in a K-map corresponding to such minterms are marked with a cross mark (X). During simplification of the function, these squares are used as follows in the map: 1. While making groups of adjacent squares, the Xs may be assumed to be either 0 or 1, whichever gives the simplest expression. That is, it can be grouped with squares having 1s (when dealing with sum-of-products simplification) or squares having 0s (when dealing with product-of-sums simplification), if its inclusion in the group gives a simpler expression. 2. An ‘X’ need not be included in any group if it does not contribute to the simplification of the function. In short, the choice of including a square containing ‘X’ in a group is optional and depends only on its contribution to the simplification of the function. The example here illustrates this.
84
Chapter 3 ■ Digital Systems Design
Example 3.12 Simplify the Boolean function F (A, B, C) = ∑ (0, 4, 5), whose minterms (2, 3, 6) are don’t-care minterms. Solution: Plot the minterms (0, 4, 5) with 1s and minterms (2, 3, 6) with Xs on a three-variable map and combine the squares containing 1s and Xs in groups of adjacent squares. Include a square containing ‘X’ in a group only if it contributes to the simplification of the function. A AB C 0 C
1
00
01
11
10
1
x
x
1
x
1
AB (set of two adjacent squares 4 and 5)
B
C (set of four adjacent squares 0, 2, 4 and 6)
Represent each group as an algebraic term and take the OR of those terms. Hence, F AB C is the simplified form of the given Boolean function. Note that don’t care minterm 3 was not included in any group because it does not contribute to the simplification of the function. Also note that if the don’t care minterms 2 and 6 were not included in the group of four adjacent squares, the minterms 0 and 4 would have formed a group of two adjacent squares, representing an algebraic term B C . Hence, the simplified expression for the function in this case would have been
F AB BC This form of the function is not as simplified as the expression obtained previously. Hence, choosing to include the don’t-care minterms 2 and 6 in group formation produces the simplest Boolean expression.
LOGIC GATES A computer carries out all operations internally by means of combinations of signals passing through standard blocks of built-in circuits known as logic gates. In other words, a logic gate is an electronic circuit that operates on one or more input signals to produce standard output signals. These logic gates are the building blocks of electronic circuits in a computer. Digital computers use a variety of logic gates in their design. Each gate has a standard graphic symbol, and a Boolean function (expression) describes its operation. We can represent the input-output relationship of the binary variables for each gate in the form of a truth table. Figure 3.14 lists the names, graphic symbols, Boolean functions, and truth tables of seven commonly used logic gates. Their description is given here. 1. AND gate. An AND gate is the physical realization of logical multiplication (AND) operation. It is an electronic circuit that generates an output signal of 1, only if all input signals are also 1, otherwise the output is 0 (see Figure 3.14(a)]. Two or more switches connected in series behave as an AND gate.
■ Logic Gates
85
AND gates may have more than two inputs, and by definition, the output will be 1, if and only if all inputs are 1. Hence, the output of an AND gate with three inputs A, B, and C will be 1 only if all three inputs are 1, so that we write the output as A B C. 2. OR gate. An OR gate is the physical realization of logical addition (OR) operation. It is an electronic circuit that generates an output signal of 1, if any of the input signals is 1 or both are 1, otherwise the output is 0 [see Figure 3.14(b)]. Two or more switches connected in parallel behave as an OR gate. OR gates may have more than two inputs, and by definition, the output will be 1, if any input is 1. Hence, the output of an OR gate with three inputs A, B, and C will be 1, if any one of the inputs is 1, so that we write the output as A+B+C. 3. NOT gate. A NOT gate is the physical realization of complementation operation. It is an electronic circuit, which generates the reverse of the input signal as output signal [see Figure 3.14(c)]. It is also known as an inverter because it inverts the input. The small circle in the output of the graphic symbol of an inverter designates a logic complement. A NOT gate always has a single input because complementation operation is unary operation defined on a single variable. Two NOT gates connected in series give an output equal to the input. This is the gating counterpart to the law of double complementation, A A. 4. NAND gate. A NAND gate is a complemented AND gate. That is, the output of NAND gate will be 1, if any one of the inputs is a 0, and it will be 0, only when all inputs are 1 [see Figure 3.14(d)]. Its name is derived from the abbreviation of NOT-AND. Hence, its graphic symbol consists of an AND graphic symbol followed by a small circle (inverter or NOT). The symbol ‘ ’ is usually used to represent a NAND operation in Boolean expressions. Hence,
A B A B A B . A NAND gate may have more than two inputs, and the output is always the complement of the AND function of all inputs. Hence, a three-input NAND gate with inputs A, B, and C will have an output equal to A B C or A B C meaning that the output will be 1, if any of the inputs is a 0, and it will be 0, only when all three inputs are 1. 5. NOR gate. A NOR gate is a complemented OR gate. That is, the output of a NOR gate will be 1, only when all inputs are 0, and it will be 0, if any input is a 1 [see Figure 3.14(e)]. Its name is derived from the abbreviation of NOT-OR. Hence, its graphic symbol consists of an OR graphic symbol followed by a small circle (inverter or NOT). The symbol ‘ ’ is usually used to represent a NOR operation in Boolean expressions. Hence, A B =
A B A B . A NOR gate may have more than two inputs, and the output is always the OR function of all inputs. Hence, a three-input NOR gate with inputs A, B, and C will have an output equal to A B C or
A B C meaning that the output will be 1, only when all three inputs are 0, and it will be 0, if any of the three inputs is a 1.
86
Chapter 3 ■ Digital Systems Design
Sr. No.
Name
Graphic symbol
(a)
AND
A B
(b)
OR
A B
(c)
NOT
A
(d)
(e)
(f)
(g)
A B
NAND
NOR
A B
Exclusive-OR (XOR)
A B
Exclusive-NOR or Equivalence
A B
C
C
Boolean functions C=AB or C = AB
C=A+B
A 0 0 1 1 A 0 0 1 1
Truth table B 0 1 0 1 B 0 1 0 1 A 0 1
C 0 0 0 1 C 0 1 1 1
C
C= A or C = A
C
C =A B or C= A B or C= A B
A 0 0 1 1
B 0 1 0 1
C 1 1 1 0
C
C= A B or C= A B or C= A B
A 0 0 1 1
B 0 1 0 1
C 1 0 0 0
C
C = AB or C = A B AB or C = AB AB
A 0 0 1 1
B 0 1 0 1
C 0 1 1 0
C
C=AB or C = AB or C = AB AB or C = AB AB
A 0 0 1 1
B 0 1 0 1
C 1 0 0 1
A 1 0
Figure 3.14. Commonly used logic gates.
6. Exclusive-OR (XOR) gate. Exclusive-OR, denoted by , is a binary operator that performs the following Boolean function: A B = A B A B Notice from the truth table of Figure 3.14(f) that the output of an Exclusive-OR gate is 1, if any input is 1 but excludes the combination when both inputs are 1. ODD function would had been a better name for ExclusiveOR operation because its output is 1, if an odd number of inputs are 1. Hence, for a three-input Exclusive-OR gate, the output will be 1, if only one input is 1, or if all three inputs are 1. Exclusive-OR gates are commonly
■ Logic Gates
87
available with two inputs only. They are rarely available with three or more inputs. Since Exclusive-OR operation is both commutative and associative, we have:
A B C A B C A B C Graphic symbol of Exclusive-OR gate is similar to OR gate with a curved line on the input side. 7. Exclusive-NOR (Equivalence) gate. Exclusive-NOR (Equivalence), denoted by , is a binary operator that performs the following Boolean function: AB=AB+ A B Notice from the truth table of Figure 3.14(g) that the output of an Equivalence gate is 1, only if both inputs are equal to 1, or both inputs are equal to 0. Also, notice that Equivalence is the complement of ExclusiveOR. Hence, graphic symbol of Equivalence is similar to Exclusive-OR gate with a small circle (inverter). Equivalence gates are also commonly available with two inputs only. They are rarely available with three or more inputs. Since Equivalence operation is both commutative and associative, we have: (A B) C = A (B C) = A B C Only a limited number of Boolean expressions can be expressed exclusively in terms of Exclusive-OR or Equivalence operations. Nevertheless, these expressions emerge quite often during the design of digital systems. The two functions are particularly useful in arithmetic operations and error detection and correction.
LOGIC CIRCUITS We seldom use logic gates alone. Normally, we use them in combinations to form gating/logic networks known as combinational logic circuits. We can derive Boolean algebra expression for a given logic circuit by systematically progressing from input to output on its gates. The example here illustrates this.
Example 3.13. Find the Boolean expression for the output of the logic circuit given below. A B
1
4 6 E
C
2 5
D
3
88
Chapter 3 ■ Digital Systems Design
Solution: At point 1, the output of the OR gate is A + B
-------------
(a)
At point 2, the output of the NOT gate is C At point 3, the output of the NOT gate is D
-------------------------
(b) (c)
Inputs to the AND gate at point 4 are (A + B), C, and D. Hence, its output will be (A + B) C D -------------
(d)
Inputs to the AND gate at point 5 are (A + B), C , and D . Hence, its output will be (A + B) C D -------------
(e)
Finally, inputs to the OR gate at point 6 are (d) and (e). Hence, its output will be A B C D A B C D Hence, E A B C D A B C D is the Boolean expression for the output of the given logic circuit.
Converting Expressions to Logic Circuits The three logic gates – AND, OR, and NOT are said to be logically complete because any Boolean expression can be realized using these three gates only. The example here illustrates the method of constructing logic circuits for Boolean expressions, by using these three gates only. Example 3.14. Construct a logic circuit for the Boolean expression (x + y + z) (x + y ) ( x y ) Solution: x
x+y+z
z
y
y
x
xy
x y z x y x y
xy
The desired logic circuit is shown above and is self-explanatory.
The Universal NAND Gate NAND gate is a universal gate because it is alone sufficient to implement any Boolean expression. To show this, we need to show that the logical operations AND, OR, and NOT can be implemented with NAND gates only. Figure 3.15 shows this.
■ Logic Gates AA A A A
A
A
89
AB AB
A B
B (b) AND gate implementation.
(a) NOT gate implementation.
A
AA A
AB A B A B B
B B B
(c) OR gate implementation. Figure 3.15. Implementation of NOT, AND, and OR gates by NAND gates.
We can implement Boolean expressions with NAND gates only by carrying out the following steps in sequence: Step 1:
From the given Boolean expression, draw a logic diagram with AND, OR, and NOT gates. Assume that both normal (A) and complement A inputs are available.
Step 2:
Draw a second logic diagram with equivalent NAND logic substituted for each AND, OR, and NOT gate.
Step 3:
Remove all pairs of inverters connected in cascade from the diagram because double inversion does not perform any logical function. Also, remove inverters connected to single external inputs and complement the corresponding input variable. The new logic diagram, so obtained, is NAND gates implementation of the Boolean expression.
The example here illustrates this process. Example 3.15.
Construct a logic circuit for the Boolean expression A E B C D using NAND gates only. Solution: Step 1: We first draw the AND/OR implementation for the given Boolean expression [Figure 3.16(a)]. Step 2: Now, we substitute the NAND equivalent of each AND and each OR gate, resulting in Figure 3.16(b). Note that Figure 3.16(b) has six inverters (single input NAND gates) and four two-input NAND gates. Step 3: We now remove one pair of cascaded inverters. We also complement the three external inputs A, B, and E going directly to inverters, and remove the corresponding inverters. Figure 3.16(c) shows the final NAND gate implementation, so obtained. The number inside each NAND gate of Figure 3.16(c) corresponds to the NAND gate of Figure 3.16(b) having the same number.
For this example, the number of NAND gates required is equal to the number of AND/OR gates plus an additional inverter at the output (NAND gate number 5). In general, the number of NAND gates required to implement a Boolean expression equals the number of AND/OR gates except for an occasional inverter. This is true only when
90
Chapter 3 ■ Digital Systems Design
both normal and complement inputs are available because the conversion forces complementation of certain input variables. AE
A E
C D
CD
2
A E B C D
B
B CD (a) AND/OR implementation.
OR
A
AE
1
E
A E B C D AND
C
OR
2
D
AND
CD 4
3
5
B B+CD (b) Substituting equivalent NAND functions.
A E
1
C D
2
B
3
4
5
A E B C D
(c) NAND implementation. Figure 3.16. Step-by-step NAND implementation for the Boolean expression of Example 3.15.
The Universal NOR Gate The NOR function is the dual of the NAND function. Hence, all procedures and rules for NOR logic form a dual of the corresponding procedures and rules for NAND logic. Like NAND gate, NOR gate is also universal because it is alone sufficient to implement any Boolean expression. To show this, we need to show that the logical operations AND, OR, and NOT can be implemented with NOR gates only. Figure 3.17 shows this.
■ Logic Gates
91
A A AA A A (a) NOT gate implementation.
A B
AB
AB AB
(b) OR gate implementation.
AB A
B
AAA
AB AB
B B B (c) AND gate implementation.
Figure 3.17. Implementation of NOT, OR, and AND gates by NOR gates.
We can implement Boolean expressions with NOR gates only by carrying out the following steps in sequence: Step 1: For the given Boolean expression, draw the logic diagram with AND, OR, and NOT gates. Assume that both normal (A) and complement A inputs are available.
Step 2: Draw a second logic diagram with equivalent NOR logic substituted for each AND, OR, and NOT gate. Step 3: Remove all pairs of inverters connected in cascade from the diagram because double inversion does not perform any logical function. Also, remove inverters connected to single external inputs and complement the corresponding input variable. The new logic diagram, so obtained, is the NOR gate implementation of the given Boolean expression. Example 3.16. Construct a logic diagram for the Boolean expression A B C A B D using NOR gates only. Solution: Step 1: We first draw the AND/OR implementation for the given Boolean expression [Figure 3.18(a)]. Step 2: Now, we substitute each OR gate with two NOR gates [as in Figure 3.18(b)] and each AND gate with three NOR gates [as in Figure 3.18(c)]. Figure 3.18(b) shows the resulting logic diagram. Note that Figure 3.18(b) has eight inverters (single input NOR gates) and five two-input NOR gates. Step 3: We now remove one pair of cascaded inverters (from the OR box to AND box). We also complement the five external inputs A, B , B, D, and C going directly to inverters and remove the corresponding inverters. The final NOR gate implementation, so obtained, is shown in Figure 3.18(c). The number inside each NOR gate of Figure 3.18(c) corresponds to the NOR gate of Figure 3.18(b) having the same number. The number of NOR gates in this example equals the number of AND/OR gates plus an additional inverter in the output (NOR gate number 6). In general, the number of NOR gates required to implement a Boolean expression
92
Chapter 3 ■ Digital Systems Design
equals the number of AND/OR gates except for an occasional inverter. This is true only if both normal and complement inputs are available. Otherwise, we need to use inverters to generate any required complemented input. Combinational circuits are more frequently constructed with NAND or NOR gates than with AND, OR, and NOT gates because NAND and NOR gates can be easily constructed with transistor circuits and Boolean expressions can be easily implemented with them. Moreover, NAND and NOR gates are considered superior to AND and OR gates because they supply outputs maintaining the signal value without loss of amplitude. OR and AND gates sometimes need amplitude restoration after the signal travels through a few levels of gates. A B
A B
A B C A B D
BD
B D
A B D
A C A B D
C
(a) AND/OR implementation AND
A 1
A B
B
A B C A B D
OR AND
B
5
6
BD 2
D OR
AND
3
A
4
C
C (A+ B D)
A B D (b) Substituting equivalent NOR functions
A B
1
A B C A B D 5
B D A C
6
2 3 4
(c) NOR Implementation Figure 3.18. Step-by-step NOR implementation for the Boolean expression of Example 3.16.
■ Design of Combinational Circuits
93
DESIGN OF COMBINATIONAL CIRCUITS Design of a combinational circuit starts from the verbal outline of the problem and ends in a logic circuit diagram. The procedure involves the following steps: 1. State the given problem completely and exactly. 2. Interpret the problem to determine available input variables and required output variables. 3. Assign a letter symbol to each input variable and each output variable. 4. Design a truth table that defines the required relations between inputs and outputs. 5. Obtain simplified Boolean expression for each output. 6. Draw a logic circuit diagram to implement the Boolean expressions. The design procedure is illustrated here with the design of adder circuits because addition is the most basic arithmetic operation for any computer system. The following four rules summarize addition in binary system: 0+0=0 0+1=1 1+0=1 1 + 1 = 10 The first three operations produce a single-digit sum, but when both augend and addend bits are equal to 1, the sum consists of two digits. The higher significant bit of this result is called a carry. When the augend and addend numbers contain more than one digit, the carry obtained from the addition of two bits at any stage is added to the next pair of significant bits. A combinational circuit that performs the addition of two bits is called a half-adder. One that performs the addition of three bits (two significant bits and previous carry) is called a full-adder. The name of the former comes from the fact that we can implement a full-adder by using two half-adders.
Design of Half-Adder From the definition given above, a half-adder needs two binary inputs and two binary outputs. Input variables designate augend and addend bits whereas output variables produce sum and carry bits. Let A and B be the two inputs and S (for sum) and C (for carry) be the two outputs. The truth table of Figure 3.19 defines the function of a halfadder.
Boolean expressions for the two outputs (obtained from the truth table) are:
S AB AB C AB
Inputs A 0 0 1 1
B 0 1 0 1
Outputs C 0 0 0 1
S 0 1 1 0
Figure 3.19. Truth table for a half-adder.
Figure 3.20 shows the logic circuit diagram to implement these expressions.
94
Chapter 3 ■ Digital Systems Design
A
AB
A
S AB AB
B
B
AB A B
C AB Figure 3.20. Logic circuit diagram for a half-adder.
A half-adder can add only two single bits. Although, it generates a carry for the next higher pair of significant bits, it cannot accept a carry generated from the previous pair of lower significant bits. A full-adder solves this problem.
Design of Full-Adder A full-adder forms the arithmetic sum of three input bits. Hence, it consists of three inputs and two outputs. Two of the input variables (A and B) represent augend and addend bits and the third input variable (D) represents carry from the previous lower significant position. Two outputs are necessary because the sum of three binary digits ranges from 0 to 3 and binary 2 and 3 need two digits. Symbols S (for sum) and C (for carry) represent these two outputs. Truth table of Figure 3.21 defines the function of a full-adder. The 1s and 0s for the output variables are determined from the arithmetic sum of the three input variables. When all input variables are 0, the output is 0 for both C and S. The S output is equal to 1, when only one input is equal to 1, or when all three inputs are equal to 1. The C output is 1, if two or three inputs are equal to 1. Inputs
We can directly obtain sum-of-products expressions for the two outputs from the truth table as:
A 0 0 0 0 1 1 1 1
S A B D A B D A B D A B D C A B D A B D A B D A B D Although, it is not possible to simplify the expression for S, we can simplify the expression for C as follows:
B 0 0 1 1 0 0 1 1
Outputs D 0 1 0 1 0 1 0 1
C 0 0 0 1 0 1 1 1
S 0 1 1 0 1 0 0 1
Figure 3.21. Truth table for a full-adder.
C A B D A B D A B D A B D A B D A B D A B D A B D A B D A B D
A A B D B B A D D D A B
A B D A B D A B D A B D A B D A B D B D A D A B A B A D B D
■ Design of Sequential Circuits We can also simplify the expression for C by map simplification method (see Figure 3.22). A AB D 00
01
11
0 D
10
1
1
1
1
AB (set of two adjacent squares 6 and 7)
AD (set of two adjacent squares 5 and 7)
1
B
BD (set of two adjacent squares 3 and 7)
Original C = ABD ABD ABD ABD Simplified C = AB + AD + BD Figure 3.22. Simplifying expression for C (carry) of a full-adder by map simplification method.
Hence, we obtain the following expressions for the two outputs:
S A B D A B D A B D A B D C A B A D B D Figure 3.23 shows the logic circuit diagrams to implement these expressions. A B D
A B D
A B D A B D
A B D
A
AB
B A S
A B D
A B D
D
A B D
B
AD
C
BD
D (a) Logic circuit diagram for sum.
(b) Logic circuit diagram for carry.
Figure 3.23. Logic circuit diagrams for a full-adder.
We can also implement a full-adder with two half-adders and one OR gate as Figure 3.24 shows. AB
A B
HA
AB AB A B
A B D
C
ABD
S
HA D
Figure 3.24. Implementation of a full-adder with two half-adders and one OR gate.
95
96
Chapter 3 ■ Digital Systems Design
S output from the second half-adder is the Exclusive-OR of D and the output of the first half-adder giving:
S A B A B D A B A B D
A B A B D A B D A B D A B A B D A B D A B D
A A A B A B B B D A B D A B D A B A B D A B D A B D
A B A B D A B D A B D
A B D A B D A B D A B D A B D A B D A B D A B D
.
In addition, we have carry output as:
C AB AB D AB
A B D A B D A B D D
A B D A B D A B D A B D We can simplify this, as before, to
C A B A D B D
A Parallel Binary Adder We use parallel binary adders to add two binary numbers. For example, if we want to add two four-bit numbers, we need to construct a parallel four-bit binary adder as Figure 3.25 shows. Such an adder requires one half-adder (denoted by HA) and three full-adders (denoted by FA). Here, the adder adds binary numbers A4 A3 A2 A1 and B4 B3 B2 B1, and the answer is: A4 B4
+ A 4 A 3 A 2 A1 B4 B3 B2 B1 S5 S4 S3 S2 S1
FA
S5
S4
Carry
A3 B3
FA
S3
Carry
A2 B2
FA
Carry
S2
A1 B1
HA
S1
Figure 3.25. A parallel four-bit binary adder.
The first column requires only a half-adder. For any column above the first one, there may be a carry from the preceding column. Therefore, we must use a full adder for each column above the first one.
■ Flip-Flops
97
To illustrate how the adder of Figure 3.25 works, let us see how it will add two numbers say, 9 and 11. The binary equivalent of decimal 9 is 1001 and that of decimal 11 is 1011. Figure 3.26 shows the binary adder with these inputs.
Carry FA
1
0
0
1
1
0
1
1
0
1
1
Carry
FA
Sum 1
0
Carry
FA
Sum 1
Carry
HA Sum
Sum 0
0
Figure 3.26. Example of adding two four-bit numbers using a parallel binary adder.
As shown in the figure, the half-adder adds 1 + 1 to give a sum of 0 and a carry 1. The carry goes into the first fulladder, which adds 0 + 1 + 1 to get a sum of 0 and a carry of 1. This carry goes into the next full-adder, which adds 0 + 0 + 1 to get a sum of 1 and a carry of 0. The last full-adder adds 1 + 1 + 0 to get a sum of 0 and a carry of 1. The final output of the system is 10100. The decimal equivalent of binary 10100 is 20, which is the correct decimal sum of 9 and 11. The parallel binary adder of Figure 3.26 has limited capacity. The largest binary numbers that it can add are 1111 and 1111. Hence, its maximum capacity is: 15 + 15
1111 + 1111
30
11110
We can increase its capacity by connecting more fulladders to the left end of the system. For instance, to add six-bit numbers we must connect two more full-adders, and to add eight-bit numbers we must connect four more fulladders to the leftmost full-adder of Figure 3.26.
FLIP-FLOPS Combinational circuits do not have the capability to store anything because they do not have any storage component. Hence, their outputs at any given time depend on their inputs at that time. Most digital systems design use combinational circuits with storage components. Digital circuits comprised of both combinational circuits and storage components are called sequential circuits. The most commonly used storage components in sequential circuits are flip-flops. A flip-flop is a clocked device that has two stable states and is, thereby, capable of storing one bit of information. It has two outputs, one for the normal value and one for the complement value of the bit stored in it. A flip-flop maintains its current binary state until directed by a clock pulse to switch its state. Hence, sequential circuits use a clock pulse generator that produces clock pulses at fixed intervals. A sequential circuit distributes the clock pulses throughout in such a way that only arrival of the clock pulses affects its flip-flops.
Types of Flip-flops There are several types of flip-flops available. They differ in the number of inputs they possess and in the manner in which the inputs affect the binary state. SR (set-reset), D (delay), T (toggle), and JK (Jack-Kilby) are the most commonly known types. They are described here.
98
Chapter 3 ■ Digital Systems Design
It is customary to represent the operational behaviour of a particular type of flip-flop in terms of a characteristic equation, which derives the next (after the next clock pulse) output [Qnext or Q(t+1)] in terms of the input signal(s) and/or the current output (Q or Q(t)). This behaviour is also often summarized in the form of a characteristic table, which shows the relations among Q, Q(t+1), and the input signal(s). Each type of flip-flop also has a graphic symbol (see Figure 3.27)
SR (Set-Reset) Flip-flop As Figure 3.27(a) shows, SR flip-flop has three inputs labeled S (for set), R (for reset), and C (for clock). It has two outputs labeled Q (for normal output) and Q (for complemented output). The arrowhead for C input designates a dynamic input and denotes the fact that the flip-flop responds to a positive transition of the input clock signal (when C changes from 0 to 1). If there is no signal at the clock input C, the output of the flip-flop does not change irrespective of the values of inputs S and R. The output of the flip-flop is affected according to the values of inputs S and R only when the clock signal C changes from 0 to 1. Sr. No.
a
b
Name (Type)
SR (SetReset)
D (Data)
Graphic symbol
Q
S C R
Q
D
Q
Characteristic equation
If S = R = 0 Q(t + 1) = Q(t)
Characteristic table S 0 0 1 1
D 0 1
Q(t + 1) = D
C Q
c
d
J K (JackKilby)
T (Toggle)
J
Q
K
Q
T
Q
Q(t 1) JQ(t) KQ(t)
Q (t 1) Q (t) T
or C Q
Q(t 1) TQ(t) TQ(t)
R 0 1 0 1
J 0 0 1 1
K 0 1 0 1
T 0 1
Q(t + 1) Q(t) 0 1 ?
Q(t + 1) 0 1
Q(t + 1) Q(t) 0 1 Q(t)
Q(t + 1) Q(t) Q(t)
Remarks No change Clear to 0 Set to 1 Indeterminate
Remarks Clear to 0 Set to 1
Remarks No change Clear to 0 Set to 1 Complement
Remarks No Change Complement
Figure 3.27. Commonly used flip-flops.
Characteristic table of Figure 3.27(a) summarizes the operation of SR flip-flop. If S = R = 0, the next state remains as current state on a clock transition (no change of state). If S = 0 and R = 1, the flip-flop goes to the 0 (clear) state, while if S = 1 and R = 0, the flip-flop goes to the 1 (set) state on a clock transition. If S = R = 1, the flip-flop produces an indeterminate next state on a clock transition (the output is unpredictable and may be either 0 or 1, depending on internal timing delays within the circuit). Hence, it should not be pulsed when S = R = 1. Due to this reason, SR flipflop is difficult to manage. Therefore, we seldom use it in practice.
■ Flip-Flops
99
D (Data) Flip-flop As Figure 3.27(b) shows, D flip-flop has two inputs labeled D (for data) and C (for clock). It has two outputs Q and Q . The output of the flip-flop is affected only when the clock signal C changes from 0 to 1. Notice from characteristic table of Figure 3.27(b) that if D = 0, the flip-flop goes to the 0 (clear) state, while if D = 1, the flip-flop goes to the 1 (set) state on a clock transition (when C changes from 0 to 1). Hence, the characteristic equation Q(t+1) = D is used to represent the operation of D flip-flop.
JK (Jack Kilby) Flip-flop JK flip-flop was designed to overcome the problem of indeterminate behaviour of SR flip-flop when S = R = 1. Hence, like SR flip-flop, it has three inputs labeled J (which corresponds to S input of SR flip-flop), K (which corresponds to R input of SR flip-flop), and C (for clock). It has two outputs Q and Q . The output of the flip-flop is affected only when the clock signal C changes from 0 to 1. Notice from characteristics table of Figure 3.27(c) that the behaviour of JK flip-flop is similar to that of SR flip-flop with inputs J and K corresponding to inputs S and R respectively, except that when J = K = 1, Q(t + 1) = Q(t) . The characteristic equation Q(t + 1) = JQ (t) + KQ (t) is used to represent the operation of JK flip-flop.
T (Toggle) Flip-flop As Figure 3.27(d) shows, T flip-flop has two inputs labeled T (for toggle) and C (for clock). It has two outputs Q and Q. Notice from characteristic table of Figure 3.27(d) that when T = 0, a clock transition (change of C from 0 to 1) does not change the state of the flip-flop. However, when T = 1, a clock transition changes the state of the flip-flop. Hence, the characteristic equation Q (t 1) Q(t) T or Q(t 1) TQ(t) TQ(t) is used to represent the operation of T flip-flop.
Excitation Tables In Figure 3.27, the characteristic tables for flip-flops specify the next state when the inputs and the present state are known. In actual design of digital circuits, designers usually know what should be the next state and need to find out the flip-flop input conditions that will cause transition from present to next state. Hence, designers use excitation tables for flip-flops that list all possible state changes and the input combinations for each state change. Figure 3.28 shows excitation tables for the four types of flip-flops described above. Notice from the figure that there are four possible transitions from present state Q(t) to next state Q(t + 1). Hence, each excitation table has four rows. For each transition, the tables show the required input conditions, which are derived from the respective characteristic tables. The don’t-care conditions (denoted by the symbol x) for certain inputs in the tables indicate that it does not matter whether those inputs to the flip-flop are 0 or 1. For example, in a JK flip-flop, transition from Q(t) = 0 to Q(t + 1) = 0 can be achieved by having inputs J = K = 0, or J = 0 and K = 1. That is, J must be 0, but K can be 0 or 1 for the required transition to occur. Hence, K input is marked as x against this transition, and it is left to the designer to choose either 0 or 1 for K input, whichever is more convenient.
100
Chapter 3 ■ Digital Systems Design
Q(t) 0 0 1 1
SR Flip-flop Q(t + 1) S 0 0 1 1 0 0 1 x
Q(t) 0 0 1 1
JK Flip-flop Q(t + 1) J 0 0 1 1 0 x 1 x
x 0 1 0
Q(t) 0 0 1 1
D Flip-flop Q(t + 1) 0 1 0 1
D 0 1 0 1
K x x 1 0
Q(t) 0 0 1 1
T Flip-flop Q(t + 1) 0 1 0 1
T 0 1 1 0
R
Figure 3.28. Excitation tables for SR, D, JK, and T flip-flops.
Edge-Triggered Flip-Flops In this type of flip-flop, output transitions occur only at a specific level (threshold level) of the clock pulse. When clock pulse level reaches the threshold level, output transition occurs and then the flip-flop becomes unresponsive to any further changes in inputs until the clock pulse returns to 0 and another pulse occurs. Two types of edge-trigged flip-flops are: 1. Positive edge-triggered in which output transition occurs when the rising edge of clock signal reaches the threshold level [see Figure 3.29(a)]. 2. Negative edge-triggered in which output transaction occurs when the falling edge of clock signal reaches the threshold level [see Figure 3.29(b)]. Figure 3.29 shows examples of a positive-edge-triggered D flip-flop and a negative-edge-triggered D flip-flop. The small circle at the input of clock signal C in Figure 3.29(b) indicates that it is a negative-edge-triggered flip-flop. Notice from Figure 3.29(a) that in a positive-edge-triggered D flip-flop, output transition occurs when clock pulse level rises from level 0 to level 1. The output cannot change when 1. Clock pulse is at level 1. 2. Clock pulse is in transition from level 1 to level 0. 3. Clock pulse is at level 0. 4. Clock pulse is in transition from level 0 to level 1 (the output changes as soon as the clock pulse reaches level 1, the threshold value in this example). The period covering 1, 2, and 3 above is called hold time, and that of 4 is called setup time. Similarly, notice from Figure 3.29(b) that in a negative-edge-triggered D flip-flop, output transition occurs when clock pulse level falls from level 1 to level 0 (the output changes as soon as the clock pulse reaches level 0, the threshold value in this example). The output cannot change otherwise.
■ Flip-Flops
101
Threshold level (output transition occurs) D
Q Clock pulse
C Q
Time
Setup time (During which positive clock transition occurs)
Hold time (During which output cannot change)
(a) Positive-edge-triggered D flip-flop
Threshold level (output transition occurs) D
Q
Clock pulse
C Q
Time Hold time (During which output cannot change)
Setup time (During which negative clock transition occurs)
(b) Negative-edge-triggered D flip-flop Figure 3.29. Edge-triggered flip-flops.
Master-Slave Flip-Flop A master-slave flip-flop is an interconnected circuit of two flip-flops in such a manner that the first one (master flipflop) responds to the positive level of clock signal, and the second one (slave flip-flop) responds to the negative level of clock signal. The result is that the output changes during the transition of the clock signal from level 1 to level 0. Figure 3.30 shows a master-slave D flip-flop created by connecting two D flip-flops in series, and inverting the clock signal input C for one of them. It behaves as a master-slave flip-flop because the second (slave) flip-flop in the series, changes only in response to a change in the first (master) flip-flop. Master D
Q
Slave D
C Q
Q
C Q
Figure 3.30. A master-slave D flip-flop.
Uses of Flip-flops Some popular uses of flip-flops are: 1. We can use a single flip-flop to store one bit of data. 2. We can use any one type of flip-flop to build any other type of flip-flop.
102
Chapter 3 ■ Digital Systems Design 3. Static RAMs use flip-flops in their design. Static RAM is the primary type of memory used in registers and in many caches. 4. Various types of counters and sequencers use flip-flops in their design. 5. Finite state machines use flip-flops and electronic logic circuits in their design. The flip-flops remember the machine’s previous state, and the electronic logic circuit uses that state to calculate the next state. 6. We can also use a chain of T flip-flops for frequency division of an input by 2 n, where n is the number of flipflops used between the input and the output.
SEQUENTIAL CIRCUITS Digital circuits that use both logic gates and flip-flops are known as sequential circuits. The logic gates constitute a combinational circuit. Hence, we also say that sequential circuits are digital circuits comprised of a combinational circuit and flip-flops. Inputs to the combinational circuit are external binary inputs and outputs of flip-flops. On the other hand, outputs of the combinational circuit serve as inputs to the flip-flops or are used as external outputs. After each clock transition, the binary value to be stored in the flip-flops is determined by the combinational circuit’s input to the flip-flops. Outputs of the sequential circuit include outputs of the combinational circuit and that of the flip-flops. The behaviour of a sequential circuit, therefore, depends on external inputs, outputs of its flip-flops, and binary states of the flip-flops. Hence, a sequential circuit is specified by a time sequence of external inputs, external outputs, and binary states of the flip-flops.
Flip-flop Input Equations We know that in a sequential circuit, outputs of its combinational circuit serve as inputs to its flip-flops. Moreover, we can express each output of a combinational circuit as a Boolean expression. Hence, inputs to the flip-flops of a sequential circuit can be described by a set of Boolean expressions. These Boolean expressions are called flip-flop input equations of the sequential circuit. An example can best explain the concept. Let us consider the sequential circuit of Figure 3.31(a). It has one input variable x and one output variable y. The AND, OR, and NOT gates form its combinational circuit. Its two clocked D flip-flops are named a and b. The input to D flip-flop a is designated as Da and its outputs are A and A . Similarly, the input to D flip-flop b is designated as Db and its outputs are B and B . C is the clock pulse input to both the flip-flops. From the figure, we can derive the flip-flop input equations as: Da = A x B x Db = A x In addition, we can derive the expression for output y as: y = A x B x
State Table We often express the behaviour of a sequential circuit in the form of a state table that consists of the following four sections: 1. Present state. It shows the states of flip-flops at any given time t. A sequential circuit with m flip-flops will contain m columns for present state. 2. Inputs. It gives a value of input variables for each possible present state. A sequential circuit with n input variables will have n columns for inputs.
■ Sequential Circuits
103
3. Next state. It shows the states of the flip-flops one clock period later at time t+1. A sequential circuit with m flip-flops will contain m columns for next state. 4. Outputs. It gives a value of output variables for each possible present state and inputs condition. A sequential circuit with p output variables will contain p columns for outputs. The present state and input columns are combined and under them, we list the 2 m+n binary combinations from 0 through 2m+n-1. The next-state and output columns are functions of the present state and input values. They are derived directly from the circuit or the Boolean equations that describe the circuit. As an example, Figure 3.31(b) gives the state table of the sequential circuit of Figure 3.31(a). We first list all possible binary combinations (000 to 111) of present states (A and B) of the two flip-flops (a and b respectively) and the single input (x). We then calculate the next state values (A and B) of the two flip-flops from their input equations. Hence, the values in next state column A are obtained by application of the input equation Da = A x B x to each row, and that of column B are obtained by application of the input equation Db = A x . Finally, we obtain the value of y in the output column by application of the expression y = A x B x .
1
x
A·x
A xB x D
5 2
B·x C
Q
a
Q
A
D
A
C 7
3
Ax
C
b
Q
B
B
Ax
A x B x
6 4
Q
y
B x
clock pulse
Da A x B x
Db A x
y A x B x
(a) A sequential circuit
Present state A B 0 0 0 0 0 1 0 1 1 0 1 0 1 1 1 1
Next state
Input x 0 1 0 1 0 1 0 1
A 0 0 0 1 0 1 0 1
Output B 0 1 0 1 0 0 0 0
(b) State table of sequential circuit of (a)
y 0 0 1 0 1 0 1 0
0/0 00
1/0
0/1 1/0
0/1
10
01
0/1 1/0
1/0 11
(c) State diagram of sequential circuit of (a)
Figure 3.31. A sequential circuit with its state table and state diagram.
104
Chapter 3 ■ Digital Systems Design
State Diagram State diagram of a sequential circuit is a graphical representation of its behaviour. The graph represents a state by a circle, and transitions between states by directed lines connecting the circles. Since both state table and state diagram are two different forms of representing a sequential circuit, we can derive the state diagram of a sequential circuit from its state table. Figure 3.31(c) shows the state diagram of the sequential circuit of Figure 3.31(a), whose state table is shown in Figure 3.31(b). In the state diagram, each circle denotes a state of the flip-flops and has a binary number inside it to identify the state. Each directed line has a label of the form “input/output” in binary. For example, the directed line labeled 1/0 from state 00 to 01 indicates that when the sequential circuit is in the present state 00 and the input is 1, the output is 0, and the circuit goes to the next state 01 on a clock transition. We can verify this transition of the circuit from the entries in the 2 nd row of the state table. Similarly, we can transform each row of the state table into a directed line of the state graph to obtain the complete state graph of the sequential circuit. A directed line connecting a circle with itself indicates that no change of state occurs. Notice from the above that state table of a sequential circuit is easier to derive from its logic diagram, and we can easily derive its state diagram from the state table. It is easier for us to interpret circuit operation from state diagram as compared to that from state table. For example, it can be seen from the state diagram of Figure 3.31(c) that when the sequential circuit is in the present state 11, then (a) if input is 0, next state will be 00 and output will be 1; and (b) if input is 1, next state will be 10 and output will be 0.
DESIGN OF SEQUENTIAL CIRCUITS Design of a sequential circuit starts from its specifications and ends in a circuit diagram. The procedure involves the following steps: 1. From the specifications of the sequential circuit, we first translate its behaviour into a state diagram. 2. We then convert the state diagram into a state table. 3. We then choose the flip-flop type to be used in the circuit and extend the state table to an excitation table by including columns for each input of each flip-flop. Figure 3.28 shows the excitation tables for the four types of flip-flops. 4. The excitation table provides the truth table for the combinational circuit part of the sequential circuit. The present-state and input columns constitute the inputs in the truth table, while the flip-flop input conditions constitute the outputs. 5. We then use map simplification method to obtain a set of flip-flop input equation for the combinational circuit. 6. Finally, we convert each flip-flop input equation into an equivalent logic diagram, whose output is connected to one of the flip-flop inputs. The combinational circuit so obtained, together with the flop-flops, constitutes the sequential circuit. We illustrate the design procedure here with the design of a binary counter because several electronic devices (clocks, ovens, microwave ovens, music systems, etc.) use counters.
■ Design of Sequential Circuits
105
Design of Binary Counter For simplicity, we first design a sequential circuit for a two-bit binary counter. Such a counter goes through a sequence of (22 = 4) binary states 00, 01, 10, 11. After 11, the counter repeats counting from 00. The counter changes state only when an external input x is equal to 1. It remains in the same state when x = 0. Hence, x serves as count enable input. From the above specifications of the 2-bit binary counter, we draw its state diagram [see Figure 3.32(a)]. The counter needs two flip-flops and four states to represent the two bits. We label the edges of the state diagram with input values only because this sequential circuit has no external outputs. The state of the flip-flops is the outputs of the counter. We then convert the state diagram of Figure 3.32(a) into the state table (first five columns) of Figure 3.32(b). Figure 3.32(b) is actually the excitation table (state table plus flip-flop inputs) obtained by extending the state table with the choice of two JK flip-flops for the design. The two flip-flops are named ‘a’ and ‘b’. Accordingly, their inputs are (J a, Ka) and (Jb, Kb) respectively, while their outputs are represented by ‘A’ and ‘B’. We extend the state table (first five columns) of Figure 3.32(b) to excitation table by adding the last four columns in the following manner: 1. In rows 1, 2 and 3 of excitation table of Figure 3.32(b), we have a transition for flip-flop ‘a’ from 0 in the present state (column 1) to 0 in the next state (column 4). In Figure 3.28, we find that a transition of states from Q(t) = 0 to Q(t+1) = 0 in a JK flip-flop requires that input J = 0 and input K = X. Hence, 0 and X are entered in columns 6 (Ja) and 7 (Ka) respectively of rows 1, 2 and 3 in excitation table of Figure 3.32(b). Due to same reason, for flip-flop ‘b’, 0 and X are entered in columns 8 (J b) and 9 (Kb) respectively of rows 1 and 5. 2. In row 4 of excitation table of Figure 3.32(b), we have a transition for flip-flop ‘a’ from 0 in the present state (column 1) to 1 in the next state (column 4). In Figure 3.28, we find that a transition of states from Q(t) = 0 to Q(t+1) = 1 in a JK flip-flop requires that input J = 1 and input K = X. Hence, 1 and X are entered in columns 6 (Ja) and 7 (Ka) respectively of row 4 in excitation table of Figure 3.32(b). Due to same reason, for flip-flop ‘b’, 1 and X are entered in columns 8 (J b) and 9 (Kb) respectively of rows 2 and 6. 3. In rows 5, 6 and 7 of excitation table of Figure 3.32(b), we have a transition for flip-flop ‘a’ from 1 in the present state (column 1) to 1 in the next state (column 4). In Figure 3.28, we find that a transition of states from Q(t) = 1 to Q(t+1) = 1 in a JK flip-flop requires that input J = X and input K = 0. Hence, X and 0 are entered in columns 6 (Ja) and 7 (Ka) respectively of rows 5, 6 and 7 in excitation table of Figure 3.32(b). Due to same reason, for flip-flop ‘b’, X and 0 are entered in columns 8 (Jb) and 9 (Kb) respectively of rows 3 and 7. 4. In rows 5, 6 and 7 of excitation table of Figure 3.32(b), we have a transition for flip-flop ‘a’ from 1 in the present state (column 1) to 1 in the next state (column 4). In Figure 3.28 we find that a transition of states from Q(t) = 1 to Q(t+1) = 1 in a JK flip-flop requires that input J = X and input K = 0. Hence, X and 0 are entered in columns 6 (Ja) and 7 (Ka) respectively of rows 5, 6 and 7 in excitation table of Figure 3.32(b). Due to same reason, for flip-flop ‘b’, X and 0 are entered in columns 8 (J b) and 9 (Kb) respectively of rows 3 and 7. 5. In row 8 of excitation table of Figure 3.32(b), we have a transition for flip-flop ‘a’ from 1 in the present state (column 1) to 0 in the next state (column 4). In Figure 3.28, we find that a transition of states from Q(t) = 1 to Q(t+1) = 0 in a JK flip-flop requires that input J = X and input K = 1. Hence, X and 1 are entered in columns 6 (Ja) and 7 (Ka) respectively of row 8 in excitation table of Figure 3.32(b).
106
Chapter 3 ■ Digital Systems Design Due to same reason, for flip-flop ‘b’, X and 1 are entered in columns 8 (J b) and 9 (Kb) respectively of rows 4 and 8 in excitation table of Figure 3.32(b). x=0
Present state A B 0 0 0 0 0 1 0 1 1 0 1 0 1 1 1 1
x=0 x=1
00
01
x=1
x=1
11
10
x=1
x=0
x=0
Next state A B 0 0 0 1 0 1 1 0 1 0 1 1 1 1 0 0
Input x 0 1 0 1 0 1 0 1
(a) State diagram
Flip-flop inputs Ja 0 0 0 1 X X X X
(b) Excitation table
A
A AB
AB 00
x
01
11
10
X
X
X
X
0 x
1
1
00
01
0
X
X
1
X
X
x
x
11
1
Ka = B · x A
A AB
AB 00
01
11
X
X
X
X
0 x
1
10
B
B Ja = B · x
x
Ka X X X X 0 0 0 1
1
x
10
x
1
00
0
X
1
X
01
11
10 X
1
1
X
B
B Kb = x
Jb = x
(c) K-maps to obtain simplified flip-flop input equations (Ja, Ka, Jb and Kb)
A
B B·x
x J C K C
b
Q
J
Q
C K
Q
a
Q
clock pulse (d) Sequential circuit diagram Figure 3.32. Design of a 2-bit binary counter.
Jb 0 1 X X 0 1 X X
Kb X X 0 1 X X 0 1
■ Points to Remember
107
The excitation table of Figure 3.32(b) obtained in this manner provides the truth table for the combinational circuit part of the sequential circuit for 2-bit binary counter. The present state and input columns (columns 1, 2 and 3) constitute the inputs in the truth table, while the flip-flop input conditions (columns labeled J a, Ka, Jb and Kb) constitute the outputs. Using this information, we now draw four 3-variable K-maps in Figure 3.32(c) and obtain the four simplified flip-flop input equations as: Ja = B x Ka = B x
Jb = x Kb = x
We now convert each of these flip-flop input equation into an equivalent logic diagram, whose output is connected to one of the flip-flop inputs. Figure 3.32(d) shows the combinational circuit so obtained, together with the flip-flops. This is the sequential circuit for the 2-bit binary counter. Note that inputs J and K determine the next state of the counter when a clock pulse occurs. If both J and K are equal to 0, a clock pulse will not have any effect (the state of flip-flops will not change). Hence, where x = 0, all four inputs to the flip-flops are equal to 0 and the state of the flip-flops do not change even if clock pulses are applied continuously. Synchronous binary counters have a regular pattern. Hence, the sequential circuit diagram of Figure 3.32(d) can be extended to design a n-bit binary counter with n JK flip-flops and n-1 AND gates. The chain of AND gates generates the required logic for the J and K inputs. Based on this idea, Figure 3.33 shows the sequential circuit diagram of a 4bit binary counter, which can count from 0000 to 1111 in binary number system. It goes through a sequence of (2 4 = 16) binary states 0000, 0001, 0010, …, 1111. After 1111, the counter repeats counting from 0000. The counter changes state only when the count-enable input (x) is equal to 1. It remains in the same state when x = 0. Count-enable input (x)
D
J C K
d
C
Q
J
Q
C K
c
B
Q
J
Q
C K
b
A
Q
J
Q
C K
Q
a
Q
Clock pulse (C) Figure 3.33. Sequential circuit of a 4-bit binary counter.
Points to Remember 1.
Boolean algebra deals with binary number system. That is, variables used in Boolean equations may have only two possible values (0 and 1).
2.
In Boolean algebra, ‘OR’ operator used for logical addition is represented by the symbol ‘+’; ‘AND’ operator used for logical multiplication is represented by the symbol ‘’; and ‘NOT’ operator used for complementation is represented by the symbol ‘’.
3.
As regards precedence of Boolean operators, ‘NOT’ takes precedence over ‘AND’ and ‘OR’; and ‘AND’ takes precedence over ‘OR’.
4.
Postulates of Boolean algebra are: (a) A = 0, if and only if, A is not equal to 1 (b) A = 1, if and only if, A is not equal to 0 (c) x 0 = x
(d) x 1 = x (e) x y = y x (f) x y = y x
108
Chapter 3 ■ Digital Systems Design (g) x (y z) = (x y) z (h) x (y z) = (x y) z (i) x (y z) = x y x z
5.
(j) x y z = (x y) (x z) (k) x x = 1 (l) x x = 0
In Boolean algebra, there is a precise duality between the operators (AND) and + (OR) and the digits 0 and 1. This property is known as the principle of duality.
6.
Figure 3.7 summarizes the basic Boolean identities. They are helpful while dealing with Boolean algebra.
7.
A Boolean function is an expression formed with binary variables, the two binary operators OR and AND, the unary operator NOT, parentheses, and equal to sign.
8.
Complement of a Boolean function F isF and is obtained by interchanging 0s for 1s and 1s for 0s in the truth table defining the function.
9.
A sum-of-products expression is a single product term (minterm), or several product terms (minterms) logically added (ORed) together. For example, x y + x y is a sum-of-products expression.
10. A product-of-sums expression is a single sum term (maxterm), or several sum terms (maxterms) logically multiplied (ANDed) together. For example, ( x + y) (x + y ) is a product-of-sums expression. 11. Sum-of-products and product-of-sums forms of Boolean expressions are known as standard forms. They are preferred for logic circuit design because of their straightforward conversion to simple gating networks. 12. K-map (Karnaugh map or simply map) is a pictorial representation of a Boolean function. It consists of squares, with each square representing one minterm. Hence, the number of squares in a map of n variable is 2 n. We use K-map for minimization of Boolean functions. 13. A logic gate is an electronic circuit that operates on one or more input signals to produce standard output signals. Logic gates are building blocks of electronic circuits in a computer. 14. Commonly used logic gates are AND, OR, NOT, NAND, NOR, Exclusive-OR (XOR), and Exclusive-NOR (Equivalence). Figure 3.14 shows their graphic symbols, Boolean functions, and truth tables. 15. Logic gates are interconnected to form gating or logic networks known as combinational logic circuits. 16. AND, OR, and NOT gates are logically complete because any Boolean expression may be realized using these three gates. 17. NAND gate is a universal gate because it is alone sufficient to implement any Boolean expression. Similarly, NOR gate is also a universal gate. 18. Most digital systems design use combinational circuits with storage components. Digital circuits comprised of both combinational circuits and storage components are called sequential circuits. 19. The most commonly used storage components in sequential circuits are flip-flops. A flip-flop is a clocked device that has two stable states and is, thereby, capable of storing one bit of information. 20. Different types of flip-flops differ in the number of inputs they possess and in the manner in which the inputs affect the binary state. SR (set-reset), D (delay), T (toggle), and JK (Jack-Kilby) are the most commonly known types. Figure 3.27 shows their graphic symbols, characteristic equations, and characteristic tables. Figure 3.28 shows their excitation tables. 21. In an edge-triggered flip-flop, output transactions occur only at a specific level (threshold level) of the clock pulse. Two types of edge-triggered flip-flop are: (a) positive edge-triggered in which output transition occurs when the rising edge of clock signal reaches the threshold level, and (b) negative edge-triggered in which output transition occurs when the falling edge of clock signal reaches the threshold level. 22. A master-slave flip-flop is an interconnected circuit of two flip-flops in such a manner that the first one (master flip-flop) responds to the positive level of clock signal, and the second one (slave flip-flop) responds to the negative level of clock signal. 23. Design of a combinational circuit or a sequential circuit starts from its problem specifications and ends in a circuit diagram. The circuit diagram of a combinational circuit consists of logic gates, while that of a sequential circuit consists of logic gates and flip-flops.
■ Questions
109
Questions 1.
Explain the principle of duality in Boolean algebra. How it is useful?
2.
Write the dual of the following Boolean expressions:
(a) A B
(d) A B
(b) A B C
(e) A A B
(c) A B A B
(f ) A A B
3.
Write the dual of the rule A A B A B .
4.
Prepare truth tables for the following Boolean expressions: (a) A B A B (e) A B C
(b) A B C B C
(f ) A B C A B C
(c) A B
(g) A B A C B C
(d) A B C
(h) A C A C
5.
State and prove the two basic De Morgan’s theorems.
6.
Prove the following rules by the method of perfect induction:
(a) A B A B A
(e) A B A B A B A B
(c) A A C A 7.
(d) A B A B A B B A
(b) A A B A B
Simplify the following Boolean expressions and draw logic circuit diagrams for your simplified expressions by using AND, OR, and NOT gates:
(a) x y z x y z x y z x y z
(d) A B C A B C A B C A B C A B C
(b) x y z x y z x y z x y z
(e) A B C A B C A B C A B C
(c) A C A B A B C B C
(f ) A B C A B C A B C A B C
8.
Let F x y z x y z x y z be a Boolean function. (a) Prepare a truth table for F. (b) Draw a logic circuit diagram for original F by using AND, OR, and NOT gates. (c) Simplify F using K-map. (d) Prepare a truth table for the simplified expression and prove that it is the same as the truth table of part (a). (e) Draw a logic circuit diagram for the simplified expression by using AND, OR, and NOT gates. Compare the total number of gates of this logic diagram and that of part (b).
9.
Find the complement of the following Boolean expressions: (a) A B A C (e) A B B C A C
(b) A B A B (c) A B C B C (d) A B C 10.
(f ) A B C C D (g) A B A B B C B C
Let F x y x y z be a Boolean function. Find F and prove that (a) F F 0 (b) F F 1
110 11.
12.
Chapter 3 ■ Digital Systems Design Express the following Boolean expressions in their sum-of-products form. Ensure that each term has all the literals.
(a) A B C
(d) A B A B C A C
(b) A B B C
(e) A C A B C A B
(c) A B C
(f ) A C A B A C B C
Express the following Boolean expressions in their product-of-sums form. Ensure that each term has all the literals.
(a) A B C
(d) A B A C B C
(b) A B C
(e) A B B C
(c) A B C
(f ) A A B A C
13.
Simplify the following Boolean functions using K-maps: (a) F (x, y, z) = Σ (0, 2, 3, 4, 6) (c) F (w, x, y, z) = Σ (3, 7, 11, 13, 14, 15) (b) F (x, y, z) = Σ (1, 2, 3, 6, 7) (d) F (w, x, y, z) = Σ (0, 1, 2, 4, 5, 7, 11, 15)
14.
Let F AC BD ACD ABCD be a Boolean expression. Find the simplified expression for F in (a) Sum-of-products form and (b) Product-of-sums form.
15.
Using K-map, simplify the Boolean function F (w, x, y, z) = Σ (0, 2, 4, 5, 6, 7, 8, 10, 13, 15) and draw logic diagrams for the simplified expression by using (a) AND-OR-NOT gates (b) NAND gates only
16.
Using K-map, simplify the Boolean function F (A, B, C, D) = Π (0, 1, 8, 9, 10, 12, 13) and draw logic diagrams for the simplified expression by using (a) AND-OR-NOT gates (b) NOR gates only
17.
Simplify the Boolean function F (A, B, C, D) = Σ (0, 1, 2, 3, 7, 8, 10) together with the don’t care conditions d (A, B, C, D) = Σ (5, 6, 11, 15) and draw logic diagram for the simplified expression by using AND-OR-NOT gates.
18.
What will be the outputs of the following logic circuits?
(a) A C
A B C D
B D
(b) A B
A C
B C
■ Questions
111
(c) A
B A B C D
B D 19.
Construct logic circuit diagrams for the following Boolean expressions by using AND, OR, and NOT gates:
(a) A B A C B C (b) A B A B B C B C (c) A B A C A B 20.
“AND, OR, and NOT gates are logically complete”. Discuss.
21.
Why are NAND and NOR gates called universal gates?
22.
Draw the implementation of logical operations AND, OR, and NOT with (a) NAND gates only, and (b) NOR gates only.
23.
Construct logic circuit diagrams for Boolean expressions of Question 19 by using (a) NAND gates only, and (b) NOR gates only.
24.
Construct logic circuit diagram for a half-adder by using NAND gates only.
25.
Construct logic circuit diagram for a half-adder by using NOR gates only.
26.
Why combinational circuits are constructed more frequently with NAND or NOR gates than with AND, OR, and NOT gates?
27.
Prove that (a) A B C A B C (b) (A B) C = A (B C)
28.
Using DeMorgen’s theorem, prove that (a)
x + y x y 0
(b) x x y x y 1 29.
Construct logic circuit diagrams for Exclusive-OR function by using (a) NAND gates only, and (b) NOR gates only.
30.
Construct logic circuit diagrams for Equivalence function by using (a) NAND gates only, and (b) NOR gates only.
31.
A logic circuit has three inputs, A, B, and C. It generates an output of 1 only when A = 0, B = 1, C = 0, or when A = 1, B = 1, C = 0. Design a combinational circuit for this system.
32.
A logic circuit has three inputs, A, B, and C. It generates an output of 1 only under the following conditions: A = 0, B = 0, C = 0 A = 0, B = 1, C = 1 A = 1, B = 0, C = 1 A = 1, B = 1, C = 1 Design a combinational circuit for this system.
112
Chapter 3 ■ Digital Systems Design
33.
Design a gating network that will have outputs 0 only when A = 0, B = 0, C = 0; A = 1, B = 0, C = 0; A = 1, B = 1, C = 0. The outputs are to be 1 for all other cases.
34.
A three-bit message is to be transmitted with an odd parity. An odd parity generator generates a parity bit (say P) to make the total number of 1s odd (including P). That is, P = 1, only when the number of 1s in the input string is even. Design a combinational logic circuit for such a parity generator.
35.
Design a combinational logic circuit to generate an even parity for hexadecimal digits.
36.
Design a combination circuit for a three-input majority function. A majority function generates on output of 1 if the input variables have more 1s than 0s, otherwise the output is 0.
37.
A sequential circuit has two D flip-flops ‘a’ and ‘b’, two inputs x and y, and one output z. The inputs to flip-flops ‘a’ and ‘b’ are designated as Da and Db respectively, and their outputs are designated as (A, A ) and (B, B ) respectively. The flipflop input equations are Da = x y x A Db =
x B x A
and the expression for output z is z =B Draw the sequential circuit diagram and tabulate the state table. 38.
Design a sequential circuit for a 2-bit count down counter. Such a counter goes through a sequence of binary states 11, 10, 01, 00. After 00, the counter repeats counting from 11. The counter changes state only when an external input x is equal to 1. It remains in the same state when x = 0.
Computer Architecture
This chapter provides an overview of computer system architecture. It first introduces the basic functional units of a computer system and then provides detail architectures of processor and memory. Subsequent chapters provide details of other units (secondary storage and I/O devices). This chapter also provides details of interconnection architectures used for interconnecting processor, memory, and I/O units. Finally, it also provides details of multiprocessor system architectures, which use multiple processors and memory units to form powerful computer systems.
BASIC FUNCTIONS OF A COMPUTER All computer systems perform the following five basic functions for converting raw input data into useful information and presenting it to a user: 1. Inputting. It is the process of entering data and instructions into a computer system. 2. Storing. It is the process of saving data and instructions to make them readily available for initial or additional processing as and when required. 3. Processing. Performing arithmetic operations (add, subtract, multiply, divide, etc.), or logical operations (comparisons like equal to, less than, greater than, etc.) on data to convert them into useful information is known as processing. 4. Outputting. It is the process of producing useful information or results for a user, such as printed report or visual display. 5. Controlling. Directing the manner and sequence in which the above operations are performed is known as controlling.
BASIC COMPUTER ORGANIZATION Even though the size, shape, performance, reliability, and cost of computers have been changing over the last several years, the basic logical structure (based on stored program concept), as proposed by Von Neumann, has not changed.
Chapter 4 ■ Computer Architecture
114
Figure 4.1 shows a block diagram of basic computer organization. In this figure, solid lines indicate flow of instruction and data, and dotted lines represent control exercised by control unit.
Basic Functional Units Figure 4.1 shows the four basic functional units of a digital computer system – input unit, storage unit, central processing unit, and output unit. Their functions are: 1.
Input unit. An input unit performs inputting operation. It carries out the following functions: (a) It accepts (or reads) instructions and data from outside world. (b) It converts these instructions and data to computer acceptable form. Units called input interfaces accomplish this task. (c) It supplies the converted instructions and data to the storage unit for storage and further processing.
2.
Output unit. An output unit performs outputting operation. It carries out the following functions: (a) It accepts the produced results, which are in coded form. We cannot understand the coded results easily. (b) It converts these coded results to human acceptable (readable) form. Units called output interfaces accomplish this task. (c) It supplies the converted results to outside world.
3.
Storage unit. A storage unit performs storing operation. Specific functions of the storage unit are to hold (store): (a) The data and instructions required for processing (received from input units). (b) Intermediate results of processing. (c) Final results of processing, before the system releases them to an output unit. Storage unit of all computers is comprised of the following two types of storage: (a) Primary storage. Primary storage of a computer system, also known as main memory, stores pieces of program instructions and data, intermediate results of processing, and recently produced results of those job(s) on which the computer system is currently working. The central processing unit can access these pieces of information directly at a very fast speed. However, primary storage is volatile, and it loses the information in it as soon as the computer system switches off or resets. Moreover, primary storage normally has limited storage capacity because it is very expensive. Primary storage of modern computer systems is made up of semiconductor devices. (b) Secondary storage. Secondary storage of a computer system, also known as auxiliary storage, takes care of the limitations of primary storage. It supplements the limited storage capacity and volatile characteristic of primary storage. This is because secondary storage is much cheaper than primary storage and it can retain information even when the computer system switches off or resets. A computer system uses secondary storage to store program instructions, data, and information of those jobs on which the computer system is currently not working but needs to hold them for processing later. The most commonly used secondary storage medium is magnetic disk.
4.
Central Processing Unit (CPU). CPU performs processing and controlling operations. It is the brain of a computer system. In a human body, the brain takes all major decisions and other parts of the body function as directed by the brain. Similarly, in a computer system, the CPU performs all major calculations and comparisons, and also activates and controls the operations of other units of the computer system. The two components of a CPU are Arithmetic Logic Unit (ALU) and Control Unit (CU). Their functions are: (a) Arithmetic Logic Unit. A computer performs all calculation and comparison (decision-making) operations in the ALU. During processing of a job, the computer transfers data and instructions stored
■ Basic Computer Organization
115
in its primary storage to ALU as and when needed. ALU does the processing and the computer temporarily transfers the intermediate results generated there back to primary storage until needed later. Hence, data may move back and forth several times between primary storage and ALU before processing of the job is over. The engineering design of a computer’s ALU determines the type and number of arithmetic and logic operations that a computer can perform. However, almost all ALUs are designed to perform the four basic arithmetic operations (add, subtract, multiply, and divide) and logic operations or comparisons such as, less than, equal to, and greater than. (b) Control Unit. A computer’s control unit does not perform any actual processing of jobs, but acts as the central nervous system for other components of the computer system. It manages and coordinates the operations of all other components. It obtains instructions from a program stored in main memory, interprets the instructions, and issues signals causing other units of the system to execute them. Storage unit
Program and data
Input unit
Secondary storage Primary storage
Output unit
Information (Results)
Control Unit Arithmetic Logic Unit Central Processing Unit (CPU)
Indicates flow of instructions and data Indicates the control exercised by the control unit
Figure 4.1. Basic organization of a computer system.
System Concept We often refer to a computer as a system (computer system). This is because a system is a group of integrated parts having a common purpose of achieving some objective(s). Hence, a system has the following three characteristics: 1.
It has more than one element.
2.
All its elements are related logically.
3.
All its elements are controlled in a manner to achieve the system goal.
Since a computer has integrated components (input unit, output unit, storage unit, and CPU), which work together to perform the steps called for in an executing program, it is a system. Input and output units cannot function until they receive signals from the CPU. Similarly, storage unit or CPU alone is of no use. Usefulness of each unit depends on other units and is realizable only when we integrate all units to form a system. Hence, we refer to a computer as a system (computer system).
116
Chapter 4 ■ Computer Architecture
Basic Processor and Memory Architecture Figure 4.1 presented the basic organization of a computer system showing its major functional units. Figure 4.2 shows the basic architecture of a computer’s main processor and main memory. These two functional units are located on the main circuit board (motherboard) of a computer. As Figure 4.2 shows, the secondary storage and I/O devices are usually in the form of independent units connected to the motherboard. ROM
EPROM
PROM Main memory (RAM) Cache memory
S E C O N D A D R E Y V I S C T E O S R A G E
Storage interfaces
Decoder
Accumulator register
Program control register
General-purpose register
Instruction register
General-purpose register
I/O
I/O interfaces
Memory address register Memory buffer register
D E V I C E S
I/O register General-purpose register
General-purpose register
Control Unit
Arithmetic Logic Unit
Central Processing Unit Figure 4.2. Processor and memory architecture of a computer system.
CPU ARCHITECTURES CPU is the brain of a computer system. It performs all major calculations and comparisons, and also activates and controls the operations of other units of the computer system. Hence, no other single component of a computer determines its overall performance as much as its CPU. To evaluate a computer’s capabilities, it is important to know how CPUs are internally structured, how different CPUs differ from each other, and how to evaluate CPU speed. These and other related concepts are described here.
Basic Components of CPU Control Unit (CU) and Arithmetic Logic Unit (ALU) are the two basic components of a CPU. Their functions are:
■ CPU Architectures
117
1.
Control Unit (CU). The control unit of a CPU selects and interprets program instructions and then coordinates their execution. As Figure 4.2 shows, it has some special purpose registers (whose functions are described in a later subsection) and a decoder to perform these activities. The special purpose registers named instruction register and program control register, respectively, hold the current instruction and the next instruction for execution, and in this way help the control unit in instruction selection. On the other hand, the decoder has necessary circuitry to decode and interpret the meaning of every instruction supported by the CPU. Each instruction is accompanied by microcode – basic directions that tell the CPU how to execute the instruction.
2.
Arithmetic Logic Unit (ALU). ALU of a CPU is the place where execution of instructions takes place during data processing. When the control unit encounters an instruction that involves an arithmetic operation (such as add, subtract, multiply, divide) or a logic operation (such as less than, equal to, greater than) it passes control to the ALU. As Figure 4.2 shows, the ALU has some special purpose registers (whose functions are described in a later subsection) and necessary circuitry to carry out all arithmetic and logic operations included in the set of instructions supported by the CPU. For example, control unit might load two numbers into ALU registers and then tell the ALU to add them (an arithmetic operation) or to check if they are equal (a logical operation). When entire CPU (both CU and ALU) is contained on a single tiny silicon chip, it is called a microprocessor.
CPU Instruction Set Every CPU has built-in ability to execute a set of machine instructions called its instruction set. Most CPUs have 200 or more instructions (such as add, subtract, and compare) in their instruction set. The list of instructions supported by a CPU in its instruction set forms the basis for designing the machine language for the CPU. Since each processor has a unique instruction set, machine language programs written for one computer will not run on another computer with a different CPU. CPUs made by different manufacturers have different instruction sets. In fact, different CPU models of the same manufacturer may have different instruction sets. However, manufacturers tend to group their CPUs into “families” having similar instruction sets. When they develop a new CPU, they ensure that its instruction set includes all instructions in the instruction set of its predecessor CPU plus some new ones. This manufacturing strategy is known as backward compatibility and the new CPU is said to be backward compatible with its predecessor. This feature allows software written for a computer with a particular CPU to work on computers with newer processors of the same family. In turn, it allows users of these computer systems to upgrade their systems without worrying about converting all their existing software.
CPU Registers As a computer’s CPU interprets and executes instructions, there is movement of information between various units of the computer. To handle this process and to speed up the rate of information transfer, the CPU uses a number of special memory units called registers. These registers hold information on a temporary basis and are part of CPU (not main memory). Length of a register is equal to the number of bits it can store. Hence, we refer to a register that can store 8 bits as an 8-bit register. Most CPUs sold today have 32-bit, 64-bit or 128-bit registers. The length of registers of a computer is sometimes called its word size. The bigger the word size, the faster a computer can process a set of data. With all other parameters being same, a CPU with 32-bit registers can process data twice as large as one with 16-bit registers. Although, the number of registers varies from computer to computer, there are some registers common to all computers. Functions of these registers are described here. Each of these registers possesses the ability to receive information, hold it temporarily, and pass it on as directed by the control unit.
Chapter 4 ■ Computer Architecture
118 1.
Memory Address Register (MAR). It holds the address of the active memory location. It is loaded from program control register when the system reads an instruction from memory.
2.
Memory Buffer Register (MBR). It holds the contents of the accessed (read/written) memory word. The system transfers an instruction word placed in this register to instruction register. A data word placed in this register is accessible for operation with accumulator register or for transfer to I/O register. To store a word in a memory location, the system first transfers it to MBR and then writes it in memory from MBR.
3.
Program Control register (PC). It holds the address of the next instruction for execution. Normally, a system stores instructions of a program in consecutive memory locations, and executes them in sequence unless it encounters a branch instruction. A branch instruction is an operation that causes a transfer to a nonconsecutive instruction. The system transfers address part of a branch instruction to PC register so that it becomes the address of the next instruction.
4.
Accumulator register (A). It holds the data on which the system has to operate, intermediate results, and results of operations performed. The system uses it during the execution of most instructions. The system returns the result of an arithmetic operation to accumulator register for transfer to main memory through memory buffer register. Many computers have more than one accumulator registers.
5.
Instruction register (I). It holds the current instruction under execution. As soon as the instruction is stored in this register, its operation and address parts (see Chapter 9) are separated. The system sends the address part of the instruction to MAR, while it sends operation part to control unit, where it is decoded and interpreted. Finally, the control unit generates and sends command signals to the appropriate unit for carrying out the task specified in the instruction.
6.
Input/Output register (I/O). The system uses it to communicate with input/output devices. An input device transfers all input information (such as instructions and data) to this register. Similarly, the system transfers all output information to this register and an output device picks up data for output from here.
Figure 4.3 summarizes the functions of these registers. Sl. No.
Name of register
1
Memory Address (MAR)
2
Memory Buffer (MBR)
Holds information on its way to and from memory
3
Program Control (PC)
Holds address of the next instruction to be executed
4
Accumulator (A)
Accumulates results and data to be operated upon
5
Instruction (I)
Holds an instruction while it is being executed
6
Input/Output (I/O)
Communicates with I/O devices
Function Holds address of the active memory location
Figure 4.3. Functions of various registers.
Execution of an instruction by the CPU during program execution normally involves the following steps: 1.
The control unit takes the address of the next program instruction for execution from program control register and reads the instruction from corresponding memory address into the instruction register of control unit.
2.
The control unit then sends the operation and address parts of the instruction to the decoder and memory address register, respectively.
3.
The decoder interprets the instruction and accordingly the control unit sends command signals to the appropriate unit for carrying out the task specified in the instruction. For example, if it is an arithmetic or logic operation, the control unit sends signal to ALU. In this case, the control unit also ensures that the data corresponding to the address part of the instruction is loaded in a suitable register in ALU, before it sends the
■ CPU Architectures
119
signal to ALU. The ALU performs the operation on the data and signals the control unit as soon as it has finished. 4.
As the system executes each instruction, it loads the address of the next instruction for execution in program control register, and repeats Steps 1 to 4.
Processor Speed CU and ALU perform operations at incredible speed. These operations are synchronized with the help of a built-in electronic clock (known as system clock), which emits millions of regularly spaced electric pulses per second (known as clock cycles). A processor fetches, decodes, and executes instructions at proper intervals with intervals timed by a specific number of clock cycles. One cycle is the time it takes to perform one operation such as moving a byte of data from one memory location to another. Normally, several clock cycles are required to fetch, decode, and execute a single program instruction. Shorter the clock cycle, faster the processor. Hence, the speed at which a computer executes an instruction depends on the computer’s built-in clock speed, which is the number of pulses produced per second. We measure clock speed in megahertz (MHz) or gigahertz (GHz), where mega means 10 6, giga means 109, and hertz means cycles per second. Over the years, clock speeds of processors have increased steadily. Most of today’s personal computers have clock speeds in 500 MHz (0.5 GHz) to 4000 MHz (4.0 GHz) range. However, effort to build processors with even faster ratings continues. With all other parameters being equal (although they never are), a CPU operating at 3 GHz can process data thrice as fast as one operating at 1 GHz. Number of operations performed per clock cycle also affects CPU performance. Most CPUs perform one operation per cycle. However, the new processors use superscalar architecture, which can execute more than one instruction in each clock cycle. It achieves this by having multiple ALUs on a single CPU chip so that it can execute two or more instructions simultaneously. Normally, we measure the processing speed of personal computers in MHz or GHz; that of workstations, mini computers, and mainframe systems in MIPS (Millions of Instructions Per Second) or BIPS (Billions of Instructions Per Second); and that of supercomputers in MFLOPS (Millions of Floating Point Operations Per Second), GFLOPS (gigaflops, which refers to a billion FLOPS), TFLOPS (teraflops, which refers to 10 12 FLOPS), or PFLOPS (petaflops, which refers to 1015 FLOPS). This is because personal computers generally employ a single microprocessor chip as their CPU, but the other classes of computers often employ multiple processors to speed up their overall performance. Thus, a workstation, minicomputer, or mainframe having a speed of 500-MIPS can execute 500 million instructions per second. Similarly, a supercomputer having a speed of 500 TFLOPS can execute 500 x 1012 floating-point operations per second. We measure the speed of supercomputers in terms of FLOPS because supercomputer applications, which are often scientific, frequently involve floating-point operations. Floating-point operations deal with very small or very large numbers, which require high precision data processing.
Types of Processors CISC Processors One of the earlier goals of CPU designers was to provide more and more instructions in the instruction set of a CPU to ensure that the CPU supports more functions. This makes it easier to translate high-level language programs to machine language and ensures that the machine language programs run more efficiently. Of course, every additional instruction in the instruction set of a CPU requires additional hardware circuitry to handle that instruction, adding more complexity to the CPU’s hardware circuitry. Another goal of CPU designers was to optimize the usage of expensive memory. To achieve this, designers tried to pack more instructions in memory by introducing the concept of variable-length instructions such as half-word, one-and-half-word, etc. For example, an operand in an immediate instruction needs fewer bits, and hence, a CPU designer can design it as a half-word instruction. Additionally, designers originally designed CPUs to support a variety of addressing modes (discussed later in this chapter during the discussion of memory). CPUs with large instruction set, variable-length instructions, and a variety of addressing modes are called CPUs based on CISC (Complex Instruction Set Computer) architecture. Since CISC processors
120
Chapter 4 ■ Computer Architecture
possess so many processing features, they make the job of machine language programmers easier. However, they are complex and expensive to produce.
RISC Processors In early 1980s, some CPU designers realized that many instructions supported by a CISC-based CPU are rarely used. Hence, they came up with the idea of reducing the design complexity of a CPU by implementing in hardware circuitry only bare minimum basic set of instructions and some of the frequently used instructions. The instruction set of the CPU need not support other complex instructions because a computer can implement them in software by using the basic set of instructions. While working on simpler CPU design, the designers also came up with the idea of making all instructions of uniform length so that decoding and execution of all instructions becomes simple and fast. Furthermore, to speed up computation and to reduce the complexity of handling a number of addressing modes, they decided to design all instructions in such a way that they retrieve operands stored in registers in CPU rather than from memory. These design ideas resulted in producing faster and less expensive processors. CPUs with a small instruction set, fixed-length instructions, and reduced references to memory to retrieve operands are called CPUs based on RISC (Reduced Instruction Set Computer) architecture. Since RISC processors have a small instruction set, they place extra demand on programmers who must consider how to implement complex computations by combining simple instructions. However, RISC processors are faster for most applications, less complex, and less expensive to produce than CISC processors because of simpler design. Popular RISC processors used in workstations are POWER (used in IBM workstations), SPARC (used in SUN workstations), and PA-RISC (used in HP workstations). Supporters of RISC technology claim that increased processing speed and lower cost of RISC processors offset the limitations of a reduced instruction set. However, critics of RISC technology are of the opinion that a RISC processor has to process more of these simple programmed instructions to complete a task, placing additional burden on system software. There seems to be no clear answer as to which technology is better. The answer may be that each technology lends itself best to certain applications, and so both technologies will coexist.
EPIC Processors Explicitly Parallel Instruction Computing (EPIC) architecture uses three key techniques for improved performance – explicit parallelism, predication, and speculation. These terminologies are explained here. 1. Explicit parallelism. EPIC technology breaks through the sequential nature of conventional processor architectures by allowing the software to communicate explicitly to the processor when the system can perform an operation in parallel. For this, it uses tighter coupling between compiler and processor. It enables the compiler to extract maximum parallelism in original code and explicitly describe it to the processor. At compile time, the compiler detects which of the instructions can the system execute in parallel. It then reorders them and groups them in such a way that the system can execute instructions belonging to separate groups in parallel. At runtime, the processor exploits this explicit parallelism information provided by the compiler to execute the instructions faster. 2. Predication. Predication technique improves performance by reducing the number of branches and branch mispredicts. The system first takes the help of compiler to reorder the instructions to reduce the number of branches as much as possible at compile time. Conventional processors use “branch prediction” technique in which the processor predicts which way a branch will fork and speculatively executes instructions along the predicted path. At the time of execution of the branch instruction, if the system finds the prediction to be correct, the processor gains performance improvement because instructions lying in the path to be executed now have already been executed and the processor can use their results directly. However, if the system finds the prediction to be wrong, it discards the results of execution of the predicted path and takes up the instructions of the correct path for execution.
■ CPU Architectures
121
However, EPIC technology uses “branch predication” instead of “branch prediction”. In this technique, instead of predicting and executing one of the paths of a branch, the processor executes instructions of all the paths of the branch exploiting as much parallelism as possible. Now when the processor discovers the actual branch outcome, it retains the valid results and discards other results. Thus, branch predication effectively removes the negative affect of branch prediction technique in cases of branch mispredict. 3. Speculation. Speculation technique improves performance by reducing the effect of memory-to-processor speed mismatch. Memory access speed is much slower than processor speed. Speculative data loading technique takes care of this by loading a piece of data and keeping it ready before the processor requires it. It not only allows the processor to load a piece of data from memory before a program needs it, but it also postpones the reporting of exceptions, if the loaded data is illegal. We can also implement speculation technique by taking help of compiler. For this, the compiler analyses a program at compile time, looking for any instructions that will need data from memory. It inserts speculative load instructions in the instructions stream of the program well ahead of the instructions that need data from memory. It also inserts a matching speculative check instruction immediately before the instructions that need data from memory. It now reorders the surrounding instructions so that the processor can dispatch them in parallel. Now when the processor encounters a speculative load instruction at runtime, it retrieves and loads the data from memory. When the processor encounters the speculative check instruction, it verifies the load before allowing the program to use the loaded data in the next instruction. If the load is invalid, the processor does not immediately report an exception. It postpones exception reporting until it encounters a check instruction that matches the speculative load. If the load is valid, the system behaves as if the exception never happened. Speculation technique combined with predication technique gives the compiler more flexibility to reorder instructions and increase parallelism. Notice that all three techniques are based on the availability of an intelligent compiler and closed coupling between compiler and processor. Hence, performance of processors based on EPIC technology comes from both hardware and software. Processors based on EPIC architecture are simpler and more powerful than traditional CISC or RISC processors. These processors are mainly for 64-bit or higher, high-end server and workstation market (not for personal computer market). Intel’s IA-64 (code-named Itanium) was the first EPIC processor.
Multicore Processors The approach used earlier for building faster processors was to keep reducing the size of chips, while increasing the number of transistors they contain. Although, this trend continued in computing industry for several decades, it was later realized that transistors cannot shrink forever. Transistor technology limits the ability to continue making singlecore processors more powerful due to the following reasons: 1.
As a transistor gets smaller, the gate, which switches the electricity ON and OFF, gets thinner and less able to block flow of electrons. Thus, small transistors tend to use electricity all the time, even when they are not swithcing. This wastes power.
2.
Increasing clock speeds causes transistors to switch faster and generate more heat and consume more power.
These and other challenges forced processor manufacturers to research for new approaches for building faster processors. In response, manufacturers came out with the idea of building multicore processor chips instead of increasingly powerful (faster) single-core processor chips. In this new architecture, a processor chip has multiple cooler-running, more energy-efficient processing cores, instead of one increasingly powerful core. The multicore chips do not necessarily run as fast as the highest performing single-core models, but they improve overall performance by handling more work in parallel. For instance, a dual-core chip running multiple applications is about 1.5 times faster than a chip with just one comparable core.
Chapter 4 ■ Computer Architecture
122
Operating system (OS) controls overall assignment of tasks in a multicore processor. In a multicore processor, each core has its independent cache (though in some designs all cores share the same cache), thus providing the OS with sufficient resources to handle multiple applications in parallel. When a single-core chip runs multiple programs, the OS assigns a time slice to work on one program and then assigns different time slices for other programs. This can cause conflicts, errors, or slowdowns when the processor must perform multiple tasks simultaneously. However, a multicore chip can run multiple programs at the same time with each core handling a separate program. The same logic holds for running multiple threads of a multithreaded application at the same time on a multicore chip, with each core handling a separate thread. Based on this, either the OS or a multithreaded application parcels out work to multiple cores. Multicore processors have the following advantages over single-core processors: 1.
They enable building of computers with better overall system performance by handling more work in parallel.
2.
For comparable performance, multicore chips consume less power and generate less heat than single-core chips. Hence, multicore technology is also referred to as energy-efficient or power-aware processor technology.
3.
Because the chips’ cores are on the same die in case of multicore processors architecture, they can share architectural components, such as memory elements and memory management. They thus have fewer components and lower costs than systems running multiple chips (each a single-core processor).
4.
Also, signaling between cores can be faster and use less electricity than on multichip systems.
Multicore processors, however, currently have the following limitations: 1.
To take advantage of multicore chips, we must redesign applications so that the processor can run them as multiple threads. Note that it is more challenging to create software that is multithreaded.
2.
To redesign applications, programmers must find good places to break up the applications, divide the work into roughly equal pieces that can run at the same time, and determine the best times for the threads to communicate with one another. All these add to extra work for programmers.
3.
Software vendors often charge customers for each processor that will run the software (one software license per processor). A customer running an application on an 8-processor machine (multiprocessor computer) with single-core processors would thus pay for 8 licenses. A key issue with multicore chips is whether software vendors should consider a processor to be a single core or an entire chip. Currently, different vendors have different views regarding this issue. Some consider a processor as a unit that plugs into a single socket on the motherboard, regardless of whether it has one or more cores. Hence, a single software license is sufficient for a multicore chip. On the other hand, others charge more to use their software on multicore chips for perprocessor licensing. They are of the opinion that customers get added performance benefit by running the software on a chip with multiple cores, so they should pay more. Multicore-chip makers are concerned that this type of non-uniform policy will hurt their products’ sales.
Chip makers like Apple, Intel, AMD, IBM, and Sun have already introduced multicore chips for servers, desktops, and laptops. The current multicore chips are dual-core (2 cores per chip), quad-core (4 cores per chip), 8 cores per chip, 16 cores per chip, 32 cores per chip, 64 cores per chip, and so on.
■ Memory Architectures
123
Power-Efficient Processors Traditionally, a system’s performance was measured in terms of its processing power. However, excessive power consumption by computing systems has now become a matter of concern for most organizations. Cost of operating computing systems often far exceeds the cost of purchasing them. Hence, considering total cost of ownership (TCO), a new measure that has become popular for measuring the performance of computing systems is power consumption to processing power ratio. Manufacturers of computing systems have therefore made attempts at all levels in computer architecture to reduce power consumption of systems. Processor manufacturers have come out with new processor architectures to reduce power consumption right at processor level. For example, Intel Xeon processor offers a technology called Demand Based Switching (DBS) for reduced power consumption. A traditional processor operates only at a single frequency and voltage, regardless of its workload. Therefore, it is always ON and always consumes full power. Processors based on DBS technology are designed to run at multiple frequency and voltage settings. In conjuction with an Operating System (OS) and Basic Input Output System (BIOS) that support DBS, such processors automatically switch to and operate at the lowest setting that is consistent with optimal application performance. The OS monitors processor utilization multiple times per second and down-shifts/up-shifts to a lower/higher frequency and voltage as appropriate. Therefore, power usage is automatically tailored to match dynamically changing system workload, which substantially reduces power wastage with minimal impact on peak performance capabilities of the processor.
MEMORY ARCHITECTURES We saw that a computer’s CPU contains necessary circuitry for data processing and controlling other components of the computer. However, one thing it does not have built into it is the place to store programs and data needed during data processing. We also saw that the CPU contains several registers for storing data and instructions, but they can store only a few bytes at a time. They are just sufficient to hold one or two instructions with corresponding data. If instructions and data of an executing program were to reside in secondary storage like a disk, and fetched and loaded one-by-one into CPU registers as the program execution proceeded, this would lead to the CPU being idle most of the time. This is because there is a large speed mismatch between the rate at which CPU can process data and the rate at which the system can transfer data from disk to CPU registers. For example, a CPU can process data at a rate of about 5 nanoseconds/byte and a disk reader can read data at a speed of about 5 microseconds/byte. Hence, within the time in which a disk can supply one byte of data, a CPU can process 1000 bytes. This would lead to poor overall performance even for a computer having a fast CPU. To overcome this problem, there is a need to have a reasonably large storage space that can hold instructions and data of the program(s) on which the CPU is currently working. The time to fetch and load data from this storage space into CPU registers must also be considerably less as compared to that from disk storage to reduce the speed mismatch problem with CPU speed. Every computer has such a storage space known as primary storage, main memory, or simply memory. Instructions and data of a program reside mainly in this area when CPU is executing the program. Physically, this memory consists of some integrated circuit (IC) chips either on the motherboard or on a small circuit board attached to the motherboard of a computer system. This built-in memory allows CPU to store and retrieve data quickly. The rate of fetching data from this memory is of the order of 50 nanoseconds/byte. Hence, the rate of data fetching from main memory is about 100 times faster than that from a highspeed secondary storage like disk.
Storage Evaluation Criteria A storage unit of a computer system is characterized and evaluated based on the following properties: 1.
Storage capacity. It is the amount of data, which the storage unit can store. Larger storage capacity is desirable. As compared to secondary storage units, primary storage units have less storage capacity.
Chapter 4 ■ Computer Architecture
124 2.
Access time. It is the time required to locate and retrieve stored data from the storage unit in response to a program instruction. Faster access time is preferable. As compared to secondary storage units, primary storage units have faster access time.
3.
Cost per bit of storage. It refers to the cost of a storage unit for a given storage capacity. Lower cost is desirable. As compared to secondary storage units, primary storage units have higher cost per bit of storage.
4.
Volatile. If a storage unit can retain data stored in it even in case of power off or interruption of power, it is called non-volatile storage. On the other hand, if a storage unit loses data stored in it in case of power off or interruption of power, it is called volatile storage. Obviously, non-volatile storage is desirable. In almost all computer systems, primary storage units are volatile and secondary storage units are non-volatile.
5.
Random access. If the time taken to access a piece of data from a storage unit is independent of the location of the data in the storage unit, it is called random access storage or random access memory (RAM). Each location of a RAM is as easy to access as any other location and takes the same amount of time. In almost all computer systems primary storage units have random access property and secondary storage units have either pseudo-random access (access time is nearly same for all locations but not exactly same) or sequential access (access time depends on the location of the data) property.
Main Memory Organization Main memory of a computer system consists of several small storage areas called locations or cells. Each of these locations can store a fixed number of bits called word length of the memory. Hence, as Figure 4.4 shows, a given memory is divided into N words, where N is generally some power of 2. Each word or location has a built-in and unique number assigned to it. This number is called address of the location and is used to identify the location. Each location can hold either a data item or an instruction and its address remains the same, regardless of its contents. The first address starts at 0 and the highest address equals the number of words that can be stored in the memory minus 1. For example, if a memory has 1024 locations, its address ranges between 0 and 1023. Hence, at address 0 we find a word, at address 1 a second word, at address 2 a third word, and so on, up to the final word at the last address (1023).
Addresses of a memory
0 1 2 3 4 5
Words of a memory
N-2 N-1 Bit 1 Bit 2
Each word contains the same number of bits = word length
Figure 4.4. Organization of a main memory having N words.
There is an important difference between address of a memory location and its content. A memory is like a large cabinet having as many drawers as there are addresses in memory. Each drawer contains a word and address of each word is written on the drawer. If we write or store a word, say 10101010 at address 125, it is like placing the word 10101010 in the drawer labeled 125. Later, reading from address 125 is like looking in that drawer to see its content, which is 10101010. We do not remove the word at an address when we read its content, but its content changes when we store or write a new word in it. Hence, entering data into a storage location is destructive of previous content, but retrieving data from a location is non-destructive. The act of entering data into a storage location is called memory
■ Memory Architectures
125
write operation, and the act of retrieving data from a storage location is called memory read operation. Hence, read is a non-destructive operation and write is a destructive operation.
Why More Bits? You might have heard about 16-bit computers, 32-bit computers, 64-bit computers, etc. They refer to memory word length (total number of bits per memory word) of a computer. Word length of small computers is 8, 16, or 32 bits, whereas that of large computers is 64 bits or more. The obvious question that arises is – “What is the advantage of having more number of bits per word, instead of having more words of smaller size (length)?” For an answer to this question, imagine a highway with eight lanes and a heavy flow of traffic. If we expand it to sixteen lanes, traffic flow speeds up considerably. “8 bits” is analogous to number of “lanes” on a microchip. More bits mean a more rapid flow of electronic signals, in other words, a faster computer. Hence, the amount of data that an 8-bit computer can transfer to and from memory in one minute, a 32-bit computer may transfer in a few seconds. Data and instructions move to and from memory as one or more words at a time. Therefore, even if the electronic circuits used are comparable in speed, computers having smaller word length will be slower in operation than computers having larger word length. This difference is analogous to providing the user of a computer having smaller word length with a small data shovel and the user of a computer having larger word length with a large data shovel. Even though they both may be shoveling at comparable speeds, the user with smaller shovel will be slower because he/she needs more shovelfuls to move the same amount of data.
Fixed and Variable Word-length Memory Main memory of some computers can store a fixed number of characters (equal to its word-length in bytes) in each numbered address location. Such computers are said to be word-addressable and they employ fixed-word-length memory approach. These computers always allocate storage space in multiples of word-length. Therefore, if a wordaddressable computer has fixed word-length of 4 bytes (4 characters), it will require one word (4 bytes) to store the word “CAT” and two words (8 bytes) to store the word “BOMBAY”. In many computers, main memory is designed to store a single character (A, B, 1, 2, +, -, etc.) at each numbered address. Computers designed in this manner are said to be character-addressable and they employ variable-wordlength memory approach. Hence, in these computers only 3 bytes are required to store the word “CAT” and 6 bytes to store the word “BOMBAY”. Figure 4.5 summarizes the difference between fixed-word-length and variable-wordlength memory approaches. Both fixed and variable word-length memory systems have their own merits and demerits. Normally, large scientific computers use fixed-word-length approach for gaining speed of calculation, while small business computers use variable-word-length approach for optimizing use of storage space. For example, let us consider a fixed-word-length memory computer with a word size of eight characters. If most of the data words to be stored are of less than five characters, more than half of its storage space will remain unused. This will not happen in case of a computer with variable-word-length memory because it can place a character in every storage cell. However, word-addressable computers possess faster calculating capability because they can add two data words in a single operation. If word length of a computer that uses fixed-word-length approach is eight characters, it can add two eight-digit numbers in a single operation. On the other hand, a character-addressable computer can add only one digit in each number during a single operation, and would need eight steps to add two eight-digit numbers. We can design a computer to employ either fixed-word-length or variable-word-length memory organization. These computers have an instruction set, using which we can operate them as either variable- or fixed-word-length memory computers. However, with memory becoming cheaper and larger day-by-day, most modern computers employ fixedword-length memory organization.
126
Chapter 4 ■ Computer Architecture
Word
Address numbers
0501 0502
B
O M B A Y D E L H I
0503 1024
Address numbers
0025 0026 0027
B O M
0028 0029
B A
0030 0031
Y
4096 (a) Fixed-word-length memory.
0051 D 0052 E 0053 L Address numbers
0054 H 0055 I 0056
4096
(b) Variable-word-length memory.
Figure 4.5. Fixed-word-length memory compared with variable-word-length memory.
Main Memory Capacity Main memory capacity of a computer is defined as number of bytes that can be stored in its main memory. It is stated normally in terms of kilobytes (KB), which is equal to 1024 (2 10) bytes of storage, or megabytes (MB), which is equal to 1,048,576 (220) bytes of storage, or gigabytes (GB), which is equal to 1,073,741,824 (230) bytes of storage. Therefore, a computer having 2 GB of memory is capable of storing 2 x 1073741824 = 2,147,483,648 bytes or characters. Notice that 1 KB is about 103 bytes, 1 MB is about 106 bytes, and 1 GB is about 109 bytes hence, the origin of the prefixes kilo (thousand), mega (million) and giga (billion). Sometimes, we also state memory capacity of a computer system in terms of number of words. In this case, we specify total number of bits per word along with total number of words. Therefore, a memory having 2 15 words with each word of 16 bits is called a “32 K, 16-bit memory”. If word size of a memory is 8 bits (equal to a byte), it becomes immaterial whether memory capacity is expressed in terms of bytes or words. Hence, a memory having 2 16 words with each word of 8 bits is simply referred to as a 64 K memory (word size of 8-bits is implicit here). Memory capacity continues to increase rapidly and it is only a matter of time before we start stating memory capacity in terms of terabytes (TB), which is about one trillion (1012) bytes. Today, we frequently use GB, TB and PB (petabytes = 1015 bytes) to refer to the capacity of secondary storage of computer systems.
Types of Memory Chips Memory chips are classified based on their capability to retain stored data when power is turned off or interrupted, and how easily stored data can be altered (overwritten). Figure 4.6 shows various types of memory chips. They are described here.
Random Access Memory (RAM) When people talk about computer memory, they usually mean the volatile RAM memory. Physically, this memory consists of some integrated circuit (IC) chips (see Figure 1.1) either on the motherboard or on a small circuit board attached to the motherboard of the computer. A computer’s motherboard usually has the flexibility to add more memory chips for enhancing the memory capacity of the system. Hence, if a user decides to have more memory than his/her computer currently has, he/she can buy more memory chips and plug them into the empty memory slots on the motherboard. Normally, service engineers do this job. Additional RAM chips, which plug into special sockets on the motherboard, are known as single in-line memory modules (SIMMs).
■ Memory Architectures
127
Memory chips
Volatile and writable
Static (SRAM)
Dynamic (DRAM)
Non-volatile and read-only
Manufacturerprogrammed (ROM)
User-programmed
PROM
UVEPROM
EPROM
EEPROM
Figure 4.6. Various types of memory chips.
RAM chips are of two types – dynamic and static. Dynamic RAM (DRAM) uses an external circuitry to periodically “regenerate” or refresh storage charge to retain the stored data, while static RAM (SRAM) does not need any special regenerator circuit to retain the stored data. Since it takes more transistors and other devices to store a bit in a static RAM, these chips are more complicated and take up more space for a given storage capacity than dynamic RAMs do. Moreover, a static RAM is faster, costlier, and consumes more power than dynamic RAM. Due to these reasons, large memories use dynamic RAM, and static RAM is used for specialized applications. The main memory of most computers uses dynamic RAM.
Read-Only Memory (ROM) A special type of RAM, called read-only memory (ROM), is a non-volatile memory chip in which data is stored permanently. Usual programs cannot alter this data. In fact, storing data permanently into this kind of memory is called “burning in of data” because data in such memory is stored by using fuse-links. Once we burn fuse-links for some data, it is permanent. We can only read and use data stored in a ROM chip (we cannot change them). This is the reason why it is called read-only memory (ROM). Since ROM chips are non-volatile, a ROM chip does not lose its stored data in case of power off or interruption of power. This is unlike a volatile RAM chip. ROMs are also known as field stores, permanent stores, or dead stores. System designers use ROMs to store those programs and data, which do not change and are used frequently. A computer carries out most of its basic operations by wired electronic circuits. However, several higher-level operations used frequently, require complicated electronic circuits for their implementation. Hence, instead of building electronic circuits for these operations, designers use special programs to perform them. These programs are called micro-programs because they deal with low-level machine functions and are essentially substitutes for additional hardware. Computer manufacturers store these micro-programs in ROMs so that users cannot modify them. A good example of micro-program is the “system boot program”, which contains a set of start-up instructions that are executed when a system is powered on to check if the system hardware (like memory, I/O devices, etc.) is functioning properly. It looks for an operating system and loads its core part in the volatile RAM of the system to produce the initial display-screen prompt. Note that a computer needs this micro-program every time it is switched on, and the computer must retain it even when it is switched off. Hence, the computer’s ROM is an ideal storage for this microprogram.
128
Chapter 4 ■ Computer Architecture
Programmable Read-Only Memory (PROM) There are two types of read-only memory (ROM) – manufacturer-programmed and user-programmed. A manufacturer-programmed ROM is one in which data is burnt in by the manufacturer of the electronic equipment in which it is used. For example, a personal computer manufacturer may store the system boot program permanently in a ROM chip located on the motherboard of each PC manufactured by it. Similarly, a printer manufacturer may store the printer controller software in a ROM chip located on the circuit board of each printer manufactured by it. It is not possible for users of such equipment to modify the programs or data stored in the ROM chip. On the other hand, a user-programmed ROM is one in which a user can load and store “read-only” programs and data. That is, it is possible for a user to “customize” a system by converting his/her programs to micro-programs and storing them in a user-programmed ROM chip. Such a ROM is known as Programmable Read-Only Memory (PROM) because a user can program it. Once the user programs are stored in a PROM chip, the system can execute them in a fraction of the time previously required. A special device known as PROM-programmer is used to program a PROM chip (record information in it). However, once a user programs a PROM chip, it becomes a ROM because the information recorded in it can only be read (it cannot be changed). PROM is also non-volatile storage, i.e., information stored in it remains intact even in case of power off or interruption of power.
Erasable Programmable Read-Only Memory (EPROM) Once information is stored in a ROM or PROM chip, a user cannot alter it. Erasable Programmable Read-Only Memory (EPROM) chip overcomes this problem by allowing users to reprogram it to store new information. R&D personnel (experimenters) often use EPROMs because they frequently change micro-programs to test a system’s efficiency with new programs. EPROMs are also useful for those applications in which one may like to store a program in a ROM that would normally not change, but under some unforeseen conditions one may like to alter it. When an EPROM is in use, the user can only “read” the information stored in it, and the information remains in the chip until the user erases it. EPROM chips are of two types – Ultra Violet EPROM (UVEPROM), which requires exposing the chip for some time to ultraviolet light for erasing information stored in it, and Electrically EPROM (EEPROM), which requires use of high voltage electric pulses for erasing information stored in it. It is easier to alter information stored in an EEPROM chip as compared to an UVEPROM chip. EEPROM is also known as flash memory because of the ease with which a user can alter programs stored in it. Many new I/O and storage devices like USB (Universal Serial Bus) pen drive and MP3 music player use flash memory.
MINIMIZING MEMORY-PROCESSOR SPEED MISMATCH We saw that the use of main memory helps in minimizing the disk-processor speed mismatch to a large extent because the rate of data fetching by CPU from main memory is about 100 times faster than that from a high-speed secondary storage like disk. Still the rate at which CPU can fetch data from memory is about 10 times slower than the rate at which CPU can process data. Performance of processors often gets limited due to this memory-processor speed mismatch, and we can improve the overall system performance by minimizing the memory-processor speed mismatch. Two approaches used commonly for doing this are use of cache memory (pronounced “cash” memory) and memory interleaving. These techniques are described here.
Cache Memory Cache memory is an extremely fast, small memory between the CPU and main memory whose access time is closer to the processing speed of CPU. It acts as a high-speed buffer between CPU and main memory and computer systems use it to temporarily store active data and instructions during processing. Since cache memory is faster than main memory, system’s performance improves considerably when the system makes those data and instructions available in cache that processes need during present processing.
■ Minimizing Memory-Processor Speed Mismatch
129
The obvious question is how the system knows in advance, which data and instructions it needs in present processing to make it available beforehand in the cache. The answer to this question comes from the principle of locality of reference. According to this principle, during the course of execution of programs, memory references by the processor for both instructions and data tend to cluster. That is, if the system executes an instruction, there is a likelihood of the system executing the same instruction again soon. This is true because typically most programs contain a number of iterative loops (such as while and for loops). Once a program enters a loop during its execution, there are repeated references to a small set of instructions (i.e., the instructions in the loop are executed repeatedly several times). A branch instruction (such as a go to statement) upsets the locality of reference to instructions, but such instructions do not occur frequently in well-written programs. Locality of reference is true not only for references to program instructions, but also for references to data. For example, operations on tables, arrays, and records in a file involve access to a clustered set of data words. Over a long period, the instructions, or data clusters in use change, but over a short period, the processor works primarily with fixed clusters of instructions and data resulting in clustered memory references. Based on the feature of locality of reference, a system makes effective use of a cache in the following manner. As Figure 4.7 shows, cache memory acts as a small, fast-speed buffer between the CPU and main memory. It contains a copy of a portion of main memory contents. When the CPU attempts to read a memory word (instruction or data) during execution of a program, the system checks whether the word is in the cache. If so, the system delivers the word to the CPU from the cache. If not, the system reads a block of main memory, consisting of some fixed number of words including the requested word, into the cache and then delivers the requested word to the CPU. Because of the feature of locality of reference, when the system fetches a block of memory words into the cache to satisfy a single memory reference, it is likely that there will soon be references to other words in that block. That is, the next time the CPU attempts to read a word, it is likely that it finds it in the cache and saves the time needed to read the word from main memory. You might think that the odds of the CPU finding the word it needs in the cache are small, but statistics shows that in more than 90% of time, the needed word is available in the cache. As the name implies, cache memory is a memory in hiding (the word “cache” literally means a hiding place for treasure or stores) and is not addressable by normal users of the computer system. The hardware transfers the needed instructions/data between the cache and main memory without any programmer intervention. In fact, application programmers are unaware of its presence and use. Main memory (Slower in speed and larger in size than cache memory) Address of accessed word when the word is not found in cache
Block of words including the accessed word
Cache memory Address of accessed word
Accessed word CPU
Figure 4.7. Illustrating the operation of cache memory.
Figure 4.7 illustrates the general concept of cache memory usage. Actual systems may have some variations. For example, many computer systems have two separate cache memories called instruction cache and data cache. They use instruction cache for storing program instructions and data cache for storing data. This allows faster identification of availability of accessed word in cache memory and helps in further improving system’s performance. Many computer systems also have multiple levels of caches (such as level one and level two caches, often referred to as L1
130
Chapter 4 ■ Computer Architecture
and L2 caches). L1 cache is smaller than L2 cache and the system uses it to store more frequently accessed instructions/data as compared to those in L2 cache. Designers need to address several design issues to use cache memory. A detailed discussion of cache design is beyond the scope of this book. Key design issues are summarized here. 1. Cache size. Cache memory is expensive as compared to main memory and hence its size is normally small. Statistical studies indicate that reasonably small caches can have a significant impact on overall system performance. Hence, designers usually decide cache memory size on the basis of statistical studies of program behavior, and choose cache size in such a way that the probability of the system finding the needed instruction/data in cache is more than 90%. As a typical example of cache size, a system having 1 GB of main memory may have about 1 MB of cache memory. 2. Block size. Block size refers to the unit of data (few memory words) exchanged between cache and main memory. As we increase block size, hit ratio (fraction of time that referenced instruction/data is found in cache) first increases because of the principle of locality since more and more useful words are brought into the cache. However, hit ratio begins to decrease if we increase block size further because the probability of using the newly fetched words becomes less than the probability of reusing the words that the system must move out of the cache to make room for the new block. Hence, the designer must suitably choose block size to maximize hit ratio. 3. Replacement policy. When the system has to fetch a new block into the cache, it may have to replace another one to make room for the new block. The replacement policy decides which block to replace in such situation. Obviously, it will be best to replace a block that the system is least likely to need again in the near future. Although, it is impossible to identify such a block, a reasonably effective strategy is to replace the block that has been in the cache longest with no reference to it. This policy is known as least recently used (LRU) algorithm. The system needs hardware mechanisms to identify the least recently used block. 4. Write policy. If the system alters the contents of a block, then it is necessary to write it back to main memory before replacing it. The write policy decides when the system writes back the altered words of a block to main memory. At one extreme, the system writes an updated word of a block to main memory as soon as such updates occur in the block. At the other extreme, the system writes all updated words of the block to main memory only when it replaces the block from the cache. The latter policy minimizes memory write operations but temporarily leaves main memory in an inconsistent (obsolete) state. This can interfere with multipleprocessor operation when multiple processors share a common memory and cache. It can also interfere with direct memory access by input/output modules. Several other write policies that are somewhere in between these two extremes are also in use. Normally, the designer of a computer’s architecture makes the choice of a suitable write policy for its cache.
Memory Interleaving In this method, the system’s main memory has n equal size modules, and the CPU has separate MAR and MBR registers for each memory module. In addition, the CPU has n instruction registers and a memory access system. When a program is loaded into main memory, its successive instructions are stored in successive memory modules. For example, if n = 4 and the four memory modules are M 1, M2, M3 and M4, the 1st instruction is stored in M1, 2nd in M2, 3rd in M3, 4th in M4, 5th in M1, 6th in M2, and so on. Now during program execution, when the CPU issues a memory fetch command, the memory access system creates n consecutive memory addresses and places them in the n MARs in the right order. A memory read command reads all the n memory modules simultaneously, retrieves the n consecutive instructions, and loads them into the n instruction registers. Thus, each fetch for a new instruction results in loading of n consecutive instructions in the n instruction registers of the CPU. Since the system normally executes instructions of a program in sequence, availability of n successive instructions in the CPU avoids memory access after each instruction execution, and total execution time speeds up. Obviously, the fetched successive instructions are not useful when the system encounters a branch instruction during program execution. This is because the system will
■ Memory and I/O Buses
131
load the new set of n successive instructions, starting from the location of the instruction to which the control branches, into the n instruction registers, overwriting the previously stored instructions that it loaded earlier but some of which were not executed. However, the method is quite effective in minimizing memory-processor speed mismatch because branch instructions do not occur frequently in a program. Figure 4.8 illustrates memory interleaving architecture with a 4-way (n = 4) interleaved memory system. Central Processing Unit (CPU) Other registers and other components of CPU Four instruction registers
I1
I2
I3
I4
Memory access system Four memory address and memory buffer registers
MAR1
Four memory modules
MBR1
M1
MAR2
MBR2
MAR3
M2
MBR3
MAR4
M3
MBR4
M4
Main memory Figure 4.8. Illustrating a 4-way interleaved memory system.
Hybrid Approach This method uses both cache memory and memory interleaving. It uses cache memory to reduce speed mismatch between CPU and main memory, and memory interleaving to reduce speed mismatch between CPU and cache memory. This approach further helps in improving total system performance.
MEMORY AND I/O BUSES Memory Buses We saw that a system stores in memory the data and instructions of a program in execution, and fetches and loads them into CPU registers as the program execution proceeds. That is, the CPU and memory interact frequently. To enable this interaction, the two units of a computer system need some type of connectivity between them. This connectivity channel is known as a bus. Physically, a bus is a set of wires, which carries a group of bits in parallel and has an associated control scheme. Width of a bus is defined as the number of parallel lines (wires) in the bus. Every computer system has the following three types of buses for interconnecting the CPU and memory: 1.
Data bus. A system uses data bus to transfer data between CPU and memory. When people refer to the bus of a computer system, they usually mean the data bus.
Chapter 4 ■ Computer Architecture
132
Width of data bus is an important parameter that affects the overall performance of a computer system. This is because each wire of a bus can transfer one bit at a time. Hence, an 8-bit bus (one having 8 parallel wires) can transfer 8 bits (one byte), a 16-bit bus can transfer two bytes, and a 32-bit bus can transfer four bytes at a time. A wider data bus enables more bits of data to be transferred simultaneously, resulting in faster exchange of data. This is analogous to a multi-lane highway, the wider the highway, the more traffic can flow simultaneously. 2.
Address bus. In order to retrieve some data from memory, it is necessary to specify the address of the location where the data is stored. A system uses address bus to carry the address of a memory location whenever it has to transfer data to or from memory. Obviously, width of address bus must be equal to the number of bits in memory address register (MAR) of the system. Width of address bus is an important parameter for a computer system because it determines the maximum number of memory locations that the computer system can have. For example, if address bus is 8 bits wide, the CPU can address only 256 bytes of memory. Early personal computers had 20-bit wide address buses, so the CPU could address 220 bytes, or 1 MB, of memory. Today, most systems have 64-bit wide address buses so that the CPU can address 264 bytes of memory.
3.
Control bus. In addition to sending addresses and exchanging data with memory, the CPU also needs to send control signals to memory to specify whether to read or write the data to the specified address location. The control bus carries such signals, which are in the form of READ/WRITE commands.
Thus, a memory access by the CPU involves all three buses – control bus carries memory access command, address bus carries memory address, and data bus carries the data. Figure 4.9 shows the three buses used for interconnecting the CPU and main memory for enabling this. Data bus CPU
Address bus
Main memory
Control bus
Figure 4.9. Three types of buses used between the CPU and main memory.
I/O Buses We saw that in addition to flow of data between CPU and memory, data flows between CPU and I/O devices. Hence, computer systems also use buses for interconnecting the CPU with I/O devices. A system uses the same three types of buses used for interconnecting CPU and memory for interconnecting CPU with I/O devices. Their roles in context of I/O devices are described here. 1.
Data bus. A system uses data bus to transfer data between the CPU and I/O devices. A wider data bus enables faster exchange of data between the CPU and I/O devices. A system normally uses industry standard data buses to connect cards of peripheral I/O devices to the system’s motherboard. Some industry standard data buses that older computer systems used are ISA (Industry Standard Architecture), MCA (Micro Channel Architecture), EISA (Extended Industry Standard Architecture), and AGP (Accelerated Graphics Port). Industry standard data buses used in modern computer systems are: (a) PCI (Peripheral Component Interface). PCI bus has a fixed width of 32 bits with maximum signaling frequency of 33.33 MHz. Hence, its peak data transfer rate is 133 MB/sec (133 Mega (million) Bytes
■ Memory and I/O Buses
133
per second) (33.33 MHz x 32 bits ÷ 8 bits/byte = 133 MB/sec). For years, PCI has been a versatile, functional way to connect sound, video, and network cards to a motherboard. (b) PCI-X (PCI Extended). PCI-X doubles the bus width to 64 bits and increases the maximum signaling frequency to 133 MHz. Hence, its peak data transfer rate is 1014 MB/sec. A newer version of PCI-X further increases the maximum signaling frequency to 266 MHz. Hence, its peak data transfer rate is 2035 MB/sec. All PCI-X versions are backward compatible with PCI. (c) PCIe (PCI Express). PCI and PCI-X are bus-based standards in which all devices connected to the 32bit or 64-bit bus share the same bidirectional, parallel signal path. This requires “arbitrating” the bus or waiting for the bus to be free. Unlike PCI and PCI-X, PCIe is a point-to-point bus. It is built around pairs of serial (1-bit), unidirectional point-to-point connections called lanes. Hence, PCIe is a serial connection that operates more like a network than a bus. Instead of one bus that handles data of multiple devices connected to it, PCIe has a switch that controls several point-to-point serial connections. In this manner, each device gets its own dedicated connection, so devices no longer share bandwidth as they do on a normal bus. Each PCIe slot has 1, 2, 4, 8, 16, or 32 lanes between the motherboard and the card. It is customary to write number of lanes with an ‘x’ prefix. For example, x1, x2, x3, …, x32 refer to one-lane, two-lane, three-lane, …, thirty-two-lane cards respectively. Each lane contains two pairs of wires, one for sending, and one for receiving. Packets of data move across a lane at a rate of one bit per cycle. Therefore, an x1 link has one lane made up of four wires and transmits one bit per cycle in each direction. An x2 link has two lanes made up of eight wires and transmits two bits at once, an x4 link transmits four bits at once, and so on. The latest available version of PCI-Express is PCTe 5.0. The bandwidth for each PCIe 5.0 lane is 4 GB/sec. Hence, a PCI-Express 5.0 x16 device will have up to 64 GB/sec bandwidth. PCIe slots come in different physical sizes referred to by the maximum number of lanes they support. A PCIe card fits into a slot of its size or bigger, but not into a smaller PCIe slot. PCI, PCI-x, and PCIe are all compatible. Because PCIe has faster data transfer rate than PCI and PCIX, and it is compatible with them, it is likely that PCIe will eventually replace PCI and PCI-X. (d) HT (HyperTransport). HT is a packet-based point-to-point bus. It has at least two unidirectional links. Each link can be of 2, 4, 8, 16, or 32 bits width with maximum signaling frequency of 800 MHz. Hence, peak data transfer rate for a pair of 8 bits links is 1.6 GB/sec (800 MHz x 2 x 8 bits ÷ 8 bits/byte = 1.6 GB/sec). This gives aggregate bandwidth of 3.2 GB/sec in both directions for a pair of 8 bits links. A system can double or quadruple aggregate bandwidth by using 16 bits or 32 bits links respectively. Currently, some systems use HT bus in conjunction with AMD processors. 2.
Address bus. Normally, a computer system has multiple I/O devices like disk, tape, network, etc. simultaneously connected to it. Each I/O device has a unique identifier (or address) associated with it. The system uses address bus to carry the address of the I/O device, which the CPU needs to access.
3.
Control bus. A system uses control bus to carry commands such as START, READ, WRITE, REWIND TAPE, etc., from CPU to I/O devices. It also carries status information of I/O devices to CPU.
Thus, an I/O device access by CPU involves all three buses – control bus carries device access command, address bus carries device address, and data bus carries the data. Figure 4.10 shows the three buses used for interconnecting the CPU and I/O devices for enabling this. Each device has a controller, which controls the operation of the device. It also temporarily stores the data transferred between the CPU and the device in a local data register.
134
Chapter 4 ■ Computer Architecture
Data bus CPU
Address bus Control bus
I/O devices and associated controllers
Figure 4.10. Three different types of buses used between the CPU and I/O devices.
INTERCONNECTION ARCHITECTURES We saw the use of buses in interconnecting the CPU with memory and I/O devices. Interconnection architecture of a computer defines the manner in which these functional units of the computer are connected to each other. Accessing mechanism and flow of data from one unit to another depends on interconnection architecture. The three commonly used interconnection architectures are described here.
Unibus Architecture In unibus architecture, there is a single data bus, a single address bus, and a single control bus for interconnecting the CPU with both memory and I/O devices (see Figure 4.11). That is, both memory and I/O devices share the buses for data transfer between CPU and memory, and between CPU and I/O devices. As a result, data transfer can take place at a time only between the CPU and memory, or between the CPU and an I/O device. Obviously, this architecture leads to slow data transfer. Hence, small computers (where performance is not an issue) use unibus architecture. The main advantage of this architecture is its simple design and ease of programming of I/O device accesses. In this case, I/O devices do not require any special I/O instructions because they use the same memory address space. Hence, a WRITE instruction with an address corresponding to an output device will write information to that device. Another advantage of this architecture is that it allows easy addition of new I/O devices to the system, as no new instructions specific to the device are required. Data bus
Address bus
Control bus
I/O devices with associated controllers
CPU
Memory
Figure 4.11. Unibus interconnection architecture.
Dual Bus Architecture In dual bus architecture, there are separate sets of buses for interconnecting the CPU with memory and I/O devices (see Figure 4.12). Hence, a system having this architecture transfers data between the CPU and memory by using one set of buses, while it transfers data between the CPU and I/O devices by using another set of buses. In this case, a system uses one of the following two mechanisms for data transfer between the CPU and an I/O device:
■ Interconnection Architectures
I/O devices with associated controllers
Data bus Address bus Control bus
135
Data bus CPU
Address bus
Memory
Control bus
Figure 4.12. Dual bus interconnection architecture.
1.
Busy wait. In this method, the CPU sends the I/O request (say a data READ request) to the device via the buses. It sends the command and the device address associated with the READ request by using control bus and address bus respectively. As soon as the device controller receives the request, it first sets ON a “device busy” indicator so that the system will not accept any further I/O request for the device. It then executes the request and stores the data for transfer in its local data register. As soon as it completes the request processing, it sets ON another indicator called “data ready” indicator. After issuing the READ request, the CPU continuously keeps checking the “data ready” indicator of the device and waits until this indicator is set to ON. As soon as the CPU finds this indicator to be ON, it reads the data from the data register of the device controller into its own register. It then sends a data received acknowledgment signal to the device. On receiving this signal, the device controller resets its “device busy” and “data ready” indicators to OFF, which indicates that it is now ready to accept new I/O requests.
2.
Interrupt. In busy wait method, after issuing an I/O request, the CPU remains busy in checking for completion of the request until request processing is over. That is, from the time of issuing an I/O request until completion of the request, the CPU does not do anything else. This is inefficient use of CPU because I/O devices are about 1000 times slower than a CPU. Interrupt mechanism was devised to overcome this problem. In this method, after issuing the READ request to the I/O device, the CPU does not wait for the “data ready” indicator of the device to be set ON. Rather, it suspends the job that made the READ request and takes up another job that is ready for execution and starts executing this job while the device controller is busy processing the READ request. When the device controller completes processing the request and the data for transfer is ready in its local data register, it sends an interrupt signal to the CPU. When the CPU receives the interrupt signal, it knows that the data it requested is now ready. Hence, it suspends the job that it is currently executing, transfers the data from the data register of the device controller to its own register and resumes execution of the job that had made the I/O request. Interrupt mechanism is more difficult to implement than busy wait mechanism because it requires a mechanism to suspend a currently executing job and resume it later. How this is done will be discussed in a later chapter. Moreover, in case of multiple I/O devices, it also requires a mechanism to distinguish among the interrupt signals of different devices and to attend to them based on some priority rules because various devices can initiate interrupt signals independent of each other.
DMA Architecture Computer systems use I/O devices either to input or to output data. Most of the time, this involves either reading data from an input device and storing it in main memory, or reading data from main memory and outputting it to an output device. That is, data transfer takes place between main memory and I/O devices. We saw in the previous two interconnection architectures that the CPU carries out every data transfer to or from an I/O device. Hence, the CPU is involved in every data transferred between main memory and I/O devices. This leads to inefficient usage of CPU. Direct Memory Access (DMA) architecture, which minimizes CPU’s participation in I/O data transfer, overcomes this problem.
Chapter 4 ■ Computer Architecture
136
DMA architecture uses an additional hardware called DMA controller [also known as Peripheral Processing Unit (PPU)]. All I/O devices connect to the DMA controller (see Figure 4.13). A set of buses connect the CPU and memory. Another set of buses connect the DMA controller with both the CPU and memory so that the system can carry out data transfer to or from I/O devices directly either with the CPU or memory. An additional address bus connects the CPU and DMA controller. Out of the two address buses, one carries memory address and the other carries device address. Hence, the figure shows them as Memory Address Bus (MAB) and Device Address Bus (DAB). DMA controller has a data register to temporarily store the data for transfer; a memory address register to keep track of the memory location from/to where data transfer takes place; a counter to keep track of how many memory words to transfer; a “busy” indicator to indicate that it is currently busy and cannot accept any new I/O requests now; and a “data ready” indicator to indicate to the CPU that the requested data is ready in its data register for transfer. Data transfer in this case takes place in the following manner: 1.
The CPU sends the I/O request (say a data READ request) to DMA controller. It sends device address of the device from which to read data, memory location at which to store data, and the READ command along with the number of memory words to read by using the device address bus, the memory address bus, and the control bus respectively. After sending the request, the CPU continues with its normal execution since it has no further direct role in processing this I/O request.
2.
On receiving the request, the DMA controller sets its “busy” indicator to ON and stores the received memory address and number of memory words to be read in its memory address register and counter respectively.
3.
It then reads the first memory word to be transferred from the specified I/O device into its data register and sets the “data ready” indicator to ON.
4.
It now sends an interrupt signal to the CPU.
5.
On receiving the interrupt signal, the CPU completes execution of the instruction that it is currently executing, then yields the memory address and data buses to the DMA controller for one memory cycle.
6.
DMA controller then transfers the data in its data register to the memory location specified by the contents of its memory address register. It then decrements the value of its counter by one and increments the value stored in its memory address register by one.
7.
DMA controller now reads the next word from the I/O device and stores it in its data register. It then interrupts the CPU, which yields a memory cycle and the memory address and data buses to the DMA controller to enable it to transfer the word from its data register to the appropriate memory location. DMA controller then decrements its counter value and increments the value stored in its memory address register. It repeats this procedure until the DMA controller’s counter value becomes zero.
This procedure of the DMA controller taking a memory cycle from the CPU to enable it to transfer data directly to memory is known as cycle stealing. DMA architecture leads to efficient CPU utilization because the DMA controller processes an I/O request without direct participation of the CPU. However, this advantage requires provisioning of an extra hardware (DMA controller) and an additional bus (device address bus).
MULTIPROCESSOR SYSTEM ARCHITECTURES Multiprocessor systems consist of multiple processors (CPUs). They are broadly classified as shared memory systems (also called tightly-coupled systems) and distributed memory systems (also called loosely-coupled systems). They are described here.
■ Multiprocessor System Architectures
137
Memory address bus CPU
Data bus
Main memory
Control bus
Data bus
Device address bus
Control bus
DMA controller (Peripheral Processing Unit)
I/O devices Figure 4.13. Direct Memory Access (DMA) interconnection architecture.
Shared Memory Systems These systems have a single system-wide primary memory, which all processors share (see Fig. 4.14). If any processor writes, for example, value 100 to memory location x, any other processor subsequently reading from location x will get the value 100. Therefore, in these systems, any communication between processors usually takes place through the shared memory.
CPU
CPU
Systemwide shared memory
CPU
CPU
Interconnection hardware Figure 4.14. Basic architecture of a shared memory multiprocessor system.
The interconnection hardware of Figure 4.14 can be organized in several different ways. Depending on this, some commonly known architectures of shared memory systems are: 1.
Shared bus architecture. As Figure 4.15(a) shows, a system having this architecture connects multiple CPUs through a common bus to memory unit (comprised of multiple memory modules). Physically, the system uses a high-speed backplane to insert CPU and memory cards. The CPUs time-share the common bus for memory accesses. That is, only one processor can access the memory at a time (the processor that is in control of the bus at the time). This means that when one processor is communicating with the memory, all other processors are either busy with internal operations or must be idle waiting for the bus. As the number of processors increases, contention for the shared bus increases and system overhead for conflicts resolution considerably goes up, resulting in significant drop in overall system performance. Hence, this architecture does not scale beyond a few CPUs (cannot support large number of processors).
2.
Shared bus with cache memory architecture. As Figure 4.15(b) shows, a system having this architecture uses a high-speed cache memory between each CPU and the bus to reduce contention for the shared bus.
138
Chapter 4 ■ Computer Architecture Each cache memory holds the most recently accessed words by its own CPU and all memory access requests of a CPU go through its cache. If a CPU finds an accessed memory word in its cache (case of cache hit), it services the access request from the cache itself and does not make any bus request. Hence, if cache memories are large enough, cache hit rates will be high, resulting in reduced contention for the shared bus. Systemwide shared memory
Systemwide shared memory MM
MM
MM
MM
MM
MM
Shared bus P
P
P
P
(a) Shared bus architecture
cache
cache
cache
cache
P
P
P
P
(b) Shared bus with cache memory architecture
Systemwide shared memory
Systemwide shared memory MM
MM
MM
MM
MM
MM
MM
MM
P
P
P
P
P
P
P
P
(c) Multiport memory architecture
(d) Crossbar switch architecture
Systemwide shared memory MM
MM
MM
MM
2 x 2 switch P
P
P
MM
Memory Modules
P
CPUs
P
(e) Omega switching network architecture Figure 4.15.
A few examples of different architectures of shared memory multiprocessor systems depending on the type of interconnection hardware used.
Since this architecture reduces contention for the shared bus, it may first appear that it is highly scalable (can support large number of processors). However, this is not true because cache memories introduce the requirement of dealing with incoherent memory. That is, if a system using this architecture caches the same memory word in cache memories of two or more CPUs, and if one of the CPUs overwrites the word, it must invalidate the word in cache memories of all other CPUs to prevent other CPUs from getting the old value from their own caches. System overhead involved in dealing with this cache consistency issue increases with the number of caches (CPUs). Hence, this architecture also does not scale beyond a few more CPUs than the shared bus architecture.
■ Multiprocessor System Architectures 3.
139
Multiport memory architecture. As Figure 4.15(c) shows, a system having this architecture avoids contention for a single shared bus by having a separate bus between each memory module and each CPU. It uses multi-port memory modules to make this possible. The memory modules also have internal control logic to resolve when two or more CPUs simultaneously access memory words in the same memory module. This architecture enables fast memory access by multiple CPUs because of availability of multiple paths between processors and memory. However, it requires expensive memory control logic and a large number of cables and connectors. Hence, the complexity and cost of hardware grows rapidly with the increase in number of processors. Therefore, this architecture also does not scale well and is suitable only for systems with a small number of CPUs.
4.
Crossbar switch architecture. The bus-based architectures are suitable for multiprocessor systems having few tens of processors. Multiprocessor systems having few hundreds of processors use switch-based architecture instead of bus-based architecture. Figure 4.15(d) shows one such architecture, which is known as crossbar switch architecture. In this architecture, there is a connection coming out of each CPU and each memory module and the system uses an electronic switch at every intersection point of the connections between CPUs and memory modules. Each switch has control logic to establish the transfer path between a processor and memory module and to resolve simultaneous access requests from multiple CPUs to the same memory module on a predetermined priority basis. This architecture supports simultaneous memory accesses by multiple CPUs because there is a separate path between each CPU and memory module. Hence, the architecture supports good scalability.
5.
Omega switching network architecture. Notice from Figure 4.15(d) that in the crossbar switch architecture, total 16 switches are required to interconnect 4 CPUs and 4 memory modules. In general, n2 switches are required to interconnect n CPUs and n memory modules. Since the number of switches required grows exponentially with n, and because switches are costly electronic component, overall cost of a multiprocessor system using this architecture becomes prohibitive for large n. Hence, multi-stage switching network architectures have been proposed and used to build large multiprocessor systems with fewer switches. One such architecture [shown in Figure 14.5(e)] is the Omega switching network architecture. In this architecture, only 4 switches are used to interconnect 4 CPUs and 4 memory modules as compared to 16 switches required in the crossbar switch architecture of Figure 14.5(d). Each switch is a 2x2 switch – has two inputs and two outputs with the capability to establish interconnection between any input to any output and to arbitrate between conflicting requests (if there are simultaneous requests from both the inputs to connect to the same output, only one of the them will be connected and the other will be blocked). With proper setting of switches, the system can establish a path between any CPU and memory module to enable memory access operation(s) between the two. Multi-stage switching network architectures can support large number of processors and memory modules with fewer switches. However, for large systems, a path between a CPU and memory module may have several switching stages, resulting in increase in memory access time. Memory access operations being critical to overall system performance, a system usually keeps the number of switching stages low.
Distributed Memory Systems We saw that shared memory systems can have only a small number of processors due to various constraints imposed by different architectures. Distributed memory systems are more scalable and can have almost unlimited number of processors. Moreover, in contrast to shared memory systems, processors of distributed memory systems can be located far from each other to cover a wider geographical area. These systems consist of multiple computers interconnected together by a communication network. Hence, they are also known as multicomputer systems. As Figure 4.16 shows, each individual computer can itself be a multiprocessor system (a shared memory system). As interconnected computers do not share a common memory (each one has its own local memory), all physical communication between them takes place by passing messages across the communication network that interconnects them. That is, all interactions between processes running on different computers takes place by message passing. Hence, distributed memory systems are also known as message-passing systems. They use exchange of messages to transfer data and work, and to synchronize actions among processes. If any computer of a distributed memory system
Chapter 4 ■ Computer Architecture
140
is a shared-memory multiprocessor system, interactions among processes running on that computer can still take place through its shared memory. For a particular computer, its own resources are local, whereas the other computers and their resources are remote. In a distributed memory system, usually we call a computer and its resources as a node, site, or machine. Computers and communication network of Figure 4.16 can take different forms. Moreover, roles of different computers and the manner in which they interact with each other may also differ from one distributed memory system to another. Hence, there are many types of distributed memory systems. Two commonly used types are:
P1
Computer 1
Computer 2
Local
Local
P2 . . . Pi
P2 . . . Pj
P1
j≥1
i≥1 Communication network k≥1
l≥1 Local
Local ...
P1
P2 . . . Pk Computer 3
P1
P2 . . . Pl Computer n Each P indicates a CPU i, j, k, and l ≥ 1 n≥2
Figure 4.16. A distributed memory multiprocessor system (also known as multicomputer system).
1.
Cluster of Workstations (COWs). COWs use off-the-shelf components. Hence, a COW consists of several standard workstations (or personal computers) interconnected with a standard network such as InfiniBand or Gigabit Ethernet. Due to the use of off-the-shelf components (nodes and network), no special measure is taken in hardware architecture to tolerate failure of one or more communication links or nodes. However, COWs often implement fault tolerance features in software to tolerate failure of one or more nodes. Moreover, COWs sometimes use multiple networks to tolerate network failures. For example, a cluster may simultaneously have both Myrinet and Gigabit Ethernet so that if one of the networks fails, the cluster can still function with the other one. Multiple networks are also useful for different applications performance because of their varying bandwidth and latency requirements. Hence, some applications may perform better on one network than on the other network. Usually, a system based on COW neatly mounts its nodes and network(s) in one or more racks (see Figure 4.17).
2.
Blade servers. There has been a rapid growth during the last few years in usage of information technology in all types of organizations. Due to this, organizations are experiencing tremendous growth in the information that they have to handle and are looking for better options to manage their data. Organizations are facing the following challenges in scaling of traditional data centers, which are built of multiple rack-mounted servers:
■ Multiprocessor System Architectures
(a) Front view
141
(b) Back view
Figure 4.17. Off-the-shelf components (nodes and network(s)) of a small Cluster of Workstations (COW) neatly mounted in a rack. Larger clusters use higher racks to accommodate more number of nodes in a single rack. Very large clusters use many racks kept side-by-side to accommodate large number of nodes.
(a) (b) (c) (d)
Power consumption and associated cooling requirements of a data centre grows fast as it scales up. Traditional servers also need more floor space. They require complex cabling, which in turn weakens system reliability. They are more difficult to manage and require expensive, skilled support resources, leading to high total cost of ownership (TCO).
Computer vendors designed blade servers to address these challenges. Blade servers are multi-processor systems, which offer a standardized method of deploying multiple processors, memory, and I/O resources by placing them on plug-in boards that slide into a standard chassis. They reduce cost as compared to separate server systems by sharing common resources like power, storage, and I/O across multiple processors. As Figure 4.18 shows, blade servers consist of a standardized chassis in which we can insert server blades. Each server blade is a separate system that runs its own operating system and application software independently of the rest of the system, relying on its local processor(s), memory, and I/O resources to handle its applications. The chassis contains shared resources (power, cooling, storage, I/O ports, network connections, etc.) that all server blades in the chassis share. A server blade slides into the chassis and connects to the shared resources through a backplane or midplane connection. It is hot swappable, allowing live upgrades to the server resource pool. Hence, we can add processing resources to a data centre by sliding in another server blade in the chassis without interrupting the functioning of other server blades. A typical blade server chassis can hold 8 to16 physical server blades, each with 2 to 8 processor cores. With virtualization, we can create several virtual machines on each server blade, yielding a compact system with hundreds of virtual servers for a data centre. Hence, the key hardware and software components of a blade server are: (a) (b) (c) (d) (e)
Standardized chassis Server blades containing multi-core processors, memory, local storage, and chassis interface Shared storage, I/O ports, network connections, etc. Shared power and cooling Virtualization software
142
Chapter 4 ■ Computer Architecture
(f) Software for management of resources of individual server blades (g) Software for power and cooling monitoring and management As compared to multiple rack-mounted servers, blade servers offer the following advantages to data centers: (a) They provide dramatic reduction in floor space, power consumption, and cooling requirement because of integration of servers, storage, networking, I/O devices, and other components in a single, modular chassis. Due to this, we often refer to a blade server as a “data centre in a box”. (b) They help eliminate complex cabling, thereby improving overall data centre reliability. (c) They provide simplified management while retaining enterprise class performance, reliability, and availability. (d) They handle scalability with greater ease. We can add new functionality by sliding another server blade in the chassis. (e) They reduce total cost of ownership (TCO) of a data centre by enabling sharing of expensive resources, dramatically improving resource utilization, and reducing management and operation cost.
Shared I/O ports, network connections, power, and cooling units are at the back of the chassis Blade server chassis
Shared storage
Handle for lifting the chassis
. . .
Disk and controller bays ... A server blade slides into the chassis
Server blades
Light indicators for power and cooling monitoring
Figure 4.18. A blade server.
In-spite-of all its advantages, users still have the following concerns with blade servers: (a) Blade servers do not perform well in case of large-scale transaction processing applications. (b) Chassis systems are proprietary, leading to vendor lock-in problem. We need to purchase all server blades from the same vendor, which supplied the chassis. (c) Blade servers are not economical for applications requiring less than 5 to 10 servers.
PARALLEL PROCESSING SYSTEMS A parallel processing system is a multiprocessor system, which treats a large number of processors as a pool and shares them among users as needed. A special server (called run server) manages and allocates processors in the pool to different users on demand basis. When a user submits a job for computation, the run server temporarily assigns an appropriate number of processors to his/her job. For example, if the user's computation job is compilation of a
■ Points to Remember
143
program having n independently compilable segments, to produce separate relocatable object files, the system can allocate n processors from the pool to this job to compile all the n segments in parallel. When the system completes the job, it returns the allocated processors to the pool for use by other users. Parallel processing systems process a job much faster by making multiple processors process different parts of the job in parallel. For this, the job has to be programmed using parallel programming libraries such as MPI (Message Passing Interface) or PVM (Parallel Virtual Machine). Multiprocessor systems (both shared memory and distributed memory systems) are often used as parallel processing systems.
Points to Remember 1.
All computer systems perform the following five basic functions for converting raw input data into useful information – inputting, storing, processing, outputting, and controlling.
2.
Figure 4.1 shows the main components of a computer system.
3.
Input unit enables feeding of data and instructions to a computer system from outside world in computer acceptable form.
4.
Input interfaces transform input data and instructions (fed to a computer through its input devices) into binary codes that are acceptable to the computer.
5.
Output unit enables a computer system to supply information (obtained from data processing) to outside world in human acceptable (readable) form.
6.
Output interfaces transform information (obtained from data processing) from binary form to human acceptable (readable) form.
7.
Storage unit of a computer system holds data and instructions to be processed and intermediate and output results of processing. Two types of storage are – primary and secondary storage. As compared to primary storage, secondary storage is slower in operation, larger in capacity, cheaper in price, and can retain information even when computer system switches off or resets.
8.
During data processing, execution of instructions takes place in Arithmetic Logic Unit (ALU) of a computer system.
9.
Control Unit (CU) of a computer system manages and coordinates the operations of all other components of the computer system.
10. Control unit and arithmetic logic unit of a computer system together form the Central Processing Unit (CPU), which serves as the brain of the computer system and is responsible for controlling the operations of all other units of the system. 11. We often refer to a computer as a computer system because it consists of integrated components (input unit, output unit, storage unit, and CPU) that work together to perform the steps called for in a program. 12. CPU is the brain of a computer system. A computer performs all major calculations and comparisons inside its CPU. CPU is also responsible for activating and controlling the operations of other units of the computer system. Hence, no other single component of a computer determines its overall performance as much as its CPU. 13. The two basic components of a CPU are control unit and arithmetic logic unit. 14. Control Unit (CU) of a computer’s CPU acts as the central nervous system for all other components of the computer. It manages and coordinates the entire computer system including the input and output units. It obtains instructions from the program stored in main memory, interprets the instructions, and issues signals that cause other units of the system to execute them. 15. Arithmetic Logic Unit (ALU) of a computer’s CPU is the place where execution of instructions takes place during data processing. 16. Every CPU has built-in ability to execute a set of machine instructions, called its instruction set. 17. As a computer’s CPU interprets and executes instructions, there is movement of information between various units of the computer. To handle this process and to speed up the rate of information transfer, the CPU uses a number of special memory units called registers. These registers hold information on a temporary basis and are part of the CPU (not main memory).
144
Chapter 4 ■ Computer Architecture
18. The speed of a processor depends on a computer’s clock speed, which is the number of pulses produced per second by the built-in electronic clock. We measure clock speed in megahertz (MHz) or gigahertz (GHz). 19. Normally, we measure the processing speed of personal computers in MHz or GHz; that of workstations, mini computers, and mainframe systems in MIPS (Millions of Instructions Per Second) or BIPS (Billions of Instructions Per Second); and that of supercomputers in MFLOPS (Millions of Floating Point Operations Per Second), GFLOPS (gigaflops, which refers to a billion FLOPS), TFLOPS (teraflops, which refers to 10 12 FLOPS), or PFLOPS (petaflops, which refers to 1015 FLOPS). 20. Three commonly known processor architectures are CISC (Complex Instruction Set Computer), RISC (Reduced Instruction Set Computer), and EPIC (Explicitly Parallel Instruction Computing). 21. CPUs with large instruction set, variable-length instructions, and a variety of addressing modes are called CPUs based on CISC (Complex Instruction Set Computer) architecture. 22. CPUs with a small instruction set, fixed-length instructions, and reduced references to memory to retrieve operands are called CPUs based on RISC (Reduced Instruction Set Computer) architecture. 23. Explicitly Parallel Instruction Computing (EPIC) architecture uses three key techniques for improved performance. They are explicit parallelism, predication, and speculation. 24. Multicore processor technology enables building of computers with better overall system performance by handling more work in parallel. In this technology, a processor chip has multiple cooler-running, more energy-efficient processing cores instead of one increasingly powerful core. A multicore chip can run multiple programs/threads at the same time with each core handling a separate program/thread. To take advantage of multicore chips, we must redesign applications so that the processor can run them as multiple threads. 25. Considering total cost of ownership (TCO), a new measure that has become popular for measuring the performance of computing systems is power consumption to processing power ratio. Processors that reduce power consumption right at processor level are known as power-efficient processors. 26. Demand Based Switching (DBS) is a technology used in power-efficient processors for reduced power consumption. 27. Instructions and data of a program reside in a temporary storage area mainly when the CPU is executing the program. This storage space is known as primary storage, main memory, or simply memory. 28. A storage unit of a computer system is characterized and evaluated based on following properties – storage capacity, access time, cost per bit of storage, volatile, and random access. 29. Main memory of a computer system consists of several small storage areas called locations or cells. Each location can store a fixed number of bits, called word length of the memory. Each location has a unique address, which remains the same regardless of its contents. The first address starts at 0 and the highest address equals the number of words that can be stored in the memory minus 1. 30. Data and instructions move to and from memory as one or more words at a time. Therefore, even if the electronic circuits used are comparable in speed, computers having smaller word length will be slower in operation than computers having larger word length. 31. In a word-addressable computer, each numbered address location can store a fixed number of characters (equal to its wordlength in bytes). These computers allocate storage space in multiples of word-length. On the other hand, in a characteraddressable computer, each numbered address can store only a single character (A, B, 1, 2, +, -, etc.). 32. Main memory capacity of a computer is the number of bytes that its main memory can store. Normally, we measure main memory capacity in terms of kilobytes (KB), which is equal to 1024 (2 10) bytes of storage, megabytes (MB), which is equal to 1,048,576 (220) bytes of storage, or gigabytes (GB), which is equal to 1,073,741,824 (2 30) bytes of storage. 33. A computer’s main memory uses volatile RAM chips. RAM chips are of two types – dynamic and static. Dynamic RAM (DRAM), uses an external circuitry to periodically “regenerate” or refresh storage charge to retain the stored data, while static RAM (SRAM), does not need any special regenerator circuit to retain the stored data. 34. A special type of RAM, called read-only memory (ROM), is a non-volatile memory chip in which data is stored permanently. Usual programs cannot alter this data. There are two types of read-only memory (ROM) – manufacturer-programmed and user-programmed. The latter is known as Programmable Read-Only Memory (PROM) because a user can program it. 35. Once information is stored in a ROM or PROM chip, a user cannot alter it. Erasable Programmable Read-Only Memory (EPROM) chip overcomes this problem by allowing users to reprogram it to store new information. EPROM chips are
■ Questions
145
of two types – Ultra Violet EPROM (UVEPROM) and Electrically EPROM (EEPROM). EEPROM is also known as flash memory. 36. We can improve overall system performance by minimizing memory-processor speed mismatch. Two approaches used commonly for doing this are use of cache memory and memory interleaving. 37. Cache memory is an extremely fast, small memory between the CPU and main memory whose access time is closer to the processing speed of CPU. It acts as a high-speed buffer between CPU and main memory and computer systems use it to temporarily store active data and instructions during processing. 38. The connectivity channel used for interaction between the CPU and memory is known as a bus. Physically, a bus is a set of wires, which carries a group of bits in parallel and has an associated control scheme. Width of a bus is the number of parallel lines (wires) in the bus. Every computer system has following three types of buses – data bus, address bus, and control bus. 39. Every computer system also uses buses for interconnecting the CPU with I/O devices. Computer systems use the same three types of buses (data bus, address bus, and control bus) for interconnecting the CPU with I/O devices. 40. Interconnection architecture of a computer system defines the manner in which its CPU, memory, and I/O devices are connected to each other. Accessing mechanism and flow of data from one unit to another depends on interconnection architecture. The three commonly used interconnection architectures are unibus architecture, dual bus architecture, and DMA (Direct Memory Access) architecture. 41. Multiprocessor systems consist of multiple processors (CPUs). They are broadly classified as shared memory systems (also called tightly-coupled systems) and distributed memory systems (also called loosely-coupled systems). 42. A shared memory system uses a single system-wide primary memory, which all its processors share. 43. A distributed memory system consists of multiple computers interconnected together by a communication network. Hence, they are also known as multicomputer systems. Each individual computer can itself be a multiprocessor system (a shared memory system). As interconnected computers do not share a common memory (each one has its own local memory), all physical communication between them takes place by passing messages across the communication network that interconnects them. Hence, these systems are also known as message-passing systems. 44. A parallel processing system is a multiprocessor system, which treats a large number of processors as a pool and shares them among users as needed. It processes a job much faster by making multiple processors process different parts of the job in parallel.
Questions 1.
What are the five basic functions performed by a computer system?
2.
Draw a block diagram to illustrate basic organization of a computer system and explain the functions of various units.
3.
What is an input interface? How it differs from an output interface?
4.
How many types of storage a computer system normally uses? Justify the need for each storage type.
5.
Differentiate between the characteristics of primary and secondary storage of a computer system.
6.
Write the logical steps taken by a computer system along with the roles of its main units in each step while transforming input data to useful information for presentation to a user.
7.
What is a system? Why do we often refer to a computer as a computer system?
8.
No other single component of a computer determines its overall performance as much as the CPU. Discuss.
9.
Write the main functions of CPU in a computer system.
10.
What are the two main components of CPU of a computer system? Write the main functions of each component.
11.
Describe the role of decoder of a CPU.
12.
The control unit acts as a central nervous system for other components of a computer system. Discuss.
13.
What is a microprocessor?
146 14.
Chapter 4 ■ Computer Architecture What is an instruction set of a computer system? Do computers made by different manufacturers have the same or different instruction sets?
15.
Machine language programs written for one computer will not run on another computer with a different CPU. Explain why.
16.
What is a family of CPUs? When do two CPUs belong to the same family?
17.
When do we say that a computer is backward compatible with another computer? How this feature is useful for users of these computers?
18.
What are registers? Name some commonly used registers and briefly describe the function of each.
19.
What does length of a register mean? What are the commonly used register-lengths in modern computer systems? Why is a larger register length desirable?
20.
Write the main steps involved in execution of an instruction by a computer’s CPU.
21.
What is clock speed of a computer system? How does it influence the overall speed of a computer system?
22.
How is the clock speed of a computer system determined?
23.
Differentiate between the terms “clock cycle” and “clock speed”.
24.
What is a superscalar CPU architecture? How does it lead to better performance of a computer system?
25.
Generally, we measure the processing speed of personal computers in MHz (Mega Hertz), but that of workstations, mini computers, and mainframe systems in MIPS (Millions of Instructions per Second). Explain why.
26.
Why do we measure the speed of supercomputers in FLOPS?
27.
Name two important design parameters that influence the performance of a CPU. Explain how each parameter influences the speed with which a CPU can execute instructions.
28.
Write features of CISC processors.
29.
Write features of RISC processors.
30.
Differentiate between CISC and RISC processors.
31.
Write relative advantages and disadvantages of CISC and RISC processors.
32.
Explain how RISC architecture leads to the design of faster and less expensive processors as compared to CISC architecture.
33.
What is EPIC technology? What are its key features?
34.
How do EPIC processors achieve better performance than CISC or RISC processors?
35.
What is explicit parallelism? How does it help in improved processor performance?
36.
What is predication? How does it help in improved processor performance?
37.
Explain the difference between "branch prediction" and "branch predication" techniques.
38.
What is load speculation? How does it help in improved processor performance?
39.
The performance of EPIC processors comes from both hardware and software. Discuss.
40.
Imagine a computer system, which has no primary memory. Explain how this system would carry out computations. What would be the major drawback of such a system? Based on this, explain the need for primary memory in a computer system.
41.
A fast processor alone is not sufficient for good performance of a computer system. Discuss.
42.
What is a multicore processor? How does it differ from a single-core processor?
43.
Explain the limitations of current transistor technology that has forced processor manufacturers to shift from making more powerful single-core processors to multicore processors.
44.
Explain how a multicore processor can handle execution of multiple simultaneous tasks more efficiently than a single-core processor.
45.
Multicore processor technology adds to extra work for programmers. Explain why.
■ Questions 46.
147
Write advantages and current limitations of multicore processor technology as compared to single-core processor technology.
47.
Why do we refer to multicore processor technology as energy-efficient or power-aware processor technology?
48.
Why software licensing is a key issue with multicore chips?
49.
What is a power-efficient processor? Explain the use of Demand Based Switching (DBS) technology in such a processor.
50.
Write key properties used to characterize and evaluate storage units of computer systems.
51.
Explain the difference between volatile and non-volatile memory. Write an example of each type of memory.
52.
Explain the difference among random access, pseudo-random access, and sequential access storage units. Write an example of each type of storage unit.
53.
Differentiate among a bit, a byte, and a word.
54.
What is a memory address? Describe with an example.
55.
Explain the difference between an address and the content of an address.
56.
Explain the key difference between read and write operations on memory.
57.
Explain why it is advantageous to have more number of bits per word instead of having more words of shorter length in the memory architecture of a computer system.
58.
Distinguish between word-addressable and character-addressable computers. Write their relative advantages and disadvantages.
59.
How many bytes will be required to store the word ‘MEMORY’ in (a) a character-addressable computer, (b) a wordaddressable computer having word-length of 64 bits?
60.
Name some commonly used units for stating memory capacity. Roughly, how many characters does each unit represent?
61.
A computer has 512 MB of memory. How many characters can be stored in its memory at a time?
62.
Is it possible to enhance the existing memory capacity of a computer system? If no, explain why. If yes, explain how.
63.
Differentiate between static and dynamic RAMs. Which one does main memory of most computers commonly use?
64.
What is a ROM? Why it is so called? Write few typical uses of ROM.
65.
What is a micro-program? Write an example of a micro-program that is a good candidate for storage in the ROM of a computer system.
66.
Differentiate between PROM and EPROM.
67.
Differentiate between UVEPROM and EEPROM.
68.
What is a flash memory? Why it is so called?
69.
Differentiate among RAM, ROM, PROM, and EPROM.
70.
The rate at which a CPU can fetch data from memory is about ten times slower than the rate at which it can process data. Name two methods that computer systems use to minimize this speed mismatch. Write details of one of the two methods and explain how it helps in minimizing the speed mismatch.
71.
What is a cache memory? How it is different from a primary memory?
72.
Explain how cache memory helps in improving overall performance of a computer system.
73.
Effective use of cache memory requires that data and instructions needed for processing by the CPU is available beforehand in the cache. Explain how a system ensures this.
74.
What is the principle of locality of reference? How speed of execution of a program depends on it? In what manner do branch statements affect the locality of reference?
75.
Many computer systems use separate data and instruction caches. Explain why.
76.
Explain the concept of multi-level cache design in a computer system. What are its advantages and disadvantages as compared to a single-level cache design?
148
Chapter 4 ■ Computer Architecture
77.
What are the key design issues involved in the use of cache memory in a computer system? Describe the mechanisms used commonly to handle each of these issues.
78.
What is cache size? How it is normally decided?
79.
What is block size in the context of cache design? How it is normally decided?
80.
What is hit ratio? How a programmer can help in improving the hit ratio of a computer system?
81.
What is replacement policy in the context of cache design? Name a commonly used cache replacement policy.
82.
Explain how the least recently used (LRU) replacement policy leads to effective cache usage.
83.
What is write policy in the context of cache design? Describe two write policies and list their relative advantages and disadvantages.
84.
What is memory interleaving? Explain how it helps in improving the overall performance of a computer system.
85.
Explain how an interleaved memory system distributes instructions in different memory modules.
86.
Explain how cache memory and interleaved memory mechanisms can be used together to improve overall performance of a computer system.
87.
What is a bus? Why it is required in a computer system?
88.
What is bus width? How does it affect the overall performance of a computer system?
89.
What are memory buses? How many types of memory buses are there? Describe the role of each type.
90.
A wider bus enables faster exchange of data. Discuss.
91.
Why the width of memory address bus is an important parameter for a computer system?
92.
How the width of memory address bus and the number of memory locations of a computer system are co-related?
93.
What are I/O buses? How many types of I/O buses are there? Describe the role of each type.
94.
Write the similarities and differences among PCI, PIC-X, and PCIe.
95.
What is the basic architecture difference between PCI and PCIe?
96.
What is an I/O device controller? Explain its role in I/O data transfer.
97.
What is interconnection architecture of a computer system? Write three commonly used interconnection architectures.
98.
Describe how data transfer takes place in case of unibus interconnection architecture. Write the advantages and limitations of this architecture.
99.
Describe the busy wait mechanism of data transfer between the CPU and an I/O device in case of dual bus architecture. Write the advantages and limitations of this mechanism.
100. Describe the interrupt mechanism of data transfer between the CPU and an I/O device in case of dual bus architecture. Write the advantages and limitations of this mechanism. 101. Draw schematic diagram of DMA interconnection architecture. Describe how data transfer takes place in this case. Explain how this architecture minimizes CPU’s participation in I/O data transfer. 102. Write the advantages and limitations of DMA data transfer mechanism. 103. What is cycle stealing in the context of DMA architecture? 104. Differentiate between loosely-coupled and tightly-coupled multiprocessor systems. 105. Differentiate between shared memory and distributed memory multiprocessor systems. Which has better scalability and why? 106. Write about three commonly known architectures of shared memory systems. 107. What are blade servers? What advantages do they have as compared to multiple rack-mounted servers in building a data centre? 108. What is a parallel processing system? How does it run a job faster?
Secondary Storage
Primary storage of computer systems has the following limitations: 1. Limited capacity. Its storage capacity is not sufficient to store large volumes of data. 2. Volatile. It loses data stored in it in case of power off or interruption of power. To overcome these limitations, computers use additional memory called auxiliary memory or secondary storage. Secondary storage is non-volatile and has lower cost per bit stored, but operates at speeds far slower than that of primary storage. Computer systems use it to store large volumes of data on permanent basis, which they transfer partially to primary storage, whenever required for processing. This chapter deals with terminologies, principles of operation, uses, and trade-offs of different types of secondary storage devices.
CLASSIFICATION OF SECONDARY STORAGE Over the years, computer systems have used several devices and media as secondary storage. Many of them such as punched paper tape, punched cards, and floppy disks have become obsolete now. Figure 5.1 shows the popular ones used in today’s computer systems. They are broadly classified as: 1. Sequential-access devices. A sequential-access storage device is one in which arrival at a desired storage location is preceded by sequencing through other locations so that access time varies according to location. That is, we can retrieve information stored on a sequential-access device only in the same sequence in which it is stored. In case of a sequential-access device, if we need to access an address that is out of order, we can reach at it only by traversing through all the addresses stored before it. For instance, to access the 28 th data record stored on a sequential-access device, a computer must first access (although not process) the first 27 data records.
150 Chapter 5 ■ Secondary Storage
Secondary storage devices
Sequential access devices
Direct access devices
Magnetic tape
Magnetic disks
Zip disk Disk Winchester CD-ROM pack disk
Memory storage devices
Optical disks
WORM (CD-R)
CD-RW
DVD
Flash Memory RAM disk drive card
Figure 5.1. Commonly used secondary storage devices and their classification.
Sequential access devices are suitable for storing data of such applications, which need to process most (if not all) data records one after another. Preparations of monthly pay slips, monthly electricity bills, etc., are examples of such applications. These applications process data records of every employee or customer at scheduled intervals (in this case monthly). Magnetic tape is an example of a sequential-access storage device. 2. Direct/Random-access devices. A direct-access or random-access storage device is one in which we can reach and access any storage location at random, and approximately equal access time is required for accessing each location. Direct-access devices are useful for such applications, which need to access information in a more direct manner than sequential-access devices allow. For example, in a computerized bank, at any instance, we may require to determine the exact balance in a customer’s savings account. Similarly, in a computerized airline ticket booking system, we may require immediate access to reservation system records to find out, if seats are available on a particular flight. In such applications, if we use a sequential-access device for data storage, accessing desired information may take a long time, causing frustration to the customer. Magnetic disks, optical disks, solid state drive and memory storage devices are examples of direct-access storage devices.
MAGNETIC TAPES Magnetic tape is the most popular storage medium for large data sets accessed and processed sequentially. Computer systems also use it as a backup storage for data stored on on-line storage devices such as a hard disk. Magnetic tape medium is a plastic ribbon usually ½-inch or ¼-inch wide and 50 to 2400 feet long. Its surface has a coating (such as iron oxide or chromium dioxide) that can record data by magnetization. Data is recorded on the coated surface as tiny, invisible magnetized and non-magnetized spots (representing 1s and 0s). Tape ribbon itself is stored in reels, or a small cartridge, or cassette. We can erase old data and record new data on magnetic tapes. As we record new data on it, it erases old data in the same area. However, we can read stored data many times without affecting it.
■ Magnetic Tapes
151
Basic Principles of Operation Storage Organization A tape consists of vertical columns called frames, and horizontal rows called channels or tracks. Most modern magnetic tapes have 9 tracks, and use 8-bit EBCDIC code format for data recording (see Figure 5.2). The fourth track records parity bit. A parity bit or check bit enables detection of errors that may occur due to loss of a bit from a string of 8 bits during data input or output operations. In odd-parity, the check bit is 0, if the total number of 1 bits for representing a character is odd and it is 1, otherwise. On the other hand, in even parity, the check bit is 0 if the total number of 1 bits for representing a character is even and it is 0, otherwise. The tape in Figure 5.2 uses the parity bit for an odd parity. Notice that, it represents letter A by its 8-bit EBCDIC code 11000001. Track/channel numbers
A frame for each character
0 1 2 3 4 5 6 7 8 9
8’s digit
Track representation
Characters for corresponding codes
A B C
9
8 2’s digit Added zone 7
Added zone 6 Zone
5
Parity bit
4
Zone
3
Unit’s digit 4’s digit
2
Each vertical line represents a binary 1 bit
1
Figure 5.2. Data recording on a 9 track magnetic tape using 8-bit EBCDIC code format. Here the 4th track is used for an odd-parity. Hence, all frames contain an odd number of 1 bits.
A magnetic tape is a continuous medium, which records data serially. There is no addressing. In this situation, how different pieces of data (known as records) are identified on a tape? For this, it separates records by blank spaces called inter-record gaps (IRG). A tape drive creates IRGs automatically as it writes data on tape. Each IRG is about 0.5 inches. While reading data from a moving tape, a tape drive stops tape movement when it encounters an IRG. The tape remains stationary until the record processing is over, and then moves again to read the next record. This process repeats for all records in a file. Records are of varying lengths. When a tape contains a large number of short records with an IRG after each record, more than half of the tape is unused (occupied with IRGs). Moreover, tape I/O operation also becomes slow due to frequent interruption in tape movement during I/O operations. To overcome these problems, it groups records in blocks, and separates blocks by blank spaces called inter-block gaps (IBG). The process of grouping two or more records together to form a block of data is known as blocking, and the number of records grouped together in each block is called blocking factor. Blocking factor depends on record length. To reduce wastage of tape storage space, the general rule followed for deciding blocking factor is – a block should be at least ten times as long as the IBG. In addition to better utilization of tape storage space, blocking also helps in faster tape I/O operations. It permits transmission of multiple records with each “read” or “write” operation. Each tape “read” operation transmits all records of the next block from the tape to memory. Similarly, a tape “write” operation transmits an entire block of records from memory to tape drive, which then writes a block of data and an IBG on the tape.
152 Chapter 5 ■ Secondary Storage Figure 5.3 illustrates the concept of blocking. Figure 5.3(a) shows an unblocked tape (one that does not use blocking), Figure 5.3(b) shows a tape that uses a blocking factor of two, and Figure 5.3(c) shows a tape that uses a blocking factor of three. Notice how tape usage increases as blocking factor increases. However, blocking factor should not be too large because in case of data loss due to some parts of the tape becoming bad, all records in a block become irretrievable. Tape motion R1
IBG
IBG
R2
IBG
R3
IBG
R4
IBG
R5
IBG
R6
(a) An unblocked tape. There is an IBG after each record.
Tape motion IBG
R1
R2
IBG
R3
R4
IBG
R5
R6
IBG
R7
R8
IBG
(b) A tape that uses a blocking factor of two. There is an IBG after every two records.
Tape motion IBG
R1
R2
R3
IBG
R4
R5
R6
IBG
R7
R8
R9
IBG
(c) A tape that uses a blocking factor of three. There is an IBG after every three records. Figure 5.3. Concept of blocking. Tape usage increases as blocking factor increases.
A block is a group of records, and a file is a set of blocks. A single tape normally stores several files. To separate one file from another when they are stored sequentially one after another on a tape, the system adds a file header label and a file trailer label respectively at the beginning and at the end of each file. File header label identifies the beginning of a file and contains the file’s attributes such as its name (file identifier), date of creation, etc. File trailer label signifies the end of a file and contains total number of records in the file. Figure 5.4 shows data organization on a magnetic tape with file header and file trailer labels. File header label
File trailer File header label label of next file Tape motion IBG
Block of records
IBG
Block of records
IBG
IBG
a file Figure 5.4. Data organization on a magnetic tape with file header and file trailer labels.
A tape drive wounds a tape on a spool and moves it during tape I/O operations from a supply spool to a take-up spool. To allow for threading on the take-up and supply spools, the first and the last several feet of a tape are unused (not used for any type of information recording). After the unused length towards the beginning of tape, there is a reflective metal foil known as beginning of tape (BOT) marker to indicate to the tape drive the beginning of usable tape. Similarly, before the unused length towards the end of tape, there is another reflective metal foil known as end of tape (EOT) marker to indicate the end of usable tape. These two markers are on opposite edges of the tape for machine identification purposes. Between BOT marker and the first file header label is a tape header label, which contains the tape’s attributes such as its identifier, number of files it contains, date of its last use, and other control information, which help to prevent an important tape from accidental eraser. Figure 5.5 shows data organization on a magnetic tape with BOT and EOT markers, and tape header label.
■ Magnetic Tapes
153
Tape header label BOT marker
unused tape
File trailer label
File header label I B G
I B G
Blocks of records of the file separated by IBGs
I B G
Tape motion
I B G
File trailer label of last file I B G
unused tape
EOT marker
Figure 5.5. Data organization on a magnetic tape with BOT and EOT markers and tape header label.
Storage Capacity Storage capacity of a tape = Data recording density × Length Data recording density is the amount of data that can be stored on a given length of tape. We measure it in bytes per inch (bpi), or number of bytes (characters) that can be stored per linear inch of tape. Tape density varies from 800 bpi in older systems to 77,000 bpi in modern systems. Hence, if a 2400 feet tape has a data recording density of 800 bpi, its storage capacity will be 2400 × 12 inches × 800 bpi = 23 × 10 6 bytes = 23 Mega bytes. This is the total (raw) storage capacity of the tape. Its actual storage capacity (storage available for storing user data) is much less due to use of IBGs. In fact, actual storage capacity of a tape may be anywhere from 35% to 70% of its raw storage capacity depending on storage organization.
Data Transfer Rate Data transfer rate of a tape is the number of characters transmitted per second from the tape to primary storage. We measure it in bytes per second (bps). Its value depends on data recording density and speed of tape movement under read/write head. Tape speed is typically of the order of 100 inches per second. Hence, for a tape having data recording density of 77,000 bpi and its drive having tape speed of 100 inches per second, data transfer rate is 77,000 × 100 = 77,00,000 bytes or roughly 7.7 MB per second.
Tape Drive A magnetic tape drive enables storage/retrieval of data on/from tape. It has read/write heads. As tape ribbon passes under the read/write heads, it can read/write data from/to the tape by giving suitable commands (read or write) to the tape drive. We have to first load a magnetic tape reel, cartridge, or cassette on to a tape drive attached to a computer for processing. The magnetic tape is on-line once we load it on a magnetic tape drive. The computer can now use it for storage or retrieval of data. When processing is complete, we remove the tape from the tape drive for off-line storage (away from the computer system), and data on it becomes inaccessible until we load it again on the tape drive.
Tape Controller We must mount a magnetic tape on a tape drive, before we can use it for reading/writing of information. A tape controller controls a tape drive connected to it. It interprets the commands for operating the tape drive. Figure 5.6 shows a set of commands, which a tape controller typically supports.
154 Chapter 5 ■ Secondary Storage Command
Meaning
Read Write Write tape header label Erase tape Back space one block
Reads one block of data Writes one block of data Updates contents of tape header label Erases data recorded on a tape Rewinds tape to the beginning of previous block
Forward space one block Back space one file Forward space one file Rewind
Forwards tape to the beginning of next block Rewinds tape to the beginning of previous file Forwards tape to the beginning of next file Fully rewinds the tape
Unload
Releases tape drive’s grip so that tape spool can be unmounted from tape drive Figure 5.6. Set of commands typically supported by a tape controller.
Types of Magnetic Tapes Commonly used types of magnetic tapes are described here.
Half-inch Tape Reel A half-inch tape reel uses ½-inch tape ribbon stored on a tape reel (see Figure 5.7). Figure 5.8 shows its tape drive on which we have to mount the tape reel for data processing. During processing, the tape moves from a supply reel to a take-up reel via two vacuum columns and through a read/write head assembly. The read/write head assembly reads/writes data from/to the tape. It is a single unit having one read/write head for each tape track. Therefore, the read/write head assembly of a 9-track tape drive will have a set of nine heads. Each head operates independently and stores information on nine tracks parallel to the edge of the tape. This method of storing data in adjacent bit configurations is known as parallel representation. In parallel representation, a tape drive reads/writes data a byte at a time. Different tape drives use different mechanisms to prevent tape damage from sudden bursts of speed. For example, the tape drive of Figure 5.8 uses two vacuum columns to take up slack tape, acting as buffers, to prevent the tapes from snapping or stretching when starting from a stationary position or slowing down from full speed. Users commonly use magnetic tape reels as archival storage for off-line storage of data and for exchange of data and programs between organizations. Mainly organizations having large computing systems (such as mainframe systems) use them because the tape controller and tape drives of magnetic tape reels are bulky and expensive. Many mediumsized business organizations have vaults containing several thousands of tape reels. For example, a large insurance company may have a storage area containing over 50,000 magnetic tape reels. However, tape cartridge, streamer tape, and digital audio tape are fast replacing magnetic tape reels because these new magnetic tape storage devices are more compact, cheaper, and easier to handle and use.
■ Magnetic Tapes Supply reel
155
Magnetic tape Take-up reel Read/write head assembly
Figure 5.7. Magnetic tape in reel form.
Vacuum columns Tape loops varying in length Figure 5.8. Tape drive of a magnetic tape reel.
Half-inch Tape Cartridge Tape cartridges look similar to audio or video cassettes used few years ago in home music or video systems (see Figure 5.9). Their tape drives are also similar in look and mechanical operation to the audio or video players used earlier in our homes. We push/eject a tape cartridge inside/from a tape drive just as we used to push/eject a videotape cassette inside/from a video player.
Figure 5.9. Magnetic tape cartridge.
Tape cartridges provide a more convenient way to package tape. They also simplify mounting of tape on tape drive as compared to magnetic tape reels. Moreover, tape cartridges provide protection against dirt and contamination because of its sealing inside the cartridge. In addition to the differences in shape and size of tapes and mechanical operation of their tape drives, the two main differences in data organization and data recording/reading of half-inch tape reels and half-inch tape cartridges are: 1.
Half-inch tape cartridge has 36 tracks, as opposed to 9 tracks for most half-inch tape reels. Like tape reels, tape cartridges also store data using parallel representation. Hence, 4 bytes of data is stacked across the width of the tape in case of tape cartridges, as opposed to a single byte of data in case of tape reels. This enables more bytes of data to be stored on the same length of tape.
2.
Tape cartridges record/read on the top half of the tape in one direction and on the bottom half in the other direction. That is, out of 4 bytes (36 tracks) of data stacked across the width of the tape, it reads 2 bytes (18 tracks) of data in each direction. Recall that tape reels record/read data in one direction only.
Quarter-inch Streamer Tape In addition to storage of data for sequential data processing applications, one of the primary uses of magnetic tapes is to serve as backup storage medium for data stored on on-line devices such as a disk. During backup or recovery process, backup mechanisms process backup tapes continuously from beginning to end because there is no need for selective access of records. Hence, there is no need to start and stop a backup tape after every few records. Based on this idea, researchers came out with a special type of quarter-inch streamer tape, eliminating the need for start/stop operation of traditional tape drives for half-inch tape reels or tape cartridges. As there is no start/stop mechanism, streamer tape drives can read/write data more efficiently than traditional tape drives, which stop and restart tape movement at each IBG. Moreover, streamer tape drives also utilize tape storage area more efficiently. Typically,
156 Chapter 5 ■ Secondary Storage streamer tapes use 97% of tape for data storage, whereas traditional start/stop tapes use only 35% to 70%, depending on blocking factor. These tapes are ¼-inch wide and sealed in a cartridge. Their tape drives are similar in look and mechanical operation to the tape drives of half-inch tape cartridges. Unlike half-inch tapes, which use parallel representation for data recording, these tapes use serial representation for data recording. That is, they align data bits in a row, one after another in tracks. A streamer tape can have from 4 to 30 tracks, depending on tape drive. Depending on tape drive, read/write head reads/writes data from/to one, two, or four tracks at a time. Figure 5.10 shows a streamer tape having 8 tracks. It records data on two tracks at a time. That is, it first writes data on the top two tracks for entire length of the tape or until the data is exhausted. If it reaches end of the tape while recording the data, it rewinds the tape, positions the read/write head on the next two tracks, and continues recording the data in a similar manner. Streamer tapes are so called because they read/write data serially as streams of bits. They use a data format known as the QIC (Quarter Inch Cartridge) standard. Standard data format helps in easy use of these tapes for exchanging data between different computers. Recording area ends here
Recording area begins here Tracks 1
1
0
1
1
0
0
1
1 1
0
1
0...
2
0
0
1
0
1
0
0
1 1
1
0
1...
3 4 5 6
Unused portion of the tape
Unused portion of the tape
7 8 Figure 5.10. Serial representation of data on a streamer tape having 8 tracks. In this example, data is recorded two tracks at a time in a serpentine manner.
4mm Digital Audio Tape (DAT) DAT provides very high data density per inch of tape. It uses a tape ribbon of 4mm width enclosed in a cartridge. Tape length is either 60 meters or 90 meters. For very high data recording density, its tape drive uses helical scan technique for data recording. As Figure 5.11 shows, helical scan has two read heads and two write heads built into a small wheel (or cylinder) that spins near the tape at about 2000 rpm. The tape also moves at a relatively slow speed of about ⅓-inch per second. The two write heads write data with opposite magnetic polarities on overlapping areas of the tape. Each read head reads only one polarity or the other. The result is high data density per inch of tape. The helical scan records data on the tape at an angle to the tape. DAT drives use a data-recording format called Digital Data Storage (DDS), which provides three levels of error correcting code to ensure excellent data integrity. Storage capacity of DAT cartridges varies from 4 GB to 14 GB, and data transfer speed of DAT drives is about 366 KB/sec.
■ Magnetic Tapes
157
Write head B Read head B
Read head A
Spinning helical
scan Write head A Moving tape Shaft Figure 5.11. Helical scan technique used for data recording by a DAT drive.
Advantages and Limitations of Magnetic Tapes Advantages 1.
Storage capacity of a magnetic tape storage is virtually unlimited because as many tapes as required can be used for storing large data sets.
2.
With low cost of tape reels and cartridges and high data recording densities, cost per bit of storage is low for magnetic tapes. An additional cost benefit comes from the fact that we can erase data on a tape and reuse it many times. In-spite-of recent advancements in many other storage technologies, tapes remain an unrivalled storage because no other technology offers high capacity advantage at such low cost.
3.
Tape reels and cartridges are easy to handle and store because they are compact and light in weight. They enable storage of huge amount of data in a small storage space.
4.
Magnetic tape reels and cartridges are easily portable because of their compact size and lightweight.
Limitations 1.
Magnetic tapes are not suitable for storage of those data that we need to access randomly because of their sequential access nature.
2.
We must store them in dust-free environment because specks of dust can cause tape-reading errors. The environment must also have controlled temperature and humidity levels to prevent the tape ribbon from twisting due to warping.
3.
We must label magnetic tapes properly and logically to remember what data is on which tape, and to prevent erasure of useful data by mistake.
Uses of Magnetic Tapes We use magnetic tapes for the following purposes: 1.
For sequential data processing applications. For example, we often use magnetic tapes for applications in which contents of an inventory master file (permanent source of inventory data) is updated periodically (weekly, monthly, etc.) using a transaction file (containing data reflecting inventory activities during the period) to create a new inventory master file, which is used in the next processing cycle (see Figure 5.12). Backing up of data stored on an on-line storage device such as a disk, for its off-line storage. However, over time, tape will become more of an archive medium because users are increasingly using disk-based solutions to meet backup requirements.
2.
Archiving of data used occasionally. Tape continues to be the preferred medium for data archival.
158 Chapter 5 ■ Secondary Storage 3.
Transferring of data and programs between two computers not linked together.
4.
Distribution of software. Vendors often distribute software products or their updates on magnetic tapes. Inventory master file
New inventory master file Next processing
Processing
Transaction file
New inventory master file
New transaction file
Figure 5.12. Illustrating the use of tapes in a sequential application.
MAGNETIC DISKS Magnetic disks are the most popular direct-access secondary storage device. They are also the most popular on-line secondary storage device. A magnetic disk is a thin, circular plate/platter of metal or plastic. Its surfaces on both sides have a coating (such as iron oxide) that can record data by magnetization. Data is recorded on its coated surfaces as tiny, invisible magnetized and non-magnetized spots (representing 1s and 0s). It uses a standard binary code, usually 8-bit EBCDIC, for recording data. The disk itself is stored in a specially designed protective envelope, or cartridge, or several of them are stacked together in a sealed, contamination-free container. Like in the case of magnetic tapes, we can erase old data and record new data on magnetic disks as well. As we record new data on it, it erases old data in the same area. However, we can read stored data many times without affecting it.
Basic Principles of Operation Storage Organization A magnetic disk’s surface has a number of invisible, concentric circles called tracks. Tracks have numbers consecutively from outermost to innermost starting from zero (see Figure 5.13). Number of tracks varies greatly, with low-capacity disks having as few as 40 tracks and high-capacity disks having several thousands tracks. A magnetic disk’s surface also has invisible, pie-shaped segments (see Figure 5.14). Hence, if there are eight such segments, each track has eight parts. Each such part of a track is called a sector. Typically, a sector contains 512 bytes. It is the smallest unit of data access by a disk drive. That is, disk drives can access (read/write) a complete sector at a time. Even if a computer needs to change just one byte out of 512 bytes stored on a sector, it rewrites the entire sector. Notice from Figure 5.14 that the sectors on the outer tracks are longer than sectors on the inner tracks. Since each sector records the same number of characters (usually 512 characters), length of the sectors on the innermost track determines the storage capacity of the sectors on all other tracks of a disk. This means that storage space remains underutilized on the sectors on the outer tracks of the disk. This is a disadvantage of dividing a disk’s surface into pieshaped segments. To avoid this problem, some systems partition a disk’s surface in such a manner that all sectors occupy approximately the same amount of space. Figure 5.15 shows one such type of partitioning. This method first partitions tracks into groups, with tracks in different groups having different number of sectors per track. For
■ Magnetic Disks
159
example, the disk in Figure 5.15, partitions tracks into three groups. Tracks in the innermost group have 8 sectors per track, tracks in the next group have 9 sectors per track, and tracks in the outermost group have 10 sectors per track. Using this new method of disk partitioning, Apple was able to increase the storage capacity of its 3½-inch floppy disk by about 40%. 200 Tracks
Track 000 A sector
…
Track 199
…
Figure 5.13. Tracks on a disk. The outermost track is numbered 000, and the innermost track is numbered one less than the total number of tracks (199 in this example).
Figure 5.14. Sectors of a disk. The number varies, but there are often 8 or more sectors per track.
If a disk has 200 tracks and 8 sectors per track, it has 1600 (200 8) sectors with each sector having a unique number. To access a piece of data (a record) from a disk, we need to specify its disk address, which represents the physical location of the record on the disk. It is comprised of sector number, track number, and surface number (when double-sided disks are used).
Figure 5.15. A disk partitioning method in which all sectors occupy approximately the same amount of space.
Note that sector size (usually 512 bytes) is the smallest unit of data access by disk drives, and a file size (number of bytes in a file) is usually not a multiple of sector size. Hence, some part of the last sector allocated to a file is unused normally, resulting in on an average wastage of half sector for each file stored on disk. Hence, sector size for a disk should not be too large. It should not even be too small, otherwise the size of mapping table used to map disk addresses to data records will become too large. Some operating systems combine two or more sectors to form a cluster. In this case, the smallest unit of data access from a disk is a cluster, instead of a sector. That is, read/write operations read/write a whole cluster at a time. Cluster sizes vary depending on the size and type of disk, but they range from 2 to 64 sectors per cluster. Notice that a cluster-based organization may lead to more wastage of disk space as compared to sector-based organization, because on an average, it wastes half of cluster size for each file stored on disk. However, cluster-based organization leads to less management overhead for mapping of data records to physical disk locations, and faster read/write operations. Often, multiple disks are stacked together as a disk pack to create large capacity disk-storage systems (see Figure 5.16). The disk pack is sealed and mounted on a disk drive consisting of a motor to rotate the disk pack about its axis. The disk drive also has an access arms assembly having separate read/write heads for each recordable surface of the
160 Chapter 5 ■ Secondary Storage disk pack. Normally, a disk pack does not use the upper surface of the topmost disk and the lower surface of the bottommost disk because these surfaces get scratched easily. However, modern disk drives use miniaturization and precision components to eliminate this drawback. Upper surface not used Surface - 0 Surface - 1 Cylinder
Surface - 2
Read/Write head Direction of movement of access arms assembly
Surface - 3 Surface - 4 Surface - 5 Lower surface not used
Access arms assembly
Central shaft
Figure 5.16. A disk pack having 4 disk platters. The upper surface of the top plate, and the lower surface of the bottom plate are not used. Hence, altogether there are 6 usable surfaces numbered 0, 1, 2, 3, 4 and 5. As shown, a set of corresponding tracks on all 6 surfaces is called a cylinder.
All access arms (on which read/write heads are fixed) of an access arm assembly for all disk surfaces move together. Hence, if the read/write head serving the 0 th recording surface is positioned over the 5th track, each of the heads on the arms serving other recording surfaces is also positioned over the 5 th track of respective surfaces. For faster access of data, disk packs use a concept called cylinder for data organization. A set of corresponding tracks on all recording surfaces of a disk pack together form a cylinder (see Figure 5.16). For example, the 5 th track of all recording surfaces together forms the 5th cylinder of the disk pack. Hence, if there are 200 tracks on each disk surface, there are 200 cylinders in the disk pack. Obviously, disk address of a data record on a disk pack consists of sector number, cylinder number, and surface number (track number is not required because track number and cylinder number are same). This addressing scheme is called CHS (Cylinder-Head-Sector) addressing. It is also known as disk geometry. Cylinderbased organization achieves faster access of data by avoiding movement of access arms when an application has to process large number of records in sequence. It stores related records of a file on the same cylinder of a disk pack so that in one revolution of the disk pack, the disk drive can read/write all records stored on, say cylinder 5 of all surfaces.
Storage Capacity Storage capacity of a disk system = Number of recording surfaces Number of tracks per surface Number of sectors per track Number of bytes per sector Let us assume that a disk pack has 10 disk plates, 2655 tracks per plate, 125 sectors per track, and 512 bytes per sector. Since the disk pack has 10 disk plates, there are 18 recording surfaces (excluding the upper surface of the topmost disk and the lower surface of the bottommost disk). Hence, its capacity = 18 2655 125 512 = 3,05,85,60,000 bytes = 3 109 bytes (approximately) = 3 GB (3 Giga Bytes). As one character is stored per byte (using 8-bit EBCDIC encoding), the disk pack can store over 3 billion characters. Designers prefer to increase the storage capacity of a disk by increasing the number of tracks per inch of surface and bits per inch of track, rather than increasing the disk size. Hence, a constant goal of designers of disk systems is to increase the data density of each square inch of disk surface.
■ Magnetic Disks
161
Access Mechanism A disk drive records data on the tracks of a spinning disk surface and reads data from the surface by using one or more read/write heads on its access arms assembly (see Figure 5.17). Most disk drives use a single read/write head for each disk surface. However, some faster disk systems use multiple heads on each access arm to service a number of adjacent tracks simultaneously. As the access arms assembly moves in and out in the direction shown in the figure, read/write heads move horizontally across the surfaces of the disks. The disk drive positions the read/write heads on any track on/from, which it wants to write/read data. In case of a disk pack, each usable surface has its own read/write head and all heads move together. Hence, the disk drive can simultaneously access information stored on the tracks constituting a cylinder through the disk pack. Recall the cylindrical storage arrangement of information in a disk pack. Read/write heads are of flying type. They do not have direct contact with disk surfaces. There is a separation of about 0.00002 inch between a read/write head and its corresponding disk surface. This prevents wear of a disk surface. However, read/write heads fly so close to the disk surface that if a dust particle (typically of 0.0015 inch size), smoke particle (typically of 0.00025 inch size), finger print (typically of 0.00062 inch size), or a human hair (typically of 0.003 inch size) is placed on the disk surface, it would bridge the gap between read/write head and disk surface, causing the head to crash. A head crash, in which the head touches the disk, destroys the data stored in the area of the crash and can destroy a read/write head as well. One read/write head per surface
Central shaft
Direction of movement of access arms assembly
. . Disk platters .
Access arms assembly Figure 5.17. Vertical cross section of a disk system. There is one read/write head per recording surface.
Access Time Disk access time is the interval between the time a computer makes a request for transfer of data from a disk system to primary storage and the time this operation completes. To access data stored on a disk, we need to specify the disk address of the desired data in terms of surface/head number, track/cylinder number, and sector number. A disk stores information always from the beginning of a sector and reads information always from the beginning of a track. Hence, disk access time depends on the following three parameters: 1. Seek time. When a disk unit receives a read/write command, it first positions the read/write heads on the specified track (cylinder) number by moving the access arms assembly in proper direction. The time required to position the read/write heads on the specified track/cylinder is called seek time. Seek time varies depending on the position of access arms assembly when a read/write command is received. If access arms assembly is on the outermost track and the specified track is the innermost one, seek time will be maximum. It will be zero, if the access arms assembly already happens to be on the specified track. Most systems specify average seek time, which is of the order of 10 to 100 milliseconds. To reduce seek time, some disk systems use multiple read/write heads on each access arm. For example, a disk system having two sets of read/write heads for each surface (one for inner tracks, and another for outer
162 Chapter 5 ■ Secondary Storage tracks), will reduce average seek time by half. This is because each read/write head needs to cover and move across only half of the total number of tracks. 2. Latency. Once the disk drive positions the read/write heads on the specified track, it activates the head on the specified surface. Since the disk is continuously rotating, this head should wait for the specified sector to come under it. This rotational waiting time, i.e., the time required to spin the specified sector under the head is called latency. Latency, also known as rotational delay time, is a variable that depends on the distance of the specified sector from the initial position of the head on the specified track. It also depends on rotational speed of the disk that is anywhere from 300 rpm (rotations per minute) to 7200 rpm. Modern ultra fast disks can reach 10,000 to 15,000 rpm or more. Thus, disk systems specify average latency, which is of the order of 5 to 80 milliseconds. Note that average latency of a disk system is equal to half the time taken by the disk to rotate once. Hence, average latency of a disk system with rotational speed of 3600 rpm will be 0.5/3600 minutes = 8.3 milliseconds. 3. Transfer rate. Transfer rate is the rate at which a computer reads/writes data from/to a disk into memory. Once the disk drive positions the read/write head on the specified sector, it reads/writes data at a speed determined by the rotational speed of the disk. If rotational speed of a disk is 3600 rpm and the disk has 125 sectors/track with 512 bytes/sector, the amount of data transferred in one full revolution of the disk is 125 512 = 64,000 bytes = 64K bytes (approximately). Hence, transfer rate of the disk system is 64,000 3600/60 bytes/second = 38,40,000 bytes/second = 3.8 Megabytes/second (approximately). Notice that transfer rate of a disk system depends on the density of stored data and rotational speed of the disk. Since data transfer time is negligible (due to high transfer rate) as compared to seek time and latency, average access time for a disk system is its average seek time plus its average latency. Average access time varies from one type of disk system to another and is anywhere from 10 to 600 milliseconds. Since access time for a piece of data stored on a disk depends on its physical location, it is more appropriate to refer to a disk system as direct access storage device instead of random access storage device. Random access refers to a storage device in which access time of any data is independent of its physical location. For example, primary storage is random access storage. However, we do not follow this distinction strictly and refer to disk systems as random access storage devices.
Disk Formatting Magnetic disks come in different sizes. Typical disk sizes include those with 14-inch, 9-inch, 8-inch, 5¼-inch, 3½inch, and 3¼-inch diameter. Obviously, for proper match of dimensions, different size disks require different disk drives. Even for disks of same size, all disk drives are not the same because disk drives of different computers often have their own way of defining tracks, sectors, and sector size (number of bytes/sector) to match their own way of organizing data. This implies that computer manufacturers should also manufacture the disks used in their computer systems. This limitation prevents use of disks manufactured by third party vendors into one’s own computer system. To overcome this problem, earlier computer systems provided low-level disk formatting utility. It enabled a user to prepare (format) a new disk before using it with the computer system. For this, the user had to insert the raw (unformatted) disk in the disk drive of the computer system and initiate disk-formatting command. The read/write head of the disk drive then laid down a magnetic pattern on the disk’s surface, making it compatible for use with it. Modern disk drives do not require low-level formatting because modern disk controllers present a consistent view of disk geometry to the computer while hiding the internal hardware arrangement. They accept and can work with disks that are factory formatted by their manufacturers. In fact, running a generic low-level format on these disks can cause irreparable damage to drive hardware. For this reason, modern computer systems do not provide low-level disk formatting utility. To store files and data on a disk, the operating system (OS) creates a file-system on the disk. It maintains a table with the sector and track locations of data on the disk. This table, known as File Allocation Table (FAT), enables the
■ Magnetic Disks
163
computer to locate data easily. The operating system’s disk formatting command takes care of scanning and marking bad sectors, creating the FAT, and setting aside sufficient disk space for it.
Disk Drive We have to mount a magnetic disk on a disk drive for reading/writing of data from/to it. A disk drive, also known as Hard Disk Drive (HDD), contains all mechanical, electrical, and electronic components for holding one or more disks and for reading/writing of data from/to it. It contains a central shaft on which the disks are mounted, an access arms assembly, a set of read/write heads, and motors to rotate the disks and move the access arms assembly. Although, disk drives vary in their shape, size, and disk-formatting pattern, we classify them broadly into two types: 1.
Disk drives with interchangeable disks. These disk drives allow loading and use of different disks in the same disk drive at different instances, enabling off-line storage of data on disks. They provide virtually unlimited capacity to a disk system because as many disks as required can be used one after another to store large data sets.
2.
Disk drives with fixed disks. These disk drives come along with a set of permanently fixed disks. The disks, read/write heads, and access mechanisms of the disk drive are housed permanently in a sealed, contaminationfree container. Sealed packaging allows the disks to operate in a dust-free environment. This enables designers to provide increased data density (by using smaller magnetized spots) on each square inch of disk surface by reducing the distance between a read/write head and its corresponding disk surface. Hence, as compared to disk drives with interchangeable disks, these disk drives provide larger storage capacity with same size disks and same number of disk surfaces. However, the disks are not removable from their disk drives because of sealed packaging. Thus, storage capacity of these disk systems is limited.
Disk Controller A disk controller controls a disk drive connected to it. It interprets the commands for operating the disk drive. Since disk is a direct access storage device, typically, a disk controller supports only Read and Write commands. A user has to specify disk address (consisting of surface number, cylinder/track number, and sector number) as a parameter to Read and Write commands. Often, a disk controller controls more than one disk drive connected to it. In this case, the user also has to specify disk drive number as a parameter to Read and Write commands.
Types of Magnetic Disks All magnetic disks are round platters. They come in different sizes, use different types of packaging, and are made of rigid metal or flexible plastic. Their size ranges from 1 to 14-inch diameter. Commonly used sizes are 1.0 inch, 2.5 inch and 3.5 inch. The disks are packaged individually or in multiples in cartridges or contamination-free containers. Depending on the type of packaging, magnetic disks are classified into Zip/Bernoulli disks, Disk packs, and Winchester disks (see Figure 5.18). They are described here. Magnetic disks
Zip/Bernoulli disks
Disk packs
Winchester disks
Figure 5.18. Broad classification of magnetic disks.
164 Chapter 5 ■ Secondary Storage 1.
Zip/Bernoulli disk. It consists of a single hard disk platter encased in a plastic cartridge. Depending on the disk drive and size of disk, storage capacity varies from a few GB to few tens of TB. Its disk drive, called zip drive, may be of portable or fixed type. The fixed type is part of a computer system, connected to it permanently. A user can bring and connect the portable type to a computer system for the duration of use, and can then disconnect and take it away. We can load/unload a zip disk into a zip drive, just as we insert/remove a video cassette in a video cassette player.
2.
Disk pack. It consists of multiple (two or more) hard disk platters mounted on a single central shaft. All disks revolve together at the same speed. Its disk drive has a separate read/write head for each usable disk surface (recall that when a disk device uses multiple disks, the upper surface of the topmost disk and the lower surface of the bottommost disk are sometimes not used). The disk drive is of interchangeable type and allows users to load/unload different disk packs whenever required. When not in use, a user stores a disk pack offline in a plastic case (see Figure 5.19). This gives virtually unlimited storage capacity to disk packs. Depending on the disk drive, size of disk, and number of disks in a pack, storage capacity of a single disk pack varies from a few TB to few hundred TB. Lid of plastic container Plastic container
Hard disk platters Figure 5.19. A disk pack.
3.
Winchester disk. A Winchester disk consists of multiple (two or more) hard disk platters mounted on a single central shaft. However, unlike a disk pack drive, a Winchester disk drive is of fixed type. That is, its hard disk platters and disk drive are sealed together in a contamination-free container and cannot be separated from each other. Hence, Winchester disks have limited capacity. However, for the same number of disk platters of the same size, Winchester disks have larger storage capacity than disk packs due to the following reasons: (a) As the disk platters and disk drive are sealed together permanently, all surfaces of all disk platters (including the upper surface of the topmost platter and the lower surface of the bottommost platter) are used for data recording. That is, for a Winchester disk with four platters, there are eight usable surfaces as opposed to six surfaces in case of a disk pack with four platters. (b) Contamination-free environment allows Winchester disks to employ greater precision of data recording and accessing, resulting in greater data storage density than interchangeable disk packs.
Advantages and Limitations of Magnetic Disks Advantages 1.
Magnetic disks support direct access of data making them more suitable for a wider range of applications as compared to magnetic tapes, which support only sequential access of data.
2.
Due to random access property, computers often use magnetic disks as a shared device, which is shared simultaneously by multiple users. For example, computers often use Winchester disks and disk packs as online secondary storage devices storing data of multiple users of a computer system. A tape is not suitable for such type of usage due to its sequential-access property.
3.
Magnetic disks are suitable for both on-line and off-line storage of data.
■ Optical Disks
165
4.
Except for fixed type Winchester disks, storage capacity of other magnetic disks is virtually unlimited because as many disks as required can be used for storing very large data sets.
5.
Due to their low cost and high data recording densities, cost per bit of storage is low for magnetic disks. An additional cost benefit comes from the fact that we can erase data on a magnetic disk and reuse it many times.
6.
Due to their compact size and high data recording densities, they enable storage of large amount of data in a small storage space.
7.
Zip disks are portable, and users often use them to transfer data and programs between two unlinked computers.
8.
Data access rate for a magnetic disk system is much superior to a tape system.
9.
Magnetic disks are less vulnerable to data corruption due to careless handling or unfavorable temperature and humidity conditions than magnetic tapes.
Limitations 1.
Although magnetic disks are usable for both random and sequential data processing applications, their usage for sequential applications is less efficient than magnetic tapes.
2.
It is more difficult to maintain security of information stored on magnetic disks used as shared, on-line secondary storage devices as compared to information stored on magnetic tapes or on other types of magnetic disks.
3.
For Winchester disks, a disk crash or drive failure often results in loss of entire data stored on it. It is not easy to recover the lost data. Hence, data stored on Winchester disks requires suitable backup procedures.
4.
Some types of magnetic disks, such as disk packs and Winchester disks, are not portable as easily as magnetic tapes.
5.
On cost-per-bit basis, cost of magnetic disks is low, but the cost of magnetic tapes is even lower.
6.
Magnetic disks must be stored in a dust-free environment.
7.
We must label zip disks and disk packs properly and logically to remember what data is on which disk/disk pack, and to prevent erasure of useful data by mistake.
Uses of Magnetic Disks We use magnetic disks for the following purposes: 1.
For random data processing applications.
2.
As a shared, on-line secondary storage device. We often use Winchester disks and disk packs for this purpose.
3.
As a backup device for off-line data storage. We often use zip disks and disk packs for this purpose.
4.
Archiving of occasionally used data. We often use zip disks and disk packs for this purpose.
5.
Transferring of data and programs between two unlinked computers. We often use zip disks for this purpose.
OPTICAL DISKS An optical-disk storage system consists of a metallic or plastic disk coated with a reflective material. It uses laser beam technology for reading/writing of data from/to disk surface. Optical disks are also known as laser disks or optical laser disks because they use laser beam technology for data read/write. Computer systems use optical disks as
166 Chapter 5 ■ Secondary Storage a random access medium for high capacity secondary storage because of their capability to store large amount of data in a limited space.
Basic Principles of Operation Storage Organization Unlike magnetic disks having several concentric tracks [see Figure 5.20(b)], an optical disk has one long track starting at the outer edge and spiraling inward to the center [see Figure 5.20(a)]. Spiral track is ideal for reading large blocks of sequential data, such as audio or video. It, however, causes slower random access of data than in case of concentric tracks used by magnetic disks. This is because in case of concentric tracks organization, disk mechanism can locate sectors faster, as they are always located on a given track at a fixed distance from the center.
(a) Track pattern on an optical disk
(b) Track pattern on a magnetic disk
Figure 5.20. Difference in track patterns on optical and magnetic disks.
Spiral track organization divides the spiral track of an optical disk into equal-length sectors regardless of the position of a sector from the center. Recall from Figure 5.14 that the sectors on a magnetic disk are of varying length with the ones near the centre being smaller than the ones away from the center. All sectors being of equal length enables data packing at maximum density over the entire disk. However, it also requires a more complicated drive mechanism because the rotational speed of the disk must vary inversely with the radius. That is, the drive must slow down the disk’s rotation speed to read/write data from/to sectors towards the periphery of the disk, and speed it up to read/write data from/to sectors towards the center of the disk.
Storage Capacity Cost-per-bit of storage is low for optical disks because of their low cost and high storage density. They come in various sizes ranging from 12.0-inch to 3.0-inch diameter. The most popular one is of 5.25-inch diameter with capacity of about 650 Megabytes, which is equivalent to about 2,50,000 pages of printed text. Optical disks are the most suitable storage media for multimedia applications because of their large storage capacity. As optical disks have a single track, Storage capacity of an optical disk = Number of sectors Number of bytes per sector The 5.25-inch optical disk typically has 3,30,000 sectors each of 2352 bytes, making its storage capacity equal to 3,30,000 2352 = 776 106 bytes = 776 Megabytes. This is unformatted capacity. Formatted capacity is about 650 Megabytes.
Access Mechanism An optical disk drive uses laser beam technology for reading/writing of data from/to an optical disk surface. It has two laser beam sources. It uses one laser beam (of greater intensity) to write data by etching microscopic pits on the disk surface and another laser beam (of lesser intensity) to read data from the light-sensitive disk surface. To write data, it turns the laser beam ON and OFF at a varying rate due to which tiny pits (visible only through a powerful microscope) are burnt into the metal coating of disk surface along its tracks. To read data, it focuses the less-powerful laser beam on the disk surface. This beam is strongly reflected by the coated surface (known as a land) and weakly
■ Optical Disks
167
reflected by the burnt surface (known as a pit), producing patterns of ON and OFF reflections, which are converted into electronic signals of binary 1s and 0s by a sensor (see Figure 5.21). It uses a mechanical forward-backward moving read/write access arm with laser source and detection lens (called eye) assembly to place the unit at the desired place on the optical disk. This assembly and the disk do not come in contact. The disk itself is on a spindle attached to a motor that rotates the disk during access. Laser beam source
Laser beam source Prism
Prism
Sensor
Laser beam is scattered by a pit
Sensor
Laser beam is reflected by a land
Pit
Land
Pit
Land
Pit
Land
Figure 5.21. Surface of an optical disk consists of tiny lands and pits. The laser beam is strongly reflected by lands and weakly reflected (scattered) by pits, producing patterns of ON and OFF reflections, which are converted into electronic signals of binary 1s and 0s by a sensor.
Optical Disk Drive A user mounts an optical disk on an optical disk drive for reading/writing of information from/to it. An optical disk drive contains all the mechanical, electrical, and electronic components for holding an optical disk and for reading/writing of data from/to it. It contains a tray on which the disk is kept, read/write laser beams assembly, and motor to rotate the disk. Figure 5.22 shows an optical disk drive. An optical disk drive differs from a magnetic disk drive in the following manner: 1. As Figure 5.22 shows, it uses laser beam technology for data reading/writing and has no mechanical read/write access arm. 2. Magnetic disk drives use a constant angular velocity (CAV) encoding scheme, whereas optical disk drives use a constant linear velocity (CLV) encoding scheme. That is, in case of magnetic disk drives, the disk spins at a constant rate of speed and each sector occupies the area subtended by a fixed angle. On the other hand, in case of optical disk drives, the rotational speed of the disk varies inversely with the radius; the motor slows down the disk to read sectors towards the periphery of the disk and speeds it up to read sectors towards the center of the disk. Doing this requires a more complicated drive mechanism that can overcome the inertia of the disk when accelerating and the momentum when decelerating, which slows the drive’s random access time. However, this encoding scheme enables data packing at maximum density over the entire disk. Light indicator
Volume control button used when the drive is used to play an audio CD Optical disk
Headphone socket enables a user to plug-in headphones and listen to recorded sound when the drive is used to play audio CDs.
Is placed on top of this groove Direction Tray of movement eject button of the tray
Optical disk tray
Figure 5.22. An optical disk drive and disk.
168 Chapter 5 ■ Secondary Storage
Access Time Optical disk drives are slower (have larger access time) than magnetic disk drives due to the following reasons: 1.
Sectors of an optical disk are on a continuous spiral track. This data organization results in slower random access of a sector than in case of concentric tracks organization (used by magnetic disks) because in case of concentric tracks, data access mechanism always finds sectors on a given track at a fixed distance from the center.
2.
In case of optical disks, each sector has the same length regardless of its position from the center. This data organization requires a more complicated drive mechanism because disk’s rotation speed must vary inversely with the radius. That is, the drive must slow down disk’s rotation speed to read/write sectors towards the periphery of the disk and speed it up to read/write sectors towards the center of the disk. This access mechanism leads to slower data access (larger access time) as compared to magnetic disks in which case the disks rotate at a constant speed irrespective of the location of accessed data.
3.
Since optical disk is a removable media, it is prone to scratches, dust, sticky prints (including fingerprints), etc. while handling. This causes the read mechanism to employ error correction mechanisms, like re-read, varying angular velocity, data reconstruction using parity, etc.
4.
The entire read/write and disk assembly is not a sealed unit. Hence, the disk drive cannot spin the disk at fast speed as it may damage the disk and other parts.
Access time of optical disks varies in the range of 100 to 300 milliseconds. Compare this with the access time of hard disks, which varies in the range of 10 to 30 milliseconds.
Types of Optical Disks All optical disks are round platters. They come in different sizes and capacities. Commonly used types of optical disks are CD-ROM, WORM (CD-R), CD-RW, and DVD. They are described here.
CD-ROM CD-ROM stands for Compact Disk-Read-Only Memory. It is a spin-off of audio CD technology and works much like audio CDs used in music systems. In fact, if your computer has sound card and speakers, you can play audio CDs in your computer. CD-ROM disk is a shiny, silver color metal disk usually of 5¼-inch (12 cm) diameter. It is made of polycarbonate plastic having thin layer of pure aluminum coating to make its surface reflective. For some good quality disks, gold layer is used. A thin layer of lacquer on top of the reflective surface protects it. It has storage capacity of about 650 Megabytes or 700 Megabytes in newer ones. It is so called because of its large storage capacity on a compact-size disk and because it is a read-only storage medium. That is, these disks come pre-recorded and a user cannot alter the information stored on them. Since an optical disk mechanism reads a disk from the center to outer edge, several sizes are possible. Apart from the most common 120mm 74/80 minute disk having storage capacity of 650MB to 700MB, 80mm 21 minute disk (called Mini-CD) having 184MB storage is also available. They are usable with the disk drive of a 120 mm disk. Mini-CDs are available in various fancy shapes like round, oval, triangle, etc., while ensuring that the storage area is not affected. The fact that a normal user cannot write data to a CD-ROM disk does not mean that this storage medium is not useful. In fact, many applications deal with huge volumes of data that rarely change. Hence, CD-ROM is preferable for such applications. For example, infopedia is a single CD-ROM disk that holds a complete encyclopedia, or a dictionary, or a world atlas, or a dictionary of quotations, or biographies of great people, or information about all educational institutions of a particular type in a country, etc. CD-ROM disk is often used for distribution of
■ Optical Disks
169
1.
Electronic version of conference proceedings, journals, magazines, books, etc.
2.
New software products
3.
Software upgrades
4.
Multimedia applications such as video games, which involve integration of text, sound, graphics, motion video, and animation.
For wide distribution and use of CD-ROM disks on different computers, the International Standards Organization (ISO) has evolved a standard for recording information on CD-ROM disks, called ISO 9660. This standard requires creation of a Volume Table of Contents (VTOC) on a disk, which allows storage and direct access of 1,40,000 files on the disk. This enables opening of any one of the 1,40,000 files with a single seek. Microsoft Corporation has developed MSCDEX (Microsoft CD Extension) software, which records data in ISO 9660 format on CD-ROM disks. All CD-ROM vendors have accepted the ISO 9660 format.
WORM Disk/CD-Recordable (CD-R) Disks WORM stands for write-once, read-many. WORM disks allow users to create their own CD-ROM disks by using a CD-recordable (CD-R) drive attached to a computer as a regular peripheral device. WORM disks look like standard CD-ROM disks, are available blank in market, and a user can encode on it using a CD-R drive. We can use any ordinary CD-ROM drive to read information recorded on a WORM disk by a CD-R drive. As the name implies, we can write data only once on a WORM disk, but can read it many times. However, we can write all desired data on a WORM disk in multiple recording sessions. The sessions after the first one are always additive and cannot alter the etched/burned information of earlier sessions. WORM-disk-storage mechanism enables hiding of information added in a session in a subsequent session by creating File Allocation Table (FAT) at a new location, but the etchings on the surface are permanent (not removable). Such disk is called Multi-Session Disk. It uses laser beam technology (discussed earlier) for data recording/reading. Because of its permanent recording nature and large storage capacity, WORM disk is the preferred storage medium for archival applications, which now depend on microfilm, magnetic, or paper file media. Data archiving enables recording of old data on a secondary storage medium (such as a WORM disk) so that they can be deleted from the online storage device (such as a hard disk), thus freeing space for new data. WORM disk is also an ideal medium for making a permanent record of data. For example, many banks use them for storing their daily transactions. They write the transactions to a WORM disk, which becomes a permanent record for them (they can only read it whenever required, but can never alter).
CD Read/Write (CD-RW) Disk A CD Read/Write (CD-RW) disk is similar to a WORM disk with the exception that we can erase its previous content and write on it multiple times. Such disks use metallic alloy layer. Laser beam changes the chemical property during writing (or burn process), changing reflectivity at desired places. The land-pit difference on CD-RW is not significant and hence CD drives have to be compatible to read such disks. A CD-RW disk usually has a lifetime of 100 or more erase-write cycle. CD-RW drives have the capability to change the chemical property again of the metallic alloy layer when we erase old data and write new data on a CD-RW disk. CD-RW disks are little expensive than CD-R disks, but offer great cost saving because of their erase and rewrite capability.
Digital Video (or Versatile) Disk (DVD) DVD is a standard format for distribution and interchange of digital content in both computer-based and consumer electronic products. The format enables storage of various types of large multimedia data on a new and improved version of CD (Compact Disk), which works on optical disk technology. That is, both DVD and CD store data by using pits embossed on a disk surface. However, these pits are about 4½ times as dense on a DVD as on a CD, and can store about seven times more data per side. The greater density is due to a more efficient data modulation scheme
170 Chapter 5 ■ Secondary Storage and error correction method that packs more data bits into every inch. DVD follows Eight-to-Fourteen Modulation Plus (EFMPlus) encoding as compared to Eight-to-Fourteen Modulation (EFM) encoding used by CD. Physically, both DVD and CD are identical in both diameter and total thickness. There are two variants of DVD – single-layer disk and double-layer disk. Single-layer disk has storage capacity of 4.7GB, whereas double-layer disk has storage capacity of 8.5GB. A single-layer disk provides about 135 minutes of video playback at an average data rate of little less than 5 Megabits/second. DVD standard has two layers of specifications known as physical layer and logical layer. Physical layer specifications apply to the physical media itself, whereas logical layer specifications are for supporting all types of applications. The physical layer specification defines the following types of physical media: 1.
DVD-ROM. Users use it for mass distribution of pre-recorded software programs and multimedia.
2.
DVD-RAM. Users use it for general read-and-write applications in PC or consumer electronics domains.
3.
DVD-R. Users use it for low-cost, write-once recordable media.
4.
DVD-RW. It is a rewritable version of DVD-R.
On the other hand, the logical layer specification defines recording formats for video and audio data for mass distribution of pre-recorded motion pictures and music. Key features of DVD-video and DVD-audio specifications are as follows: 1.
DVD-video. DVD-video is now the most dominant movie storage format used. It allows storage of video in 4:3 or 16:9 aspect ratios in MPEG-2 video format using NTSC or PAL resolution. NTSC has resolution of 720x480 and PAL has resolution of 720x576. The audio is usually Dolby Digital (AC-3) or Digital Theater System (DTS), and can be either monaural or 5.1 Surround Sound. In addition, it has multiple selectable language soundtracks and subtitles, voice-over commentaries by the movie’s cast or director, and convenient random access to special features.
2.
DVD-audio. It offers multiple choices of sampling rate and number of bits per sample. For super-high fidelity, the music producer might opt for two channels with 24-bit sampling at a rate of 192 KHz. Contrast the basic CD standard with its two channels and 16-bit sampling at 44 KHz. The specification also supports up to six channels of multichannel surround sound with 24-bit sampling at a 48 KHz rate. Besides the improved audio features, DVD-audio specification goes beyond CD by making provision for visual menus, still images, and video to accompany the audio program.
DVD has enough space to store movie and support multi-lingual subtitles, multi-lingual audio, multiple camera angles, etc. It supports region marking to protect against piracy and use of DVD from one region to another. It also supports Content Protection for Pre-recorded Media (CPPM) security technique to safe guard against copying, etc.
Advantages and Limitations of Optical Disks Advantages 1.
Cost-per-bit of storage for optical disks is low because of their low cost and high storage density. Additional cost benefit comes from the fact that we can erase data on some optical disks and reuse them many times.
2.
Use of a single spiral track makes optical disks an ideal storage medium for reading large blocks of sequential data such as audio or video.
3.
Optical disk drives do not have any mechanical read/write heads to rub against or crash into the disk surface. This makes optical disks more reliable storage medium than magnetic tapes or magnetic disks.
4.
Optical disks have data storage life in excess of 30 years, making them better storage medium for data archiving as compared to magnetic tapes or magnetic disks.
■ Memory Storage Devices
171
5.
Since data once stored on CD-ROM/WROM disks becomes permanent, there is no danger of losing stored data by erasing it or overwriting on it by mistake.
6.
Due to their compact size and lightweight, optical disks are easy to handle, store, and port from one place to another.
7.
We can use a computer having a CD-ROM drive, a sound card, and speakers as a music system, whenever desired, for playing audio CDs.
8.
We can use a computer having a DVD drive to watch videos (such as movies) by playing DVDs.
Limitations 1.
Data access speed of optical disks is slower than that of magnetic disks.
2.
Optical disks require more complicated drive mechanism than magnetic disks due to the need to have laser generating source and detection lens, which require precision engineering and careful handling.
3.
Since optical disk is a removable media, it is prone to scratches, dust, sticky prints (including fingerprints), etc. while handling. Hence, they need careful handling.
4.
When used for off-line storage, we must label optical disks properly for easy identification.
Uses of Optical Disks We use optical disks for the following purposes: 1.
For distributing large amounts of data at low cost. For example, companies/ organizations often distribute an encyclopedia, dictionary, world atlas, dictionary of quotations, biographies of great people, information about all educational institutions of a particular type in a country, etc. on CD-ROM disks.
2.
For distribution of electronic version of conference proceedings, journals, magazines, books, product catalogs, etc.
3.
For distribution of audio such as songs.
4.
For distribution of new or upgraded versions of software products by software vendors.
5.
For storage and distribution of wide variety of multimedia applications such as video games.
6.
For archiving of data used occasionally.
7.
End-user companies often use WORM disks to make permanent storage of their own proprietary information. For example, many banks use them for making permanent record of their daily transactions.
8.
DVDs have become a popular medium for distribution of movies.
MEMORY STORAGE DEVICES With the advent of efficient manufacturing technologies, price of memory chips has fallen considerably. As a result, storage manufacturers have introduced a new breed of electronic secondary storage devices (which do not use any mechanical component for operation). They often provide applications (like music player, etc.) on top of these storage devices for better marketability. Some of the popular ones are described here.
Solid State Drive (SSD) Until recently, the secondary storage (used as non-volatile storage device for permanent storage of data) provided in laptops, personal computers, desktops, workstations and other types of computers by their manufacturers was usually
172 Chapter 5 ■ Secondary Storage a magnetic disk, also popularly known as Hard Disk Drive (HDD). The buyers of these systems only had the option to choose the capacity of secondary storage, but had no option to choose the type of secondary storage device. Today, manufacturers and buyers of these systems have the option to choose between HDD and Solid State Drive (SSD), or a combination of both in the same system. Obviously, to make this choice, we need to know the difference between the two (HDD and SSD) and their relative advantages and disadvantages.
Difference between HDD and SSD As explained earlier, an HDD consists of one or more metal platters with read/write heads mounted on access arms in an enclosure (see Figure 5.23 (a)). The access arms assembly moves the read/write heads to the desired location on the spinning metal platter(s) to read/write data on the disk(s). The disk(s) and the access arm assembly are either permanently fixed inside the system unit (in case of fixed type secondary storage device) or are packaged in a small box enclosure, which can be connected with a short cable to one of the system’s I/O port (in case of portable type secondary storage device). An SSD is a relatively new type of non-volatile storage device used as secondary storage in computer systems. It consists of interconnected flash memory chips, which are installed either on the system’s motherboard (in case of fixed type secondary storage device) or on a separate electronic card (printed circuit board) that is packaged in a small box enclosure, which can be connected with a short cable to one of the system’s I/O port (in case of portable type secondary storage device) (see Figure 5.23 (b)). The flash memory chips of an SSD are typically faster and more reliable than the flash memory chips used in USB pen drives. Hence, SSD storage is more expensive than USB pen drive storage of same capacity.
(a) Hard Disk Drive (HDD)
(b) Solid State Drive (SSD)
Figure 5.23. Inside view of HDD and SSD. When packaged in an enclosure, both may look similar from outside, but internally their design is different.
Notice that an SSD is built entirely out of semiconductors (hence the name solid state) and does not have any moving parts. On the other hand, an HDD has mechanical components like motors to spin the disk(s) and move the access arms. Due to this, as compared to HDDs, SSDs are more compact in size, quieter in operation, consume less power, have faster data access time and have higher reliability. However, currently SSDs are four to five times costlier than HDDs of same capacity. Hence, SSDs usually have much smaller storage capacity as compared to HDDs. Figure 5.24 provides a detailed comparison of HDD and SSD, with their relative advantages and disadvantages, to enable users to determine which is better for their requirement. Notice from the figure that SSD scores over HDD for most of the attributes except price and storage capacity. Over the last few years, SSD prices have dropped considerably making SSDs as viable alternative to HDDs in many systems. Today, desktop-class and laptop-class HDDs are available in 3.5-inch and 2.5-inch form factor respectively. Currently, a 3.5-inch HDD can have storage capacity as high as 10TB. On the other hand, most SSDs of today are available in 2.5-inch form factor having maximum storage capacity of 4TB. Since both HDD and SSD are available in 2.5-inch form factor, a laptop user can replace an existing HDD with an SSD to fit it into the same slot.
■ Memory Storage Devices
Sr. No.
HDD (Hard Disk Drive)
Attribute
Large. Varies from 500GB to 10TB or more Cheap. Cost per gigabyte of storage is five to six times less for HDD than SSD. Relatively slow
SSD (Solid State Drive)
Which scores more? HDD
1.
Storage capacity
2.
Price
3.
Data access speed
4.
Operating system boot time
Relatively more
5.
Power consumption
Relatively more
6.
Less
Relatively more because SSD consumes less power than HDD.
SSD
7.
Battery life (is important for battery-charged devices like laptop) Heat generation
Less
SSD
8.
Cooling requirement
Less
SSD
9.
Noise generation
Relatively more because of moving parts and more power consumption. Relatively more because of more heat generated. Mechanical movement of spinning disk(s) and access arm assembly generates noise.
No noise as there is no moving part.
SSD
10.
Vibration
No vibration as there is no moving part.
SSD
11.
Reliability
Moving parts result in some vibration. Less reliable. Mean Time Between Failure (MTBF) is about 1.5 million hours.
More reliable. MTBF is about 2.0 million hours.
SSD
12.
Safety magnetism
Required because magnets can erase data stored in HDD.
Not required because data stored in SSD is safe against magnetic effects.
SSD
13.
Ruggedness
Less safe against rough handling because of moving parts.
SSD
14.
Compact factor)
More safe against rough handling, like dropping or hitting your laptop against a hard surface, or moving your system from one place to another, while it is operating. Size can reduce substantially, making future laptops and other devices slimmer and smaller.
against
size
(Form
Size cannot reduce beyond a limit because of spinning platters and access arms assembly. Current smallest form factor is 1.8-inch.
Relatively small. Varies from 250GB to 4TB. Relatively costly
Fast. Data access speed is three to four times faster for SSD than HDD. Less. Operating system boot time is three to four times less for SSD than HDD. Less. Power consumed by SSD is two to three times less than that consumed by HDD.
173
HDD
SSD SSD
SSD
SSD
Figure 5.24. Comparison between HDD and SSD with their relative advantages and disadvantages. SSD scores over HDD for most of the attributes except storage capacity and price.
Flash Drive (Pen Drive) Flash drive is a compact device of the size of a pen. It comes in various shapes and stylish designs (such as pen shape, wallet shape, etc.), and may have different added features (such as with a camera, with a built-in MP3/WMA/FM Radio play back for music on the go, etc.). It enables easy transport of data from one computer to another.
174 Chapter 5 ■ Secondary Storage A user simply plugs it into a USB (Universal Serial Bus) port of a computer. The computer detects it automatically as a removable drive. Now the user can read, write, copy, delete, and move data from the computer’s hard disk drive to the flash drive or from the flash drive to the hard disk drive. The user can even run applications, view videos, or play MP3 files from it directly. Once done, the user simply plugs it out of the USB port of the computer. The user can carry it in pocket anywhere. A flash drive does not require any battery, cable, or software, and is compatible with most PCs, desktop, and laptop computers with USB 2.0 port. Today, it is the most preferred external data storage for mobile people to carry or transfer data from one computer to another. As the name implies, it uses flash memory storage technology discussed in earlier chapter. Recall that flash memory is non-volatile, Electrically Erasable Programmable Read Only Memory (EEPROM) chip. It is a highly durable solidstate (electronic) storage having data retention capability of more than 10 years. Available storage capacities are 1GB, 2GB, 4GB, 8GB, 16GB, 32GB, 64GB, 128GB and 256GB. Figure 5.25 shows a flash drive. It has a main body and usually a port connector cover. To plug the drive into a USB port of a computer, a user has to remove the cover or push out the port connector. Usually, the main body has a write protect tab, a read/write LED (Light Emitting Diode) indicator, and a strap hole. Some manufacturers also provide software for use with the drive. Part that plugs into a Read/write light computer’s USB port indicator Main body
Cover
Strap hole (can be used to hang it with a string)
Write protect tab Figure 5.25. A flash drive (pen drive).
Memory Card (SD/MMC) Similar to flash drive, flash memory based cards (see Figure 5.26) are available as removable storage device in different types of electronic equipment. Some of the most popular ones are Secure Digital (SD) and Multimedia Card (MMC). Some other manufacturers have their own proprietary cards based on flash memory. Storage capacity of these cards range from 8MB to 128GB. In addition to computers, various types of digital devices (such as digital camera and cell phone) use these cards. This also facilitates easy transfer of data from these devices to a computer for storage in the computer’s hard disk or for further processing by the computer. For example, a user can transfer photographs taken by a digital camera to a computer for storage or editing. Each of these cards has its own interface and specific design features for use with certain types of devices.
Write-protect lock (slides down/up to lock/ unlock write operation)
Direction notch (guides which side of the card should be inserted first in the electronic equipment)
Vendor name Storage capacity
Figure 5.26. A flash memory based card (SD/MMC). The label on it contains vendor identification, storage capacity, etc.
■ Hybrid Secondary Storage Drives
175
RAM Disk We saw that most modern computer systems use hard disks as on-line secondary storage device. However, the data access speed of a hard disk is much slower than that of a semiconductor RAM (recall that main memory uses RAM chips). This is because hard disks involve mechanical movements, whereas semiconductor RAMs are pure electronic devices. To make applications execute faster, some computer systems use a block of semiconductor RAM chips (other than the RAM chips used for main memory) as a simulated disk. That is, to execute an application, they first load it from hard disk into the simulated disk and then only they start its execution. The application executes faster than normal in this case because the method avoids most of the delay caused otherwise by frequent calls to the hard disk for data loading during execution. RAM chips used as a simulated disk are known as RAM disk, silicon disk, or pseudo disk. They either are placed on an add-on circuit board that fits inside the main computer box, or are housed in a separate cabinet. In either case, these extra RAM chips look just like a disk drive to the control programs used in the computer’s operating system. Note that RAM disk is not really a disk at all. A RAM disk helps in enhancing the speed of a computer system because its CPU can now access the desired data instantly from RAM disk instead of waiting for few milliseconds to obtain the same data from hard disk. However, RAM chips are volatile and lose data stored in them in case of power off or interruption of power. Thus, a computer system using RAM disk must store programs and data files produced during a processing session on a permanent storage device, such as a hard disk, before power off.
HYBRID SECONDARY STORAGE DRIVES Manufacturers are now providing hybrid secondary storage drives in modern computer systems to combine the advantages of both HDD and SSD. Both SSD and HDD coexist in such systems. The following two configurations are commonly used (other types of configurations may also be used): 1. Dual-drive system: In this configuration, SSD and HDD are configured as separate drives (say, C: and D: drives respectively). Drive C: is used for storage of system files such as the operating system and system applications, whereas drive D: is used for storing user files. With this configuration, users get large storage space, whereas system performance gets better because booting of the system, performing system tasks, and launching of important applications becomes faster. 2. SSD as cache and HDD as normal secondary storage: In this configuration, SSD is invisible to users and is configured as a cache to store frequently used files. All files are stored on the HDD and cache replacement policy is used to transfer any required file from HDD to SSD, if it is not found in the cache (SSD) at the time of accessing it. The SSD also stores the boot program. Hence, this configuration helps to boot the system faster as well as to quickly access most of the needed files, as they are likely to be available in the cache. In this manner, it helps to speed up the overall system performance. Several users are now also augmenting their system’s secondary storage by buying portable SSD and storing their frequently used files on it. They purchase a system with cheaper HDD having large storage capacity and then purchase a smaller capacity portable SSD and use it by plugging it to I/O port of their computer as and when required.
MASS STORAGE DEVICES Mass storage devices are storage systems having storage capacity of several trillions of bytes of data. They use multiple units of a storage media (such as multiple disks, multiple tapes, multiple CD-ROMs, etc.) as a single secondary storage device. Commonly used types of mass storage devices are disk array, automated tape library, and CD-ROM jukebox. They are described here.
176 Chapter 5 ■ Secondary Storage
Disk Array It uses a set of magnetic disks. A disk array is a set of hard disks, hard disk drives, and a controller mounted in a single unit. The unit has one or more disk drives to read/write data from/to the disks in the disk array. A large disk array has several high capacity disks; making the storage capacity of the storage unit to several terabytes or several petabytes (1 Terabyte = 1012 bytes and 1 Petabyte = 1015 bytes). A disk array is commonly known as a RAID (Redundant Array of Inexpensive Disks). The term “inexpensive” comes from the fact that each of the medium-sized hard disks in the disk array is much less expensive than a single large hard disk. Computer systems often use RAID as on-line secondary storage devices. Although, RAID systems were originally popular for providing large secondary storage capacity with enhanced performance, today they are more popular due to enhanced reliability. This chapter later explains how RAID systems provide enhanced storage capacity, performance, and reliability.
Automated Tape Library It uses a set of magnetic tapes. An automated tape library is a set of magnetic tapes, magnetic tape drives, and a controller mounted in a single unit. The unit has one or more tape drives to read/write data from/to the tapes in the tape library. In case of units having multiple tape drives, it is possible to read/write data from/to multiple magnetic tapes simultaneously, resulting in faster data transfer rates. Multiple drives also lead to improved reliability of storage unit because if one of the drives fails, the unit can continue to function with other drives at a slower speed. The unit has robotic arms to retrieve the appropriate tape from the tape library and mount it on one of the tape drives for processing. The robot returns the tape to the library automatically after processing. A large tape library can accommodate up to several hundred high capacity magnetic tapes, making the storage capacity of the storage unit to several terabytes or several petabytes. Large computer centers use automated tape libraries for data archiving and as on-line backup devices for automated data backup.
CD-ROM Jukebox It uses a set of CD-ROMs. A CD-ROM jukebox is a set of CD-ROM disks, CD-ROM drives, and a controller mounted in a single unit. The unit has one or more CD-ROM drives to read data from CD-ROM disks in the jukebox. In case of units having multiple CD-ROM drives, it is possible to read data from multiple CD-ROM disks simultaneously, resulting in faster data transfer rates. Multiple drives also lead to improved reliability of storage unit because if one of the drives fails, the unit can continue to function with other drives at a slower speed. The unit has robotic arms to retrieve the appropriate CD-ROM and mount it on one of the CD-ROM drives for processing. The robot returns the CD-ROM to the appropriate slot automatically after processing. A large CD-ROM jukebox can accommodate up to several hundred CD-ROM disks, making the storage capacity of the storage unit to several terabytes or several petabytes. Computer systems use CD-ROM jukeboxes for archiving read-only data that needs on-line access, but where access time is not critical. For example, on-line museums, on-line digital libraries, and on-line encyclopedia are a few systems that often make use of this mass storage device in an effective manner. __________________________________________________________________ We measure average access time of mass storage devices in seconds, instead of milliseconds, because a transport mechanism must first move to retrieve the storage media (such as disk, tape, or CD-ROM) on which the accessed data is stored. It requires several seconds to locate the storage media specified, and then few milliseconds to transfer the data from it to memory. However, a mass storage device has huge storage capacity and a small cost per bit stored.
■ RAID Storage
177
Relatively slow access time limits the use of mass storage devices in many applications. However, they are costeffective alternative to on-line magnetic disk storage in applications requiring huge storage capacity and in which rapid access to data is not essential.
RAID STORAGE RAID stands for Redundant Array of Inexpensive Disks. It is a disk array with special features to improve fault tolerance, offer better performance, and allow easier storage management. It presents multiple disks of a disk array as a single storage volume, which simplifies storage management. How RAID storage offers enhanced storage capacity, performance, and fault tolerance is explained here. Suppose we have a 4 Gigabyte disk, whose each track contains 128 sectors, each of 512 bytes. The system uses 4 sectors per cluster (also known as a block), so that every read/write access to the disk transfers 2 Kilobytes (512 bytes 4) of data. The disk makes one revolution in 10 ms and can transfer a block in 0.15 ms. Now, suppose a RAID system uses 4 of these disks and puts them in the same enclosure. This 4-disk RAID unit will have total storage capacity of 4 4 = 16 Gigabytes. This results in a four-fold increase in storage capacity as compared to a single disk’s storage capacity. Furthermore, this RAID unit will have blocks of 4 2 = 8 Kilobytes, which will consist of the matching blocks (the ones with the same block number) on the four disks (recall the cylinder concept of data organization when multiple disks are used in a disk pack). The access time of a block on this RAID unit will be the same as that of the individual disks, but the transfer rate will be four times since we can now deliver an 8 Kilobyte block in 0.15 ms. The transfer rate has increased from 2K/0.00015 = 13.33 Megabytes/second to 8K/0.00015 = 53.33 Megabytes/second. We can further enhance storage capacity and performance by using more number of disks in the RAID with parallel data transfer.
RAID Techniques Various RAID types use one or more of the following three fundamental RAID techniques: 1.
Mirroring. In mirroring, the system stores the same data on two disks. Mirroring improves fault tolerance capability because if one copy of the data is corrupted, the other copy is available for accessing. It also improves “read” performance because the system can process two independent read requests simultaneously. Notice that it does not improve “write” performance because the system has to write data to both disks simultaneously. Hence, it cannot process independent write requests simultaneously. The disadvantage of mirroring is that it reduces effective storage capacity of a disk array to half its total capacity, so it is expensive.
2.
Striping. In striping, the system partitions a piece of data into smaller pieces and distributes the pieces across multiple disks. Striping improves both “read” and “write” performance of single tasks, but improvement in “read” performance is less than mirroring when processing multiple tasks. Striping also enables effective storage capacity of a disk array to be equal to its total storage capacity. The disadvantage of striping is that it provides zero fault tolerance. In fact, striping leads to worse availability than just using disks without RAID because a single disk failure causes the entire RAID system to fail. Hence, systems never use striping alone. They use it together with mirroring or parity.
3.
Striping with parity. In striping with parity, the system partitions a piece of data into smaller pieces, distributes the pieces across multiple disks, generates a parity for the data, and stores the parity on one or more disks. With parity data, a system can reconstruct a RAID volume if one disk (sometimes two disks) fails. Parity generation is done either by the RAID controller hardware or by special software at driver level, OS level, or volume manager level. Although parity data uses up some storage space, it uses far less storage than mirroring for supporting fault tolerance. Hence, striping with parity is a cost-effective technique of converting a disk array into reliable large-capacity storage.
178 Chapter 5 ■ Secondary Storage
RAID Levels RAID levels define RAID types. Different RAID levels use one or more of the RAID techniques described above. Following are the commonly accepted RAID levels: 1.
RAID level 0. It uses cluster-level striping. Clusters can vary in size and are user-definable, but they are typically blocks of 64,000 bytes. The system partitions data into clusters and distributes the clusters evenly across multiple disks. Obviously, this RAID type does not provide any fault tolerance. Video editing professional sometimes use it as a temporary storage space.
2.
RAID level 1. It uses mirroring. The system copies all data on one disk to another disk. Both disks are peers (not primary and secondary). This RAID type provides fault tolerance at the cost of reducing effective storage space to half of total storage space. However, it is easy to manage and does not require significant processing time for normal operations or for recovery. Server computers often use it for storing system partition for enhanced reliability.
3.
RAID level 2. It uses bit-level data striping with parity. The system distributes the bits evenly across multiple disks and uses one of the disks to store parity. Hence, it reduces the effective storage capacity of a disk array by one disk. That is, if a system configures a disk array having “N” disks with RAID Level 2, its effective storage capacity is equal to the sum of storage capacity of “N-1” disks. This RAID type is rarely used.
4.
RAID level 3. It uses byte-level data striping with parity. The system partitions data into bytes, distributes them evenly across multiple disks, and uses one of the disks to store parity. It reduces the effective storage capacity of a disk array by one disk. This RAID type is rarely used.
5.
RAID level 4. It uses cluster-level data striping with parity. Clusters can vary in size and are user-definable, but they are typically blocks of 64,000 bytes. The system partitions data into clusters, distributes the clusters evenly across multiple disks, and uses one of the disks to store parity. It reduces the effective storage capacity of a disk array by one disk. This RAID type is rarely used.
6.
RAID level 5. It uses cluster-level data striping with distributed parity for enhanced performance. The system partitions data into clusters and distributes the clusters and parity evenly across multiple disks. This provides better performance than using a single disk for parity. It reduces the effective storage capacity of a disk array by one disk. This RAID type is the most common implementation of data striping with parity.
7.
RAID level 6. It uses cluster-level data striping with dual distributed parity for enhanced fault tolerance. It is similar to RAID Level 5, except that it uses the equivalent of two disks to store two sets of parities for all data. Hence, it reduces the effective storage capacity of a disk array by two disks. It also degrades performance as compared to RAID Level 5 because it calculates each set of parities separately. Computer systems use this RAID type for building highly reliable storage systems. Dual parity allows any two disks to fail without data loss. Notice that if a disk fails in a disk array configured as RAID Level 5, we can restore the disk array to a healthy state in a few hours, provided a second failure does not occur during recovery time. RAID Level 6 can restore the disk array to healthy state even if a second failure occurs during recovery time. Hence, storage industry considers it the ultimate RAID level for fault tolerance.
8.
RAID level 10 (composite of 1 and 0). It is also called RAID Level 1 + 0. It uses mirroring and striping, with mirroring done before striping. This provides better fault tolerance because it can survive multiple disk failures, and performance does not degrade as much when a single disk fails. RAID Level 0 + 1, which does striping before mirroring, is an inferior RAID type. RAID Level 10 is the most common composite RAID type. Database applications commonly use it because it provides good I/O performance when the application cannot distribute its own data across multiple storage volumes.
■ Storage Interfaces
179
With such a wide range of reliability features inbuilt in them, modern RAID systems have mean time between data loss (MTBL) of well over a million hours. Hence, system designers often consider them as a basic unit for building reliable computer systems. For organizations relying on information technology to run their business, unscheduled interruptions have serious consequences. As a result, today many organizations are turning to RAID as a safer way to store large amounts of data.
STORAGE INTERFACES A secondary storage device needs a controller, which acts as an intermediary between the device and a computer system. In some computers, the controller is an integral part of the computer’s main motherboard. In others, it is an expansion board, which connects to the system bus by plugging into one of the expansion slots of the computer. To enable use of devices manufactured by independent vendors with computers manufactured by different vendors, controllers follow some drive interfacing standards. Storage devices use industry standard storage interfaces to connect to a system’s motherboard. Some industry standard storage interfaces that were used in older computer systems and that have now become obsolete are IDE (Integrated Drive Electronics), ESDI (Enhanced Small Device Interface), IPI-3 (Intelligent Peripheral Interface-level 3), and ATA (Advanced Technology Attachment). Industry standard storage interfaces used currently in computer systems are SCSI, SAS, SATA, FC, FireWire, and iSCSI. They are described here.
SCSI (Small Computer System Interface) In earlier storage interfaces, each type of device required a separate device controller for connecting devices of that type to a computer system. Researchers introduced SCSI (pronounced “scuzzy”) interface as an interesting variation of the separate device controller for each device type idea. It uses a generic device controller (called SCSI controller) on a computer system and allows any device having SCSI interface to connect directly to the SCSI bus of the SCSI controller. SCSI interface of a device contains all circuitry that the device needs to operate with the computer system. As Figure 5.27 shows, a SCSI controller connects to a computer bus on one side and controls another bus (called SCSI bus) on the other side. Either the controller is a card that we can plug into an available slot, or it is inbuilt into the computer’s motherboard. The SCSI bus is a bus designed for connecting devices to a computer in a uniform way. In effect, the SCSI controller extends the computer’s bus outside the main system box and enables connection of other devices to the computer with the help of this extended bus. There is a standard protocol for sending device control messages and data on the SCSI bus. Each device connected to the SCSI bus has a standard SCSI interface (a single chip) that knows about the bus protocol. Since SCSI extends a computer’s bus outside the main computer box, it enables connection of any SCSI device to the computer without the need to open the computer box and install a device controller on its internal bus. A SCSI being an extension of a computer’s bus, it can accommodate multiple devices simultaneously. Most current implementations of SCSI limit the number of devices to sixteen on a given chain due to bus capacity limitation. Moreover, SCSI being an extension of the bus, any type of device (hard-disk drive, optical-disk drive, tape drive, printer, plotter, scanner, etc.) can be linked (or daisy-chained) on a single SCSI port (see Figure 5.27). Each SCSI device on a SCSI bus must have a unique identifier (ID) so that the controller can send an I/O request to the appropriate device. If a SCSI bus supports sixteen devices, their IDs range from zero to 15. The SCSI controller itself uses one of the IDs, typically the highest one, leaving room for 15 other devices on the bus. Introduced in 1981, there have been eight generations of SCSI namely, SCSI-1, SCSI-2, SCSI-3, Ultra SCSI, Ultra2 SCSI, Ultra160 SCSI, Ultra320 SCSI, and Ultra640 SCSI. All these SCSI standards are backward compatible. This has provided SCSI users the utmost in flexibility and has helped to maintain SCSI’s affordability. Ultra640 SCSI, the current SCSI, is the most robust and powerful I/O enterprise solution available today. It allows data to transfer safely and reliably at 640 Mbytes/second.
180 Chapter 5 ■ Secondary Storage
Processor
Memory Main computer system
Computer bus SCSI controller
SCSI buses
SCSI interface
SCSI interface
SCSI interface
Disk drive
Disk drive
CD-ROM drive
…
SCSI devices connected to the computer Figure 5.27. SCSI drive interface standard.
As the name implies, SCSI started out as a standard interface for connecting devices to small computer systems. However, the advantages of a common interface soon became apparent in all sizes of systems. Today, SCSI commands a substantial presence in both the desktop and server systems.
SAS (Serial Attached SCSI) All SCSI types mentioned above are Parallel SCSI in the sense that bits of data move through the bus simultaneously rather than one bit at a time. Hence, a Parallel SCSI using a bus of 16 bits width moves 16 bits simultaneously through the bus. The newest type of SCSI, called Serial Attached SCSI (in short, SAS), uses SCSI commands, but transmits data serially (one bit at a time). SAS uses a point-to-point serial connection to transfer data at 3.0 Gigabits/sec. Each SAS port can support up to 128 devices or expanders. With expanders, SAS allows maximum possible connectivity of up to 16,384 devices. SAS is therefore the logical choice, if scalability is a key requirement. With SAS expanders (which are like high-speed switches) within the enclosures, each device has a dedicated connection to the computer making arbitration unnecessary and resulting in improved performance. SAS dual porting capability and the ability to support I/O requests from more than one controller at a time (multi-initiator) also enables the design of dynamic load balancing systems and redundant path configurations to further increase performance and reliability. In addition to being software compatible with Parallel SCSI (using same command set), SAS is interoperable with Serial ATA (SATA – described later in this section) as well. As a result, an organization can choose a mix of both SAS and SATA hard disk drives for an optimal mix of high-performance (SCSI-based) and low cost (SATA-based) per GB storage required to meet its unique storage requirements. Compatibility with Parallel SCSI is valuable because it protects an organization’s investments in SCSI. Recall that SCSI technology is more than 30 years old and most servers and many storage applications across the industry use it. Due to its small form factor (compactness), SAS also addresses the issues of space, heat, and power consumption, which are important in today’s data centers. Its backplane, enclosures, and cabling are less cumbersome, connectors are smaller, and cables are thinner and easier to route. Hence, enterprise users who need more power, easier connectivity, greater scalability, and greater flexibility in building storage solutions also prefer to use SAS.
■ Storage Interfaces
181
SATA (Serial Advanced Technology Attachment) Serial Advanced Technology Attachment (SATA) interface is the latest technology of Advanced Technology Attachment (ATA) interface standard. It is fast replacing the older Parallel ATA (PATA) interface, which has been the most popular disk drive interface for PC platforms. PATA’s popularity was due to its relatively low cost and simplicity. However, it was a storage-centric bus with limited scope. It allowed only two storage devices inside the box on an 18-inch cable, with a simple protocol and a single host. Researchers introduced SATA to take care of these limitations of PATA. It is primarily for desktop and mobile computing systems. Key features of SATA are: 1.
It is fully compatible with PATA.
2.
It supports data transfer rates of 150 Mbps, 300 Mbps, and 600 Mbps.
3.
It provides hot-pluggable support.
4.
It can support cable lengths up to 1 meter.
5.
It provides improved reliability with 32-bit cyclic redundancy checking for all transfers and improved signalintegrity.
6.
It is a point-to-point interconnection architecture, which provides a direct path and dedicated bandwidth between the disk drive and the host system.
7.
It uses Advanced Host Controller Interface (AHCI) and Native Command Queuing (NCQ) to achieve a much higher throughput than older PATA devices.
8.
It uses differential signaling, making it easy to filter out noise from data signal at receiving end.
Due to its features listed above, SATA is fast replacing PATA disk drives not only in desktop systems, but also in other types of systems such as personal video recorders, electronic gaming, and some network storage applications. It is also finding acceptance in enterprise datacenters.
Fiber Channel (FC) The conventional method of connecting a storage device to a computer system is to attach the storage device directly to the computer using a storage interface. This methodology is known as Direct Attached Storage (DAS) (more details of DAS are given in the next section). Although, the method is simple, easy, and cost-effective, its main limitations are that it enables attachment of only a few devices and the distance between the controller and storage cannot exceed a few meters. Few years ago, researchers introduced a new paradigm in storage, called Storage Area Network (SAN) to overcome these limitations (more details of SAN are in the next section). Fiber Channel (FC) is an enabling technology for SAN. It has become the de-facto standard interconnect for SAN environment. Existing FC topologies describe port interconnect. A port is usually an active communication entity, such as a disk, a communication switch, or a server. Popular FC interconnection techniques are: 1.
Point-to-Point (FC-P2P). It provides direct connection between two ports. It is the simplest topology, but is limited in scope of use.
2.
Arbitrated Loop (FC-AL). It enables connection of multiple ports in a loop or ring topology. It functions in a way similar to that of a Token-ring topology, and has similar benefits and drawbacks.
3.
Switched Fabric (FC-SW). In this topology, all devices or loops of devices are connected to Fibre Channel switches. Conceptually, this is similar to Ethernet. The switches manage the state of the fabric, providing optimized interconnections on fixed or dynamic possibilities/links.
FC has several features for addressing enterprise storage requirements. These include:
182 Chapter 5 ■ Secondary Storage
1.
It operates as a SCSI protocol superset, but also lets other higher-level protocols access the underlying transport layer, including TCP/IP over FC.
2.
It allows connection of 126 devices per controller, and can address more than 16 million devices in a FC SAN.
3.
It works with both copper twisted-pair cables and optical fiber cables. This dual capability allows use of inexpensive copper connections for intra-cabinet, intra-chassis, and short distances, and use of expensive optical connections for longer distances.
4.
Depending on the medium (copper or optical fiber cables) and interconnection topology used, speed of FC varies from 1 Gigabits/sec to 128 Gigabits/sec.
5.
It allows copper connections to several meters, basic optical connections to 500 meters, and long-wave connections to 10 kilometers. Some implementations allow direct connections to distances of 80 kilometers. The distances allow direct data center connections via native fiber channels. With TCP/IP protocol, FC allows long distance connections worldwide, enabling interconnection of islands of SANs anywhere across the globe.
6.
It allows hot plugging of devices, enabling expansion of a storage solution without any disruption to an operational system.
FireWire FireWire, also called IEEE 1394 standard, is primarily a mass-storage interface. Today, several other products use FireWire interface, including consumer products such as cameras, scanners, printers, televisions, game consoles, and many kinds of storage devices. Apple’s iPod MP3 player uses FireWire as its only data and power connection. FireWire is a high performance serial bus that can support packet transfer at 100, 200, 400, and 800 Megabits/sec simultaneously. A single bus can freely mix up to 63 devices supporting any or all of these speeds. The bus transports each packet at the best possible speed depending on the source and destination nodes. Hence, adding a digital video camera capable of only a 100 Megabits/sec rate need not force computers and storage devices to slow down from a 800 Megabits/sec rate they commonly support. Like a bus, it makes all packets visible to all nodes, with the exception that it cannot send higher-speed packets to or through lower-speed devices. FireWire uses isochronous transport to provide real-time data delivery, reserving up to 80% of the bus bandwidth for one or more isochronous channels. The hardware then provides guaranteed transmit opportunities at defined intervals to ensure timely data transfer. FireWire uses Open Host Controller Interface (OHCI) specification, which can service read and write requests by Direct Memory Access (DMA) to the specified memory address, without assistance from the local CPU or any interrupts. With this architecture, each storage device added to a FireWire bus acts as an additional DMA controller and can move data to or from host memory at its own pace. FireWire devices can be connected or disconnected from the bus at any time. Self-describing and self-configuring, these devices free users from having to adjust ID selectors or assign drivers to devices individually. FireWire provides an easy and quick way to add, upgrade, and share computer storage devices. High-performance RAID applications have also adopted FireWire. FireWire is particularly useful for connecting multiple devices. Using peer-to-peer access, the devices can interoperate as peers to deliver greater capability as a system than each device can offer alone.
■ Storage Interfaces
183
iSCSI (Internet SCSI) Internet SCSI (iSCSI, for short) is an enhancement of SCSI technology for use in Storage Area Network (SAN) environment. iSCSI is a device control and communication protocol, which uses SCSI commands over commonly available network fabric like Ethernet or Internet. This approach has several advantages: 1.
It uses SCSI commands set, enabling all existing SCSI devices to work in an iSCSI environment.
2.
Use of SCSI devices and protocol allows high data throughput with direct device control.
3.
It allows use of SCSI commands over a common TCP/IP network, enabling device connectivity over long distances.
4.
It allows a datacenter to consolidate its storage at identified locations while the devices that use them can be at other locations, enabling better and efficient planning and resource utilization.
5.
It allows easy handling of failures by simply changing network address of the failed device in user systems to point to another device.
iSCSI defines the following active components that together form an iSCSI environment: 1.
Initiator. It is an iSCSI client. It may be either a hardware, software, or hardware/software combination connected with or stored on a computer system. It communicates with a remote iSCSI device over the network by generating, encrypting, and transmitting iSCSI packets to the remote device. It also receives, decodes, and acts upon the responses sent from the remote iSCSI device. It also synchronizes multiple requests made to the remote iSCSI device.
2.
Target. It is an iSCSI enabled storage array or iSCSI service on a storage server. It services the requests made by the initiators over the network.
3.
Network. It forms communication channels between initiators and targets. It can be any high-speed network such as TCP/IP Gigabit Ethernet. Initiators and targets are addressed using iSCSI Qualified Name (IQN).
FC versus iSCSI Both FC and iSCSI technologies are targeted towards SAN environments. They have many features in common. Both use an underlying high-speed serial transport to carry SCSI read and write commands to storage devices. They both have a variable network address identity and a fixed unique World Wide Name (WWN) address. They both also rely on flow-control mechanisms to maintain stable conversations between pairs of communicating devices across the network. However, the two differ in the following manner: 1.
While FC uses a dedicated gigabit network infrastructure separated from the corporate LAN, iSCSI uses mainstream Gigabit Ethernet switching, enabling tight integration with corporate LAN. By integrating storage data into mainstream data communications infrastructures, iSCSI makes shared storage access ubiquitous.
2.
An iSCSI SAN differs from a FC SAN primarily in plumbing that supports SCSI exchanges. FC provides Layer 2 architecture, analogous to bridged LANs before the introduction of TCP/IP. On the other hand, iSCSI relies on Layer 3 architecture that includes network routing. Once the system puts SCSI commands, status, and data in an IP format, storage data can use conventional equipment to traverse any common IP network
184 Chapter 5 ■ Secondary Storage infrastructure, including Gigabit Ethernet, ATM (Asynchronous Transfer Mode), SONET (Synchronous Optical NETwork), or Frame Relay.
DAS, SAN, AND NAS In earlier computer systems, the only way to connect a storage device to a computer’s processor was to attach it directly to its system board by a cable. However, with advancements in networking and storage technologies, today there are multiple options available for processor-to-storage connectivity. Newer options offer significant capabilities and flexibilities that were not available earlier. As, different options have their own tradeoffs in cost, ease-ofmanagement, performance, and distance, understanding these technologies is important for making right choices while planning a storage solution. Three commonly used processor-to-storage connectivity technologies are DAS, SAN, and NAS. They are described here.
DAS (Direct Attached Storage) This is the conventional method of processor-to-storage connectivity. In this method, a connector cable directly attaches a storage system board/card to the processor system board/card (see Figure 5.28). The simplest example of DAS is a disk drive inside a PC, or a tape drive attached to a single server. Some disk systems allow partitioning of total capacity of disk storage into multiple logical disks and assignment of different partitions to different processors. Further, the sub-system may allow mutual reassignment of partitions from one processor to another. This is essentially still a DAS. …
Personal Computer (PC)
Client systems LAN – Local Area Network S1, S2 – File servers D1, D2 – Storages
LAN
S1
S2
File server S1 can access data only on storage D1 File server S2 can access data only on storage D2
D1 (a) A storage attached to a PC directly.
D1
D2
Clients can access data on storage D1 or D2 through their respective file servers
(b) Storages attached to file servers. Clients access them over the LAN through their respective file servers. Figure 5.28. Direct Attached Storage (DAS).
DAS is the best choice for connecting a storage device to an individual, isolated processor such as a laptop, desktop PC, or a single small server. These systems connect disk storage (usually residing inside the processor enclosure) to the processor using DAS technology. DAS has low initial cost and is simple to implement. It however, does not allow sharing of storage with processors (see Figure 5.28). Hence, when an organization has multiple computers, DAS may initially appear to be low cost from the point of view of each user or department. However, from the point of view of the entire organization, the total cost of ownership (TCO) of DAS may turn out to be higher than other processor-to-storage connectivity approaches because DAS does not allow sharing of unused capacity of storages with other processors and it also lacks a central point of management for multiple storages.
■ DAS, SAN and NAS
185
SAN (Storage Area Network) In this method, one or more processors and one or more storage devices are interconnected, forming a dedicated network called SAN. SAN technology provides any-to-any connection for processors and storage devices on the same SAN (see Figure 5.29). As compared to DAS, SAN has the following advantages: 1.
It provides scalability, enabling dynamic configuration of a large number of processors and storage devices in any-to-any connection model.
2.
It enables storage sharing among multiple processors. Essentially, it makes multiple storage devices on the same SAN to appear as a single pool of storage devices accessible to all processors on the SAN.
3.
It enables easy-manageability by allowing management of all storage devices on a SAN from a single point of control.
4.
It enables building of reliable storage solutions, which can tolerate failures of both processors and storage devices.
However, as compared to DAS, SAN has the following limitations: 1.
Initial cost of implementation is higher for SAN than DAS.
2.
A SAN requires specialized hardware and software to manage the SAN and provide many of its potential benefits.
3.
Processors cannot access storage devices on another SAN, which is part of the same system (see Figure 5.29). …
…
LAN
LAN
S1
S1
S2 SAN
D1
D2
(a) A storage solution with one SAN.
LAN – Local Area Network SAN – Storage Area Network
S2 SAN-1
D1
D2
S3
S4 SAN-2
D3
D4
(b) A storage solution with two SANs.
Si – File servers Di – Storages
File server S1 can access data on storage D1 or D2, but not on D3 and D4 File server S2 can access data on storage D1 or D2, but not on D3 and D4 File server S3 can access data on storage D3 or D4, but not on D1 and D2 File server S4 can access data on storage D3 or D4, but not on D1 and D2 Figure 5.29. Storage Area Network (SAN).
186 Chapter 5 ■ Secondary Storage
NAS (Network Attached Storage) A NAS is a storage device (also called NAS appliance) consisting of its own processor and disk storage. It can be attached to a TCP/IP-based network (LAN or WAN), and accessed using specialized file access or file sharing protocols. When a NAS appliance receives a file access request over the network, its processor translates the request to device access request for accessing the appropriate device. In this manner, a NAS system enables any file server to access data on any NAS appliance on the same network (see Figure 5.30). It, therefore, allows file sharing using lowcost, Ethernet-based networks. While NAS and SAN seem similar because they both deal with storage networking, they differ as follows: 1.
SAN is a dedicated storage network, whereas NAS is a device on a conventional LAN/WAN network.
2.
SAN uses block-level I/O, whereas NAS does file-level I/O.
3.
Initial cost to implement a SAN is higher than that for NAS.
4.
NAS is easier to install and manage than a SAN.
5.
SAN has better scalability than NAS. This is because NAS allows storage capacity pooling within a NAS appliance only, and there is little if any sharing of resources across multiple appliances. In contrast, SAN allows pooling of all devices on a SAN (multiple disk and tape devices) as a large shared resource with a single point of control and specialized backup facilities. Hence, as total storage capacity grows, a SAN-based storage solution becomes easier to manage and most cost effective at some point.
6.
In a SAN-based storage solution, SAN and LAN are independent networks and file servers can share storage only on the same SAN (see Figure 5.29), whereas in a NAS-based storage solution, there is only one network and file servers can share all NAS appliances on the network (see Figure 5.30).
… LAN – Local Area Network Si – File servers Ni – NAS devices N4
N1
LAN
N2
N3
File server S1 can access data from any of the NAS devices N1, N2, N3, or N4 File server S2 can access data from any of the NAS devices N1, N2, N3, or N4
S1
S2 Figure 5.30. Network Attached Storage (NAS).
As there are tradeoffs, choice of DAS, SAN, or NAS for building an organization’s storage solution depends on the environment. No solution is best for all types of environments. In fact, for large organizations, a mix of DAS, SAN, and NAS may be used.
■ Data Backup
187
DATA BACKUP What is Data Backup? Data backup is the process of creating a copy of some data from an on-line storage device to a secondary storage backup device such as, magnetic tape, zip disk, disk pack, or WORM disk. Usually, the copy serves the purpose of off-line storage of the data so that if the system somehow loses original data from on-line storage device, it can retrieve it from the backup device and restore it on on-line storage device.
Why Backup Data? Taking periodic backup of on-line data and keeping them on off-line secondary storage devices is important because a system can lose data from its on-line storage device (such as a hard disk) in any one or more of the following ways: 1.
A disk crash.
2.
A virus attack.
3.
A hardware malfunction.
4.
An unintended accidental deletion of useful files.
5.
A natural disaster like fire or earthquake damaging the computer system.
The amount of time it would take us to re-create all our files, if we lost them, is far greater than the few minutes it takes to back them up. Hence, protecting valuable data with regular backups is necessary for all computer users.
Types of Backup Two basic types of backup are: 1. Full backup. This method copies all data from an on-line secondary storage device to a backup device at the time of backup. The backup procedure creates a copy of every file from the on-line secondary storage device on to the backup device, irrespective of whether the file was changed or not. Depending on the volume of data being backed up and backup device used, full backups can take anywhere from a few minutes to a few hours to complete. 2. Incremental backup. Backup is an expensive process both in terms of time taken to complete and storage space occupied on backup device. To save on backup time and storage space, one can often go for incremental backup instead of full backup. This method copies only newly created files or files changed since the last backup from on-line secondary storage device to the backup device at the time of backup. As every file stored in a system contains the date and time of its creation and last update, incremental backup utility software compares these dates to those in its log for deciding which are the newly created and modified files since the last backup. Incremental backup is faster than full backup because it creates a copy of only the new and modified files since the last backup. This may result in copying of only a few files out of hundreds of files stored on the on-line secondary storage device. However, full backup is simpler and a little safer for restoring data back from the backup device on to the on-line secondary storage device. If we want to restore a particular file that was lost, we need to search for it at only one place in case of full backup, whereas in case of incremental backup, we need to search for it on several backups tracing back from the most recent backup until we find the file. Similarly, in the
188 Chapter 5 ■ Secondary Storage event of a disk crash, when all the files are lost, it is easy to restore the files back on to a new disk with full backup as compared to incremental backup because the full backup is a mirror image of the entire disk and the system can restore it as it is.
Backup Policy Every computer system needs to have a well-defined backup policy. Backup policy for a computer system depends on its nature of usage, types of users, its operating environment, etc. A backup policy defines the following: 1.
What is the periodicity of backup? Is the backup procedure executed hourly, daily, weekly, fortnightly, or monthly? Normally, periodicity depends on the nature of usage and criticality of stored data of a system.
2.
Whether to take full or incremental backup? This again depends on the nature of usage of the system. For example, if a user creates only a few files (say letters) a day, an incremental backup performed at the end of each day or even once a week may be sufficient. On the other hand, if a user frequently creates many files, weekly full backups are safer. A compromise that works well for many systems is performing incremental backup daily and full backup weekly.
3.
What storage media to use for backup? Average volume of backup data in each backup cycle normally decides this. We normally take small backup on CD/pen drive, whereas we take larger backup on magnetic tapes, and very large backup on disk packs.
4.
Who takes backup? This normally depends on the usage environment of computer systems. For example, a PC user normally takes the backup of his/her PC. In case of large computer centers, where many users share a system, a system administrator normally takes backup of the system. Often backup is an automated procedure in large computer centers. In this case, the system automatically executes backup procedure at defined periodicity for backup of data on hard disk to an on-line backup device such as a tape library. Hence, the system itself takes backup in such systems.
5.
Where to store the backup media with the backed up data? We just now saw that the backup device along with the media might be an on-line device such as a tape library. In this case, the backup media is stored near to the computer system. This is not acceptable if we need to protect the data against natural calamities. Hence, off-line storage of backup data is always preferred to its on-line storage. Moreover, to protect the data of a computer system against natural calamities, users should store the backup media with the backed up data in a building that is away from the building where the computer system is located.
Backup versus Archiving Backup is often confused with archiving probably because they both deal with copying/moving of data from secondary storage to another secondary or tertiary storage. However, backup and archiving are two different processes, involving different capabilities. Having an understanding of both these processes and associated challenges helps an organization to develop the foundation for an intelligent and robust storage solution. The two concepts have the following basic differences: 1.
Backup deals with active data, whereas archiving deals with inactive data. A system accesses active data frequently, whereas it accesses inactive data occasionally. Hence, it is less likely for a system to access inactive data. Reasons for maintaining inactive data might be to comply with corporate and government regulations on data authenticity, to perform trend analysis in future, or to maintain a history of customers or transactions.
2.
The goal of backup is active data security through redundancy on backup storage, whereas the goal of archiving is inactive data retention through its movement to archival storage.
■ Hierarchical Storage System
189
3.
Backup creates secondary copy of active primary information on backup storage, whereas archiving moves inactive primary information to archival storage.
4.
Backup provides short-term protection of active data to ensure business continuity in case of failures, whereas archiving provides long-term retention of inactive data for future reference.
5.
In most cases, a system retains backup data only for a few days or weeks, as new backup data supersedes previous versions. On the other hand, a system retains archived data for several years and data archived periodically does not supersede previously archived data.
6.
A system generates backup data in a periodic automated fashion, and systematically overwrites it. On the other hand, archived data is in its final form, and does not undergo any modification.
7.
Normally, a system architect chooses backup storage media to enable modification by overwriting on previous version, whereas he/she chooses archiving storage media to prevent modification (WORM) of archived data because data authenticity is the key to archiving.
8.
Backup requires ease of restoring lost data from backup storage in case of failures. Hence, easy access to all data is crucial. On the other hand, archiving requires access and retrieval of a specific piece of information rather than access to all data. Hence, organization of backup and archival data is done differently.
HIERARCHICAL STORAGE SYSTEM (HSS) We saw that different types of storages have different capacity, speed of access, and cost per bit stored. Since a single type of storage is not superior in all aspect, most computer systems build their storage solutions by making use of a hierarchy of storage technologies. Such a storage solution, called Hierarchical Storage System (HSS), is a costeffective way of designing computer systems with very large storage capacities.
Cache memory Less storage capacity, faster access time, and higher cost per bit stored
Main memory On-line, SSD-based secondary storage
More storage capacity, slower access time, and lower cost per bit stored
On-line, HDD-based secondary storage devices such as hard disk or disk array Near-line, tertiary mass storage devices such as tape library, CD jukebox, etc. Off-line, direct-access, and sequential-access storage devices such as magnetic tape, magnetic disk, optical disk, memory storage devices, etc.
Figure 5.31. A typical storage hierarchy pyramid.
Figure 5.31 shows a typical storage hierarchy pyramid of six layers. It includes cache memory, main memory, SSDbased secondary storage, HDD-based secondary storage, near-line mass storage devices, and off-line direct/sequential
190 Chapter 5 ■ Secondary Storage access storage devices. As we move up the pyramid, we encounter storage elements having less storage capacity, faster access time, and higher cost per bit stored. More storage capacity, slower access time, and lower cost per bit stored are the results of moving down the pyramid. Hence, cache memory has the fastest access time, the least storage capacity, and the highest cost per bit stored. Primary storage (main memory) falls next in the storage hierarchy list. On-line, SSD-based secondary storage forms the next layer of the storage hierarchy pyramid, whereas HDD-based secondary storage devices such as magnetic hard disks make up the layer of hierarchy just below that. Near-line, tertiary mass storage devices such as tape library, CD jukebox, etc. fall next in the storage hierarchy. Off-line, directaccess, and sequential-access storage devices such as magnetic tape, magnetic disk, optical disk, memory storage devices, etc. are at the bottom of the storage hierarchy. They are cost-effective for storage of large quantities of data when fast access time is not an important parameter. As we move down the storage hierarchy pyramid, the increasing width of the layers in the figure represents the increasing size of their storage space.
On-line, Near-line, and Off-line Storage Yet another classification of secondary storage devices is on-line, near-line, and off-line. The differences among the three are as follows: 1.
On-line storage. An on-line storage device is under direct control of the processing unit of the computer to which it belongs. It provides rapid access to data on a constant basis. It stores all frequently used data. Computer systems often use SSD, hard disk or disk array as an on-line storage device.
2.
Near-line storage. A near-line storage device is an intermediate storage between on-line storage and off-line storage. It means nearly on-line. It is so called because it is slower than on-line storage, but data can still be accessed from it without any human intervention (off-line storage requires human intervention). Computer systems use near-line storage to store infrequently used data. They often use mass storage devices such as tape library, CD jukebox, etc. as a near-line storage device.
3.
Off-line storage. An off-line storage is not under direct control of any computer. A user records data on it using a computer and then physically removes or disconnects it for storing away from the computer. Hence, it is also known as disconnected storage. To reuse data stored on it, a human operator has to insert it or connect it to a computer system. Hence, it requires human intervention for storage/retrieval of data on/from it. Computer systems often use magnetic tape, magnetic disk, optical disk, and memory storage devices as an off-line storage device. Users often use off-line storage to transfer data between two computers, which are not physically connected. They also use it for off-line data backup to prevent loss of data in case of a disaster at computer’s site, for example a fire, flood, or earthquake. Off-line storage (with backup data) kept at a remote location remains unaffected, enabling disaster recovery. Off-line storage also provides information security against computerbased attack because data on off-line storage is physically inaccessible from a computer.
Hierarchical Storage Management (HSM) Hierarchical Storage System (HSS) is also known as tiered storage. An important issue in HSS is how to decide which data should reside on which storage tier at what point of time, and when and how to move data from one storage tier to another. With the huge volume of data handled by today’s storage solutions, it would be a nightmare if this required human intervention. Fortunately, Hierarchical Storage Management (HSM) software automatically handles this. Hence, all HSSs have HSM software, which monitors the way data is used, and makes decisions as to which data can be moved safely to slower devices (near-line storage) and which data should stay on fast devices (online storage). In a typical scenario, HSM stores frequently used data on on-line storage, but eventually moves them to near-line storage, if the system does not access them for a certain period, typically a few months. If the system accesses a data on near-line storage, HSM moves it back automatically from near-line storage to on-line storage. The advantage is that since only rarely used data is on near-line storage, most users do not notice any slowdown in data access. Moreover, users do not need to know or keep track of where the accessed data is stored and how to get it back.
■ Points to Remember
191
The computer retrieves the data automatically. Notice that movement of data to/from off-line storage still requires human intervention because a human operator has to insert or connect an off-line media or device to the computer system for initiating data transfer.
Points to Remember 1.
Primary storage of a computer system has limited capacity and is volatile. Hence, most computer systems use additional memory, called auxiliary memory or secondary storage.
2.
Secondary storage of a computer system is non-volatile and has low cost per bit stored, but operates at speeds far slower than that of primary storage.
3.
A sequential-access storage device is one in which the arrival at a desired location is preceded by sequencing through other locations so that access time varies according to location. On the other hand, a random-access storage device is one in which we can reach and access any storage location at random, and approximately equal access time is required for accessing each location.
4.
Magnetic tape is the most popular storage medium for large data sets, which applications need to access and process sequentially. Magnetic tape medium is a plastic ribbon usually ½-inch or ¼-inch wide. Its surface has a coating (such as iron oxide or chromium dioxide) that can record data by magnetization. Tape ribbon itself is stored in reels, or a small cartridge, or cassette.
5.
The tape of magnetic tape storage has vertical columns, called frames, and horizontal rows called channels or tracks. Each frame records a character.
6.
Storage capacity of a magnetic tape is a multiple of its data recording density and length.
7.
A magnetic tape drive enables storage/retrieval of data on/from magnetic tape.
8.
A tape controller controls a tape drive connected to it. It interprets the commands for operating the tape drive.
9.
The four commonly used types of magnetic tapes are ½-inch tape reel, ½-inch tape cartridge, ¼-inch streamer tape, and 4-mm digital audio tape.
10. Magnetic disk is the most popular direct-access secondary storage device. A magnetic disk is a thin, circular plate/platter made of metal or plastic and coated on both sides with a recording material (such as iron oxide), which can be magnetized. The disk itself is stored in a specially designed protective envelope, or cartridge, or several of them are stacked together in a sealed, contamination-free container. 11. A magnetic disk’s surface has a number of invisible, concentric circles called tracks. Each track has multiple sectors. A sector is the smallest unit of data access by a disk drive. In case of disk units having multiple disk plates, a set of corresponding tracks on all recording surfaces together form a cylinder. 12. Storage capacity of a disk system is a multiple of its number of recording surfaces, number of tracks per surface, number of sectors per track, and number of bytes per sector. 13. A disk’s data access time depends on seek time, latency, and transfer rate. The time required to position the read/write heads on the specified track/cylinder is called seek time. The time required to spin the specified sector under the head is called latency. Transfer rate is the rate at which a system reads/writes data from/to the disk into main memory. 14. A magnetic disk drive contains all mechanical, electrical, and electronic components for holding one or more disks and for reading/writing of data from/to it. 15. A disk controller controls a disk drive connected to it. It interprets the commands for operating the disk drive. 16. Depending on the type of packaging, magnetic disks are classified into Zip/Bernoulli disks, disk packs, and Winchester disks. 17. An optical-disk storage system consists of a metallic or plastic disk coated with a reflective material. It uses laser beam technology for reading/writing of data from/to disk surface.
192 Chapter 5 ■ Secondary Storage 18. Storage capacity of an optical disk is a multiple of its number of sectors and number of bytes per sector. 19. An optical disk drive contains all the mechanical, electrical, and electronic components for holding an optical disk and for reading/writing of data from/to it. 20. Commonly used types of optical disks are CD-ROM, WORM (CD-R), CD-RW, and DVD. CD-ROM disk is a read-only storage medium. WORM (CD-R) disk is a write-once, read-many storage medium. CD-RW disk allows erasing of previous content and writing on it multiple times. DVD is a standard format for distribution and interchange of digital content in both computer-based and consumer electronics products. The format enables storage of various types of large multimedia data on a new and improved version of CD, which works on optical disk technology. 21. Memory storage devices use flash memory technology for secondary storage devices. Three popular memory storage devices are Solid State Drive (SSD), flash drive (pen drive) and memory card (SD/MMC). 22. Hybrid Secondary Storage Drives use both SSD and HDD. 23. Mass storage devices use multiple units of a storage media to create a single secondary storage device. Commonly used types of mass storage devices are disk array (also known as RAID), automated tape library, and CD-ROM jukebox. 24. A disk array (also known as a RAID) is a mass storage device that uses a set of hard disks, hard disk drives, and a controller mounted in a single unit. 25. An automated tape library is a mass storage device that uses a set of magnetic tapes, magnetic tape drives, and a controller mounted in a single unit. 26. A CD-ROM jukebox is a mass storage device that uses a set of CD-ROM disks, CD-ROM drives, and a controller mounted in a single unit. 27. RAID (Redundant Array of Inexpensive Disks) is a disk array with special features to improve fault tolerance, offer better performance, and allow easier storage management. It uses mirroring, striping, and striping with parity techniques for improved fault tolerance and better performance. 28. To enable devices manufactured by independent vendors to be used with computers manufactured by different vendors, controllers follow some drive interfacing standards. Commonly used drive interface standards are Small Computer System Interface (SCSI), Serial Attached SCSI (SAS), Serial Advanced Technology Attachment (SATA), Fiber Channel (FC), FireWire, and Internet SCSI (iSCSI). 29. Three commonly used processor-to-storage connectivity technologies are Direct Attached Storage (DAS), Storage Area Network (SAN), and Network Attached Storage (NAS). 30. Data backup is the process of creating a copy of some data from an on-line storage device to a secondary storage backup device so that if the original data on the on-line storage device is corrupted or lost by accident, it can be retrieved from the backup device and stored back on the on-line storage device. 31. Two basic types of backup are full backup an incremental backup. Full backup method copies all data from an on-line secondary storage device to a backup device at the time of backup, whereas incremental backup method copies only newly created files or files changed since the last backup. 32. As a single type of storage is not superior in all aspect (speed of access, capacity, and cost), most computer systems build their storage solutions by making use of a hierarchy of storage technologies. Such a storage solution, called Hierarchical Storage System (HSS), is a cost effective way of designing computer systems with large storage capacities. A typical storage hierarchy pyramid includes cache memory, main memory, on-line secondary storage devices, near-line mass storage devices, and off-line storage devices. All HSSs have Hierarchical Storage Management (HSM) software, which monitors the way data is used, and makes decisions as to which data can be moved safely to slower devices (near-line storage) and which data should stay on fast devices (on-line storage).
■ Questions
193
Questions 1.
What is a secondary storage? How it differs from a primary storage?
2.
What are the main limitations of primary storage of a computer system?
3.
Why do most computer systems use secondary storage?
4.
Name some popular secondary storage devices used in today’s computer systems.
5.
Draw the broad classification diagram of popular secondary storage devices used in today’s computer systems.
6.
What is a sequential-access storage device? Write examples of a few applications for which such a storage device is suitable.
7.
What is a random-access storage device? Write examples of a few applications for which such a storage device is suitable.
8.
“The storage approach selected for a particular application is determined by the way data is organized and processed.” Discuss.
9.
Differentiate between on-line and off-line storage of data. Name a storage device suitable for each type of data storage.
10.
Explain how a system records information on a magnetic tape.
11.
What is a parity bit? Explain how a system uses it for detecting errors.
12.
What is a magnetic tape drive? Describe the main components and basic operation mechanism of a half-inch magnetic tape reel drive.
13.
Define “data recording density” and “data transfer rate” for a magnetic tape drive. What is the data transfer rate of a magnetic tape system of 800 BPI tape density and 200 inches per second tape speed?
14.
A magnetic tape is a continuous medium which records data serially and there is no addressing mechanism. In this situation, explain how a system accesses a desired data record stored on a magnetic tape efficiently.
15.
In context of magnetic tape storage, define the following terms and explain their role: (a) Inter-record gap (c) Blocking (b) Inter-block gap (d) Blocking factor
16.
Tape usage increases as blocking factor increases. Write an example to illustrate this. How does a system generally decide blocking factor?
17.
Tape usage increases as blocking factor increases. This means that blocking factor should be as large as possible. What is the problem in making blocking factor too large?
18.
In context of magnetic tape storage, explain the role of the following: (a) File header label
(c) Tape header label
(b) File trailer label
(d) End of tape marker
Beginning of tape marker 19.
What is a magnetic tape controller? Write some commands that a magnetic tape controller typically supports.
20.
Write advantages of half-inch tape cartridges over half-inch tape reels.
21.
Write differences in the way data is organized and accessed in case of half-inch tape reels and half-inch tape cartridges.
22.
Differentiate between half-inch tape and quarter-inch streamer tape. Which one better utilizes tape storage and why?
23.
Differentiate between serial and parallel representation of data recording on magnetic tapes. Write one example of each to illustrate the two types of data recording.
24.
Explain how digital audio tape obtains high data recording density.
25.
Write advantages and limitations of magnetic tapes as a secondary storage device.
194 Chapter 5 ■ Secondary Storage 26.
Write typical uses of magnetic tapes.
27.
What is a magnetic disk? Explain how data is stored and organized on a magnetic disk.
28.
In context of magnetic disk storage, define the following terms and write the relationship among them (if any): (a) Track (b) Sector (c) Cluster
(d) Cylinder (e) Disk address
29.
Describe the two commonly used methods of partitioning a disk surface into sectors and write their relative advantages and disadvantages.
30.
What will be the storage capacity of a double-sided disk that has 400 tracks, 16 sectors per track, and 512 bytes per sector?
31.
Write relative advantages and disadvantages of sector-based and cluster-based disk organizations.
32.
Sector size of a disk should not be too large or too small. Explain why.
33.
What is a disk pack? Explain how data is organized and accessed from a disk pack.
34.
What is a cylinder? A disk pack has 16 surfaces and 200 tracks on each surface. How many cylinders are there in this disk pack?
35.
Explain how cylinder-based organization leads to faster access of data in a disk pack.
36.
What factors determine storage capacity of disks?
37.
A disk pack consists of 6 disk plates. Each plate has 400 tracks and there are 50 sectors per track. If 512 bytes can be stored per sector, calculate its total storage capacity.
38.
Describe the structure of the two commonly used types of access arms assembly in disk systems. Also, write their relative advantages and disadvantages.
39.
What is a head crash in case of a disk system? How does it affect a computer system?
40.
Define the following terms in context of a disk storage: (a) Access time (b) Latency
(c) Seek time (d) Transfer rate
41.
For every read/write access to a disk, is seek time constant or variable? If it is constant, what is its value? If it is variable, what are its minimum and maximum values?
42.
For every read/write access to a disk, is the value of latency constant or variable? If it is constant, what is its value? If it is variable, what are its maximum and minimum values?
43.
Explain how disk systems with multiple read/write heads per surface help in reducing seek time as compared to those with single read/write head per surface.
44.
What is transfer rate of a disk system? What are the two main parameters on which it depends?
45.
Rotational speed of a disk system having a single recording surface is 300 rpm. It has 80 sectors/track and 512 bytes/sector. What is the transfer rate of this disk system?
46.
Explain access time, seek time, and latency for disk storage. What is the access time of a disk system with average seek time of 20 milliseconds and average latency of 7 milliseconds?
47.
What is “flying-head?” How it helps in increasing the life of disk storage?
48.
Differentiate between direct and random access storage devices. In which category does a magnetic disk fall? Write reasons for your answer.
49.
What is a File Allocation Table (FAT) in context of disk storage? Why disk storage systems need FAT? How do they create it?
50.
What is a magnetic disk drive? Differentiate between disk drives with interchangeable magnetic disks and disk drives with fixed magnetic disks. Write their relative advantages and limitations.
■ Questions
195
51.
What is a disk controller? Write the typical commands supported by a disk controller along with the parameters required for each command.
52.
List various types of magnetic disks commonly used in today’s computer systems. Write some typical uses of each type.
53.
What is a disk pack? Write similarities and differences between a disk pack and a Winchester disk.
54.
For the same number of disk platters of same size, Winchester disks can manage to have larger storage capacity than disk packs. Explain why.
55.
Write advantages and limitations of magnetic disks as a secondary storage device.
56.
Write advantages of magnetic disks as compared to magnetic tapes as a secondary storage device.
57.
Write typical uses of magnetic disks.
58.
What is an optical disk? How a system records/reads data on/from an optical disk?
59.
Differentiate between data organization on a magnetic disk and an optical disk. Which data organization leads to faster random access time and why?
60.
Why optical disk drives require a more complicated drive mechanism than magnetic disk drives?
61.
What are the main parameters on which storage capacity of an optical disk depends?
62.
A 5¼-inch optical disk has 3,00,000 sectors each of 2352 bytes. What is its total storage capacity?
63.
Why optical disk drives are slower (have larger access time) than magnetic disk drives?
64.
What are the commonly used types of optical disks? Write typical uses of each type.
65.
What is an “infopedia”? Write some typical examples of infopedia.
66.
What is a DVD? Write down its features and uses.
67.
What is data archiving? Why do systems archive data? Name some secondary storage media used for data archiving.
68.
Write advantages and limitations of optical disks as a secondary storage device.
69.
Write reasons why optical disks are a better storage medium for data archiving as compared to magnetic tapes and magnetic disks.
70.
Write typical uses of optical disks.
71.
What is a Solid State Drive (SSD)? Why is the speed of data access and reliability better for SSD than HDD?
72.
Write the relative advantages and disadvantages of SSD and HDD.
73.
What is a flash drive (pen drive)? How does a user connect it to a computer? Write its features and typical uses.
74.
What is a memory card? Write its features and typical uses.
75.
What is a RAM disk? How does it help in faster execution of an application?
76.
What is a pseudo disk? How does it help in enhancing the speed of a computer system?
77.
Write relative advantages and disadvantages of a RAM disk and a magnetic disk.
78.
What is a hybrid secondary storage drive? Describe its two commonly used configurations.
79.
What is the basic principle of having larger capacity in mass storage devices?
80.
What are the three commonly used types of mass storage devices? What are the basic storage media components used in each type?
81.
What is a disk array? How does it provide enhanced storage capacity and enhanced performance?
82.
What is a RAID? Why it is so called? Explain how it provides enhanced reliability.
83.
Describe the three fundamental RAID techniques with their relative advantages and disadvantages.
196 Chapter 5 ■ Secondary Storage 84.
What is mirroring? Explain how mirroring leads to enhanced reliability and faster read operations in case of a RAID. What is the disadvantage of mirroring?
85.
What is striping? Explain how striping leads to enhanced performance of read and write operations. What is the disadvantage of striping?
86.
Explain why striping leads to worse availability than using multiple disks without RAID configuration.
87.
What is a RAID level? Name some commonly used RAID levels and the techniques used by each for enhanced reliability.
88.
Differentiate between RAID Level 5 and RAID Level 6.
89.
What is an automated tape library? What are its main components? How are these components used together to provide a mass storage device? Write some uses of an automated tape library.
90.
What is a CD-ROM jukebox? What are its main components? How are these components used together to provide a mass storage device? Write some uses of a CD-ROM jukebox.
91.
What is a drive interface standard? How it is useful? Name commonly used industry standard storage interfaces.
92.
What are (a) SCSI controller, (b) SCSI bus, and (c) SCSI device?
93.
Draw a diagram to illustrate how SCSI devices connect to and communicate with a computer system.
94.
Both FC and iSCSI are for SAN environments. Differentiate between the two.
95.
Differentiate among DAS, SAN, and NAS as processor-to-storage connectivity technologies. Write their relative advantages and limitations.
96.
What is data backup? Why do systems need to backup data?
97.
Name some secondary storage devices that are used commonly for data backup.
98.
Write four different ways in which a system may lose data on an on-line storage device.
99.
Name the two basic types of backup. Which one normally takes more time and storage? Write reasons for your answer.
100. Differentiate between full backup and incremental backup. Write their relative advantages and disadvantages. 101. What is a backup policy? Why do systems need it? What types of issues it deals with? 102. What is periodicity of backup? How it is normally decided? 103. What is automatic backup? How it is different from manual backup? 104. How can we protect critical data against natural calamities? 105. Differentiate between backup and archiving of data. 106. What is a hierarchical storage system? Draw a typical storage hierarchy pyramid. 107. Explain how computer systems build large storage capacity in a cost effective way. 108. Differentiate among on-line, near-line, and off-line storage. Write names of storage devices typically used for each type of storage. 109. What is Hierarchical Storage Management (HSM)? Where does a computer system need it and why?
Input-Output Devices
To be useful, a computer needs to communicate with its external environment (its users). Input-output devices (abbreviated I/O devices) provide this capability to a computer. They are also known as peripheral devices. Input devices enter data from outside world into primary storage, and output devices supply the results of processing from primary storage to users. Today, many types of I/O devices are available. For an application, one type may be more suitable than another type. Some I/O devices perform both input and output functions. This chapter describes the commonly used I/O devices. I/O devices are much slower in operation than primary storage and CPU. This is because they have mechanical parts, and the potential for improvement in speed of such parts is limited.
INPUT DEVICES An input device is an electromechanical device that accepts data from outside world and translates them into a form that a computer can interpret. Commonly used input devices are broadly classified into the following categories: 1. Keyboard devices
5. Electronic cards based devices
2. Point-and-draw devices
6. Speech recognition devices
3. Data scanning devices
7. Vision based devices
4. Digitizer
These types of input devices and their applications are described here.
Keyboard Devices Keyboard devices allow data entry into a computer by pressing a set of keys (labeled buttons) mounted on a keyboard. They are broadly classified into two types: general-purpose keyboards and special-purpose keyboards. These two types and some other related concepts are described here.
198 Chapter 6 ■ Input-Output Devices
General-purpose Keyboards General-purpose keyboards are standard keyboards that have enough keys to meet the data entry requirements of a wide range of computer users and applications.
Figure 6.1. Layout of keys on a QWERTY keyboard.
The most popular general-purpose keyboard used today is the 101-keys QWERTY keyboard (see Figure 6.1). It has 101 keys out of which the keys for alphanumeric characters follow the commonly used QWERTY layout. QWERTY layout adopts the standard typewriter keyboard layout, in which the positioning of character keys ensures faster typing speed. This layout organizes alphabetic keys so that the upper-left row of letters begins with the six letters Q, W, E, R, T, and Y (see Figure 6.1). A QWERTY keyboard groups the keys into the following functional groups: 1.
Alphanumeric keys. This group has the keys for the letters of English alphabet, the numbers 0 to 9, and several special characters such as /, ?, $, &, @, whose arrangement follows QWERTY layout.
2.
Numeric keypad. Numeric keypad, located on the right side of the keyboard, has the keys for the ten digits and mathematical operators (+, , * and /). Notice from Figure 6.1 that these keys are also present in the set of alphanumeric keys, making the numeric keypad redundant. Although redundant, the numeric keypad permits rapid entry of numeric data. Keyboards of portable PCs (laptops) do not have numeric keypad due to space limitation.
3.
Arrow keys. These are a set of four keys up (), down (), left () and right (), which allow us to move the cursor up and down (usually a line at a time), and left and right (usually a character at a time). A cursor, which usually appears as an underscore (_), a vertical line (), or a rectangle ( ) on the terminal screen, always indicates the location of the next keyed-in character on the screen. These keys are also used for vertical scrolling (using up and down keys) and horizontal scrolling (using left and right keys) of the contents displayed on the terminal screen, when a displayed document or worksheet extends beyond the size of the terminal screen.
4.
Special function keys. Figure 6.2 lists some of the important keys that belong to this set.
5.
Other function keys. These are user-programmable keys labeled as F1, F2, F3, and so on. The function assigned to each of these keys is different for different software packages. For example, a word processing program can use F1, F2 and F3 keys to make the selected text underlined, italic, and bold, respectively. These keys are also known as soft keys because their functionalities are software definable.
■ Input Devices Special function keys Enter Shift Ctrl Alt Backspace
Function performed Sends keyed-in data or a selected command to the memory. Enters a capital letter or one of the special characters above the numbers when pressed with the corresponding character key simultaneously. Used in conjunction with other keys to expand the functionality of the keyboard. The exact functionality depends on the software used. Used in conjunction with other keys to expand the functionality of the keyboard. Moves the cursor one position to the left and deletes the character in that position.
Tab
Advances the cursor to the next user-defined tab position.
Caps Lock
Toggles (alternate/switch) the capital letter lock feature on and off.
Num Lock
Toggles the number lock feature on and off. Moves the cursor to the beginning of a work area (line/screen/document depending on the software used). Moves the cursor to the end of a work area.
Home End Delete
Spacebar
Deletes the character at the cursor position. Displays the previous page of the current document or the previous record of a database. Displays the next page of the current document or the next record of a database. Toggles between insert mode (adds entered character at cursor position) and typeover mode (replaces character at cursor position with entered character) of data entry. Negates the current command or menu and returns to the work screen. Sometimes performs other functions depending on the software used. Enters a space at cursor position.
Print Screen
Prints what is displayed currently on the screen.
Page Up Page Down Insert Escape
199
Figure 6.2. Some special function keys on a QWERTY keyboard and their functions.
Special-purpose Keyboards Computer systems used for special-purpose applications often use special-purpose keyboards to enable faster data entry and rapid interaction with the system because their data entry requirements may not be as versatile as generalpurpose computer systems. For example, a bank ATM (Automatic Teller Machine) uses a simple keyboard with only a few keys, which are sufficient for financial transactions by customers.
Foreign Language Keyboards Several countries have designed special keyboards for data entry in their local languages. Some of these keyboards have a large number of keys and use a coding scheme with more bits to accommodate the large number of characters used in the language. For example, QWERTY keyboards work with an 8-bit coding scheme, which can accommodate 28 = 256 different characters. However, Japanese language keyboards work with a 16-bit coding scheme to accommodate all characters used in Japanese language.
200 Chapter 6 ■ Input-Output Devices
Keyboard Templates We saw that the function assigned to the function keys F1 to F12 and to some of the special function keys depends on the software used. When different software packages program the same keys to perform different functions, it becomes difficult for a user using multiple software to remember the functions assigned to different keys by different software. Hence, several software vendors distribute their software packages with keyboard templates that designate assignment of various commands to function keys. These templates, made of plastic, nicely fit over the keyboard. The functionality of various function keys is written on top of the plastic at the positions of those function keys. With this, a user need not remember the functions assigned to different keys by different software packages.
Software Simulated Keys We often come across the situation of entering a non-standard character such as “” or “”, or a phrase or name such as “Prentice-Hall of India” repeatedly while preparing a document. For faster data entry, a user may reassign a keyboard key to produce them. For example, a user may write software to reassign the character “#” to the name “Prentice-Hall of India” so that the system automatically enters “Prentice-Hall of India” in place of the character # whenever this key is pressed. Such arrangements of making a key produce something different from what it is supposed to produce is known as software simulation of the key.
Autorepeat Feature If a user keeps a key pressed for an auto-repeat initial delay period (usually of the order of 300 milliseconds), its corresponding character, or functionality starts auto-repeating. For example, if the user keeps the key with character “a” pressed, the characters “aaaaa…..” start appearing on the terminal screen till he/she stops pressing it. This feature enables faster data entry when the user wants to enter the same character multiple times, or when he/she wants to skip multiple lines or pages.
Keyboard Buffer A keyboard buffer is a small memory inside either the terminal or the keyboard. It temporarily stores keyboard input data before transferring it to the computer’s main memory for processing. It has the capacity to store a few tens to a few hundreds of keystrokes. Storing of keystrokes in keyboard buffer instead of sending them directly to main memory makes user interaction with the system more efficient and smooth. This is because when a user presses a key, the processor may be busy doing something else and may not be in a position to transfer the keyboard input to main memory immediately. In this case, in a system without keyboard buffer, the system locks the keyboard after every keystroke, not allowing the user to enter anything until the system completes transferring the keyboard input to main memory, and releases the lock. This causes inconvenience and delay in data entry, especially for operators with fast typing speeds. Keyboard buffer helps users to enter keyboard inputs at their own convenient speeds because the input data is temporarily stored in the keyboard buffer and the processor keeps transferring them to the main memory whenever it finds time to attend to the keyboard device.
Point-and-Draw Devices Initially, interaction with computers was restricted mainly to text mode. Text-mode interaction being cumbersome, time-consuming, and limited in scope of operation, computer designers came up with a new type of user interface, called graphical user interface (GUI). A GUI provides a screen with graphic icons (small images on the screen) or menus and allows a user to make rapid selections from them to give instructions to a computer (see Figure 6.3). GUI requires an input device with which a user can rapidly point to and select a graphic icon or menu item from multiple options displayed on the screen. The keyboard, though usable, is inconvenient and unsuitable for this requirement. Hence, research efforts to find a suitable input device to meet this requirement gave birth to several input devices like mouse, track ball, joystick, light pen, and touch screen. Many of these devices, like mouse and light pen, are also useful for creating graphic elements on the screen such as lines, curves, and freehand shapes. With this new ability,
■ Input Devices
201
manufacturers and users of these devices started calling them point-and-draw devices. These devices enable computers to be used by a wide range of users including children, illiterate citizens, and graphic designers. Some commonly used point-and-draw devices are described here. Menus File
Edit
View
Image
Options
Help
Graphic icons
Figure 6.3. A graphical user interface (GUI) of Microsoft® Windows desktop.
Mouse A mouse is a small hand-held device that fits comfortably in a user’s palm (see Figure 6.4). It rolls on a small bearing and has one or more buttons on the top. When a user rolls a mouse (connected to a user terminal) on a flat surface, a graphics cursor moves on the terminal screen in the direction of the mouse’s movement. Since movements of the graphics cursor on the screen reproduce all movements of the mouse, we can easily position the graphics cursor at a menu item or an icon by using the mouse.
Click buttons
Figure 6.4. A mouse.
Different applications display the graphics cursor as different symbols such as an arrow ( ) , a wrist with a pointing finger (), etc. An application may also display the text and graphics cursor on the screen simultaneously. The graphics cursor, irrespective of its size and shape, has a pixel-size point that is the point of reference to decide the position of the cursor on the screen. This point is called hot spot of the graphics cursor. The graphics cursor points to a menu item or icon, when a user positions the hot spot on it. With a click of the mouse’s button, a user can notify the system of this choice. This is much easier than notifying a choice by pressing various key combinations. With proper software, we can also use a mouse to draw pictures on the screen and edit text.
Mouse Operations Figure 6.5 lists simple operations to carry out various functions with a mouse. A mouse reports movement and button click events in a sequential stream as they happen. The system codes them as multi-byte records. The first byte is event type, and the following bytes are particulars of the event (the change in vertical and horizontal position of the mouse for mouse movement events; which button and the type of click for mouse click events). A mouse uses an interrupt mechanism to report mouse operations to a computer system. It does not send a message directly to the program that is currently executing, but stores the sequential stream of events in a
202 Chapter 6 ■ Input-Output Devices
Sr. No. 1
2 3 4
Operation Point
Click (Left button) Click (Right button) Double-click (Left button)
5
Double-click (Right button)
6
Simultaneousclick
7
Drag
Meaning Move the mouse so that hot spot is positioned at a menu item or an icon. Press and release left mouse button once. Press and release right mouse button once. Press and release left mouse button twice in rapid succession. Press and release right mouse button twice in rapid succession. Press and release both left and right mouse buttons simultaneously. Move the mouse from one hot spot position to another hot spot position with the left mouse button pressed.
Function Graphics cursor points to the menu item or icon at the hot spot position. Selects the menu item or icon at the hot spot position. Function varies with software used, but it is used often to call up a menu of options. Initiates execution of the program represented by the icon at the hot spot position. Function varies with software used.
Provides added functionality to the mouse. Actual functionality varies with software used. Use to (a) Change the location of an icon on the screen; (b) Draw graphics objects (lines, circles, curves, freehand, drawings, etc.); (c) Select a block of text for performing some operation on the entire block.
Figure 6.5. Simple operations to carry out various functions with a mouse.
memory buffer and sends an interrupt request to the CPU. The currently executing program checks periodically to see whether a mouse event has occurred. On sensing an interrupt signal, the program reads the sequential stream of events from the buffer and acts accordingly. Types of Mouse Mouse devices are usually categorized as: 1.
Mechanical and optical mouse. A mechanical mouse has a ball that partially projects out through an opening in its base. The ball rolls due to surface friction when a user moves the mouse on a flat surface. On two sides of the ball (at 900 angles from each other) are two small wheels that spin to match the speed of the ball. Each wheel of the mouse connects to a sensor. As the mouse ball rolls, the sensors detect how much each wheel spins and send this information to the computer in the form of changes to the current position of the graphics cursor on the screen. An optical mouse has no mechanical parts like the ball and wheels. Instead, it has a built-in photo-detector. It comes with a special pad with gridlines printed on it. When a user moves the mouse on the pad, the photodetector senses each horizontal and vertical line on the pad, and sends this information to the computer in the form of changes to the current position of the graphics cursor on the screen.
2.
One, two, and three buttons mouse. A mouse can have one, two, or three buttons. A one-button mouse is good enough for programs designed to use only a single button. Some programs operate with a two-button or three-button mouse, but can still run on a system having fewer buttons by providing an alternative way to indicate a click of these buttons – such as by pressing shift key on keyboard while the mouse button is clicked. The leftmost button of a multi-button mouse is the main button that allows for most mouse operations because most people are right-handed and use their right index finger to click the main button. However, a user can configure another button as main button. For example, a left-handed user can configure the rightmost button as main button.
■ Input Devices
203
3.
Serial and bus mouse. A serial mouse plugs into a normal serial port (a built-in socket in a computer’s back panel to which we can connect external devices) of a computer, while a bus mouse requires a special electronic card, which provides a special port for connecting the mouse to a computer. The special electronic card fits into one of the expansion slots of the computer. Thus, a bus mouse occupies one of the expansion slots, whereas a serial mouse does not need any expansion slot.
4.
Wired and cordless mouse. A wired mouse connects to a computer with a small cord (wire), while a cordless mouse has no wired connection to a computer. It has a controller that communicates with the computer by transmitting a low-intensity radio or infrared signal. Although more expensive than a wired mouse, a cordless mouse allows easier movement of the mouse.
Trackball A trackball is a pointing device similar to a mechanical mouse. Its roller ball is on the top (instead of the base) along with the buttons (see Figure 6.6). To move the graphics cursor on the screen, the user simply rolls the ball with hand (instead of moving the whole device). Hence, a trackball requires less space than a mouse for operation, and is often attached to or built into the keyboard. Trackballs are usually built into the keyboard of laptops, because laptop users do not always have a flat surface for moving the mouse. Three shapes in which trackballs are commonly found in laptops are ball, button, and square. To move the graphics cursor at a desired position on the terminal screen, the user rolls the ball with fingers, or pushes the button with a finger, or moves a finger on the square platform. Trackball is preferred for CAD/CAM (Computer Aided Design/Computer Aided Manufacturing) applications, because a designer can move the graphics cursor with hand movements only without moving the device, which suits the working style of designers working on large drawings. Several equipment having embedded computers also use a trackball. For example, an Ultrasound machine usually has a trackball that a sinologist (medical expert in sonography) uses to move the cursor (usually in cross-hair shape) to mark the points of references on the image on the screen.
Joystick A joystick is a pointing device that works on the same principle as a trackball. To make the movements of the spherical ball easier, it is placed in a socket with a stick mounted on it (see Figure 6.7). A user moves the stick forward or backward, left or right, to move and position the graphics cursor at a desired position. Most joysticks use potentiometers to sense stick and ball movements, and a click button on top of the stick to select the option pointed to by the cursor. Click button
Light indicator
Click buttons
Stick
Ball Ball is rolled with fingers Figure 6.6. A trackball.
Socket
Figure 6.7. A joystick.
Typical uses of a joystick include video games, flight simulators, training simulators, and remote control of industrial robots.
204 Chapter 6 ■ Input-Output Devices
Electronic Pen An electronic pen is a pen-based point-and-draw device. Its two commonly used types are: 1.
Light pen. It uses a photoelectric (light sensitive) cell and an optical lens assembled in a pen-shaped case. When a user moves the tip of the pen over the screen surface, the photoelectric cell and lens assembly detects the light emitted from a limited field of view of the screen’s display. The system transmits this electric response to a processor, which identifies the menu item or icon that is triggering the photocell. In this manner, the graphics cursor locks on to the position of the pen and tracks its movement over the screen. The pen has a finger-operated button, which the user clicks to select an option from the screen. A user can also draw on the screen with a light pen. For example, CAD applications allow users to use it with a keyboard to select different colors and line thickness, reduce/enlarge drawings, and add/erase graphics objects such as lines, circles, rectangles, etc.
2.
Writing pen with pad. This type comes with a special writing pad. A user writes on the pad with the electronic pen the data to be input to the computer. With handwriting recognition software, this device enables input of text and freehand drawings into computer for word processing. Modern PDAs (Personal Digital Assistants) provide this feature. Moreover, signatures and messages stored and transferred as graphic images are input with the use of this device. For example, delivery persons of goods delivery organizations often use pocket computers with this device to accept recipients’ signatures against their names. The device captures the image of a signature and stores it in the computer for future references.
Touch Screen Touch screen enables a user to choose from available options by touching with a finger the desired icon or menu item displayed on a computer’s screen, which has optical sensors to detect the touch of a finger. When a user touches the screen with a finger, the sensors communicate the position of touch to the computer, which interprets the input made by the user. Another type of touch screen uses a flat pad located beneath the screen, which has sensitive sensors to measure the pressure at many points. When a user touches the screen, the changing pressure allows the device to detect the location of touch, which it transmits to the computer as user’s desired input. Information kiosks often use touch screens. An information kiosk is an unattended system located at a public place that stores information of public interest and allows common people to access stored information as per their requirement. For example, information kiosks are often located at: 1. An airport or a railway station to provide information to arriving passengers about hotels, restaurants, tourist
spots, etc. in a city. 2. In large museums or zoos to guide the visitors to locations of various attractions and facilities, and to caution
them against things they are not supposed to do while inside. 3. In a large bank, post office, or insurance company to introduce various types of services to customers, and to
guide them to appropriate counters for their specific jobs. Bank ATMs also use them to enable customers to carry out their transactions easily. 4. In environments having dirt or weather conditions, which prohibit use of keyboards and/or pointing devices.
Data Scanning Devices To reduce human error in data entry, the trend in data entry to computers has always been to decrease the number of steps involved in transcription (copying data by typing, keying, or writing). One way to accomplish this is by enabling data entry directly from the source documents by using data scanning devices. These devices have the following characteristics: 1.
They eliminate the need for manual entry of data by human beings, thus, improving data accuracy and timeliness of the data processed.
■ Input Devices
205
2.
They demand high quality of source documents and often reject documents typed poorly, having strikeovers, or having erasures.
3.
Form design and ink specification requirements become more critical with the use of these devices than with the use of keyboard devices for data entry.
4.
Some of these devices are also capable of recognizing marks or characters.
Some commonly used types of data scanning devices are described here.
Image Scanner An image scanner is an input device that translates paper documents into an electronic format for input to a computer. The input documents may be typed text, pictures, graphics, or even handwritten material. A copy of a document stored in a computer in this manner does not deteriorate in quality with age, and can be displayed or printed, whenever desired. We can alter and manipulate the stored images in interesting ways, if the computer has imageprocessing software. Image scanners use optical technology to convert an image into electronic form. They shine light on a document and sense the intensity of the reflection at every point of the image. For example, suppose a document has the word “BIT” written on it. The scanner treats the document as if it is on a graph sheet (see Figure 6.8(a)). An intersection of a horizontal and vertical line of the graph is a grid point. If there is a dark spot at a grid point, it is represented by a 1, otherwise it is represented by a 0. Figure 6.8(b) shows the representation of the image of Figure 6.8(a). This representation is called bit map representation of an image. Each bit in the representation is called a pixel (a picture element). 0 0 0 0 0 0 0 0 0 0 0 (a) A scanned document
1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0 0 0 1
0 1 1 1 1 0 1 1 1 1 0
1 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
(b) Bitmap representation of the scanned document
Figure 6.8. A scanned document and its bitmap representation.
The finer is the granularity of grid points, the better the scanner is because it can convert an image with greater accuracy. Scanners having 300 to 600 grid points per inch are common. Moreover, Figure 6.8 shows the use of just 1 bit for encoding of each grid point. High quality black-and-white scanners and color scanners use more bits to represent each grid point. For example, a good quality black-and-white scanner might use 4 bits per grid point to encode different shades from black to gray to white. Similarly, a color scanner might use 24 bits per grid point to represent a maximum of 256 different shades of color. The two commonly used types of image scanners are: 1.
Flatbed scanner. A flatbed scanner is like a document copy machine. It has a box with a glass plate on its top and a lid that covers the glass plate (see Figure 6.9). To scan a document, a user places it upside down on the glass plate. A light beam below the glass plate moves horizontally from one end to another when activated. After scanning one line, the light beam moves up a little and scans the next line. The process repeats until the beam finishes scanning the entire document. It takes about 20 seconds to scan a document of 21 cm 28 cm size.
206 Chapter 6 ■ Input-Output Devices 2. Hand-held scanner. A hand-held scanner (see Figure 6.10) has a set of light emitting diodes encased in a
small case that conveniently fits in hand. To scan a document, a user drags it steadily over the document from one end to another with its light on. Hand-held scanners are used to scan low volume documents. They are much cheaper than flatbed scanners.
Figure 6.9. A flatbed scanner.
Figure 6.10. A hand-held scanner.
Optical Character Recognition (OCR) Device We can use image scanners to input a variety of documents including pictures, graphics, typed text, and handwritten text. When we use them for inputting text documents (typed or handwritten), we face the following limitations: 1.
Since a scanned input document is stored as an image, instead of text, it is not possible to do any word processing of the document (the computer cannot interpret the stored document as letters, numbers, and special characters).
2.
Storage space required for storing the document as an image is much more than that required for storing the same document as a text. For example, a page of printed text having 2000 characters can be stored as 2000 bytes by using the ASCII representation. A bitmap-image representation of the same document requires 10 to 15 times more storage depending on the resolution (granularity) of grid points.
An OCR device overcomes these limitations. It has a scanner equipped with character recognition software (called OCR software), which converts bitmap images of characters to equivalent ASCII codes. The scanner first creates the bitmap image of the document and then the OCR software translates the array of grid points into ASCII text that the computer can interpret as letters, numbers, and special characters. To translate bitmaps of characters into equivalent text, OCR software examines the bitmap of each character and compares with the pre-programmed set of characters, which the machine can recognize. Whichever character pattern it matches, or nearly matches, the machine considers it as the input character. If a scanned character does not match satisfactorily with any of the stored character patterns, the OCR software rejects it. OCR software is designed to recognize texts written using standard type fonts (called OCR fonts) only. Two such standard fonts are OCR-A (American standard) and OCR-B (European standard) (see Figure 6.11). If a document contains italics or bold face letters, or text written using fonts which the OCR software cannot recognize, then the OCR software will not be able to translate it correctly. Modern OCR devices can also recognize handwritten characters. In this case, users have to take special care to ensure that in the handwritten document, characters are of standard size, lines making up the characters are connected, and no stylish loops etc. are used. OCR devices for converting handwritten text into ASCII are more complex and difficult to design than those for printed text. This is because handwritings of different users are different, making the parameters for how individual characters are formed very broad.
■ Input Devices
(a) OCR-A fonts (American standard)
207
(b) OCR-B fonts (European standard)
Figure 6.11. Two standard OCR fonts.
Optical Mark Reader (OMR) OMR is a scanner that can recognize a pre-specified type of mark made by pencil or pen. For example, in objective type tests, applicants mark their choices of correct answers on a special pre-printed test scoring sheet by darkening a small square, circular, or oval shaped box by a pencil (see Figure 6.12). These answer sheets are fed to an OMR device for automatic grading by a computer. For each question, four options are given out of which only one is correct. Choose the correct option and mark your choice against the corresponding question number in the given answer sheet by darkening the corresponding circle with a lead pencil. 1.
Binary equivalent of decimal 4 is: (a) 101 (c) 001 (b) 111 (d) 100
2.
Full form of CPU is: (a) Cursor Positioning Unit (b) Central Power Unit (c) Central Processing Unit (d) Control Power Unit
3.
Which is the largest unit of storage among following: (a) Terabyte (c) Megabyte (b) Kilobyte (d) Gigabyte (a) Question sheet
Indicates direction in which the sheet should be fed to OMR device 1. a
b
c
d
a
b
c
d
a
b
c
d
2.
3. (b) Pre-printed answer sheet
Figure 6.12. A sample question sheet of an objective type test and its pre-printed answer sheet, which is read by an OMR device to grade the test.
An OMR device focusses a light beam on the document and detects the reflected light pattern from the marks. Pencil marks made with a soft lead pencil (high graphite content) reflect the light strongly, enabling the OMR device to determine which responses are marked. OMR devices require that the pre-printed forms use good quality paper without any foldings. They also require accurate alignment of markings in the boxes for correct interpretation of input data. We can use OMR devices for any input data that is of a choice or selection nature. Thus, in addition to their use for grading tests with objective type questions, they are often used to tabulate responses to large surveys where responses are restricted to one or more out of a few possibilities.
208 Chapter 6 ■ Input-Output Devices
Bar-Code Reader Data coded in the form of small lines (known as bars) are known as bar codes. Bar codes represent alphanumeric data by a combination of adjacent vertical lines (bars) by varying their width and the spacing between them. All types of goods, books, postal packages, badges, tags, etc. use bar codes for unique identification purpose. A bar-code reader is a device for reading (decoding) bar-coded data. It is either a hand-held or a stationary scanner having bar-code reader embedded in it. It scans a bar-code image and converts it into an alphanumeric value for input to a computer connected to it. A bar-code reader uses laser-beam scanning technology. It focuses a laser beam across the pattern of bars of a bar code. Different bar codes having different patterns of bars reflect the beam in different ways. A light-sensitive detector senses the reflected light patterns and converts them into electrical pulses, which the reader then transmits to recognition logic circuits. Finally, the logic circuits convert the electrical pulses into an alphanumeric value. There are many types of bar-coding systems. The most widely known one is the Universal Product Code (UPC). It appears on almost all retail packages in USA and now in India as well on many products. The UPC bar-code patterns decode as 10 digits. The first five digits identify the manufacturer or supplier of the product, and the next five digits identify a specific product of the manufacturer (see Figure 6.13). We often find bar code readers in supermarkets and department stores. When a customer picks up a product for purchasing and brings it to the cash counter for payment, the sales person at the counter uses a bar-code reader to scan the bar code printed on the product. The bar-code reader converts the bar code into an alphanumeric value and feeds it to a computer that looks up the price of the product, possibly updates inventory and sales records, and uses the price and description information to prepare a purchase bill for the customer. Postal and goods delivery services also use bar codes to identify and track each packet. Product category character 0 – grocery products 3 – drugs and health related products, etc.
0
21000 Manufacturer/supplier identification number
67520 Specific product code number
Figure 6.13. An example of UPC bar code.
Magnetic-Ink Character Recognition (MICR) MICR is similar to OCR. Banking industry uses it for faster processing of large volume of cheques handled every day by this industry. A bank cheque has bank’s identification code (name, branch, etc.), account number, and cheque number pre-printed (encoded) on it by using characters from a special character set. The characters on the cheque are pre-printed using a special ink containing particles of iron oxide (having magnetic property). Figure 6.14 shows a sample cheque that employs MICR technology.
■ Input Devices
209
Pre-printed MICR characters Figure 6.14. A bank cheque that employs MICR technology.
When a customer presents a filled-in cheque at a bank, a bank employee enters manually (keys in) the customersupplied information, including the amount written on the cheque. The bank then processes this cheque by using an MICR reader-sorter, which reads the pre-printed data on the cheques, and sorts the cheques into different pockets according to their identification code numbers for distribution to other banks, or for further processing. E13B font is the most commonly used character set by MICR devices. It consists of numerals 0 to 9 and four special characters (see Figure 6.15). Besides enabling faster processing of cheques, MICR technology also ensures greater accuracy of data entry because of mostly pre-printed information on the cheque and direct feeding of the cheques to the computer. However, other industries have not adopted MICR because it has only 14 symbols (10 digits and 4 special characters). No alphabetic characters are available.
Figure 6.15. E13B font (character set) used by MICR devices.
Digitizer A digitizer is an input device used for converting (digitizing) pictures, maps, and drawings into digital form for input to computers. For example, x and y coordinates of points in a line drawing may be stored in digital form. This enables re-creation of the drawing from the stored information and easy incorporation of changes in the drawing, whenever required. A digitizer consists of a digitizing tablet (also known as graphics tablet) and a stylus (see Figure 6.16). The digitizing tablet is a flat surface that contains hundreds of fine copper wires forming a grid. Each copper wire receives electric pulses. The digitizing tablet spreads over a working table and connects to a computer. The stylus is like a pen or a lens-like cursor with a cross hair and button. It
Digitizing tablet Stylus (cursor with a cross hair and button) Tabletop
Figure 6.16. A digitizer.
210 Chapter 6 ■ Input-Output Devices connects to the tablet and a user can press it down at a point on the tablet to input the (x, y) coordinates of the point. As a user moves the stylus on the tablet, a cursor on computer’s screen moves simultaneously to a corresponding position on the screen to provide visual feedback to the user. This enables the user to draw sketches or to input sketched drawings. Inputting drawings or developing sketches by using a digitizer is simpler because it inputs poorly sketched lines, arcs, and other graphical objects as mathematically precise objects (like straight lines and smooth curves). Architects and engineers use digitizers in the area of Computer Aided Design (CAD) for designing cars, buildings, medical devices, robots, mechanical parts, etc. Designers of Geographical Information System (GIS) applications also use them for digitization of maps available in paper form.
Electronic-card Reader Electronic cards are small plastic cards having encoded data appropriate for the application for which they are used. An electronic-card reader, normally connected to a computer, reads the data encoded on an electronic card and transfers it to the computer for further processing. Banks issue electronic cards to their customers for use with automatic teller machines (ATMs). An ATM is an unattended machine connected to a bank’s computer, which allows a customer to perform certain bank transactions 24 hours a day, without the need to interact with a bank employee. An electronic card for use in an ATM has its customer’s account number and credit limit (maximum amount that he/she can withdraw in a day) encoded on the card. When a customer inserts his/her card into the ATM (actually the electronic-card reader of the ATM), the reader reads the data encoded on the card and transmits it to the bank’s computer activating the customer’s account. The customer then carries out the desired transaction by following the instructions displayed on the screen of the ATM and by pressing a few keys on its customized keyboard or touch screen. Many organizations also use electronic cards for controlling access to physically secured areas. In this case, an organization issues an electronic card to each employee with his/her authorization data encoded on it. Authorization data specifies which employee has access to which areas. All physically secured areas of the organization have an electronic-card reader installed at its entrance. When an employee wants to enter into a physically secured area, he/she inserts his/her electronic card into the reader installed at the entrance of that area. If the electronic card and its reader use RFID technology, a user simply needs to flash his/her electronic card near the reader instead of inserting it into the reader. The reader reads and checks the authorization data encoded on the card before permitting the individual to enter the area (the door has electronic connection with the reader and opens only if the reader finds that the individual has authorization to access the area). In such applications, a central computer having link to the electronic-card reader maintains a record of the identity and time of people entering/leaving a secured area. In case of security breaches, an organization uses this record to trace the problem. Depending on the manner in which data is encoded, electronic cards are of the following types: 1.
Magnetic strip cards. These cards have a magnetic strip on which encoded data is stored. It can contain much more data per unit of space than do printed characters or bar codes. Moreover, we cannot read data stored in it visually. Many organizations issue magnetic strip cards to their employees or customers with their confidential data stored on it.
2.
Smart cards. Smart cards have a built-in microprocessor chip in whose memory the encoded data is stored. It can store much more data than the magnetic strip of a magnetic strip card. A smart card also has some processing capability because its microprocessor has its own processor and memory. This feature attributes intelligence to smart cards and makes them more suitable for a wider variety of applications than magnetic strip cards. For example, a smart card can be used as an electronic wallet by storing a specific amount of cash in the microprocessor chip before the card is issued to a customer. Whenever the customer uses the card to make purchases, the amount of purchase made at every instance is deducted from the balance amount stored in the card by inserting the card in a card read/write machine (also called electronic registers), which is available with shops that allow use of smart cards for making purchases. At any time, the customer can pay additional money to the card company and get the balance amount stored in the card increased. Because smart
■ Input Devices
211
cards can hold more information, have some processing capability, and is almost impossible to duplicate, they are fast replacing magnetic strip cards.
Speech Recognition Devices Speech recognition devices are input devices that allow a user to input data to a computer system by speaking to it. As users speak with different accents (pronounce differently) and intonations (with different tone or pitch of the voice), and the fact that the meaning of a word varies depending on the context in which it is used, speech recognition systems are limited to accepting few words or sentences within a relatively small domain. A computer with a speech recognition device consists of a microphone and follows the following steps to recognize and act upon a spoken input data (see Figure 6.17):
STOP
3 Convert analog signals to digital form Word STOP in digital form
1 User speaks the word STOP to a computer
2 Convert spoken word into electrical signals (analog form)
5 Display the recognized word and/or perform command
4 Match with MOVE NO PUT STOP TEN WAIT YES ZERO
pre-stored words in computer’s electronic dictionary
Figure 6.17. Steps followed in speech recognition by a speech recognition system.
1.
Speak. A user speaks into the microphone attached to it.
2.
Convert into electrical signals. The device converts the spoken word(s) into its analog form by converting the word(s) into electrical signals by breaking down each sound into its various frequencies.
3.
Digitize. A digital-to-analog converter in the device then converts the analog form of the spoken word(s) into its digital form (in 1s and 0s) so that the computer can store and interpret it.
4.
Match with pre-stored words. The device next matches the digitized version of each spoken word against similarly formed pre-stored words in the computer’s electronic dictionary. A user creates the electronic dictionary (a database of pre-stored words) during a training session, when he/she speaks several times the words and phrases to be stored in its electronic dictionary to train the system to recognize them. When operated in training mode, the device creates and stores the patterns for all spoken words and phrases for future matching.
5.
Perform corresponding action. When the device finds a match for the spoken word(s), it displays the word(s) on computer’s screen and/or initiates appropriate action corresponding to the input (for example, stops doing whatever it was performing). In some case, the system displays the word(s) and waits for the user’s confirmation to prevent doing something different from what the user intended (in case of wrong interpretation of the spoken word(s) by the device). If it does not find a match for the spoken word(s), the system informs the user by displaying a message on computer’s screen.
212 Chapter 6 ■ Input-Output Devices Speech recognition systems are of two types: 1.
Speaker-dependent. Due to vast variation in the accent of different speakers, speech recognition systems are usually speaker-dependent. They recognize the speech of only a single individual or a few individuals. In case of multiple individuals, each one of them participates separately in a training session to train the system to recognize his/her accent of pre-stored words in computer’s dictionary. The system usually maintains a separate database of pre-stored words for each individual because the digital form of the same word may be different for different individuals.
2.
Speaker-independent. Speaker-independent speech recognition systems can recognize words spoken by anyone. Since such a system requires a large database of pre-stored words to accommodate anyone’s voice pattern, speaker-independent systems usually have a limited vocabulary. For example, the vocabulary of such a system may have the words YES, NO, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, and TEN.
According to another classification mechanism, two types of speech recognition systems are: 1.
Single word recognition systems. These systems can recognize only a single spoken word, such as YES, NO, MOVE, STOP, at a time. Speaker-independent systems are mostly of this type.
2.
Continuous speech recognition systems. These systems can recognize spoken sentences such as MOVE TO THE NEXT BLOCK. Since recognizing continuously spoken sentences is more difficult than recognizing words, continuous speech recognition systems are usually speaker-dependent.
A wide range of applications use speech recognition systems. A few examples are: 1.
For data input to a computer system by a person in situations where his/her hands are busy or he/she must continuously look at a measuring instrument or some other object. For example, doctors in an operation room can request certain information about a patient while operating.
2.
For data input by dictation of long text or passage for later editing and review.
3.
For authentication of a user by a computer system based on speech input.
4.
For limited use of computers by individuals with physical disabilities.
In addition to making input of data easier, speech recognition systems also provide freedom of movement because the user is free to stand up, move around, or do some other work by hand while inputting voice data to the system.
Vision-Input Systems A vision-input system consists of a digital camera to accept input by seeing an object. Input data in this case is normally an object’s shape and features in the form of an image. A computer with a vision-input device follows the following steps to recognize a given object: 1.
It focuses the camera on the given object to take its picture.
2.
The camera creates a digital image of the object and feeds it to the computer.
3.
The computer matches the image with similarly formed pre-stored images in its image database.
4.
The computer takes appropriate action depending on whether it finds a match or not.
Vision-input systems are often part of industrial robots that are used for quality control and assembly processes. For example, the robot of Figure 6.18 inspects the objects passing on a conveyor belt and separates those not meeting certain quality-control specifications. It also inspects the shapes of objects and separates objects of different shapes in different bins.
■ Output Devices
Computer
213
Contains a database of images of correct samples
Camera
The computer activates one of the robotic arms at the right time to separate the objects in appropriate bins
Conveyor belt movement Reject Bin for Bin for circular square bin objects objects Figure 6.18. An application of a vision-input system.
OUTPUT DEVICES An output device is an electromechanical device that accepts data from a computer and translates them into a form that is suitable for use by outside world (users). Commonly used output devices are broadly classified into the following categories: 1. Monitors 4. Computer output microfilm 2.
Printers
5.
Screen image protector
3.
Plotters
6.
Voice response system
These types of output devices and their applications are described here. Output devices generate computer outputs that are broadly of the following two types: 1.
Soft-copy output. A soft-copy output is temporary in nature because it does not appear on a paper or some material that a user can carry for showing to others, or for filing and use later when he/she is not using a computer. For example, contents displayed on a terminal screen, or words spoken out by a voice response system are soft-copy output.
2.
Hard-copy output. A hard-copy output is permanent in nature because it appears on a paper or some material that a user can carry for showing to others, or for filing and use later when he/she is not using a computer. For example, output produced on paper by printers or plotters are hard-copy output.
Monitors Monitor
Monitors generate soft-copy output by displaying an output on a screen (see Figure 6.19). A monitor and a keyboard together form a video display terminal (VDT). A VDT (often referred to as just terminal) is both an input and output device. A user uses the keyboard to input data to a computer and the monitor to view the output from the computer. The name “terminal” comes from the fact that a terminal is at the terminus or end of a communication path. It is the most popular I/O device used today.
Keyboard Figure 6.19. A video display terminal consists of a monitor and a keyboard.
214 Chapter 6 ■ Input-Output Devices Two basic types of monitors are cathode-ray-tube (CRT) and LCD (Liquid Crystal Display) flat-panel. As compared to CRT monitors, LCD flat-panel monitors are thinner, lighter, and are suitable for use with both portable and nonportable computer systems. Users prefer them because they occupy less table space and consume less power.
How a CRT Monitor Displays an Output? A CRT monitor is a glass tube with a screen at one end and an electron gun at the other end. Figure 6.20 shows its major components. The electron beam generator has an oxide-coated metal (called cathode) and a heating filament. The cathode emits a large number of electrons when heated by the filament. In front of the electron beam generator is a control grid applied with an appropriate positive voltage. It allows the exact number of electrons, out of the generated ones, to pass through it for striking at the monitor’s screen. Larger is the positive voltage applied to the control grid, larger is the number of electrons that pass the control grid and strike the screen, and larger is the beam’s intensity. If the grid voltage is negative, no electrons pass the control grid and the beam is “blanked”. An assembly unit consisting of accelerating electrodes and a focusing electron lens accelerates and focuses the electrons, which pass through the control grid. The unit applies a positive voltage to accelerating electrodes to accelerate the electrons. The focusing electron lens consists of a cylindrical metal enclosure with a hole through which the electrons pass in the form of an electron beam. The electron gun of the CRT monitor includes the electron beam generator, control grid, accelerating electrodes, and focusing electron lens. Consists of a cathode and a heating filament
Accelerating electrodes with focusing electron lens
Horizontal deflection plates
Electron beam generator
Electron beam
Screen Phosphor Control grid
Vertical deflection plates
Figure 6.20. Major components of a CRT monitor.
The electron beam generated by the electron gun passes through a pair of vertical deflection plates and a pair of horizontal deflection plates. It applies appropriate positive voltages to one of the vertical plates and one of the horizontal plates to focus the beam at a desired point on the screen. For example, the display mechanism applies a suitable positive voltage to the top/bottom vertical plate to move the beam up/down, and to the left/right horizontal plate to move the beam left/right. Thus, the electron beam hits at the desired point on the screen. The backside of the screen has phosphor coating, a chemical that glows for a short time when hit by the electron beam. The screen’s phosphor coating is in the form of a grid of dots called pixels (a contraction of picture elements). Since phosphor glows only for a short time (few microseconds), an image drawn on the screen would be visible only for a few microseconds and then would fade away, unless the display mechanism draws the image repeatedly. This may appear to be an inconvenience, but it is also an advantage since there is no need to erase anything. The display mechanism just draws new images and the old ones fade away automatically. The electron gun does not draw an image directly by focusing and shooting on the pixels forming the image. Instead, it traverses the screen in a fixed pattern (see Figure 6.21). That is, it aims at every pixel on the screen systematically, starting at the top-left corner and scanning to the right horizontally, then dropping down a small distance and scanning another line. Each horizontal line of pixels is called a scan line. Each scan line has a fixed number of pixels, and the electron gun can write or not write a pixel in each position by turning the beam on or off. When the beam reaches the right end of a scan line, the device turns it off and moves it to the starting point of the next scan line. This is called
■ Output Devices
215
horizontal trace. After scanning all scan lines, when the beam reaches the right end of the bottom-most scan line, the device turns it off and moves it to the starting point of the top-most scan line. This is called vertical trace. Scan lines
Vertical retraces
(a) Progressive scan
Horizontal retraces
(b) Interlaced scan
Figure 6.21. Scanning of a monitor’s screen by its electron beam.
In one pass of scanning the entire screen, if the electron beam travels all scan lines from top to bottom sequentially, it is called a progressive scan (see Figure 6.21 (a)). On the other hand, if the electron beam travels only odd numbered scan lines in one pass and even numbered scan lines in the next pass, it is called an interlaced scan (see Figure 6.21 (b)). In case of interlaced scan, since the monitor scans every other line instead of each line in order, it has to transmit only half as much data in a single pass, but takes two passes to draw the entire screen. Interlaced scan may cause flicker of images drawn on the screen. Hence, low quality monitors use interlaced scan and high quality monitors use progressive scan.
How to Compare CRT Monitors We often use the following attributes to compare CRT monitors: 1.
Screen size. Display screens of monitors vary in size from 5 to 25 inches (diagonal dimension). Monitors with 17-inch to 21-inch screen size are common with personal and desktop computers. Although, monitors with larger screen are costlier, users prefer them because they enable viewing of multiple windows, video clips, animated objects, and complex graphics.
2.
Resolution. Resolution refers to the number of points to which the electron beam can be directed (pixels) on the screen. As the pixels are arranged in scan lines, resolution of a monitor depends on its total number of scan lines and total number of pixels per scan line (each scan line has same number of pixels). Total number of scan lines is called a monitor’s vertical resolution and total number of pixels per scan line is called its horizontal resolution. Overall resolution of a monitor is a multiple of its horizontal and vertical resolutions. A low-resolution monitor has a resolution of about 320 × 200, while high-end monitors have resolutions of 1024 × 768, 1280 × 1024, or more. The higher the resolution of a monitor, the sharper is its displayed images.
3.
Refresh rate. Since a pixel on phosphor-coated screen glows for a short time only (few microseconds) and then fades away, the display mechanism redraws an image 30 to 60 times a second to make it appear flickerfree. Hence, the electron beam should scan the entire screen 30 to 60 times a second. The actual number of times that the electron beam scans the entire screen per second is called refresh rate of the monitor. The higher the refresh rate, the better is the display quality. Today, most monitors operate at 60 Hz, refreshing the screen 60 times per second. Better quality monitors operate at 70 to 90 Hz.
4.
Color. Monitors are either monochrome or color. A color monitor displays multi-colored images, while a monochrome monitor displays images only in a single color, usually white, red, green, blue, or amber. Although a monochrome monitor displays only a single color, it is usually capable of displaying multiple shades (increased and reduced color intensities) of the same color. The term gray scales is used to refer to the number of shades of a color that a monitor can display. Monochrome monitors have become obsolete, as most applications today require color monitors.
216 Chapter 6 ■ Input-Output Devices To add color, a color monitor uses three electron beams instead of one, and each pixel on screen consists of three tiny red, green, and blue phosphors arranged in the shape of a triangle. When the device combines three electron beams and focuses the combined beam on a pixel, phosphors of that pixel light up to form a tiny spot of white light. The device displays different colors by combining various intensities of the three beams. Color monitors are also called RGB monitors because they use red, green, and blue as primary additive colors for generating multiple colors. Color monitors use multiple bits to represent each pixel. For example, if display mechanism represents each pixel by one byte (8 bits), it can indicate one of 2 8 = 256 different colors to draw the pixels. That is, the monitor supports 256 different colors. There are millions of colors when we consider all shades and mixtures of color possible, for which 256 is far too few colors. High-end monitors use 24 bits to represent each pixel. Such monitors can support 224 or about 16 million different colors. Based on the resolution and number of colors supported, several standards for color monitors have evolved. We can implement a standard by installing an add-on board (commonly known as a graphics adapter board or a video card) in the main computer box and connecting it to the monitor. The four popular standards are: (a) (b) (c) (d)
CGA (Color Graphics Adapter). It has a low resolution of 320 × 200 and supports up to 16 colors. EGA (Extended Graphics Adapter). It has a resolution of 640 × 350 and supports up to 16 colors. VGA (Video Graphics Array). It has a resolution of 640 × 480 and supports up to 256 colors. Super VGA. It provides resolution from 800 × 600 to 1280 × 1024 and supports 256 or more colors.
All modern personal computers come with at least VGA output. A video card (graphics adaptor card) has its own memory called video RAM. The higher the resolution and the more colors a monitor supports, the larger is the video RAM required. Figure 6.22 summarizes the video RAM capacity required for different types of monitors. Memory required for number of colors supported (in bytes)
Resolution (Total number of pixels)
2 colors (Requires 1 bit per pixel)
4 colors (Requires 2 bits per pixel)
8 colors (Requires 3 bits per pixel)
16 colors (Requires 4 bits per pixel)
256 colors (Requires 8 bits per pixel)
640 × 480 (3,07,200)
(307200 × 1)/8 = 38,400 bytes
(307200 × 2)/8 = 76,800 bytes
(307200 × 3)/8 = 1,15,200 bytes
(307200 × 4)/8 = 1,53,600 bytes
(307200 × 8)/8 = 3,07,200 bytes
800 × 600 (4,80,000)
(480000 × 1)/8 = 60,000 bytes
(480000 × 2)/8 = 1,20,000 bytes
(480000 × 3)/8 = 1,80,000 bytes
(480000 × 4)/8 = 2,40,000 bytes
(480000 × 8)/8 = 4,80,000 bytes
1024 × 768 (7,86,432)
(786432 × 1)/8 = 98,304 bytes
(786432 × 2)/8 = 1,96,608 bytes
(786432 × 3)/8 = 2,94,912 bytes
(786432 × 4)/8 = 3,93,216 bytes
(786432 × 8)/8 = 7,86,432 bytes
1280 × 1024 (13,10,720)
(1310720 × 1)/8 = 1,63,840 bytes
(1310720 × 2)/8 = 3,27,680 bytes
(1310720 × 3)/8 = 4,91,520 bytes
(1310720 × 4)/8 = 6,55,360 bytes
(1310720 × 8)/8 = 13,10,720 bytes
Figure 6.22. Video RAM capacity required for different types of monitors.
Flat Panel Monitors A flat panel monitor (see Figure 6.23) is only about one inch thick, is light in weight, and consumes less electricity as compared to a CRT monitor. It is most suitable for use with portable computer systems, such as a laptop, because such systems must be compact in size, light in weight, and should consume less power.
.
■ Output Devices
The display technologies used by flat panel monitors include liquid-crystal display (LCD), electro-luminescent display (ELD), and gas-plasma display (GPD). Out of these, LCD monitors are in most common use. As the name implies, LCD monitors use a special kind of liquid crystals to display images on the screen. The liquid crystals used in these monitors are normally transparent, but become opaque when charged with electricity.
217
Foldable flat screen
Keyboard, trackball, hard disk, USB port, etc. are in this unit
Figure 6.23. A notebook computer with a flat panel monitor.
Liquid crystals do not glow when charged, so there is not enough contrast between the images displayed and their background to make them clearly legible under all lighting conditions. Hence, LCD monitors use a light source behind the screen to make the screen easier to read.
Dumb, Smart, and Intelligent Terminals Depending on their local processing capability, we classify terminals (monitors along with keyboards) as dumb, smart, and intelligent. A dumb terminal is a low-cost terminal, without any local processing capability. It transmits all input data as received to the computer. This type of data is called “dirty” data because it contains errors along with good data. The computer identifies errors and informs the terminal operator. In this process, processing based on input data is delayed until “clean” data is obtained from the operator. This delay, accompanied by the need to process the data again, can be expensive. A smart terminal has a microprocessor (a CPU) and some internal storage in addition to I/O capabilities. It has local data editing capability and the ability to consolidate input data prior to sending it to the computer. Preliminary processing of input data takes place locally, and the terminal cleans up the data before transmitting it to the main computer for processing. This saves clean data preparation time as compared to dumb terminals. However, users cannot program smart terminals. An intelligent terminal is a small computer by itself. It consists of a built-in microcomputer and users can program it. These programmable terminals have extensive local data processing capability. They can maintain large working files locally and can handle small data processing jobs, without the need to interact with the main computer. They have their own secondary storage devices like disk, which they use during processing of these small jobs. Programmed error-detection tests can also be stored in the terminal for validation of input data, which can ensure transmission of clean data to the main computer system for further processing. Most terminals used today are intelligent or at least smart.
Terminal Emulators Computers are often connected to networks and communicate with other computers on the network. In this case, computers interact with each other in client-server mode and may keep changing their roles of client and server at different occasions. In such a situation, a full-fledged computer like a PC or a workstation is required to serve as a terminal for communicating with a remote computer. However, a PC or a workstation is not a terminal, and does not act like a terminal. A simple way to solve this problem is to write a software program that runs on the PC or workstation, communicates with the network interface over a serial port on the PC or workstation, and pretends that it is a terminal. Such a program is called terminal emulator.
218 Chapter 6 ■ Input-Output Devices
Privacy Filters Often users work in an open area with sensitive information displayed on their monitors, and have the fear of their coworkers around them viewing the displayed information. A privacy filter, which fits over a monitor’s screen, hides the displayed contents from prying eyes. It is made of a plastic film that is inlaid with microscopic black strips. A user sitting in front of the screen can see through the filter clearly, but the screen looks black to others standing/sitting nearby on either side.
Printers Printers are the most popular hard-copy output devices used today.
Parameters for Classifying Printers We classify modern printers based on: 1.
Printing technology. According to this classification, common types of printers are dot-matrix printers, inkjet printers, drum printers, chain/band printers, and laser printers.
2.
Printing approach. According to this classification, printers are of impact and non-impact types. Impact printers use the approach of hammering a typeface against paper and inked ribbon to produce printed output. Nonimpact printers do not hit (impact) a ribbon to print. They use thermal, electrostatic, chemical, or inkjet technology to produce printed output. Impact printers can produce multiple copies by using carbon in a single print cycle, which is not possible with a non-impact printer. However, impact printers are noisy in operation and normally use a cover to reduce noise level. Non-impact printers are quiet in operation.
3.
Printing speed. According to this classification, printers are classified as character, line, and page printers. As the names imply, a character printer prints one character at a time, a line printer prints one line at a time, and a page printer prints an entire page at a time. Accordingly, we measure printer speeds in characters-persecond (cps) for character printers, lines-per-minute (lpm) for line printers, and pages-per-minute (ppm) for page printers. Print-speed ranges for the three types of printers are 40 to 450 cps, 1000 to 5000 lpm, and 4 to 800 ppm.
4.
Printing quality. Quality of printed output is directly proportional to resolution of printing, measured in dots per inch (dpi). The dpi qualifier refers to the capability of a printer to print number of dots per linear inch, horizontally or vertically. That is, a 300-dpi printer is capable of printing 90,000 (300 × 300) dots per square inch. According to this classification, we classify printers as near-letter-quality (NLQ), letter-quality (LQ), near-typeset-quality (NTQ), and typeset-quality (TQ). The resolution of near-letter-quality printers is about 300 dpi, letter-quality printers is about 600 dpi, near-typeset-quality printers is about 1200 dpi, and typesetquality printers is about 2000 dpi. Users use NLQ and LQ printers for ordinary day-to-day printing jobs, and NTQ and TQ printers for top-quality professional printing, such as in publishing industries.
5.
Language scripts. Printers are available in almost all scripts, such as, English, Devnagari, Japanese, Arabic, etc. According to this classification, printers are classified based on the scripts (also called fonts) supported. A printer may support multiple scripts simultaneously to print in multiple languages. For example, a printer may be capable of printing in both English and Devanagari.
6.
Color. Printers can print either in a single color (such as black, red, blue), or in two to three colors (such as a document printed in black and white with keywords printed in red), or in multiple colors. Although printers that print in a single or two to three colors are suitable for printing textual documents, multi-color printers are desirable for printing other types of documents, such as those containing graphs and images.
Different types of printers available today are dot-matrix, inkjet, drum, chain/band, laser, and hydra printers. They are described here.
■ Output Devices
219
Dot-Matrix Printers Dot-matrix printers are character printers that print one character at a time. They form characters and all kinds of images as patterns of dots (see Figure 6.24). Figure 6.25 shows the printing mechanism of a dotmatrix printer. It has a print head that moves horizontally (left to right and right to left) across the paper. The print head contains an array of pins. The printer can activate each pin independent of others to extend and strike against an inked ribbon to form a dot on the paper. To print a character, the printer activates the appropriate set of pins as the print head moves horizontally. For faster printing, many dot-matrix printers print both ways – while the print head moves from left to right and while it moves from right to left, on return. Such method is called bi-directional printing. Figure 6.26 shows a dot-matrix printer. Paper below the ribbon
Inked ribbon
Printed characters formed of dots in a 5 x 7 matrix
ABCDEFGHIJKLMN OPQRSTUVWXYZ 0123456789-., &/$*#%@=(+) Figure 6.24. Formation of characters as patterns of dots.
Print head pins
Direction of movement of print head pins
Direction of movement of print head Print head
Figure 6.25. Printing mechanism of a dot-matrix printer.
Print head Pins
Figure 6.26. A dot-matrix printer.
Figure 6.27. Arrangement of pins in an 18-pin print head of a dot-matrix printer. It permits dots to overlap to increase the density of dots and, therefore, quality of printed output.
220 Chapter 6 ■ Input-Output Devices In Figure 6.24 all characters are formed by 5 horizontal and 7 vertical dots. Such a pattern is called a 5 × 7 matrix. It can be generated by a print head having 7 pins. However, notice that the print head in Figure 6.25 has 9 pins. The printer uses the bottom 2 pins for printing lower-case letters that extend below the line (for example, p, g, q, and y). Quality of printed output is directly proportional to the density of dots in the matrix. Dot matrix printers with 9, 18, or 24 pins are available. Printers with 24 pins can print near-letter-quality characters that appear solid. Figure 6.27 shows the arrangement of pins in an 18-pin print head of a dot-matrix printer. Dot matrix printers can print any shape of character that a program can describe as patterns of dots. Hence, they can print special characters, different sizes of print, and graphics such as charts and graphs. Dot-matrix printers are impact printers because they print by hammering pins on an inked ribbon to leave ink impressions on a paper. Hence, we can use them for producing multiple copies by using carbon paper or its equivalent. Due to impact printing, dot-matrix printers are noisy as compared to non-impact printers. Dot-matrix printers are usually monochrome. However, some dot-matrix printers use a ribbon having rows of two to three colors to produce printed output in few colors. Dot-matrix printers are slow, having printing speeds in the range of 30 to 600 characters per second. However, they are cheap in terms of both initial cost and cost of operation. Hence, individuals and organizations prefer them for generating hard-copy outputs, if the speed and quality of printing are not important factors. They also use them for applications requiring multi-copy output such as shipping forms and invoices that rely on impact printing for generating multiple copies.
Inkjet Printers Inkjet printers are character printers that form characters and images by spraying small drops of ink on a paper. The print head of an inkjet printer contains up to 64 tiny nozzles. The printer can heat up the nozzles selectively in a few microseconds by an integrated circuit resistor. When the resistor heats up, the ink near it vaporizes and ejects through the nozzle making a dot on the paper placed in front of the print head. To print a character, the printer selectively heats the appropriate set of nozzles as the print head moves horizontally. Figure 6.28 shows an inkjet printer. Inkjet printers produce higher quality output than dot-matrix printers do, because they form characters by very tiny ink dots. A highresolution inkjet printer has as many as 64 nozzles within a height of 7mm, providing print resolution of around 360 dots per inch. Figure 6.28. An inkjet printer.
Inkjet printers can print any shape or characters that a program can describe as patterns of tiny dots. Hence, they can print special characters, different sizes of print, and graphics such as charts and graphs. Inkjet printers are non-impact printers because they print by spraying ink on paper. Hence, they are quieter in operation, but we cannot use them for producing multiple copies of a document in a single printing. Inkjet printers are both monochrome and color. A monochrome inkjet printer uses a single cartridge with black ink, whereas a color inkjet printer uses two ink cartridges – black and tri-color. The tri-color cartridge contains red, blue, and green colors in a package that can mix appropriate amount of these colors with black from the other cartridge to get any desired color. Hence, inkjet printers can produce multi-colored and photo-quality output.
■ Output Devices
221
Inkjet printers are slower than dot-matrix printers, with printing speeds ranging from 40 to 300 characters per second. They are costlier than dot-matrix printers. Users prefer them for better quality output, if speed of printing is not an important factor.
Drum Printers Drum printers are line printers that print one line at a time. Figure 6.29 shows the print mechanism of a drum printer. It consists of a solid cylindrical drum with characters embossed (raised characters) on its surface in the form of circular bands. Each band consists of all printing characters supported by the printer in its character set. Total number of bands is equal to the maximum number of characters (print positions) that the printer can print on a line. Hence, a drum printer with 132 characters per line and supporting a character set of 96 characters will have altogether 12,672 (132×96) characters embossed on its surface. In addition to the drum, the printer has a set of hammers (one opposite each band) mounted in front of the drum in a manner that an inked ribbon and paper can be placed between the hammers and the drum. The total number of hammers is equal to the total number of bands on the drum, which is also equal to the total print positions per line. The drum rotates at a high speed. To print a character at a desired print position, the printer activates the appropriate hammer when the character embossed on the band at the print position passes below it. Hence, the drum would have to complete one full revolution to print each line of output. This means that the printer does not print all characters on a line at exactly the same time, but the time required to print an entire line is so fast that it appears as if it prints one line at a time. Figure 6.30 shows a drum printer. The drum of a drum printer is expensive and we cannot change it often. Hence, drum printers can print a pre-defined set of characters only in a pre-defined style embossed on the drum. Drum printers, therefore, do not have the ability to print any shape of characters, different sizes of print, and graphics such as charts and graphs. Hammers (one for each band) Paper Ribbon WW W WWWWWWWWWWW V V V V V V V V V V VV V V U U U U U U U U U U U U U U T T T T T T T T T T T T T T S S S S S S S S S S S S S S
Solid cylindrical drum with embossed characters
R R R R R R R R R R R R R R Q Q Q Q Q Q Q Q Q Q Q Q Q Q P P P P P P P P P P P P P P O O O O O O O O O O O O O O N N N N N N N N N N NN N N
Total number of bands is equal to the maximum number of characters (print positions) on a line. Each band has all characters supported by the printer. Figure 6.29. Printing mechanism of a drum printer.
222 Chapter 6 ■ Input-Output Devices Drum printers are impact printers because they print by hammering on a paper and inked ribbon against characters embossed on the drum. Hence, we can use them for producing multiple copies by using carbon paper or its equivalent. Due to impact printing, drum printers are noisy in operation and often use a cover to reduce noise level. Printing speeds of drum printers are in the range of 300 to 2000 lines per minute. Figure 6.30. A drum printer.
Chain/Band Printers Chain/band printers are line printers that print one line at a time. Figure 6.31 shows the print mechanism of a chain/band printer. It consists of a metallic chain/band on which all characters of the character set supported by the printer are embossed. A standard character set may have 48, 64, or 96 characters. To enhance printing speed, these printers have several sets of embossed characters in the character set on the chain/band. For example, the chain/band of a 64 character set printer may have four sets of 64 characters each embossed on it. In this case, the chain/band will have altogether 256 (64×4) characters embossed on it.
Complete chain is composed of five sections of 48 characters each
One section of 48 characters
Direction of movement of the chain
Paper Ribbon Hammers 132 print positions Figure 6.31. Printing mechanism of a chain/band printer.
The printer has a set of hammers mounted in front of the chain/band in a manner that we can place an inked ribbon and paper between the hammers and chain/band. The total number of hammers is equal to the total number of print positions. Therefore, a printer supporting 132 print positions will have 132 hammers. The chain/band rotates at a high speed. To print a character at a desired print position, the printer activates the appropriate hammer when the character embossed on the chain/band passes below it. Since the character set appears several times on the chain/band, it is not necessary to wait for the chain/band to make a complete revolution to position the desired character in the correct print position. Unlike the drum of a drum printer, we can change the chain/band of a chain/band printer. This allows use of different fonts (styles of characters) and different scripts (languages) with the same printer. Like drum printers, chain/band printers can print only pre-defined sets of characters embossed on the chain/band used with the printer. Chain/band printers, therefore, do not have the ability to print any shape of characters, different sizes of print, and graphics such as charts and graphs. Chain/band printers are impact printers because they print by hammering on a paper and inked ribbon against characters embossed on the chain/band. Hence, they enable production of multiple copies by using carbon paper or its equivalent. Due to impact printing, chain/band printers are noisy in operation and often use a cover to reduce noise level. Printing speeds of chain/band printers are in the range of 400 to 3000 lines per minute. Figure 6.32 shows a chain/band printer.
■ Output Devices
223
Laser Printers Laser printers are page printers that print one page at a time. Main components of a laser printer are a laser beam source, a multi-sided mirror, a photoconductive drum, and toner (tiny particles of oppositely charged ink). To print a page of output, the printer focuses the laser beam on the electro statically charged drum by the spinning multi-sided mirror. The mirror focuses the laser beam on the surface of the drum in a manner to create the patterns of characters/images that the printer has to print on the page. Since the drum is photoconductive, print mechanism creates a difference in electric charge on those parts of the drum surface, which are exposed to the laser beam. As a result, the toner, composed of oppositely charged ink particles, sticks to the drum in the places where the laser beam has charged the drum’s surface. The printer then applies heat and pressure to fuse the toner permanently on the paper to generate the printed output. It then rotates the drum and cleans it with a rubber blade to remove the toner sticking to its surface to prepare the drum for next page printing. Figure 6.33 shows a laser printer.
Figure 6.32. A chain/band printer.
Figure 6.33. A laser printer.
Laser printers produce high quality output because they form characters by tiny ink particles. The most common laser printers have resolution of 600 dpi (dots per inch), whereas some high-end laser printers have resolution of 1200 dpi. Due to their high resolution, these printers give excellent graphics art quality. Laser printers can print any shape of characters that a program can describe as patterns generated by the laser beam. Hence, they can print special characters, different sizes of print, and graphics such as charts and graphs. Laser printers are non-impact printers. Hence, they are quiet in operation, but we cannot use them for producing multiple copies of a document in a single printing. Although, earlier laser printers could print only in black color, because they had a single toner-drum arrangement, now color laser printers are also available at affordable price. A color laser printer has four-color toners, each with a drum of its own. It uses the drums and toners in the same manner as described above, but charges each drum in such a way that together they produce the desired color output. Laser printers are faster in printing speed than other printers discussed earlier. Low-speed laser printers can print 4 to 12 pages per minute. High-speed laser printers are also available. Owing to their better print quality and printing speed, laser printers are more expensive than other types of printers.
Hydra Printers Today, modern offices need the following office equipment – a printer, a copy machine, and a scanner. The need for these machines at one place and considerable overlap in the technologies used in them has triggered their manufacturers to create an all-in-one multifunctional device, which can perform all these functions. Such a device is called a hydra printer. Organizations prefer them because they occupy less table space and consume less power as compared to multiple devices.
224 Chapter 6 ■ Input-Output Devices
Plotters Many engineering design applications like architectural plan of a building, design of mechanical components of an aircraft or a car, etc., often require high-quality, perfectly-proportioned graphic output on large sheets. Various types of printers described earlier are not suitable for meeting this output requirement of such applications. Hence, such applications use plotters. A plotter is an ideal output device for architects, engineers, city planners, and others who need to generate high precision, hard-copy, graphic output of varying sizes. The two types of plotters in common use are: 1. Drum plotter. It consists of a drum, one or more penholders mounted perpendicular to the drum’s surface,
and a graph-plotting software. The drum rotates in both clockwise and anti-clockwise directions to produce vertical motion. The pen(s) clamped in the holder(s) moves left to right or right to left to produce horizontal motion. The graph-plotting software controls movements of the drum and pen(s). Hence, under software control, the drum and pen(s) move simultaneously to draw designs and graphs on a sheet placed over the drum. The plotter can also annotate a design or graph so drawn by drawing characters of various sizes with the pen. Since each pen is program selectable, a user can use the plotter to produce multi-colored designs by mounting pens having ink of different colors in different holders. Figure 6.34 shows a drum plotter. 2. Flatbed plotter. It consists of a rectangular flatbed table, one or more penholders mounted perpendicular to
the table’s surface, and a graph-plotting software. It plots a design or graph on a sheet of paper spread and fixed over the table. Normally, the paper does not move and the pen holding mechanism provides all types of motions necessary to draw complex designs and graphs. Hence, under software control, the pen(s) move in the required manner to draw designs and graphs on the sheet placed on the flatbed table. The plotter can also annotate a design or graph so drawn by drawing characters of various sizes with the pen. Provision is also there to mount more than one pen in the pen(s) holding mechanism. Since each pen is program selectable, a user can use the plotter to produce multi-colored designs by mounting pens having ink of different colors in different holders. The plot size is restricted by the area of the bed. Some are as small as A4 size (8"×11" page), while some large beds used in the design of cars, ships, aircrafts, buildings, highways, etc. are up to 20 ft. by 50 ft. Some plotters also etch on plastic or metal plates. In this case, the plastic or metal sheet spreads on the bed and the drawing pen has a sharp-edged needle. Figure 6.35 shows a flatbed plotter.
Paper Design drawn on the paper
Design drawn on the paper Paper
Figure 6.34. A drum plotter.
Figure 6.35. A flatbed plotter.
Plotters are slow in operation because of excessive mechanical movement required during plotting. Hence, mismatch between CPU speed and plotter speed is large. Hence, in most cases of plotting an output, the CPU first transfers the output onto a tape or disk, and then activates the plotter to plot the output from the tape or disk.
3D Printers As the name signifies, a 3D printer prints (creates) three-dimensional objects. Figure 6.36 shows an example of an object printed by a 3D printer. This is contrary to the types of printers that we discussed earlier, which print on 2D surface (on paper or on sheet of plastic/metal).
■ Output Devices
3D printers use Additive Manufacturing (AM) techniques to create 3D objects. In an AM technique, manufacturing material is combined/joined/added/glued together to create a 3D object. For example, a potter makes a pitcher using AM technique. To create a pitcher, he/she adds layers of mud, giving proper shape to each layer, while moving from bottom to top. This is contrary to Subtractive Manufacturing (SM) technique, in which material is carefully removed/cut out/hollowed from a piece. For example, a sculptor often uses SM technique to carve out a statue from a single piece of stone/wood. As compared to SM techniques, AM techniques are generally cheaper because they enable manufacturing of objects having complex shapes with less material waste. 3D printers are gaining popularity because they automate the AM techniques of creating 3D objects, thus enabling creation of 3D objects in less time and at lower cost than manual AM techniques.
225
Figure 6.36. An example of an object printed by a 3D printer.
How 3D Printers Print 3D Objects? A 3D printer requires the following three steps to print a 3D object: 1. First create a 3D digital file (3D model) of the object. The first step is to create a 3D digital file of the object to be printed. For this, the user either uses a 3D modeling software to create a CAD (Computer Aided Design) file for the object, or uses a 3D scanner to create a 3D digital file of the object. 2. Then slice the 3D model into horizontal layers. As the next step, the user uses a slicing software to slice the 3D model of the object into hundreds/thousands of thin horizontal layers (16 to 180 microns). Note that the 3D printer forms the 3D object by placing and gluing these layers one-by-one on top of each other in the right sequence. Slicing software often comes with a 3D printer, and the user should use the slicing software of the same 3D printer on which he/she is going to print the object. 3. Then upload the sliced file and print. Finally, the user uploads the sliced file to the 3D printer by giving a print command to it. The 3D printer prints the object layer-by-layer by depositing and solidifying the manufacturing material used by the printer on top of an already printed layer. As soon as the printer completes depositing and solidifying the last layer of material, the printed 3D object is ready.
Types of 3D Printers All 3D printers use additive manufacturing technology. However, they mainly differ in the way they build an object layer-by-layer and the material they use for manufacturing objects. We describe here some commonly used 3D printing technologies. 1. Fused Deposition Modeling (FDM). The method uses plastic/metal filament (wire) as the build material that melts when heated and quickly solidifies after extrusion (release) from a nozzle. The printer consists of a supply reel, which supplies material filament to an extrusion nozzle. The extrusion nozzle has a heating mechanism to melt the filament. It can move horizontally in both left and right directions under direct control of computer-aided manufacturing (CAM) software. To print an object, the printer releases the molten material from the nozzle on a specific path determined by the software, to create the first horizontal layer of the object on a build platform/tray located below the nozzle. The molten material instantly cools down and solidifies. The printer then lowers the build platform by a distance equal to the thickness of a layer, and again releases the molten material from the nozzle to form the next layer on top of the already built layer. The printer repeats this process until all layers are completed. In this manner, the printer creates the entire 3D object
226 Chapter 6 ■ Input-Output Devices layer-by-layer. Color printers based on this technology use multiple supply reels (each having filament of different color) and multiple nozzles. The software takes care of releasing the right color of molten material from the appropriate nozzle to create a multi-color object. 2. STereoLithography (STL) and Digital Light Processing (DLP). Both the methods use photopolymerization technique, which involves solidification of photosensitive resin (sticky substance) by means of a light source. 3D printers based on this technique use a vat (tub/tank) containing liquid (photosensitive polymer) resin as the build material. The printer sprays the liquid resin through a nozzle onto a build platform/tray in ultra-thin layers and uses a light source to solidify the liquid resin. SLA uses laser, while DLP uses traditional arc lamp as the source of light. The printer lowers the build platform by distance equal to the thickness of a layer, again sprays the liquid resin from the nozzle, and solidifies it using the light source, to form the next layer on top of the already built layer. The printer repeats this process until all layers are completed. In this manner, the printer creates the entire 3D object layer-by-layer. Color printers based on this technology use multiple vats (each having liquid resin of different color) and multiple nozzles. The software takes care of releasing the right color of liquid resin from the appropriate nozzle to create a multi-color object. 3. Selective Laser Sintering (SLS). The method uses small particles of plastic, ceramic or glass in powder form as the build material. The printer uses high power laser to melt and fuse (sinter) these particles to create the desired 3D object. The printer spreads a thin layer of plastic/ceramic/glass powder onto a build platform/tray and selectively sinters the powder particles using a high power laser source, mapping the shape of the desired 3D object. The printer lowers the build platform by distance equal to the thickness of a layer, again spreads the powder particles, and selectively sinters the powder particles using the laser source, to form the next layer on top of the already built layer. The printer repeats this process until all layers are completed. In this manner, the printer creates the entire 3D object layer-by-layer. As the printer creates the 3D object layer-by-layer, excess un-melted powder acts as support for the 3D object and the printer finally removes it from the object and reuses it during the next printing cycle. 3D printers that use this method of printing, but use metal powder as the build material, instead of plastic/ceramic/glass powder, are called Direct Metal Laser Sintering (DMLS) printers. 4. A much faster, experimental 3D printing technique. A team of scientists at the University of Michigan, USA is currently experimenting with a new STL-based 3D printing technique, which promises to print 3D objects at about 100 times the speed of other 3D printers based on current technologies. The technique uses a vat of specially engineered chemical resin along with Ultra Violet (UV) and blue LED lights, instead of laser beam. It prints a 3D object by sending UV light through a glass-bottomed vat of specially designed resin. Then, at the same time, through that same glass window, it sends patterns of bright and dim blue light. The specially engineered resin has the property that it hardens (polymerizes) when exposed to a particular shade of blue light, but does not harden in the presence of a certain wavelength of UV light. Hence, the technique uses UV light inhibitor in conjunction with blue light to sculpt 3D surfaces and contours of hardened resin. It also uses it to prevent the hardened resin from sticking to the window. The team of scientists working on this technique claim that this is the first truly 3D printing technique in that it prints not just a series of single 2D layers, but rather entire 3D wedges of material in one pass. 3D printing technologies are still evolving with gradual addition of many new technologies to the list above. To choose a 3D printer based on a particular technology, a user needs to make his/her judgement based on function, material, budget and design parameters.
Applications of 3D Printers It is faster and cheaper to manufacture many types of 3D objects by 3D printing method than by other manufacturing methods. By using 3D printing, it is also possible to create complex and customized designs with greater ease and cheaper cost as compared to other manufacturing methods, because no expensive tooling is required to create a new type of object through 3D printing method. Hence, many types of industries are now using 3D printing technologies as their preferred manufacturing method. We provide here a few such examples.
■ Output Devices
227
1. Rapid prototyping. Manufacturers extensively use 3D printers in their design process to create prototype designs, before finalizing a design for mass production. They quickly print an object from its paper design, and if they do not like the design, they make instant changes to the design on the computer and re-print the revised prototype, saving enormous time and effort as compared to conventional methods of creating prototype designs. 2. Rapid manufacturing of customized designs. With 3D printing, manufacturers are allowing customers to place orders for products with customized designs. For example, customers can now order for decoration pieces of fancy designs and matching sizes, lighting fixtures and lamp shades of unique shapes/sizes/colors, chocolates/candies/cakes of fancy shapes and colors, ornaments/jewelry of fancy designs and colors, etc.; and suppliers can rapidly manufacture and deliver these products to customers by using 3D printers. This was not possible so easily with conventional methods of manufacturing.
Filament Gears pull & feed the filament to extrusion nozzle
Supply reel
Heating mechanism to heat & melt the filament
X
Y
- - - - - - - - - - - - - -- - - - - - - - - - - - - - Nozzle extruding molten material Melted material is deposited in layers to create the 3D object
Build platform is lowered one layer thickness at a time
Z Figure 6.37. Schematic of FDM (Fused Deposition Modeling) technology.
228 Chapter 6 ■ Input-Output Devices 3. Manufacturing body/engine parts. Several types of industries use 3D printing for manufacturing body/engine parts. For example, car manufacturers manufacture parts of car body/engine, aircraft manufacturers manufacture parts of airplane body/engine; furniture manufacturers manufacture parts of assembled furniture; construction companies manufacture parts of assembled buildings; etc., by using 3D printers. 4. Manufacturing implants and prosthetics. In the medical world, implants and prosthetics (an artificial device that replaces a missing body part, which might have been lost through accident, disease, or congenital conditions) need to be custom designed for each patient. Manufacturers of such implants and prosthetics are quickly manufacturing such designs by using 3D printers. Although, grown up people may use their prosthetic devices for long time, but children need to change their prosthetic devices periodically, as they grow. It is possible now to manufacture such prosthetic devices at affordable costs with 3D printing. Due to this, such patients around the world are experiencing improved quality of medical care, which was not available earlier. 5. Manufacturing hearing aid and dental appliances. Just as implants and prosthetics, hearing aid and dental appliances (such as, braces, dentures, crowns, night guards, retainers, etc.) also need to be custom designed for each patient. Many dentists are now using 3D printers to manufacture such dental appliances in the exact shape and size that their clients need at a fraction of the cost and time required earlier for making such appliances. 6. Manufacturing museum souvenirs. Museums across the world are using 3D printers to manufacture miniature versions of objects kept in the museum for sale as museum souvenirs. Figure 6.37. shows the schematic diagram of FDM technology. The figure shows a flower vase printed by the author using a 3D printer based on FDM technology. The author had used orange color plastic filament to print an orange color flower vase made of plastic.
Computer Output Microfilm (COM) COM is an output device that records computer output as microscopic filmed images on a sheet of microfilm usually measuring 4"×6". This sheet of film is called a microfiche (“fiche” is pronounced as “fish”. It is a French word that means card). COM recording process produces page images that are 48 or more times smaller than those produced by conventional printers. Hence, a single microfiche can hold about 300 times as much information as a standard computer printed page. A microfiche roll consists of several microfiche sheets. COM uses a microfilm recorder for recording output on a microfiche, and a microfilm reader to view recorded information. The microfilm recorder receives output from a computer system, which it displays page-by-page on to a screen. A high-speed camera (in-built into the system) then takes picture of the displayed information. In some systems, the recorder processes the film from the camera, while other systems use a separate automated film developer to produce filmed images of the output. A user uses a microfilm reader (see Figure 6.38) to view the output recorded on a microfiche. It operates on a “back projection” principle and displays a frame (a single page of information) at a time on a translucent screen, whose size is about 8"×11". A user reads the displayed information on the screen. Most microfilm readers also have a printer in-built into it (or attached to it) to allow a user to print selected pages for hard-copy output.
Figure 6.38. A microfilm reader.
■ Output Devices
229
Most COM systems come with computer-assisted-retrieval software, which allows a user to search a document quickly from thousands of pages of recorded information on the film by specifying pre-assigned index terms. As compared to a printer, a COM system has the following advantages and limitations: 1. Information recording speed is many times faster for a COM system as compared to a printer. A COM system
is 20 to 25 times faster than an average line printer. 2. Microfilm output is many times more compact than a printed page output. A single microfiche can hold about
300 times as much information as a standard computer printed page. Thus, the mailing and storage costs of filmed documents are much less than paper documents. 3. Microfilm output is less costly than printed output. The cost of paper needed for printing a 1000 page
document is about 20 to 30 times more than the cost of the film needed to record the same information. 4. The initial investment required for installing a COM system is much more than that required for buying an
ordinary printer. Hence, use of a COM system is justified only in case of high volume workload. 5. Many people feel uncomfortable using a COM system because of retrieval delays and lack of facility to write
notes on margins of the reports. A COM system provides an easy and compact way of recording and storing information, and retrieving a piece of information in a matter of seconds. It is ideal for use in applications that need to record and store large volume of information, and selectively retrieve them off-line. Organizations often need to maintain records of their various types of documents, such as catalogues, bills, invoices, etc., for a number of years before destroying them. A COM system is most suitable for this purpose. Insurance companies, banks, libraries, public utilities departments, government agencies, and many other types of organizations are regular users of COM systems.
Screen Image Projector Screen image projector is an output device, which is used to project information from a computer on a large screen (such as a cloth screen or wall) so that a group of people can view it simultaneously. It is useful for making presentations to a group of people with direct use of a computer. We can plug a screen image projector to a computer system directly, and a presenter makes a presentation to a group of people by projecting the presentation material page-by-page on a large screen with the help of computer’s keyboard or mouse. With this facility, the presenter can also directly point to, mark, or edit certain portions of the contents of the displayed material during the presentation to make the presentation more understandable. Additionally, a presenter can prepare a full-fledged multimedia presentation with audio, video, image, and animation, and can make a lively and interesting presentation by using this device. Screen image projectors have become common presentation equipment today. Users often use them with portable notebook computers to setup a modern presentation facility quickly at any place with great ease. Like monitors, screen image projectors provide a temporary, soft-copy output. Figure 6.39 shows a screen image projector.
Operations buttons On/Off light indicator Projection lens Figure 6.39. A screen image projector.
Voice Response Systems Just as a speech recognition system allows a user to talk to a computer, a voice response system enables a computer to talk to a user. A voice response system has an audio-response device that produces audio output. Obviously, the
230 Chapter 6 ■ Input-Output Devices output is temporary, soft-copy output. Voice response systems are of two types – voice reproduction system and speech synthesizer. They are described here.
Voice Reproduction System A voice reproduction system produces audio output by selecting an audio output from a set of pre-recorded audio responses. The set of pre-recorded audio responses may include words, phrases, or sentences spoken by human beings, music or alarms generated by musical instruments, or any other type of sound. The system first converts the analog recordings of the pre-recorded sounds into digital data and stores them on a disk or in its memory chip permanently. To produce an audio output, the system selects the corresponding sound from the set of pre-recorded sounds. It then converts the selected sound back into analog form and routes it to a speaker. Several applications use voice reproduction systems, including the following: 1.
Audio help for guiding how to operate a system. For example, banking industry uses voice reproduction systems in automatic teller machines to provide systematic guidance to customers on how to transact with the bank by using an ATM.
2.
Automatic answering machines. For example, automatic answering machines often take care of telephone enquiries for new telephone numbers in place of old numbers, or vacancy status of a flight/train.
3.
Manufactures of video games make them exciting and interesting by playing an event-based sound from a set of pre-recorded sounds.
4.
Talking alarm clocks. For example, every hour the clock speaks out what is the time by selecting the voice message corresponding to that hour from the set of pre-recorded voice messages. In addition, the clock may speak “its time to wake up” at the time set for alarm.
5.
Talking toys and home appliances also use a voice reproduction system.
Speech Synthesizer A speech synthesizer converts text information into spoken sentences. To produce speech, it combines basic sound units called phonemes. From a given text information, it combines the sequence of words into phonemes, amplifies it, and produces the output through a speaker. Several applications use speech synthesizers, including the following: 1.
For reading out text information to blind persons. For example, we can scan a recently published book using a scanner, convert it into text using OCR software, and then a speech synthesizer can read it out to blind persons. This allows blind persons to know the latest information published in a book as soon as it is printed, rather than wait for the book to appear in Braille.
2.
For allowing those persons who cannot speak, to communicate effectively. For example, a person with this type of disability types the information he/she wants to communicate to another person, and the speech synthesizer converts it into spoken words.
3.
For translation systems that convert an entered text into spoken words in a selected language. For example, a foreign tourist in India may enter a text he/she wants to communicate to an Indian, and the speech synthesizer converts it into spoken words of the selected Indian language.
■ I/O Interfaces
231
Ergonomically Designed Devices Ergonomics is the science that deals with designing and arranging machines and other things in such a way that their users can use them with maximum comfort. This helps in achieving maximum efficiency of the people working on these machines and protecting them from accidents or medical problems caused due to continuous usage of a machine for long hours. For example, prolonged usage of a badly designed keyboard can cause wrist problems, and that of a badly designed monitor can cause eyesight problems. Hence, manufactures of I/O devices try making ergonomically better devices. A few examples are (there are hundreds of such examples): 1. Originally, the keyboard of a computer was attached to its monitor. However, now keyboards are attached to
the monitor with a reasonably long cable, which enables a user to move the keyboard to a comfortable position on the table, or to place it on his/her lap during operation. 2. Many new keyboards have tilt adjustments and hand-rest pads to make their use more comfortable for the
operators. 3. A cordless mouse is more comfortable to use than a wired mouse. 4. A monitor mounted on a ball bearing enables a user to tilt and rotate it to suit his/her comfortable height and
angle of operation. Hence, it is more comfortable to use than a monitor fixed at one angle. 5. Flat-screen monitors have replaced curved-screen monitors because they improve readability and reduce
reflections. 6. Color monitors have replaced monochrome monitors because they improve readability and are more
comfortable to view. 7. Higher resolution monitors are preferred because they prevent flicker of displayed objects, which may lead to
eye problems. 8. Monitors also use special screen coatings for reducing glare on eyes.
I/O INTERFACES I/O interfaces deal with how I/O devices connect to and communicate with a computer system. The commonly used I/O interfaces are described here.
Device Controllers We saw in Chapter 4 that all components of a computer communicate with its CPU through the system bus. Hence, I/O devices need to be connected to the system bus. However, they are not directly connected to a computer’s system bus, but via an intermediate electronic device called device controller. Hence, a device controller is an interface unit between an I/O device and a computer’s system bus. On one side, it communicates with the I/O device connected to it, and on the other side, it communicates with the computer’s CPU and memory through the system bus. A single device controller usually controls multiple I/O devices. It comes in the form of an electronic circuit board (also known as an electronic card) that plugs into the system bus. There are cables from the controller to each device it controls. The cables coming out of the controller terminate usually at the back panel of the main computer box in the form of connectors called I/O ports. Figure 6.38 illustrates how device controllers connect I/O devices to a computer system. Using device controllers for connecting I/O devices to a computer system, instead of connecting them directly to the system bus has the following advantages: 1. Multiple I/O devices can share a controller, thus, allowing us to connect many I/O devices to a system.
232 Chapter 6 ■ Input-Output Devices 2. We can easily upgrade or change I/O devices without any change in the computer system. 3. We can easily plug-in to a computer system those I/O devices that are manufactured by manufacturers other
than the computer manufacturer. This provides greater flexibility of buying I/O devices of ones own choice. Device controllers may or may not have DMA (Direct Memory Access) capability. As Figure 6.40 shows, a device controller without DMA capability transfers data between a computer’s memory and an I/O device connected to it via the CPU of the computer. On the other hand, a device controller with DMA capability transfers data directly between the memory and an I/O device without the help of CPU. Obviously, a device controller with DMA capability can transfer data much faster than a device controller without DMA capability. Hence, almost all modern device controllers have DMA capability.
Memory
CPU
non-DMA path
System bus Multi-port device controller
DMA path
Single-port device controller
I/O port
I/O port
I/O port
I/O port
I/O device
I/O device
I/O device
I/O device
Figure 6.40. Connecting I/O devices to a computer system through device controllers.
Serial and Parallel Ports We connect I/O devices to I/O ports of a device controller, which in turn connects to a computer’s system bus. We plug in I/O devices into I/O ports of a computer system in the same manner as we plug in electrical appliances into electric sockets in a house. I/O ports are broadly of two types: serial ports and parallel ports. They are described here.
Serial Ports A serial port sends data bits one at a time (that is, serially). It has only two wires – one for sending data and another for receiving data. Hence, it sends eight bits of a byte one at a time. Usually, it sends two extra bits with a byte of data to help in the control and timing of communication. A chip called UART on the device controller’s card converts parallel data from the system bus into serial data that can flow through a serial cable or telephone wire. Figure 6.41 illustrates the process. Similarly, the UART converts serial data received by the device controller from the I/O device into parallel data before the device controller sends it to the system bus.
■ I/O Interfaces
233
Bit 0 Stop bit
Bit 1 S Y S T E M
Bit 2
Bit 5 B U S
Data bits of 2nd byte
Bit 3 Bit 4
Start bit
UART of device controller card
Stop bit
Start bit
Data bits of 1st byte
11100111001010110010
Bit 6
Direction of flow of data in this example
I/O P O R T
Serial I/O device
Bit 7 Parity bit
Figure 6.41. Process of data transfer in case of a serial I/O port.
Users use serial ports for connecting slow I/O devices, such as terminal, keyboard, mouse, modem, etc., that send/receive a stream of bytes serially (one bit at a time). RS-232C interface is a popular serial port standard for transmission of data over telephone lines. Figure 6.42 shows the physical appearance of a serial port.
Figure 6.42. Physical appearance of a serial port.
Parallel Ports Parallel ports transfer 8 or 16 bits of data at a time. Hence, a parallel port has 8 or 16 parallel wires for sending and receiving data. Obviously, a parallel port with 8 wires transfers one byte (8 bits) at a time, and one with 16 wires transfers two bytes (16 bits) at a time. Figure 6.43 illustrates the process. Notice that unlike in the case of serial I/O port, conversion of bytes into bit streams and vice-versa is not required in this case. This further leads to faster data transfer rate than in case of serial I/O ports. Users use parallel ports for connecting faster I/O devices such as high-speed printers and plotters, analog-to-digital and digital-to-analog converters, magnetic tape backup units, external hard disk units, etc. Figure 6.44 shows the physical appearance of a parallel port.
234 Chapter 6 ■ Input-Output Devices
Byte 2
Byte 2 Byte 1
Byte 3
S Y S T E M B U S
Byte 1
Byte 3
Bit 0
010
Bit 0
010
Bit 1
111
Bit 1
111
Bit 2
110
Bit 2
110
Bit 3
101
Bit 3
101
Bit 4
000
Bit 4
000
Bit 5
110
Bit 5
110
Bit 6
001
Bit 6
001
Bit 7
010
Bit 7
010
Parity bit 0 1 1
Device controller card
Parity bit
I/O P O R T
Parallel I/O device
011
Figure 6.43. Process of data transfer in case of a parallel I/O port.
Figure 6.44. Physical appearance of a parallel port.
SCSI Interface A computer’s system bus has finite number of slots (called expansion slots) to which we connect device controllers. If every I/O device comes with its own device controller card, we can connect only a limited number of I/O devices to a computer because it will soon run out of available expansion slots. Use of SCSI interface overcomes this problem, As described Figure 6.45. SCSI interface. in Chapter 5, a SCSI interface uses a generic device controller (called SCSI controller) and allows any device having SCSI interface to connect to the SCSI bus of the SCSI controller. That is, instead of connecting device controller cards into the computer’s system bus via the expansion slots, SCSI extends the computer’s system bus outside the computer by way of a cable, and just as we plug one extension cord into another to lengthen circuits; we can plug one SCSI device into another to form a daisy chain.
■ I/O Interfaces
235
Most current implementations of SCSI limit the number of devices to sixteen on a given daisy chain due to bus capacity limitation. This saves expansion slots in a computer for connecting other devices. If none of a computer’s device controller cards has a SCSI interface, we can purchase a separate SCSI interface (as the one shown in Figure 6.45) to plug into an existing parallel port to which we can connect additional SCSI devices.
USB (Universal Serial Bus) Standard We often use USB standard to connect I/O devices to a computer. It is a serial bus, which supports four speeds – a super-speed mode of 5 Gbits/sec, a high-speed mode of 480 Mbits/sec, a full-speed mode of 12 Mbits/sec, and a lowspeed mode of 1.5 Mbits/sec. USB version 3.0 supports super-speed mode, version 2.0 supports high-speed mode, while the earlier version 1.1 supported full-speed and low-speed modes. The latest USB version 4.0 promises to support 40 Gbits/sec. USB is host controlled in the sense that there can be only one host per bus. USB standard aims at and is limited to single point-to-point connections such as a mobile phone and personal organizer, and not multiple hub, multiple device desktop configurations. The USB host is responsible for undertaking all transactions and scheduling bandwidth. USB uses a tiered star topology, similar to that of 10BaseT Ethernet. This imposes the use of a hub somewhere for connecting devices to USB. Many devices have USB hubs integrated into them. For example, keyboard and monitor contain a hub connected to a computer’s USB port. We can connect other I/O devices to the keyboard or monitor using the hub they have. For example, we often plug a mouse into the back of a keyboard, and a digital camera into the front/back/side of a monitor. USB’s tiered star topology, rather than daisy chaining devices together, has the following benefits: 1.
We can monitor power to each device and even switch off a device, if an over current condition occurs without disrupting other USB devices.
2.
It can support all four types (super-speed, high-speed, full-speed, and low-speed) of devices, with the hub filtering out transactions of different speed, so that low-speed devices do not receive high speed transactions.
At any given time, we can connect up to 127 devices to a single USB. If we need to connect more devices, we can add another port/host. While earlier USB hosts had two ports, the latest ones support 4 and 5 port host cards with an internal port for hard disks. The early hosts had one USB controller, and thus, both ports shared the same available USB bandwidth. As bandwidth requirements grew, manufacturers are now making multi-port cards with two or more controllers, allowing individual channels for each port. USB supports plug-and-play facility with dynamically loadable and unloadable drivers. When a user plugs in a device into the bus, the host detects this addition, interrogates the newly inserted device, and loads the appropriate driver. Once the user finishes using the device, he/she pulls the cable out and the host detects its absence and automatically unloads the driver. A pen drive plugs into a USB port of a computer. Another important feature of USB is its support for bus-powered devices – devices that obtain its power from the bus and require no batteries, or external plugs, or additional cables. Additionally, a USB device enters suspend mode automatically when there is no activity on the bus for more than 3.0 ms. It then has a further 7.0 ms to shutdown the device and draw no more than the designated suspend power. Hence, it draws only the rated suspend power from the bus 10.0 ms (3 ms + 7 ms) after bus activity stops. This feature makes USB devices power-efficient.
236 Chapter 6 ■ Input-Output Devices
Points to Remember 1.
Input-output devices (abbreviated I/O devices) provide the capability to a computer to communicate with its external environment (its users). They are also known as peripheral devices.
2.
An input device is an electromechanical device that accepts data from outside world and translates them into a form that a computer can interpret.
3.
Keyboard devices are the most commonly used input devices today. They allow data entry into a computer by pressing a set of keys (labeled buttons) mounted on a keyboard.
4.
A graphical user interface (GUI) provides a screen with graphic icons (small images on the screen) or menus and allows a user to make rapid selections from them to give instructions to a computer.
5.
Point-and-draw devices are input devices used to rapidly point to and select a graphic icon or menu item from multiple options displayed on a screen. Many of these devices are also useful for effectively creating graphic elements on the screen such as lines, curves, and freehand shapes. Some commonly used point-and-draw devices are mouse, trackball, joystick, electronic pen, and touch screen.
6.
Data scanning devices are input devices that allow data entry directly from the source documents. Some of these devices are also capable of recognizing marks or characters.
7.
An image scanner is an input device that translates paper documents into an electronic format for input to a computer. Input documents may be typed text, pictures, graphics, or even handwritten material. Image scanners use optical technology to convert an image into electronic form.
8.
An optical character recognition (OCR) device is a scanner equipped with character recognition software. Users use it for scanning text documents and converting the scanned bitmap images of characters into equivalent ASCII text, which a computer can interpret as letters, numbers, and special characters for word processing.
9.
An optical mark reader (OMR) is a scanner that can recognize a pre-specified type of mark made by pencil or pen. We can use OMR device for any input data that is of a choice or selection nature.
10. Bar codes represent alphanumeric data by a combination of adjacent vertical lines (bars) by varying their width and spacing between them. A bar-code reader is a device for reading (decoding) bar-coded data. 11. Banking industry uses Magnetic-ink character recognition (MICR) technology for faster processing of large volume of cheques. Besides enabling faster processing of cheques, this technology also ensures greater accuracy of data entry because of mostly pre-printed information on the cheque and direct feeding of the cheques to the computer. 12. A digitizer is an input device used for converting (digitizing) pictures, maps, and drawings into digital form for input to computers. This enables re-creation of the drawing from the stored information and easy incorporation of changes in the drawing, whenever required. 13. Electronic cards are small plastic cards having encoded data appropriate for the application for which they are used. An electronic-card reader, normally connected to a computer, reads the data encoded on an electronic card and transfers it to the computer for further processing. 14. Depending on the manner in which they encode data, electronic cards are of two types: magnetic strip cards and smart cards. Magnetic strip cards store encoded data on a magnetic strip attached to the card, while smart cards have a built-in microprocessor chip in whose memory the encoded data is stored. 15. Speech recognition devices are input devices that allow a user to input data to a computer system by speaking to it. They are of two types: speaker-dependent and speaker-independent. Speaker-dependent systems recognize the speech of only a single individual or a few individuals, while speaker-independent systems can recognize words spoken by anyone. 16. A vision-input system consists of a digital camera to accept input by seeing an object. Input data in this case is normally an object’s shape and features in the form of an image. 17. An output device is an electromechanical device that accepts data from a computer and translates them into a form that is suitable for use by outside world (users). 18. Computer output generated by output devices are of two types – soft-copy output and hard-copy output. Soft-copy output is temporary in nature because it does not appear on a paper or some material that a user can carry for showing to others, or for filing and use later when he/she is not using a computer. Hard-copy output is permanent in nature because it appears on a
■ Points to Remember
237
paper or some material that a user can carry for showing to others, or for filing and use later when he/she is not using a computer. 19. Monitors generate soft-copy output by displaying an output on a screen. 20. A monitor and a keyboard together form a video display terminal (VDT) often referred to as just terminal. A terminal serves as both an input and output device. It is the most popular input/output (I/O) device used today. 21. Depending on their local processing capability, we classify terminals as dumb, smart, and intelligent. A dumb terminal is a low-cost terminal, without any local processing capability. A smart terminal has local data editing capability and the ability to consolidate input data prior to sending it to the computer. It has a microprocessor and some internal storage in addition to I/O capabilities. An intelligent terminal is a small computer by itself that has extensive local data processing capability. It consists of a built-in microcomputer and users can program it. 22. A terminal emulator is a software program that runs on a PC or workstation, communicates with the network interface over a serial port on the PC or workstation, and pretends that it is a terminal. 23. Monitors often use privacy filters to prevent others sitting/standing nearby from viewing the contents displayed on it. 24. Printers are the most popular hard-copy output devices used today. Based on the printing technology used, we classify printers as dot-matrix printers, inkjet printers, drum printers, chain/band printers, and laser printers. According to printing approach that a printer uses, we classify printers as impact printers and non-impact printers. 25. Dot-matrix printers are character printers that form characters and images as patterns of dots. 26. Inkjet printers are character printers that form characters and images by spraying small drops of ink on paper. 27. Drum printers are line printers that print characters by striking a set of hammers on an inked ribbon and paper placed against a solid cylindrical drum with characters embossed on its surface in the form of circular bands. 28. Chain/band printers are line printers that print characters by striking a set of hammers on an inked ribbon and paper placed against a metallic chain/band on which all characters of the character set supported by the printer are embossed. 29. Laser printers are page printers that produce high quality output by forming characters and images with tiny ink particles. 30. Hydra printers are multifunctional devices that perform the functions of two or more of the following office equipment – printer, copy machine, and scanner. 31. A plotter is an ideal output device for architects, engineers, city planners, and others who need to generate high precision, hard-copy graphic output of varying sizes. Two types of plotters in common use are drum plotter and flatbed plotter. 32. 3D printers print (create) 3D objects by using additive manufacturing techniques. 33. Some commonly used 3D printing technologies include Fused Deposition Modeling (FDM), STereoLithography (STL), Digital Light Processing (DLP), and Selective Laser Sintering (SLS). 34. Computer Output Microfilm (COM) is an output device that records computer output as microscopic filmed images on a sheet of microfilm. It uses a microfilm recorder for recording output on a microfiche, and a microfilm reader to view recorded information. 35. Screen image projector is an output device, which is used to project information from a computer on a large screen (such as a cloth screen or wall) so that a group of people can view it simultaneously. 36. A voice response system enables a computer to talk to a user. Two types of voice response systems are – voice reproduction system and speech synthesizer. A voice reproduction system produces audio output by selecting an audio output from a set of pre-recorded audio responses, while a speech synthesizer converts text information into spoken sentences. 37. Ergonomically designed I/O devices aim at achieving maximum efficiency of the people working on them and protecting them from accidents or medical problems caused due to continuous usage of a device for long hours. 38. I/O interfaces deal with how I/O devices connect to and communicate with a computer system. 39. A device controller is an interface unit between an I/O device and a computer’s system bus. On one side, it communicates with the I/O device connected to it, and on the other side, it communicates with the computer’s CPU and memory through the system bus. 40. I/O devices connect to I/O ports of a device controller, which in turn connects to a computer’s system bus. I/O devices plug into I/O ports of a computer system. I/O ports are of two types – serial ports and parallel ports.
238 Chapter 6 ■ Input-Output Devices 41. A user can use a serial port of a computer to connect any device that sends or receives a stream of bytes serially, one bit at a time. Users normally use it for connecting slow I/O devices such as terminal, keyboard, mouse, modem, etc. 42. A parallel port can handle higher volume of data than a serial port because it transfers 8 or 16 bits of data at a time. Users normally use it for connecting faster I/O devices such as printer, analog-to-digital and digital-to-analog converters, etc. 43. SCSI interface uses a generic device controller (called SCSI controller) and enables connection of any device (having SCSI interface) with the SCSI bus of the SCSI controller. This helps save expansion slots in a computer for connecting other devices. 44. We often use USB (Universal Serial Bus) standard to connect I/O devices to a computer. It is a serial bus, which supports four speeds – a super-speed mode of 5 Gbits/sec, a high-speed mode of 480 Mbits/sec, a full-speed mode of 12 Mbits/sec, and a low-speed mode of 1.5 Mbits/sec.
Questions 1.
Why are I/O devices necessary for a computer system?
2.
What are peripheral devices? Why are they so called?
3.
Differentiate between an input and an output device. Can a device be used as both an input and an output device? If no, explain why. If yes, give an example of such a device.
4.
Why are I/O devices slow as compared to the speed of primary storage and CPU?
5.
What is an input device? Name some commonly used input devices.
6.
What are keyboard devices? Differentiate between general-purpose and special-purpose keyboards.
7.
Describe the organization of the keys on a QWERTY keyboard.
8.
Explain the use of the following types of keys found on a keyboard: (a) Arrow keys (c) Alphanumeric keys (b) Soft keys (d) Special function keys
9.
What are soft keys on a keyboard? Why are they so called? How are they used?
10.
Name some special function keys along with their use.
11.
Write examples of two special-purpose keyboards.
12.
What is a keyboard buffer? How does it help in the operation of a keyboard?
13.
What are software-simulated keys? How do they help in faster data entry?
14.
What are point-and-draw devices? Name some commonly used point-and-draw devices.
15.
What is a mouse? Explain how a user uses it to notify the system of a particular user choice out of a given set of choices on a monitor’s screen.
16.
What is a graphics cursor? What is its relationship with a mouse?
17.
List and describe the basic operations used to carry out various functions possible with a mouse.
18.
Differentiate between a mechanical and an optical mouse.
19.
Differentiate between a serial and a bus mouse.
20.
Differentiate between a wired and a cordless mouse.
21.
What is the minimum number of buttons required in a mouse? How does it help having more number of buttons than the minimum requirement?
22.
What is a trackball? How is it different from a mouse?
23.
Explain how a user uses a trackball to notify the system of a particular user choice out of a given set of choices on a monitor’s screen.
24.
What is a joystick? How is it different from a trackball? Write some uses of a joystick.
■ Questions 25.
Explain how a user uses an electronic pen as an input device.
26.
Describe the working mechanism of a light pen input device.
27.
Describe some applications for which writing pen with pad are suitable as an input device.
28.
What is a touch screen device? Explain the technology used in making touch screen devices.
29.
Write three applications for which touch screen is the most suitable input device.
239
30.
What is an information kiosk? Write three uses of an information kiosk.
31.
What are data scanning devices? How do they help in improving input data accuracy as compared to keyboard devices?
32.
What is an image scanner? Explain how it inputs an image document.
33.
What is bit map representation of an image? Draw bit map representation of the letter ‘A’. Use any suitable grid size for your representation.
34.
What is the minimum number of bits required to encode each grid point (pixel) in bit map representation of an image? How does it help having more number of bits than the minimum requirement?
35.
Differentiate between a flatbed and a hand-held image scanner.
36.
What are the limitations of an image scanner when we use it for inputting text documents? How an OCR device overcomes these limitations?
37.
What is an OCR device? Write its advantages and limitations for inputting text documents.
38.
Explain how OCR devices convert bit map images of characters to equivalent ASCII codes.
39.
Explain why OCR devices for handwriting recognition are more complex and difficult to design than those for printed text.
40.
What is an OMR device? Explain the technique used by it for recognition of marks. Write some applications that make use of it.
41.
Differentiate between an OMR device and an OCR device.
42.
What are bar codes? How does a bar-code reader read bar-coded data? Write a use of a bar-code reader.
43.
What is the Universal Product Code (UPC)? Which input device is suitable for inputting data based on UPC?
44.
What is an MICR device? Which industry is the primary user of MICR?
45.
Explain how an MICR device helps in faster processing of bank cheques with greater accuracy.
46.
What is the main limitation of MICR technology?
47.
Write the similarities and differences between an OCR device and an MICR device.
48.
What is a digitizer? What are its two main components? What are its main areas of application?
49.
Explain how we can use a digitizer to convert a drawing into digital form for storage in computers.
50.
What is an electronic card? Write two applications that can make use of electronic cards effectively.
51.
What is an automatic teller machine (ATM)? Explain how a customer transacts with a bank using an ATM.
52.
Explain how an organization can use electronic cards to control access of various categories of people to physically secured areas.
53.
What is a smart card? How is it different from a magnetic strip card?
54.
Explain how we can use smart cards as electronic money.
55.
What is a speech recognition device? Write three applications of speech recognition systems.
56.
Explain how a speech recognition device recognizes and acts upon a spoken input data.
57.
What is a vision-input system? Write three applications of vision-input systems.
58.
Explain how a vision-input device recognizes a given object and takes appropriate action.
59.
Differentiate between speaker-dependent and speaker-independent speech recognition systems. Which one is more difficult to design? Write reasons for your answer.
240 Chapter 6 ■ Input-Output Devices 60.
How are graphs and pictures fed to a computer?
61.
What is an output device? Name some commonly used output devices.
62.
Differentiate between soft-copy output and hard-copy output.
63.
Name two output devices used for producing soft-copy output and two output devices used for producing hard-copy output.
64.
What is a computer terminal? Why is it so called?
65.
What is a monitor device? Name the two basic types of monitors along with their relative advantages and disadvantages.
66.
Explain how a CRT monitor displays an output.
67.
Why does the display mechanism of a CRT monitor needs to refresh CRT screen?
68.
Why does the display mechanism of a CRT monitor turns off the electron beam during horizontal and vertical traces?
69.
Why does the display mechanism of a CRT monitor represents displayed characters and images as a pattern of dots?
70.
71.
Explain the meaning of the following terms with respect to a CRT monitor: (a) Scan line (e) Interlaced scan (b) Horizontal trace (f) Resolution (c) Vertical trace (g) Refresh rate (d) Progressive scan Differentiate between progressive scan and interlaced scan monitors. Which type of monitor has better quality and why?
72.
Explain how a color monitor displays color images.
73.
Differentiate among CGA, EGA, VGA, and super VGA standards for color monitors.
74.
What is a video RAM? What factors normally decide its capacity?
75.
A monitor has a resolution of 800x600. It supports 256 different colors. Calculate the video RAM capacity (in bytes) required by this monitor.
76.
What is a flat panel monitor? Where is it commonly used?
77.
Explain how LCD monitors display images.
78.
Write relative advantages and limitations of a CRT monitor and a flat-panel monitor.
79.
Differentiate among dumb, smart, and intelligent terminals.
80.
What is a terminal emulator? What type of scenario requires it?
81.
What is a privacy filter? How does it work?
82.
What is a printer? What are the commonly used types of printers?
83.
Differentiate between impact and non-impact printers. Write their relative advantages and disadvantages. Write names of two printers of both types.
84.
What are the units used for measuring printer speed. Also, specify which unit is suitable for measuring the speed of what type of printer.
85.
What is the unit for measuring printer output quality? According to this feature, what are the common classifications of printers?
86.
Can a printer support multiple scripts simultaneously? If no, explain why. If yes, name two types of printers with this capability.
87.
Explain the printing mechanism of dot-matrix printers.
88.
Write features and limitations of dot-matrix printers.
89.
Explain the printing mechanism of inkjet printers.
90.
Write features and limitations of inkjet printers.
91.
Write relative advantages and limitations of an inkjet printer and a dot-matrix printer.
92.
Explain the printing mechanism of drum printers.
■ Questions 93.
Write features and limitations of drum printers.
94.
Explain the printing mechanism of chain/band printers.
95.
Write features and limitations of chain/band printers.
96.
Differentiate between a drum printer and a chain/band printer. Write their relative advantages and limitations.
97.
Explain the printing mechanism of laser printers.
98.
Write features and limitations of laser printers.
99.
What is a hydra printer? How is it more useful than a normal printer?
241
100. Name four types of printers that modern computer systems use. Describe the print mechanism of any one of them. 101. What is a 3D printer? Explain the basic technique used by 3D printers. 102. Explain the three steps that 3D printers require to print a 3D object. 103. Name three commonly used 3D printing technologies. Describe any one of them. 104. What is a plotter? What types of users need it? 105. Explain the working mechanism of a drum plotter. 106. Explain the working mechanism of a flatbed plotter. 107. What is a COM device? Explain how it records information and how a user retrieves information using a COM device. 108. Write advantages and limitations of a COM system as compared to a printer. 109. For what type of applications is a COM system most suitable? Write its typical users. 110. What is a screen image projector? Write its uses. 111. What is a voice response system? Write its applications. 112. What is a voice reproduction system? How does it function? Write its applications. 113. What is a speech synthesizer? How does it function? Write its applications. 114. What are ergonomically designed devices? Write five examples of such devices. 115. Write five features of keyboards and monitors that make computer terminals easier to use. 116. What is a device controller? Explain with a diagram its role in connecting I/O devices to a computer. 117. Why do computers use device controllers for connecting I/O devices, instead of connecting them to the system bus directly? 118. Differentiate between device controllers with DMA capability and without DMA capability. Which one can transfer data faster from an I/O device to a computer’s memory and why? 119. What is an I/O port? Differentiate between serial and parallel ports. 120. What is a serial port? Why is it so called? Explain how an I/O device connected to a serial port of a computer system transfers data to the computer’s system bus. 121. What is a parallel port? Why it so called? Explain how an I/O device connected to a parallel port of a computer system transfers data to the computer’s system bus. 122. What is a SCSI interface? Explain how it helps in expanding the capability of a computer system in having more I/O devices connected to it. 123. Define the following terms: (a) SCSI controller (b) SCSI bus (c) SCSI device 124. What is an expansion slot? Explain how a SCSI interface helps in saving expansion slots in a computer system for connecting other devices. 125. What are the advantages of a SCSI controller over a conventional device controller? 126. Write key features of USB as a standard interface for connecting I/O devices to a computer.
Software
We often use the terms “hardware” and “software” in connection with computers. Hardware refers to the physical devices of a computer system. Thus, input, storage, processing, control, and output devices are hardware. In fact, the earlier few chapters covered the hardware of computer systems. In this chapter, we will learn about software, software development, and examples of some popular software.
BASIC CONCEPTS AND TERMINOLOGIES What is Software? A computer cannot do anything on its own. We need to give instructions to it to make it do a job desired by us. Hence, it is necessary to specify a sequence of instructions a computer must perform to solve a problem. Such a sequence of instructions written in a language understood by a computer is called a computer program. A program controls a computer’s processing activity, and the computer performs precisely what the program wants it to do. When a computer is using a program to perform a task, we say, it is running or executing that program. The term software refers to a set of computer programs, procedures, and associated documents (flowcharts, manuals, etc.) describing the programs, and how they are to be used. A software package is a group of programs that solve a specific problem or perform a specific type of job. For example, a word-processing package may contain programs for text editing, text formatting, drawing graphics, spelling checking, etc. Hence, a multipurpose computer system, like a personal computer, has several software packages, one each for each type of job it can perform.
Relationship between Hardware and Software The following points explain the relationship between hardware and software:
■ Basic Concepts and Terminologies
243
1.
Both hardware and software are necessary for a computer to do useful job. Both are complementary to each other.
2.
We can load different software on the same hardware to make a computer perform different types of jobs.
3.
Except for upgrades (like increasing main memory and hard disk capacities, or adding speakers, modems, etc.), hardware is normally a one-time expense, whereas software is a continuing expense. We buy new software for running on the same hardware as and when need arises, or funds become available.
Types of Software Although, the range of software available today is vast and varied, we classify most software into two categories: 1.
System software. System software is a set of one or more programs, which controls the operation and/or extends the processing capability of a computer system. It makes the operation of a computer system more effective and efficient. It helps hardware components work together, and provides support for the development and execution of application software (programs). Good system software enables development and execution of application packages on a computer system with less time and effort. A computer without some kind of system software would be ineffective and most likely impossible to operate. Programs included in a system software package are called system programs, and programmers who prepare system software are referred to as system programmers. Some commonly known types of system software are operating systems, programming language translators, utility programs, performance monitoring software, and communications software. They are described in subsequent chapters.
2.
Application software. Application software is a set of one or more programs, which solves a specific problem, or does a specific task. For example, payroll processing software, examination results processing software, railway/airline reservation software, and computer games software are application software. Similarly, a program written by a scientist to solve a research problem is also application software. Programs included in an application software package are called application programs, and programmers who prepare application software are referred to as application programmers. Today, millions of application software packages are available for a wide range of applications. They range from simple applications such as word processing, inventory management, preparation of tax returns, banking, hospital administration, insurance, publishing, to complex scientific and engineering applications such as weather forecasting, space shuttle launching, oil and natural gas exploration, design of complex structures like aircrafts, ships, bridges, sky-rise buildings, etc. With so many applications available, it is not possible to categorize them all and to cover them here. Later in the chapter, we describe some commonly known application software.
Logical System Architecture Figure 7.1 shows the logical architecture of a computer system. It depicts the relationship among hardware, system software, application software, and users of a computer system. At the centre is hardware comprising of physical devices/components of the computer system. System software layer surrounds the hardware, hiding hardware details of the system from application programmers. It defines the operating and programming environment of the computer system and coordinates operations of various hardware devices for optimizing the performance of all devices. Application software layer then surrounds the system software. It consists of a wide range of application software. Finally, the users’ layer consists of the user interfaces provided by the application software. Different application software usually provides different user interfaces. Hence, how a user interacts with the computer system depends on which application he/she is using.
Chapter 7 ■ Software
244
Firmware Firmware refers to a sequence of instructions (software) substituted for hardware. For example, when cost is more important than performance, a computer system architect might decide not to use special electronic circuits (hardware) to multiply two numbers, but instead write instructions (software) to cause the machine to accomplish the same function by repeated use of circuits designed already to perform addition. This software (known as firmware) is stored in a read-only memory (ROM) chip of the computer, and is executed (used) whenever the computer has to multiply two numbers. Hence, firmware is software substituted for hardware and stored in read-only memory. USERS APPLICATION SOFTWARE SYSTEM SOFTWARE HARDWARE Physical devices/components of the computer system Software that constitute the operating and programming environment of the computer system Software that do a specific task or solve a specific problem Users normally interact with the system via the user interfaces provided by the application software Figure 7.1. Relationship among hardware, system software, application software, and users of a computer system.
Initially, vendors supplied only system software in the form of firmware. However, many vendors now supply even application programs as firmware. Vendors also program dedicated applications in this fashion and make them available as firmware. Because of rapid improvement in memory technology, firmware is frequently a cost-effective alternative to wired electronic circuits, and its use in computer design has been increasing gradually. In fact, increased use of firmware has today made it possible to produce smart machines of all types. These machines have microprocessor chips with embedded software.
Middleware In early days of computer networks, the two-tier, client-server system architecture [shown in Figure 7.2(a)] was used to implement distributed applications (computer networks and distributed applications are described later in the book). In this two-tier system architecture, one or more server computers served several client computers connected to them. This architecture was inflexible and had its own limitations due to the following reasons: 1.
Programmers had to include the code for management and support services (such as directories for locating resources, authentication for handling security, exception management for handling failures, and coscheduling of networked resources for better performance) within the client and server parts of the application software.
2.
Programmers had to synchronize client and server processes for exchange of messages between them (they used synchronous mode of communication rather than asynchronous mode, which is more flexible).
3.
Programmers had to include the code for business application logic inside the user interface on the client, or within the application software on the server, or both.
■ Basic Concepts and Terminologies
Client computer
Server computer
Client part of application software – Includes user interface, application logic, messaging services, and management and support services
Server part of application software – Includes applicationspecific services, application logic, messaging services, and management and support services
Operating system and network interfaces
Operating system and network interfaces
245
Network connection (a) Two-tier, client-server architecture
Client computer
Server computer
Client part of application software – Includes user interface and business and application logic
Server part of application software – Includes application specific services
Middleware services – Includes APIs and client part of messaging, management, and support services
Middleware services – Includes APIs and server part of messaging, management, and support services
Operating system and network interfaces
Operating system and network interfaces
Network connection (b) Three-tier, client-server architecture Fig. 7.2. Illustrating the use and location of middleware.
As a result, there was close knitting between client and server systems, making independent development of interoperable client and server software difficult. Hence, rapid development and deployment, ease of management, and portability of distributed applications were difficult. Distributed application developers invented the three-tier system architecture [shown in Figure 7.2(b)] to overcome these problems. The basic idea here was to have a separate software layer, which acts as “glue” between the client and server parts of an application. It provides a programming abstraction that masks the heterogeneity of underlying network, hardware, and operating systems from application programmers. This software layer is known as middleware because it sits in the middle, between the operating system and applications. In general, middleware is a set of tools and data that help applications use networked resources and services.
Chapter 7 ■ Software
246
ACQUIRING SOFTWARE Earlier, application and system software were included in the purchase price of a computer. Today, software is usually not included in the purchase price of a computer. For most computer manufacturers, the purchase price of a computer includes only its hardware and a minimum of system software. A customer has to pay extra charges for additional system software and application software that he/she wants to purchase. We can obtain the desired software in one or more ways described here. The description also provides the relative advantages and limitations of each way of obtaining software.
Buying Pre-written Software Thousands of pre-written software packages are available today. If we can find a software package that meets our requirements, purchasing it is the best option. We follow the following steps in selecting and buying a pre-written software package: 1.
Prepare a list of all available software packages that can perform the desired task(s).
2.
From this list, select those software packages only that meet the system specifications. For example, we should check for compatibility with user’s available/planned hardware, I/O devices, Operating System, etc.
3.
Now choose the best one (based on factors such as supported features, duration of warranty support, cost, etc.) from the list of those selected in Step 2.
4.
Now find out the source from where you can purchase the chosen software at the cheapest price. Different vendors offer different discount rates on the list price, and selecting the best vendor in terms of price and after-sale support is important.
Following are the advantages and limitations of buying a pre-written software package: 1.
Pre-written software packages usually cost less because many customers share their development and maintenance cost.
2.
With a pre-written software package, a user can start the planned activity almost immediately. The user need not wait for the software to be developed and tested. This may be important, if the development and testing efforts involve several months.
3.
Pre-written software packages are usually general purpose, so that they can meet the requirements of as many potential users as possible. Due to this, many times, the operating efficiency and capability to meet the specific needs of a user more effectively is not as good for pre-written software packages as for in-house developed software packages.
Ordering Customized Software If none of the available pre-written software packages meet the specific requirements of a user (an organization or an individual), it becomes necessary for the user to create a customized software package. If the user has an in-house software development team, the user can create the software package in-house. However, if such a team does not exist in-house, the user must get it created by another organization by placing an order for it. We follow the following steps for this:
■ Acquiring Software
247
1.
The user prepares a list of all user requirements carefully.
2.
The user then floats a tender for inviting quotations for creation of the requisite software. Sometimes, the user contacts few software houses instead of floating a tender for quotations.
3.
After receiving the quotations, the user selects a few of them for further interaction based on the cost quoted by them, their reputation in the market, their submitted proposal, etc.
4.
The user then interacts with the representative(s) of each of the selected vendors. Based on this interaction, the user chooses a vendor to offer the contract for creation of the requisite software.
5.
The chosen vendor then creates the software package and delivers it to the user. Often, the vendor has to closely interact with the user during the software development process.
The vendor need not develop everything from scratch to meet the user’s requirements. If one or more pre-written software packages meet the user’s requirements partially, the vendor may use them along with its own developed software in such a manner that the overall software package so created meets the user’s requirements fully. This helps in reducing the development cost and time needed for creation of the required software package. If a user has to order for both hardware and software for his/her requirement, the user may choose to place the order for both to a single vendor. The vendor develops the software on the chosen hardware, and delivers the software along with the hardware to the user. We refer to this as end-to-end solution or turnkey solution. Following are the advantages and limitations of ordering a customized software package rather than developing it inhouse: 1.
The user need not maintain its own software development team. Maintaining and managing such a team is expensive, and may not be justified for an organization not needing to develop software on a regular basis.
2.
It is easier to carry out changes in the software, if an in-house team develops it. For ordered customized software, the user needs to always depend on the vendor for carrying out the changes, and the vendor often charges separately for each request for change.
Developing Customized Software If none of the available pre-written software packages meet the specific requirements of an organization, and if the organization has an in-house software development team, the organization may choose to develop a customized software package in-house for its requirements. We follow the following steps for this: 1.
The organization first constitutes a project team to develop the software.
2.
The team studies the requirements and plans functional modules for the software.
3.
It then identifies the functional modules, which the team has to develop. It also identifies existing pre-written software, which can meet the requirements of other functional modules.
4.
For the functional modules that the team needs to develop, the team next plans their programs and does coding, testing, debugging, and documentation for the planned programs.
5.
The team then tests all the modules in an integrated manner.
6.
The team then deploys the software for use by users.
7.
The users then use the software and the project team members responsible for maintenance activities maintain it.
Following are the advantages and limitations of developing a customized software package in-house by an organization rather than getting it developed by an outside party:
Chapter 7 ■ Software
248 1.
It is easier to carry out changes in the software, if the organization develops it in-house.
2.
Developing software in-house means a major commitment of time, money, and resources for the organization because the organization needs to maintain and manage an in-house software development team.
Downloading Public-domain Software Public-domain software is software available free or for a nominal charge from the bulletin boards or user-group libraries on the Internet. Basic objective of the creators of such software is to popularize their software to as many users as possible. Users are encouraged to copy such software and try them out. Usually, the creators share the software freely with other users. Hence, we also refer to public-domain software as shareware/freeware. They are also known as community-supported software as mostly the authors do not support the product directly and users of the software support and help each other. While some shareware software remain full-featured and free perpetually, some other have to be bought to use all features (such partial packages are referred to as Lite/Free/Personal edition) or after some use (referred to as trial period). Another type of public-domain software becoming popular among users, and fellow programmers equally, are those that come with their source code. We refer to such software as Open Source Software (OSS). Usually, open source software allows a user to download, view, modify, and distribute modified source code to others. There are special licensing systems for open source software and their source code. They promote open source movement and protect the copyright of original and subsequent authors of open source software. General Public License (GPL) of GNU organization and BSD License from Berkeley Software Division of University of California are some of the most popular licensing systems in place and used world over. Not all open source software is necessarily free, and not all free software is necessarily open source. If a user finds a public-domain software suitable for his/her requirements, he/she can obtain it by downloading it from the Internet. Following are the advantages and limitations of downloading and using public-domain software packages: 1.
Usually, they are free and accompanied with their source code.
2.
A user can download and use them immediately. The user need not wait to start the planned activity until a team develops the required software.
3.
Normally, the creators of OSS do not test the software properly before release, and their support is poor as compared to commercial software. Hence, OSS may fail during operation, and even if a user reports a bug, the creators may not fix it soon.
SOFTWARE DEVELOPMENT LIFE CYCLE (SDLC) Someone must develop all software, no matter by which source we acquire software. Developing software, putting it to use, and maintaining it is a complex process involving the following phases: 1.
Requirement Specification
2.
System Analysis and Design
3.
Implementation (also known as Construction or Code Generation)
4.
Testing and Debugging
■ Software Development Life Cycle (SDLC) 5.
Deployment (also known as Installation)
6.
Maintenance
249
Evolving software products through these phases is known as Software Development Life Cycle (SDLC) model. In this model, we go through the above listed phases sequentially, with each phase feeding in inputs to the next phase [Figure 7.3]. Development process proceeds from one phase to the next in a purely sequential manner. Hence, the next phase in the life cycle starts only after the defined set of goals are achieved for its previous phase and it is completed fully, perfected, and frozen. This model is also known as waterfall model because it is a sequential development process, which flows steadily downwards (like a waterfall) through the various phases of the process.
Requirement specification System analysis and design
Implementation
Testing and debugging
Deployment
Maintenance
Figure 7.3. Phases of Software Development Life Cycle (SDLC) model. It is also known as Waterfall model.
A software development team performs the various activities involved in SDLC model. For small software this may be a one-person team (who alone carries out all the activities of all phases), whereas for large software the team may consist of several tens or even hundreds of people with different types of skills. The main activities performed in the various phases of SDLC model are: 1.
Requirement specification. The team defines all possible requirements of the software in this phase. These include functionality, capability, performance, user interface, inputs, and outputs requirements. The team records the defined requirements in a requirement specification document, which serves as input to the next phase of the model.
2.
System analysis and design. The team studies the requirements specified in the first phase with respect to available hardware and software technologies and prepares a system design document. The system design document defines the overall system architecture. It specifies the various hardware and software components of the system and the interfaces between the components. It includes the hierarchy of software components, rules for component selection, and interfaces between components. The design document serves as input to the next phase of the model.
Chapter 7 ■ Software
250 3.
Implementation. This phase is also known as construction or code generation because in this phase, the team constructs (codes in selected programming languages) the various software components specified in the system design document. While constructing the software components, the team keeps track of the hardware components specified in the system design document so that the constructed software components are compatible with the specified hardware components. To construct a software component, the team first divides it into modules, and then codes each module separately. The team does this, especially for large software, to enable multiple programmers to work parallely on different modules so that they can carry out the implementation phase faster. While dividing software components into modules, the team also defines module interfaces to enable interaction between the modules. The team codes and tests each module independently. This type of testing of a module done by the programmer who developed the module is referred to as unit testing. Unit testing mainly verifies if a module meets its specifications.
4.
Testing and debugging. In this phase, the team integrates the independently developed and tested modules into a complete system. The team then tests the integrated system to check if all modules coordinate properly with each other and the system as a whole behaves as per the specifications. They debug (correct) any bug (error) that they find during integrated system testing, and repeat testing of that functionality of the system. After the team completes this phase, the software is ready for release and deployment.
5.
Deployment. Once the team tests the software and removes all known errors, the team deploys the software at user(s) site on (or along with) associated hardware for use by the intended user(s). At this stage, the team phases out old system (if any in operation) and phases in the new system and puts it into operation. The team also evaluates the system against the specified requirements in this phase.
6.
Maintenance. Some problems with the system, which the team is unable to find during various phases of the development life cycle of the system, surface only after the team deploys the system and puts it in operation. The team fixes these problems in the system in its maintenance phase. Additionally, the team needs to make changes in the system from time-to-time to take care of new user requirements, changes in technology, changes in organizational policies, etc. The team incorporates all these changes in the system in its maintenance phase. Hence, maintenance phase virtually lasts for the entire life of the software after its deployment.
Advantages of SDLC Model Many software engineering texts and courses use the waterfall model as a beginning example of software development model. It has the following advantages: 1.
It is simple to understand and easy to follow because the development process progresses sequentially through a well defined set of discrete, easily understandable, and easily explainable phases.
2.
It provides a disciplined and structured approach to software development.
3.
It also enables easy demarcation of milestones in the development process.
4.
It is now a well-established fact that time spent early on in planning and design of software before coding it makes better economic sense in software lifecycle. For example, a bug found in early stages (such as during requirement specification phase or system analysis and design phase) is easier and cheaper to fix than the same bug found in a later phase. The waterfall model supports this central idea of economy of software development.
5.
The model places emphasis on documentation (such as need for requirement specification document, system design document, implementation document, testing document, etc.). Documentation helps new team members or even entirely new teams to familiarize themselves and carry on the development activity from any intermediate stage in the development process.
■ Software Development Life Cycle (SDLC)
251
Limitations of SDLC Model Many have criticized the waterfall model because of its following drawbacks: 1.
In a project of reasonable size and complexity, it is impossible to perfect one phase of software development before moving on to the next phase.
2.
Often, clients are not clear about their requirements in the beginning. They are able to articulate and define their requirements only when they see a working prototype of the system they desire. Hence, following the phases sequentially (as required in the waterfall model) may not be possible in such cases.
3.
Often, clients keep changing their requirements constantly, and software development team has to accommodate several (if not all) of these proposed changes. If clients propose such changes during the implementation phase (after completion of system design), the team must revisit the requirement specification as well as system analysis and design phases to accommodate the new requirements, invalidating some of the original specifications and system design. Hence, following the phases sequentially (as required in the waterfall model) may not be possible in such cases.
4.
Often, designers are not aware of future implementation problems when designing and preparing the architecture of unimplemented software. The team may realize only during the implementation phase that certain aspect of the design is difficult to implement, and hence, it should modify the design. A purely sequential approach (like the waterfall model) is not suitable for taking care of such requirements.
5.
Often, developers discover many of the system’s details/problems as they progress from one phase to another. Some of the details/problems that they discover at certain points require the developers to backtrack into earlier phases to accommodate the discovered details or to correct the discovered problems. A purely sequential approach (like the waterfall model) is not suitable for taking care of such requirements.
Lightweight SDLC Models Many software developers have found the traditional SDLC model to be unsuitable in today’s fast-paced world because it is not possible to remain competitive, if they follow the procedures strictly as required by the SDLC model. Hence, they have introduced several modified SDLC models, which are popularly known as lightweight SDLC models. These models address some or all of the drawbacks of the pure waterfall model. They are a compromise between no process and too much process. Some popular lightweight SDLC models are: 1.
Extreme Programming (XP)
2.
Agile Software Process (ASP)
3.
Adaptive Software Development (ASD)
4.
Feature Driven Development (FDD)
5.
Rational Unified Process (RUP)
All lightweight SDLC models follow iterative approach of software development with overlapping phases. For example, code generation phase runs together with testing and debugging phase. Lightweight SDLC models are also known as agile development methods because they are quite flexible in iterative refinement of the requirements, design, and generated code (unlike the rigid approach taken in the traditional SDLC model for freezing the requirements and design). For example, in agile development methods:
Chapter 7 ■ Software
252 1.
Requirements can change any time during the project. The client is part of the project team and keeps giving feedback on requirements as things become clearer in various phases of development.
2.
Code generation may start even before program specifications are fully ready and frozen. Team members start coding based on their understanding of the project from its requirement specification.
3.
Team members might like to create test cases for unit testing or integrated testing from requirement specification even before actual coding starts. This provides quick way to test the software.
4.
Team members might also like to create test cases for unit testing during code generation and run the test cases iteratively to refine the code.
5.
Team members might like to evaluate the system several times before deployment of the system.
6.
Team members might like to review and refine the design and code repeatedly as they (team members including the client) meet periodically.
In short, the focus in agile development methods is more on deliverables than strict procedures.
SOFTWARE DESIGN APPROACHES The three software design approaches used commonly are top-down, bottom-up, and out-of-the-middle. They are described here. 1.
Top-down approach. The top-down approach starts with system architecture definition in which the designer defines the overall system architecture in terms of its major components. The architecture includes a set of relationships among components (for example, hierarchical structure, interfaces, communications, and process dependencies). In the next step, the designer defines the architecture of each major component in terms of its sub-components and a set of relationships among the sub-components. He/she then defines the architecture of each sub-component in terms of its sub-sub-components and a set of relationships among the sub-sub-components. This process of decomposing components and sub-components continues until the designer reduces all components and sub-components to base elements, further decomposition of which is not possible. All components, sub-components, sub-sub-components, and so on defined in the process along with their specifications and relationships among them form the complete system architecture. The components, sub-components, sub-sub-components, and so on are implemented (coded in a programming language) and linked properly to build the system in the implementation phase. Figure 7.4 illustrates this approach. It is also known as decomposition approach because it arrives at the complete system architecture by decomposing components and sub-components into additional subcomponents, until the entire system specification is reduced to base elements. This approach was popular until the late 1980s. The other two approaches have now replaced it. However, designers still use this approach in those situations where gap analysis between the requirements of the target system and the available software components indicates low functional fit.
2.
Bottom-up approach. The bottom-up approach starts with base elements selection and specification in which the designer first does a survey of available software libraries and/or existing applications and selects those base elements that can serve as useful building blocks for the target software system. He/she also lists the current specifications of the selected base elements. In the next step, the designer lists modifications required (if any) in the base elements to change their current specifications to match the specifications/requirements of the target software system. For example, modifications may be required to make them suitable for the hardware, operating system, etc. of the target system. He/she then defines linkages among the base elements to create sub-components, which form sub-systems of the target system. In the next step, he/she defines linkages among the sub-systems to create larger sub-systems of the target system. The designer continues with this process of defining linkages among the sub-systems of the previous step to create larger sub-systems until he/she forms a complete top-level system. The selected base elements along with their current specifications and customization requirements (if any), and the linkages defined at various levels to create
■ Software Design Approaches
253
sub-components, larger sub-systems, and the complete top-level system together form the complete system architecture. The base elements are customized and linked properly (as defined in the system architecture) to build the system in the implementation phase.
System architecture definition Step 1
Defines overall system architecture in terms of its major components and a set of relationships among the components
Components definition Step 2
Defines architecture of each major component defined in Step 1 in terms of its sub-components and a set of relationships among the sub-components
Step 3
Defines architecture of each sub-component defined in Step 2 (if it has to be further decomposed) in terms of its sub-sub-components and a set of relationships among the sub-sub-components
Sub-components definition
Subsequent steps
This process of decomposing components and subcomponents continues until all components and subcomponents are reduced to base elements, which cannot be further decomposed Figure 7.4. The top-down approach of software design.
Figure 7.5 illustrates this approach. It is also known as synthesis approach because it arrives at the complete system architecture by synthesizing base elements into sub-systems, and sub-systems into larger sub-systems, and so on until a complete top-level system is formed. Re-usability of code is one of the main benefits of this approach. Object-oriented paradigm, which promotes re-usability of code in the form of re-usable objects, popularized this approach. This approach is suitable for use in those situations where gap analysis between the requirements of the target system and the available software components indicates high functional fit. 3.
Out-of-the-middle approach. This approach combines both top-down and bottom-up approaches. It is the most popular approach for designing modern software systems because software industry has matured over the years and re-usable software components for almost all domains are available today. Hence, most modern software projects make use of a mix of existing software components and newly developed components to build the complete target software system. In this approach, the designer first uses the top-down approach to define a high-level architecture of the target system. He/she then uses the bottom-up approach to fill in the detailed architectural levels. Thereafter, he/she identifies additional components and their relationship with other components to fill the gaps remaining to complete the architecture. In the implementation phase, the team customizes (if required) the components that have been identified by the bottom-up approach and implements the new components. The team then links all the components (as defined in the system architecture) to build the target system.
Chapter 7 ■ Software
254
Subsequent steps
This process of defining linkages among the subsystems of the previous step to create larger subsystems continues until a complete top-level system is formed
Larger sub-systems creation Step 4
Define linkages among the sub-systems to create larger sub-systems of the target system
Sub-components creation Step 3
Define linkages among the base elements to create sub-components, which form sub-systems of the target system
Base elements customization Step 2
List modifications required (if any) in the base elements to change their current specifications to match the specifications/requirements of the target software system
Base elements selection and specification Step 1
From available software-libraries and/or existing applications select base elements that can serve as useful building blocks for the target software system and list their current specifications Figure 7.5. The bottom-up approach of software design.
SOFTWARE IMPLEMENTATION Software implementation deals with code generation of various software components specified in system design document and linking them to build the target software system. Code generation of software components and linking them involves: 1.
Planning of computer programs
2.
Selecting a programming language for coding a component
3.
Coding the component in the selected programming language
4.
Using linking tools to link the codes of different components
Chapters 8 and 9 deal with these and other associated activities involved in software implementation.
■ Software Testing and Debugging
255
SOFTWARE TESTING AND DEBUGGING What are Testing and Debugging? As long as human beings program computers, computer programs will have errors. Program errors are known as bugs, and the process of detecting and correcting these errors is called debugging. In general, testing is the process of making sure that the program performs the intended task, and debugging is the process of locating and correcting program errors. Testing and debugging are important steps in program development. They are also time-consuming steps. In fact, for large programs, the time spent in testing and debugging often exceeds the time spent in coding the program.
Types of Program Errors Generally, the following two types of errors occur in a computer program: 1.
Syntax errors. Syntax errors occur when the rules or syntax of a programming language are not followed. Such program errors typically involve incorrect punctuation, incorrect word sequence, undefined terms, or misuse of terms. For example, the FORTRAN statement C = (A + B/2 has a syntax error; the missing closing parenthesis that should be placed in the appropriate place depending on the logic of the program. A compiler will not compile a program successfully until its programmer corrects all its syntax errors.
2.
Logic errors. Logic errors occur when we make errors in planning the program’s logic or while converting the logic into program code. They cause the program to produce incorrect result or behave unexpectedly. A compiler will compile a program successfully, if it is free of syntax errors, even if it has one or more logic errors. Although we can execute such a program, but it will not produce correct result or will not behave as expected. For example, if a FORTRAN instruction should be “A = B + C” but the programmer has coded it wrongly as “A = B – C”, it is an example of a logic error because the program will not produce correct result. Similarly, if one has wrongly entered 999999 (only six 9s) instead of 9999999 (seven 9s) as the sentinel value for Rollno in the instruction for detecting the end of last record, the program will expect more records even after the last record (sentinel values are introduced in a later chapter). In case of a logic error, since a programmer does not violate any rule of the programming language, the computer cannot identify the error. Hence, it does not produce any error message for a logic error. Therefore, identifying logic errors in a program is the responsibility of the program development and testing team. The testing procedure involves running the program to process input test data, and comparing the obtained results with the known correct results. If the results obtained for the test data do not match with the known correct results, most likely the program contains one or more logic errors. Logic errors are typically due to either missing logic or incorrect logic. If the logic is incorrect, the results obtained for the test data will be wrong. These errors are the easiest of the logic errors to find. Errors caused by missing logic result from logical situations that the programmer did not think of while planning/writing the program. For example, let us assume that an arithmetic process uses a numeric field and the data entry operator has entered a non-numeric value in it. The program logic should determine that the data is not numeric before attempting the arithmetic process. If this logic is missing, the program will fail when the arithmetic process encounters non-numeric data. This type of logic error can be difficult to find. This error will occur only when a user enters nonnumeric data in a numeric field. Users may use the program for weeks, months, or years before this happens and the error in program logic shows up.
Debugging a Program for Syntax Errors Both syntax and logic errors are known as bugs and the process of locating and correcting these errors is known as debugging. As compared to logic errors, syntax errors are easier to locate and correct because almost all language
Chapter 7 ■ Software
256
processors detect syntax errors automatically. When a language processor processes the source code of a program, it indicates on the source listing of the program each program statement having one or more syntax errors, and gives hints regarding the nature of the error. Programmers use these error messages to rectify all syntax errors in their programs. Hence, it is relatively easier to detect and correct syntax errors. Note that in high-level languages such as FORTRAN and COBOL, a single error in a program often causes generation of multiple error messages by the compiler/interpreter. There are two reasons for this. One is that highlevel language instructions often convert to multiple machine instructions. The other one is that symbolic instructions are often dependent upon other instructions. Hence, if a program instruction contains an error that defines a field name, all program instructions that use that field name will have an error message. The error message will indicate that a field used is not a defined name. In such cases, removal of the single error will result in the removal of all associated error messages.
Debugging a Program for Logic Errors Unlike syntax errors, a computer does not produce any error message for logic errors in a program. Hence, logic errors are more difficult to locate and correct than syntax errors. However, once the testing of a program indicates the presence of a logic error in it, a programmer uses one or more of the following methods to locate and correct the error: 1.
Doing hand simulation of the program code. One approach is to take a printout of the source code of the program and go through its execution manually with the test data input that produced incorrect results. During manual execution, the programmer follows the same sequence of paths that a computer would follow for the test data, while keeping track of changes in values of various variables that come across during the execution. Before he/she completes manual execution of the program, he/she will most likely detect the cause of the problem. This method is suitable only for simple and small programs because manual execution of a program is a tedious process.
2.
Putting print statements in the program code. In this approach, the programmer puts several print/write statements at appropriate locations in the program so that during execution of the program, the computer prints/displays values of different variables to indicate intermediate computations results. The programmer recompiles the program with these statements and executes it. These statements print/display the values of intermediate computations, telling what is happening during the execution of the program. By doing a proper analysis of the printed/displayed intermediate results, the programmer can detect the cause of the problem. The programmer removes the print/write statements from the program after correcting the detected errors, and re-compiles the program for future executions.
3.
Using a debugger. This is the most commonly used approach. Debugger is a software tool that assists a programmer in following a program’s execution step-by-step by allowing him/her to display intermediate calculation results and field values whenever desired. In effect, a debugger helps a programmer in debugging a program’s logic more easily than having to put (and later remove) print/write statements into the program’s source code. Almost all modern programming language processors support some kind of debugger. When using a debugger for debugging program logic, the programmer can move either from statement to statement or from one checkpoint to another, pausing the execution of the program as needed to examine the values of key variables or machine registers. Most debuggers support two kinds of checkpoints called breakpoints and watchpoints as explained here: (a) Breakpoint. A breakpoint is a point at which the debugger stops during program execution and awaits further command from the user. When it stops, the user may instruct it to display the current values of key variables or machine registers. A debugger provides the flexibility of setting or removing breakpoints after any program statement. The user may even set multiple breakpoints, and set or remove breakpoints when the program execution stops at a breakpoint and awaits further command.
■ Software Testing and Debugging
257
(b) Watchpoint. Suppose a program contains a variable named “radius” whose initial value of 5 is not supposed to change during execution of the program or after some point in the program. However, during debugging of the program, a programmer discovers that its value is changing. To know the exact point in the program where its value is changed, the programmer can declare “radius” as a watch variable. Then the debugger keeps track of the variable “radius” and the moment its value changes, it stops after the statement that causes the value of the variable “radius” to change and awaits further command from the programmer. Both kinds of checkpoints are helpful in detecting logic errors in programs. 4.
Using memory dump. Generally, programmers use this approach when the program “hangs up” during a test run. In this approach, a programmer prints/displays the contents of main memory and CPU registers at the time when the program hangs up. This information, called memory dump or storage dump, lists the instructions and data held in the computer’s main memory and CPU registers in their raw form, that is, their binary or equivalent hexadecimal or octal form. By doing a proper analysis of this listing, the programmer can detect the cause of the problem.
Software Testing Concepts Testing is the process of ensuring that the software performs its intended functions. It also ensures that the software behaves properly in case of abnormal or unusual input data. Testing and debugging are two separate tasks, but people often confuse between the two because they both deal with program errors. Figure 7.6 clarifies the difference between the two. Software developers assign increased weightage to testing activities in software lifecycle because software bugs often lead to high cost of system downtime. Hence, software testing has today evolved into a multi-stage process with its own methodology, structure, organization, and documentation. In modern software development projects, testing takes place in parallel with other activities, starting as soon as the project begins. Test planning can start immediately after the team has defined software requirements. Based on software requirements, test planners can build a master test plan, which includes testing strategies, priorities, and objectives. Based on the master test plan, testers can define test requirements (what all to test), or specific testing goals (such as performance goals). Once the testers have defined the requirements, they can start developing testing procedures designed to meet/validate those requirements and run the tests. The advantage of doing testing activities in parallel with other activities is that the team can make use of the knowledge that it had put into designing of the software for designing testing strategy. Sr. No.
Testing
Debugging
1
Testing is the process of validating the correctness of a program. Its objective is to demonstrate that the program meets its design specifications.
Debugging is the process of eliminating errors in a program. Its objective is to detect the cause of error, and remove known errors in the program.
2
Testing is complete when all desired verifications against specifications are completed.
Debugging is complete when all known errors in the program are fixed. Note that, debugging process ends only temporarily because it restarts whenever new errors are detected in the program.
3
Testing is a definable process that can and should be planned and scheduled properly.
Debugging, being a reactive process, cannot be planned ahead of time. It is carried out whenever errors are detected in a program.
4
Testing can begin in the early stages of software development. Although the test runs of a program are carried out only after the program is coded, but the decision of what to test, how to test, and with what kind of data to test can and should be done before the coding is started.
Debugging can begin only after the program is ready. The approach used for debugging depends largely on the personal choice of a programmer and the type of error in the program.
Figure 7.6. Difference between testing and debugging.
Chapter 7 ■ Software
258
Testing Methodologies Methodologies used commonly for testing software systems are described here.
Unit and Integrated Testing In unit testing, the programmer who develops a module of software tests the module independently for its functionality. Unit testing verifies if a module meets its specifications. When the team integrates the independently developed and tested modules into a complete system, the team then tests the integrated system to check, if all modules coordinate properly with each other and the system as a whole behaves as per the specifications. This is known as integrated testing. The software is ready for release after completion of integrated testing.
Alpha and Beta Testing After completion of integrated testing, the team releases the software for further testing to a select user group within or outside the organization. This group tests the software by using it as if they are its users. This release version of the software is known as alpha version and the test conducted on it is called alpha testing. If the software is to be sold commercially, instead of releasing it directly in the commercial market after alpha testing, the team sometimes releases it for additional testing to a selected set of users such as long time customers, partner agencies, and at times put for download and trial by anyone. This release version of the software is known as beta version. The selected set of users use the beta version of the software for an identified period and report any bug found in the software to the company developing the software. The use of beta version of the software by the selected set of users for additional testing is known as beta testing. After beta testing, the team fixes the reported bugs (if any), and then the company releases the software to the commercial market. Even at this stage the software may contain certain errors.
Functional and Non-Functional Testing Functional testing verifies whether the software functions as expected with all types of possible valid input data and meets all the design requirements. This ensures that the software operates correctly for all types of input data falling within its design requirements and scope. Functional testing is necessary, but insufficient to guard against certain risks. It does not test the error handling capability of software (how the software behaves in case of abnormal or unusual input data). Good software must produce appropriate error messages for invalid data instead of hanging (not continuing with its execution), or generating meaningless output. Non-functional testing verifies whether the software behaves properly even when it receives invalid or unexpected inputs (such as incorrect, incomplete, or inappropriate data). Non-functional testing tests non-functional aspects of software such as: 1.
Scalability testing, which tests if the software can handle large quantities of data or large number of users.
2.
Usability testing, which tests if the software is easy to learn and use.
3.
Security testing, which tests if the software can protect use of confidential data by unauthorized users.
4.
Internationalization testing, which tests if users from other countries can use the software.
5.
Localization testing, which tests if the software takes care of country-specific parameters (currency, formats, look-and-feel, etc.).
■ Software Testing and Debugging
259
Black-box, White-box, and Grey-box Testing In black-box testing, the tester treats the software as a black box because he/she has no knowledge of its internal data structures, algorithms, or the code of the software. The tester designs test cases based on the system specifications and planned usage to test the functionality according to the requirements. The tester inputs data for a test case and verifies if the output is the same as the expected value specified in the test case (the system behaves as expected). Black-box testing has the advantage of an unbiased opinion because the tester is not part of the design or implementation team. Hence, the tester can find those bugs, which the design or implementation team cannot think of. On the other hand, black-box testing has the disadvantage of blind exploration because the tester has no idea of the internal construction details of the system. Due to this, it is possible that the tester writes many test cases to test something, which a white-box tester can test by only one or two test cases. It is also possible that the test cases written by a black-box tester do not test some parts of the system at all. Hence, testing teams use black-box testing primarily for specification-based testing and usage-based testing. These two types of testing are described later. In white-box testing, the tester has good knowledge of the internal data structures, algorithms, and the code of the software. He/she designs test cases based on the knowledge of internal construction details of the system. This allows the testing team to ensure that all parts (function points) of the system are tested. For example, testing team can generate test cases to cause all statements in a program to be executed at least once (this is known as code-coverage testing). Testing teams use white-box testing primarily for code-based testing and fault-based testing. These two types of testing are described later. In grey-box testing, the testing team designs test cases based on requirement specification as well as internal data structures, algorithms, and code of the software. Hence, grey-box testing ensures that the testing team generates test cases to test the system in optimal time. It also includes testing of non-functional aspects of the system. Once the members responsible for generation of test cases have completed their job, they hand over the test cases to a different set of members, who carry out black-box type testing of the system with the received test cases. They generate a set of input data for each test case and compare the system’s output for each input with the expected output.
Test-set-generation Techniques Test cases form an important component of software testing. Efficiency and effectiveness of software testing depends largely on the choice and generation of right test cases. Hence, researchers have proposed several test-set-generation techniques, which testers use today for software testing. Figure 7.7 summarizes the popular ones. They are also described here.
Technique
Description Specification-based techniques
Equivalence partitioning
A test case is generated for each equivalence class found. The test case is selected at random within the class.
Boundary-value analysis
Several test cases are generated for each equivalence class: one that belongs to the inside of the class and as many as necessary to cover its limits (boundaries).
Decision tables/ cause-effect graphing
Test cases are derived considering every possible combination of inputs and outputs.
Finite-state-machinebased
Test cases are selected to cover states and transitions of a state machine that models the program.
Derived from formal specifications
Test cases are generated from formal specification languages.
Random testing
Test cases are generated at random according to the input domain defined in the specification. Figure 7.7. Test-set-generation techniques.
(Continued on next page…)
260
Chapter 7 ■ Software
Technique
Description Code-based techniques based on control-flow criteria
Sentence coverage
Test cases are generated to ensure that all program sentences are executed at least once.
Decision (branch) coverage (all edges)
Test cases are generated to ensure that all program decisions take the value true and false.
Condition (predicate) coverage
Test cases are generated to ensure that all conditions (predicates) forming logical expression of the decisions take the value true and false.
Decision/ condition coverage
Test cases with condition coverage are supplemented to achieve decision coverage.
Modified condition/ decision coverage
Test cases are generated to ensure that independent influence of every condition on the decision is tested (every condition is varied while holding fixed all other possible conditions).
Reinforced condition/ decision coverage
The modified condition/decision rule is required, and every condition must independently keep the decision value to true and false.
Path coverage
Test cases are generated to ensure that all program paths are executed. This criterion isn’t workable in practice.
Code-based techniques based on data-flow criteria All-definitions
Test cases are generated to ensure that each definition of each variable is covered for at least one use of the variable.
All-c-uses, some-puses
Test cases are generated to ensure that there is at least one path of each variable definition to each use of the variable in a computation. If some variable definitions aren’t covered, use puses.
All-p-uses, some-cuses
Test cases are generated to ensure that there is at least one path of each variable definition to each use of the variable in a predicate. If some variable definitions aren’t covered, use c-uses.
All-c-uses
Test cases are generated to ensure that there is at least one path of each variable definition to each use of the variable in a computation.
All-p-uses
Test cases are generated to ensure that there is at least one path of each variable definition to each use of the variable in a predicate.
All-uses
Test cases are generated so that there is at least one path of each variable definition to each use of the definition.
All-du-paths
Test cases are generated to ensure that all possible paths of each definition of each variable to each use of the definition are executed.
All-dus
Test cases are generated to ensure that all possible executable paths of each definition of each variable to each use of the definition are executed.
Strong (standard) mutation
Test cases are generated to ensure that all mutants generated by applying all mutation operators are covered.
Selective (constrained) mutation
Test cases are generated to ensure that all mutants generated by applying some mutation operators are covered.
Weak mutation
Test cases are generated to ensure that a given percentage of mutants generated by applying all mutation operators are covered.
Schema-based mutation
All mutants for a program are encoded into one metaprogram to improve test-execution performance.
Fault-based techniques based on mutation testing
Figure 7.7. Test-set-generation techniques.
(Continued on next page…)
■ Software Testing and Debugging
261
Usage-based techniques Operational profile
Test cases are generated according to their probability of occurring in actual operation.
Software reliabilityengineered testing
Tests are designed according to reliability objectives, expected use, and criticality of different functions. Figure 7.7. Test-set-generation techniques.
1.
Specification-based techniques. Testers use specification-based techniques to generate test cases based on requirement specification of the system. They use this technique primarily for black-box testing of software. Figure 7.7 lists popular techniques used for specification-based test-set generation.
2.
Code-based techniques. Testers use code-based techniques to generate test cases based on the knowledge of the code of the programs of the system. Two popular categories of code-based techniques are control-flowbased criteria and data-flow-based criteria. Testers use control-flow-based criteria to generate test cases to cover a program’s control flow, whereas they use data-flow-based criteria to generate test cases to cover the execution space between where variables are defined and where they are used. Figure 7.7 lists popular techniques used for control-flow-based criteria and data-flow-based criteria. Studies indicate that the effectiveness of testing is greater if testers apply both criteria together. Specification-based and code-based techniques complement each other in the kind of faults they detect.
3.
Fault-based techniques. Testers use fault-based techniques to generate test cases based on tester’s knowledge of what are the typical faults that the system can have. They also use it for mutation testing of programs. Mutation operators represent the faults a program will likely contain. Testers use them to generate program variants containing source code errors, called mutants. Testers use mutants to generate a set of test cases. Figure 7.7 lists popular techniques used for mutation testing.
4.
Usage-based techniques. Testers use usage-based techniques to generate test cases based on the idea of usage pattern of various functionalities of software. Figure 7.7 lists popular techniques used for usage-based test-set generation.
Test-case-selection Techniques Testing teams use test-set-generation techniques to generate test cases. Out of the test cases generated, it is important to select the test cases that the testers should use for testing. Testing teams use test-case-selection techniques primarily for three purposes: 1.
Filtering. Testing team must remove redundant test cases from the generated test cases to optimize testing time. Filtering techniques help in this process.
2.
Prioritization. Time and resource constraints make it difficult to conduct comprehensive testing of all requirements of a software system. Hence, testing team must prioritize the requirements so that they pay more time and attention to testing of high priority requirements. Ideally, the testing team should test each requirement at least once, and should test higher priority requirements several times. Prioritization techniques help in this process.
3.
Regression. When maintenance team modifies software during its maintenance phase, the testing team must re-test it. However, instead of running all the test cases in the existing test suite, it is more logical to run only selected test cases depending on the modifications made. Regression techniques help in selecting such test cases.
Figure 7.8 lists the commonly used test-case-selection techniques for these purposes.
Chapter 7 ■ Software
262
Technique
Description Filtering techniques
Cluster filtering
Generated test cases are filtered on the basis of cluster analysis of execution profiles.
Failure pursuit
Generated test cases are filtered on the basis of failure pursuit of execution profiles.
Prioritization techniques Branch coverage
Generated test cases are ordered according to their achieved branch coverage.
Statement coverage
Generated test cases are ordered according to their achieved statement coverage.
Function coverage
Generated test cases are ordered according to their achieved function coverage.
FEP (Fault Exposing Potential)
Generated test cases are ordered according to the probability a modified statement will cause a failure for a given test case.
Optimum
Generated test cases are ordered to optimize fault detection.
Regression techniques DejaVu
Test cases are selected by detecting modified code entities such as functions, variables, types, and preprocessor macros.
Test Tube
Test cases are selected according to a variant of DejaVu that is less precise but more robust in analyzing large software systems.
Textual differencing
Test cases are selected by comparing source files from the old and the new version of the program.
Modifications based
Test cases are selected to cover modified program components and those that the modifications might affect.
Data-flow-coverage based
Test cases are selected to exercise data interactions that the modifications have affected. Figure 7.8. Test-case-selection techniques.
SOFTWARE DEPLOYMENT Once the team has tested the software, and has removed all known errors in it, the team deploys the software at site, along with associated hardware for use by intended users. At this stage, the deployment team, and the users phase out the old system (manual system in most cases) and phase in the new system.
Changeover Operations Usually, the changeover process involves the following operations: 1.
Imparting system and user training to the members of the user organization of the system. The team imparts system training to the members responsible for managing and maintaining the system, whereas it imparts user training to the members responsible for operating the system and/or using the results produced by the system. The team also imparts some training to all those who will be affected by the new system to make them familiar with the changes.
2.
Replacing old operation procedures by new ones. This involves things such as discarding of old forms, manual registers, etc.
■ Software Deployment
263
3.
Replacing the old input and output devices by those of the new system.
4.
Defining the roles of different members, and assigning responsibilities to them as per the requirements of the new system.
5.
Converting the data in currently existing files into a form acceptable to the new system. This involves inputting of data stored in manual registers and hard-copy files through the input devices of the new system. While converting existing files, the team consolidates their records and eliminates duplicate records in them. The team also detects and removes inconsistencies or any errors in existing files.
Changeover Methods The three methods normally followed to carry out the changeover process are immediate changeover, parallel run, and phased conversion. Each one has its own advantages and disadvantages. No single method is suitable for converting all types of systems, and the choice of a particular method depends on the prevailing conversion circumstances. These methods are described here.
Immediate Changeover In this method, the deployment team and user organization decide a suitable cut-off date jointly, and make the new system operational from that day onwards [Figure 7.9(a)]. Operation of the old system stops from that day. This method is risky because any failure in the new system during the changeover process may cause total breakdown of those operations of the organization that are dependent on the new and old systems. The work cannot progress because the old system is not operational. Organizations prefer this method in those situations where available manpower and changeover time is less, and the system is not so critical in the sense that some problem during the changeover process would not lead to a disaster or great loss to the organization.
Parallel Run In this method, the organization operates both the old and new systems parallely with the same data for the initial three to four cycles [Figure 7.9(b)]. During this overlapping period of complete operation of the two systems, end users compare the results produced by the new system with those produced by the old system to develop confidence in the new system. They may discover some discrepancies. Often, these are due to inaccurate design of the old system, or due to missing program logic, or due to mistakes in the program itself. Programmers correct them by further debugging before the conversion is complete. This is the safest method because of the availability of the old system as a backup. If there are problems in the new system, there is no interruption of work because the old system is still in operation. The team can correct the problems found while the old system is still in use. This method, however, is expensive because the organization needs additional manpower during the overlapping period for operation of the two systems. Due to the requirement of additional manpower, the organization is under considerable strain during the period of parallel operation, and organizational breakdown may occur, if the period of parallel operation is long. Hence, the deployment team must ensure that they do not carry on parallel operation any longer than needed to establish confidence in the new system. Continuing it too long is a sign of weakness in the new system. Organizations do not prefer to use this method in situations where manpower resource is scarce. It is also not suitable for use in situations where the new system greatly differs from the old system in its functions, input, and output.
264
Chapter 7 ■ Software
Phased Conversion In this method, the complete changeover to the new system takes place incrementally over a period [Figure 7.9(c)]. The deployment team and user organization implement the new system gradually in parts. The end users compare the results produced by each part of the new system with the results of the old system. The team corrects any discrepancies or errors found. As soon as the organization develops confidence in a particular part, the team phases in that part of the new system, and phases out the corresponding part (operations) of the old system. This approach continues for each part of the new system. Hence, over a period, the team phases in the new system gradually, and phases out the old system gradually. Old system in operation
New system in operation
Old system
New system
Cut-off date Time (a) Immediate changeover
Old system in operation
New system in operation Overlapping period of complete operation of both the old and new systems.
Old system
New system Time (b) Parallel run
Old system completely operational
Old and new systems in operation in parts
New system completely operational
Old system
New system Time (c) Phased conversion Figure 7.9. Methods of system changeover to the new system.
■ Software Maintenance
265
This method is not as expensive as the parallel run method because changeover process being gradual, the organization can handle the changeover with existing manpower. Moreover, there is no danger of interruption of work, if there are problems with the new system because the corresponding part of the old system is still in operation. Users also get sufficient time to become familiar with the new system. Hence, they can handle the new system confidently when the deployment team hands over the complete system to them. Organizations, however, cannot use this method in situations where the time supplied for conversion process is short, or when the new system significantly differs from the old system.
System Evaluation Once the new system becomes operational after deployment, an evaluation team evaluates it to verify whether it meets its objectives. Requirement specification document states these objectives. The evaluation team normally consists of those people who have independent viewpoint (are not part of the development team). While evaluating a system, the evaluation team considers the following points: 1.
Performance evaluation. The evaluation team evaluates the performance of the new system by comparing it with that of the old system. Generally, the new system should be at least as efficient as the old one. If the evaluation team reports any slack, the development team analyzes the reason, and if possible, incorporates necessary changes in the new system to rectify it.
2.
Cost analysis. The evaluation team does cost analysis to verify whether cost estimates done for various phases of the project during the planning phase match with the actual cost incurred in each phase. In case of discrepancies, cost analysis helps in finding out the reasons. This knowledge is useful in making correct cost estimates for future systems.
3.
Time analysis. The evaluation team does time analysis to verify whether time estimates done for various phases of project during the planning phase match with the actual time taken in each phase. In case of discrepancies, time analysis helps in finding out the reasons. This knowledge is useful in making correct time estimates for future systems.
4.
User satisfaction. The evaluation team does user satisfaction analysis to verify things such as – How useful is the system for users? How enthusiastic they are about the service they receive? Do they receive outputs in time to take necessary action? The morale of people using or affected by the new system is a good measure of the success of the system.
5.
Ease of modification. All systems need modification due to one or more of the reasons mentioned in the next section. Hence, the ease with which a programmer can modify a system to incorporate the suggested changes is also an important parameter to judge the quality of the system.
6.
Failure rate. The quality of a system also depends on its failure rate. A system that fails frequently cannot meet its objectives successfully. Hence, it is of poor quality.
SOFTWARE MAINTENANCE
Definition Software maintenance is the process of modifying software system or component after deployment to correct faults, add functionality, improve performance or other attributes, or adapt to a change in environment.
Chapter 7 ■ Software
266
Need for Software Maintenance Law of software evolution states that any software used in real world environment must change with time or become less useful progressively. Hence, no matter how good a new software system is, and how well it is operating currently, it needs modification from time-to-time due to one or more of the following reasons: 1.
Changes in business conditions or operations of the organization using the software.
2.
Changes in organizational policies or enforcement of new laws.
3.
Changes in user needs, such as demand for additional features or output reports.
4.
Changes in technology, such as availability of new and more powerful processors, more efficient storage, more convenient input/output devices, new version of operating system used, new operating system, new version of database system used, new database system, etc.
Types of Software Maintenance Software products undergo one or more of the following types of maintenance: 1.
Perfective maintenance. It deals with making changes in software products to accommodate changes in requirements suggested by the user(s). For example, user(s) may ask for additional functionality, different user interface, better performance, etc.
2.
Adaptive maintenance. It deals with making changes in software products to make them suitable for use in new environments, such as a new hardware platform, a new operating system, a new version of an existing operating system, a new database system, or a new version of an existing database system.
3.
Corrective maintenance. It deals with making changes in software products to remove faults, which user(s) or internal testing team(s) report.
4.
Preventive maintenance. It deals with making changes in software products to make them more maintainable. For example, software organizations update their products and/or apply software patches in advance, so that issues/problems do not occur in the products supplied to the user(s).
Importance of Software Maintenance Maintenance is an important phase in SDLC. Studies have shown that, on an average, maintenance cost of software systems is two to four times the development cost. This means that more than half of the programming tasks in this world involve program maintenance. Hence, some software organizations have a group of programmers whose only job is to maintain production programs.
Controlling Modifications Frequent change is disrupting and disturbing. Therefore, some control over changes is required. One method of achieving this is to have a change control board, which evaluates all requests for change. Usually, this board is comprised of the principal users of the system, a system analyst, and data processing personnel who are familiar with the system. The change control board need not approve normal maintenance operations. Maintenance staff should record and summarize these operations for periodic reporting to the board. Examples of maintenance activities are modifying the format of a report, or rewriting a part of software to improve performance. Major changes are those that alter the system significantly, or require extensive personnel, hardware, or software. For example, conversion of software from Unix to Windows environment is a major change.
■ Software Documentation
267
Whenever a programmer modifies a program, the concerned members must also modify the associated documents. Without proper documentation that is consistent with the programs, carrying out future changes would be difficult and expensive.
Tools for Effective Software Maintenance Software maintenance is a major activity in SDLC that consumes large resources. Hence, over the years, software industry has developed and used several tools that simplify the maintenance processes and increase the productivity of members involved in maintenance activities. Out of several tools that software maintenance personnel use, two of the most important ones are: 1.
Configuration management/version control tool. Software developers use this tool to make changes to the correct files and to keep track of who is changing what parts of the source code. When multiple members of a development team work on the same software, it becomes a challenge to keep track of who is changing what parts of the source code. When the members of the development team are located in different locations (cities, countries, or even continents), the problem becomes compounded due to limited communication among the remotely located developers. The version control or configuration management tool proves to be useful for development teams in such situations. It allows developers to check out code, make changes, and save both the old and new versions of the changed file(s). Visual Source Safe (VSS) from Microsoft, ClearCase by IBM Rational and monotone an Open Source tool are three examples of version control tool.
2.
Bug reporting/tracking tool. Software developers, testers, maintenance team members, and even the users (customers) use this tool to report bugs/problems in software along with information associated with a bug/problem. The development team also uses the tool to record the solution provided and associated information when they fix a reported bug/problem. In this manner, the tool helps in creating a knowledge repository of all known bugs/problems in software and the solutions provided for them. This knowledge repository is useful for support/maintenance team members in finding solutions to newly reported bugs/problems. BugZilla from the open source community is an example of bug reporting/tracking tool. JIRA is a commercial bug-tracking tool. Several major software vendors use their own in-house tool for this purpose.
SOFTWARE DOCUMENTATION What is Documentation? Documentation is the process of collecting, organizing, storing, and maintaining a complete historical record of programs and other documents used or prepared during the different phases of the life cycle of software. Without proper documentation of software, the software industry does not consider it complete. In fact, documentation is an on-going process that starts as early as in the study phase of the software and continues until its implementation and operation phase. Moreover, documentation is a never-ending process throughout the life of the software. The maintenance team has to carry out documentation from time-to-time, whenever it modifies the software.
Need for Documentation Proper documentation of software is necessary due to the following reasons: 1.
It solves the problem of indispensability of an individual for an organization. Even if the designer or programmer of the software leaves the organization, the documented knowledge remains with the organization that is useful for the continuity of the software.
2.
It makes software easier to modify and maintain in future. The key to maintenance is proper and dynamic documentation. It is easier to understand the logic of a program from documented records rather than its code.
Chapter 7 ■ Software
268
System flowcharts, program flowcharts, or comments written within the programs are quite helpful in this regard. 3.
It helps in restarting a software project postponed earlier due to some reason. The job need not start from scratch. The new team can recapitulate the old ideas from the available documents, avoiding duplication of work, and saving lot of time and effort.
Forms of Documentation The six commonly used forms of documentation for software are requirement specification document, design document, comments, system manual, user manual, and marketing document. They are described here. All these forms of documentation are necessary to document any software completely.
Requirement Specification Document Before developing some software, the development team must analyze its requirements and document them. Requirement specification document is an outcome of this exercise. It specifies the objectives of developing the software and its usefulness to various categories of users. It also specifies the functionalities, capabilities, performance, inputs, and outputs requirements of the software. It specifies what all the software will do when developed. We can specify the requirements in requirement specification document in a variety of styles, notations, and formats. For example, we may use textual statements in a natural language like English, or diagrams, or mathematical formulae, or a combination of all of these. The requirement specification document is useful for a variety of purposes such as: 1.
It serves as an agreement between the customer requesting development of the software and the developer on what is the scope of the software.
2.
Software developers use it to define the modules, user interfaces, performance parameters, etc. during the design phase to meet the specified requirements.
3.
Testing team uses it to test whether the software performs its stated objectives as per the specified requirements.
4.
Deployment team uses it to evaluate the deployed system against the specified requirements. Maintenance team finds it useful when maintaining changes in software. When they modify the software, they find requirement specification document useful for verifying, if the modifications affect any of the software’s original requirement.
Design Document Design document defines the overall architecture of the software. It specifies the various hardware and software components of the software and interfaces between the components. It includes a hierarchy of software components, rules for component selection, and interfaces between the components. A good design document provides an overview of the total system architecture and does not get into implementation details of the various components.
Comments Comments are natural language statements put within a program to assist anyone reading the source program listing in understanding the logic of the program. They do not contain any program logic, and are ignored (not translated) by a language processor. From maintenance point of view, comments are necessary. All high-level languages provide the
■ Software Documentation
269
facility to write comments in the source code of a program. Liberal use of this facility by programmers for documentation of their programs is highly recommended. Comments should not be redundant, incorrect, incomplete, or written in a manner that others find difficult to understand. For example, a redundant comment for the statement N = N + 1 would be “INCREMENT N BY 1”. Useful comments describe the meaning of a group of statements such as, “READ AND ECHO PRINT THE INPUT DATA”. Good comments are those that mediate between a program and its problem domain.
System Manual Good software must have a standard system manual containing the following information: 1.
Specific module names along with their description and purpose.
2.
Detailed system flowcharts and program flowcharts for each module cross-referenced to program listing.
3.
Description of program listings and control procedures.
4.
Source listing of all programs with full details of all modifications made from time-to-time.
5.
Specifications of all input and output media required for operation of various programs.
6.
Specimen of all types of inputs/outputs to/from software.
7.
File layout, that is, detailed layout of input and output records.
8.
Structure and description of all test data, test results, storage dumps, trace program printouts, etc., for testing and debugging of programs.
User Manual User manual describes how to use the software. Good software must have a good user manual to ensure its easy and smooth usage. It is the users, not the developers, who use the software regularly. Hence, the user manual must contain the following information: 1.
Installation and operational details of software.
2.
Loading and unloading procedures.
3.
Starting, running, and terminating procedures.
4.
Description and example of any control statements used.
5.
All console commands with errors and console messages, their meaning, reply, and/or operation action.
6.
List of error conditions with explanation for their re-entry into the system.
7.
List of programs, which users must execute before and after execution of each program.
8.
Special checks (if any) and security measures, etc.
User manual describes each feature of software in a manner that users can easily make use of these features. The three commonly used ways to do this are (many software use all the three): 1.
Commands/functions list. In this method, the user manual lists the commands or functions of the software alphabetically or grouped logically, often with cross-referenced indexes. Each item in the list describes a command or function in detail with examples.
2.
Tutorial. In this method, the user manual contains tutorials for various functionalities or tasks of software, which provide step-by-step guidance to users on how to accomplish a task by using the software.
Chapter 7 ■ Software
270 3.
Online help. In this method, the user manual contains online information, which users can use whenever needed while using the software, without the need to refer to any other document associated with the software. This saves time and effort of users, which they would require otherwise to trace and refer to a hardcopy user manual for solving problems while using the software.
Marketing Document Marketing document serves as promotional material for software products. Marketing personnel use it to promote the product. Typically, a marketing document consists of the following: 1.
Usefulness of the software product
2.
Its salient features
3.
Its comparison with other competing products
4.
Its system requirements (hardware, operating system, etc.)
Documentation Standard There have been many problems in the past with poorly documented software. Elimination of errors and incorporation of modifications are usually difficult in such software, making them expensive to maintain. Realizing the importance of software documentation, several software organizations have developed strict documentation standards. These standards describe how to do documentation, how to choose meaningful program variable names, how to design the GUI for any software, how and up to what detail to include comments in program code, and what diagrams, charts, reports, outputs, etc. are necessary for completing documentation. Software developers and programmers working in such an organization are required to adhere to its documentation standards.
SOFTWARE ENGINEERING What is Software Engineering? “Software” is a set of computer programs, procedures, and associated documents. On the other hand, “engineering” is systematic application of scientific knowledge in creation and building of cost-effective solutions to practical problems. Hence, “software engineering” is systematic application of principles of computer science and mathematics in creation and building of cost-effective software solutions.
Need for Software Engineering With software products growing in scale and complexity, number of software developers involved in a software development project has been increasing proportionately. Software industry soon realized that managing the development of large software products and maintaining them is a difficult task. Software development projects frequently faced the problems of missed schedules, cost overruns, and low quality software products. Moreover, use of progressively larger software products in sensitive applications, where errors in software products could have severe and even life-threatening consequences, required correctness and reliability of software products to be of high degree. These quality and productivity demands for software products led to the introduction of systematic practices (later on known as software engineering practices) in software industry.
■ Software Engineering
271
Goals of Software Engineering Software engineering aims at producing software products having the following properties: 1.
Their correctness should be of high degree. Correctness refers to the degree to which a software product performs its intended functions properly, consistently, and predictably.
2.
Their usability should be of high degree. Usability refers to the ease with which a software product and its associated documentation are usable by the intended audience.
3.
They should be cost-effective. Cost-effectiveness means that the total development and operational costs of a software product should be as low as possible without compromising on the quality of the product in any way. Overall cost-effectiveness also requires that the maintenance team can modify and enhance a software product easily.
Software products that meet these goals do not cause unusual stress during their various phases of development and are adaptable to unanticipated needs.
Principles of Software Engineering Principles of software engineering are the generally accepted guidelines that software designers and developers should follow to produce a software product for meeting the goals of software engineering. These guidelines are described here.
Precise Requirements Definition The designer of a software product must define its requirements precisely to ensure that the resulting product meets users’ needs. Ill-defined software requirements lead to problems in the later phases of software life cycle.
Modular Structure The designer of a software product must structure it in a modular fashion so that it is easy to program, implement, and maintain. A modular design helps in distribution of development task of different modules to different programmers, enabling development of the product in a shorter time. It also helps in easier testing, debugging, and maintenance of the product because respective teams can test, debug, and maintain individual modules independently. Designers can do feature enhancements by adding new modules. Modular design also enables software reusability because if a module exists for a desired functionality, it can be used as it is, or with minor customization. Third party library routines are an example of reusable software modules.
Abstraction As far as practicable, a software product should use abstraction and information hiding. For example, in modular design, a programmer should implement each module in such a manner that its implementation details are hidden inside the module and only module interface is visible outside to allow other modules to interact with the module. Object-oriented programming takes care of this aspect of software engineering. Abstraction helps in easy reusability of existing modules because for reusing a module, a programmer needs to understand only its interface and not its internal details. Uniformity A software product should maintain uniformity in design, coding, documentation, etc. Uniformity ensures consistency, which makes the product easier to develop and maintain, as well as easier to learn and use. For example, if all graphical user interfaces of a software product follow a uniform style (providing the same look-and-feel
Chapter 7 ■ Software
272
throughout), it becomes much easier for a user to learn and use the software. Similarly, if all programmers working on different modules of a software project follow uniform coding and documentation styles, it becomes easier for someone else to understand and maintain the modules. Software organizations define/adapt and use several software engineering standards, such as design standards, coding standards, documentation standards, etc. to take care of this aspect of software engineering.
CASE Tools Software tools play an important role in software engineering. They help in increasing professional capability and eliminate or reduce sources of error in software products. A large number of software tools are available to aid software developers in different phases of a software product’s life cycle. In fact, popularity of these tools has resulted in a new area called CASE (Computer Aided Software Engineering). CASE tools provide a wide range of features for creation of better and more reliable software products within less time and with less effort. A few of the most common features are: 1.
Design specification tools allow programmers to visually design screens, menus, database tables, reports, dialog boxes, and other key components of a program. With individual components defined, a programmer can link the components together and then test the operation of the program, without writing even a single line of program code.
2.
Code generation tools generate source code (programs) from the design specification of a software product. That is, once satisfied with the structure, design, and components of the product, a programmer can invoke a code generator, which creates the source code for the program automatically. Automatic creation of programs by the code generator also leads to error-free program codes.
3.
Testing and debugging tools help programmers in testing and debugging of their programs.
4.
Source-code analysis tools help in optimizing a program by pointing out unreachable lines of code and those functions that the program never uses (calls). They also assist in carrying out program changes consistently and uniformly by indicating, what other parts of a program would be affected by introducing a change in the program.
5.
Documentation tools assist in automatic generation of technical documents for a software product. For example, automatic flowchart generation tools create flowcharts for already written programs. Similarly, as a programmer uses a design specification tool to design the screens, menus, reports, dialog boxes, and other key components of a software product, a documentation tool collects this information for producing the product’s documentation automatically.
Good CASE tools reduce much of the tedious work of creating a software product. Hence, they save several hours of time to get a new program up and running. They allow a programmer to concentrate on important functional parts of the program, rather than spending time in writing the code to display screens, dialog boxes, and menus. CASE tools, however, are not an answer for every situation. Even in case of software products that make extensive use of CASE tools during their development, some hand coding and manual documentation is necessary.
SOME POPULAR APPLICATION SOFTWARE PACKAGES Millions of application software packages are available today. Some popular ones, which many of us use or come across in our day-to-day life are word-processing package, spreadsheet package, graphics package, personal assistance package, education software, and entertainment software.
■ Some Popular Application Software Packages
273
Word-processing Package The term word-processing describes the use of hardware and software to create, edit, view, format, store, retrieve, and print documents (written material such as letters, reports, books, etc.). A word-processing package enables us to do all these on a computer system. Some commonly supported features found in modern word-processing packages are: Importing text, graphics and images
1.
Entering text
9.
2.
Editing text
10. Searching and replacing text string
3.
Formatting page style
11. Checking spelling
4.
Formatting text
12. Checking grammar and style
5.
Entering mathematical symbols
13. Using thesaurus
6.
Displaying documents
14. Creating table-of-contents and index
7.
Saving, retrieving and deleting documents
15. Using mail-merge
8.
Printing documents
Spreadsheet Package Spreadsheet is a numerical data analysis tool that allows us to create a computerized ledger. A manual ledger is a book having rows and columns that accountants use for keeping record of financial transactions and preparing financial statements. Accountants use pencil, erasure, and hand calculator to prepare financial statements using manual ledger. This is a tedious task and takes time to come out with an acceptable and satisfactory financial statement, due to several iterations of formula calculations. A spreadsheet offers considerable ease of performing such tasks by automating all arithmetic calculations, and making it easier to change certain numerical values and seeing the effect of these changes across the worksheet (ledger) immediately. With the availability of spreadsheet packages, we are no longer required to use pencils, erasers, and hand calculators for dealing with tasks that require numerical data analysis. Whereas paper ledgers were tools for accountants, spreadsheets are tools for anyone who needs to record, organize, or analyze numbers as rows and columns of data. Some typical uses of spreadsheets are: 1.
For maintaining and analyzing inventory, payroll, and other accounting records by accountants.
2.
For preparing budgets and bid comparisons by business analysts.
3.
For recording grades of students and carrying out various types of analysis of grades by educators.
4.
For analyzing experimental results by scientists and researchers.
5.
For tracking stocks and keeping records of investor accounts by stockbrokers.
6.
For creating and tracking personal budgets, loan payments, etc. by individuals.
This list gives an idea of how flexible and versatile tool a spreadsheet is. It is not an exhaustive list. Some key features of modern spreadsheet packages are: 1.
Support for a large number of cells
Chapter 7 ■ Software
274 2.
Support for addressing a range of cells by the addresses of the endpoint cells
3.
Support for different types of cell data (such as, label, numeric value, formula, and date and time)
4.
Support for use of relative and absolute cell addresses in formula
5.
Support for a wide range of commands
6.
Support for displaying numeric data in the form of graphs and charts
Graphics Package A graphics package enables us to use a computer system to create, edit, view, store, retrieve and print designs, drawings, pictures, graphs, and anything else that can be drawn in traditional manner. Some commonly supported features found in modern graphics packages are: 1.
Drawing designs
2.
Painting drawings and pictures
3.
Presenting graphs and charts
4.
Dragging-and-dropping graphic objects
5.
Importing graphic objects
6.
Capturing screen snapshots
Personal-assistance Package A personal-assistance package allows us to use personal computers to store and retrieve personal information, as well as to plan and manage schedules, contacts, finances, and inventory of important items. Some commonly supported features found in modern personal-assistance packages are: 1.
Calendar
2.
To-do-list
3.
Address book
4.
Investments book
5.
Inventory book
Education Software An education software package allows us to use a computer as a teaching and/or learning tool. Today, several types of education software packages are available. Examples of some commonly used types are: 1.
Software packages that teach young children to do mathematics, to recognize alphabets, or to read whole words and sentences
2.
Software packages that allow students to prepare for tests and entrance examinations
3.
Software packages that allow an individual to take an on-line examination in which an examinee has to use a computer during the examination
■ Points to Remember
275
4.
Software packages that allow individuals to learn foreign languages
5.
Software packages that allow hard-copy books and other reading material to be made available in soft-copy form on computers
Entertainment Software An entertainment software package allows us to use a computer as an entertainment tool. Two examples of software that belong to this category are: 1.
Computer video games
2.
Interactive television with video-on-demand facility
Points to Remember 1.
The term hardware refers to the physical devices of a computer system. Thus, input, storage, processing, control, and output devices are hardware.
2.
The term software refers to a collection of programs and associated documents.
3.
A program is a sequence of instructions written in a language understood by a computer. It controls a computer’s processing activity, and the computer performs precisely what the program wants it to do.
4.
A software package is a group of programs that solve a specific problem or perform a specific type of job.
5.
Hardware and software of a computer must work together for the computer to produce useful output. Both hardware and software are necessary for a computer to do useful job.
6.
We classify most software into two categories – system software and application software. System software is a set of one or more programs, which controls the operation and/or extends the processing capability of a computer system. Application software is a set of one or more programs, which solves a specific problem, or does a specific task.
7.
A few examples of system software are operating systems, programming language translators, utility programs, performance monitoring software, and communications software.
8.
A few examples of application software are word-processing software, spreadsheet software, graphics software, personal assistance software, education software, and entertainment software.
9.
Figure 7.1 shows the relationship among hardware, system software, application software, and users of a computer system.
10. Firmware is software substituted for hardware and stored in a read-only memory (ROM) chip. 11. Middleware is a set of tools and data that help applications use networked resources and services. It is a separate software layer, which acts as “glue” between client and server parts of an application. It provides a programming abstraction that masks the heterogeneity of underlying network, hardware, and operating system from application programmers. 12. A user can acquire a desired software in one or more of the following ways: By buying pre-written software By developing customized software By ordering customized software By downloading public-domain software Each of these ways of acquiring software has its own advantages and limitations. 13. Developing software, putting it to use, and maintaining it is a complex process involving the following phases: Requirement specification System analysis and design Implementation (also known as Construction or Code generation) Testing and debugging Deployment (also known as Installation)
276
Chapter 7 ■ Software Maintenance Evolving software products through these phases is known as Software Development Life Cycle (SDLC) model. This model is also known as waterfall model.
14. The three software design approaches used commonly are top-down (also known as decomposition approach), bottom-up (also known as synthesis approach), and out-of-the-middle. 15. Software implementation deals with code generation of various software components specified in system design document and linking them to build the target software system. 16. Testing is the process of making sure that the program performs the intended task. 17. Program errors are known as bugs, and the process of locating and correcting these errors is called debugging. 18. The two types of errors that occur in a computer program are syntax errors and logic errors. Syntax errors occur when the rules or syntax of a programming language are not followed. Logic errors occur when we make errors in planning the program’s logic or while converting the logic into program code. Such errors cause the program to produce incorrect result or behave unexpectedly. 19. As compared to logic errors, syntax errors are easier to locate and correct because language processors detect syntax errors automatically. 20. The commonly used methods for locating and correcting logic errors are doing hand simulation of the program code, putting print statements in the program code, using a debugger, and using memory dump. 21. Debugger is a software tool that assists a programmer in following a program’s execution step-by-step by allowing him/her to display intermediate calculation results and field values whenever desired. 22. The commonly used testing methodologies for testing software systems are: Unit and integrated testing Alpha and beta testing Functional and non-functional testing Black-box, white-box, and grey-box testing 23. The commonly used test-set-generation techniques are broadly classified into: Specification-based techniques Fault-based techniques Code-based techniques Usage-based techniques 24. Once the software development team has tested the software, and has removed all known errors in it, the team deploys the software at site, along with associated hardware for use by intended users. At this stage, deployment team and users phase out the old system (manual system in most cases) and phase in the new system. The three methods followed normally to carry out the changeover process are immediate changeover, parallel run, and phased conversion. 25. System evaluation is the process of evaluating a system (after it is put to operation) to verify whether it meets its objectives. The points considered for evaluating a system are performance evaluation, cost analysis, time analysis, user satisfaction, ease of modification, and failure rate. 26. Software maintenance is the process of modifying software system or component after deployment to correct faults, add functionality, improve performance or other attributes, or adapt to a change in environment. 27. Software products undergo one or more of the following types of maintenance: Perfective maintenance Corrective maintenance Adaptive maintenance Preventive maintenance 28. Documentation is the process of collecting, organizing, storing, and maintaining a complete historical record of programs and other documents used or prepared during the different phases of the life cycle of software. 29. The six commonly used forms of documentation for software are requirement specification document, design document, comments, system manual, user manual, and marketing document. 30. Software engineering is systematic application of principles of computer science and mathematics in creation and building of cost-effective software solutions. 31. A word-processing package enables us to create, edit, view, format, store, retrieve, and print documents on a computer system.
■ Questions
277
32. Spreadsheet is a numerical data analysis tool that allows us to create a computerized ledger. It is useful for any numerical analysis problem, whose data we can organize as rows and columns. 33. A graphics package enables us to use a computer system to create, edit, view, store, retrieve, and print designs, drawings, pictures, graphs, and anything else that can be drawn in traditional manner. 34. A personal-assistance package allows us to use personal computers to store and retrieve personal information, as well as to plan and manage schedules, contacts, finances, and inventory of important items. 35. An education software package allows us to use a computer as a teaching and/or learning tool. 36. An entertainment software package allows us to use a computer as an entertainment tool. Computer video games belong to this category of software.
Questions 1.
Define the terms hardware and software. Explain the relationship between them.
2.
What is a computer program?
3.
What is running/executing a computer program?
4.
What is a software package?
5.
Normally, hardware is a one-time expense, whereas software is a continuing expense. Explain.
6.
Define the and distinguish between application software and system software.
7.
Define the following terms: (a) System software (b) System program (c) System programmer
(d) Application software (e) Application program (f) Application programmer
8.
Write key functions performed by the system software of a computer system.
9.
Explain the importance of system software for a computer system.
10.
Explain the relationship among hardware, system software, application software, and users of a computer system.
11.
How does a normal user interact with the hardware of a computer? Describe the various in-between layers.
12.
What is firmware and what is its importance to a computer system architect?
13.
What is middleware? Why is it so called? How is it useful?
14.
Differentiate among hardware, software, firmware, and middleware.
15.
What are the different ways of acquiring software? What are their relative advantages and limitations?
16.
Write the steps followed typically by a user in selecting and buying a pre-written software package.
17.
Write the steps followed typically by a user in placing an order on another organization for creation of a customized software package.
18.
Write relative advantages and disadvantages of creating customized software in-house by an organization versus getting it created by another organization.
19.
What is a turnkey solution?
20.
Write the steps followed typically in in-house development of a software package.
21.
What is a shareware? What are the advantages and limitations of using a shareware?
22.
Are all public-domain software “freeware”? Explain.
23.
Are all public-domain software “Open Source Software (OSS)”? Explain.
24.
What is Open Source Software (OSS)? What are its advantages as compared to conventional commercial software?
25.
Why is public-domain software also known as community-supported software?
278
26.
Chapter 7 ■ Software
Write the various phases of Software Development Life Cycle (SDLC) model and the main activities performed in each phase.
27.
Why is SDLC model also known as waterfall model?
28.
Write advantages and limitations of SDLC model.
29.
What are lightweight SDLC models? Why are they also known as agile development methods? Name three agile development methods.
30.
How are agile development methods of software development different from the traditional SDLC model?
31.
Describe the three approaches used commonly for software design.
32.
Differentiate between top-down and bottom-up approaches of software design.
33.
What are the two types of errors that can occur in a computer program? Write an example of each type to illustrate their nature.
34.
Differentiate between syntax errors and logic errors in computer programs.
35.
Will a program run if there are syntax errors in it? Write reasons for your answer.
36.
Will a program run if there are logic errors in it? Write reasons for your answer.
37.
What is testing of a program? Why do we need to test a program?
38.
What are the different types of test data used for testing a program?
39.
Why is it not possible to certify for a complex software system that it is error free?
40.
What is alpha version of software? What is alpha testing?
41.
What is beta version of commercial software? What is beta testing?
42.
Differentiate between alpha and beta testing of software.
43.
How can we detect and correct syntax errors in a program?
44.
How can we detect and correct logic errors in a program?
45.
Is it easier to detect a syntax error or a logic error in a program? Write reasons for your answer.
46.
What is a program bug? What is debugging?
47.
In high-level languages, a single syntax error often causes generation of multiple error messages. Explain why.
48.
Describe the various methods to locate and correct logic errors.
49.
What is a debugger? How does it help a programmer?
50.
What is a memory dump? How is it useful for a programmer?
51.
In the context of debugging of a program, what is a checkpoint? What are the two commonly supported types of checkpoints in modern debuggers? How are they useful?
52.
Differentiate between a breakpoint and a watchpoint in the context of program debugging.
53.
Differentiate between testing and debugging.
54.
Testing can begin in the early stages of software development, while debugging can begin only after coding of the program. Discuss.
55.
Differentiate between: (a) Unit and integrated testing (b) Alpha and beta testing (c) Functional and non-functional testing (d) Black-box and white-box testing
56.
What is test-set generation? Describe some commonly used test-set-generation techniques.
57.
How do test-case-selection techniques help software testing teams?
■ Questions
279
58.
Write the operations normally carried out in the system changeover process.
59.
Describe the three methods of system changeover with their advantages and disadvantages.
60.
Immediate changeover is a risky method of deploying a new system. Explain why.
61.
The method of parallel run is one of the safest ways to deploy a new system. Explain why.
62.
In what situations organizations do not prefer the parallel run method of deploying a new system?
63.
Why do we need to evaluate a new system? What parameters do we normally use to evaluate a new system?
64.
What is software maintenance? Why do we need to modify software?
65.
Differentiate among perfective, adaptive, corrective, and preventive maintenance of software.
66.
How can we control frequent program modifications?
67.
What is software documentation? Why do we need it?
68.
Describe different types of documentation normally used for documenting software systems.
69.
What are program comments? Why do we need them?
70.
What is system manual of a software system? What type of information does it typically contain?
71.
What is user manual of a software system? What type of information does it typically contain?
72.
What is documentation standard? Why do we need it?
73.
What is software engineering? Why do we need it?
74.
Write the goals of software engineering.
75.
Write the main principles of software engineering. Explain how each of these principles helps in achieving one or more goals of software engineering.
76.
What is modular structure of a software system? How does it help?
77.
What is abstraction in software systems? How does it help?
78.
Why should software systems maintain uniformity in design, coding, documentation, etc.? How can we ensure uniformity?
79.
What are CASE tools? How are they useful?
80.
Write three types of CASE tools. Explain the use of each.
81.
What is a source-code generator? How is it useful?
82.
What is a source-code analyzer? How is it useful?
83.
CASE tools are not the answer for every situation. Discuss.
84.
What is a word-processing package? Write key features supported by modern word-processing packages.
85.
What is a spreadsheet package? Write its typical uses.
86.
What is graphics software? Write features supported normally in graphics software.
87.
What is a personal-assistance package? Write features supported normally by it. Explain the usefulness of each of these features.
88.
What is education software? Write three examples of such software.
89.
What is entertainment software? Write three examples of such software.
Planning the Computer Program
In Chapter 7, we saw that computer software is a set of computer programs. We also saw that before writing a program, one must first plan the program. That is, to write a correct and effective computer program we must first plan its logic. If we attempt to plan the logic and write the program at the same time, we are likely to become so involved with the required instruction formats that program logic will suffer. Hence, before learning how to write a computer program (in next chapter) we will first learn how to plan its logic (in this chapter).
PURPOSE OF PROGRAM PLANNING If your teacher asked you to solve an arithmetic problem and you are not familiar with the steps involved in solving the problem, you will not be able to solve it. The same principle applies to writing computer programs. A programmer cannot write the instructions for a computer to solve a problem unless the programmer knows how to solve the problem manually. If you know the steps for solving a problem, but while solving it, if you forget to apply some of the steps or you apply the steps in wrong sequence, you get a wrong answer. Similarly, while writing a computer program, if a programmer leaves out some of the instructions or writes the instructions in wrong sequence, the programmer gets a wrong answer when he/she executes the program on a computer. Hence, to ensure that the instructions of a program are appropriate for the problem at hand and are in the correct sequence, we must plan the program before writing it.
ALGORITHM What is an Algorithm? Planning a program involves defining its logic (the correct sequence of instructions needed to solve the corresponding problem). The term algorithm refers to the logic of a program. It is a step-by-step description of how to arrive at a solution to a given problem. It is a sequence of instructions that when executed in the specified sequence, gives the desired results. To qualify as an algorithm, a sequence of instructions must possess the following characteristics:
■ Algorithm 1.
Each instruction should be precise and unambiguous.
2.
Each instruction should execute in a finite time.
3.
No instruction should repeat infinitely. This ensures that the algorithm terminates ultimately.
4.
After executing the instructions (when the algorithm terminates), the desired results are obtained.
281
Sample Algorithms To gain insight into algorithms, let us consider some simple examples. Example 8.1. Fifty students in a class appeared in their final examination. The division column of their mark sheets contains the division (FIRST, SECOND, THIRD, or FAIL) obtained by them. If their mark sheets are available with you, write an algorithm to calculate and print the total number of students who passed in FIRST division. Algorithm: Step 1:
Initialize Total_First_Division and Total_Marksheets_Checked to zero.
Step 2: Take the mark sheet of the next student. Step 3:
Check the division column of the mark sheet to see if it is FIRST. If no, go to Step 5.
Step 4: Add 1 to Total_First_Division. Step 5: Add 1 to Total_Marksheets_Checked. Step 6: Is Total_Marksheets_Checked = 50? If no, go to Step 2. Step 7: Print Total_First_Division. Step 8: Stop. Example 8.2. There are 100 employees in an organization. The organization wants to distribute annual bonus to the employees based on their performance recorded in their annual appraisal forms. Each employee’s appraisal form contains his/her basic salary and grade for his/her performance during the year. Grades are of three categories – ‘A’ for outstanding, ‘B’ for good, and ‘C’ for average performance. Bonus for an employee is 100% of basic salary for outstanding performance, 70% of basic salary for good performance, 40% of basic salary for average performance, and none for all other cases. Write an algorithm to calculate and print the total bonus amount.
Algorithm: Step 1:
Initialize Total_Bonus and Total_Employees_Checked to zero.
Step 2:
Initialize Bonus and Basic_Salary to zero.
Step 3: Take the appraisal form of the next employee. Step 4:
Read the employee’s Basic_Salary and Grade.
Step 5:
If Grade = A, then Bonus = Basic_Salary. Go to Step 8.
Step 6:
If Grade = B, then Bonus = Basic_Salary x 0.7. Go to Step 8.
Chapter 8 ■ Planning the Computer Program
282
Step 7:
If Grade = C, then Bonus = Basic_Salary x 0.4.
Step 8:
Add Bonus to Total_Bonus.
Step 9:
Add 1 to Total_Employees_Checked.
Step 10: If Total_Employees_Checked < 100, then go to Step 2. Step 11: Print Total_Bonus. Step 12: Stop. Programmers design such algorithms to solve complex programming problems. The examples show that algorithm development even for simple problems is not as simple as it might appear initially, and requires some thinking. The examples also show that to solve a problem, every instruction must be carried out strictly in a particular sequence. It is this fact that a beginner to problem solving using computers finds difficult to appreciate.
Quality of Algorithms Given a solvable problem, often there are many methods to solve it, not all of which may be equally good. Similarly, for a given problem, there may be many algorithms, not all of equal quality. Factors often used to judge the quality of an algorithm are: 1.
Time requirement. The lesser is the time required for executing the corresponding program on a given computer, the better is the algorithm.
2.
Accuracy of solution. Although, multiple algorithms may provide correct solutions to a given problem, some of these may provide results that are more accurate. For example, out of two algorithms for calculating the square root of a given number, if one produces results up to three decimal places, while the other one produces results up to six decimal places, the latter is better from the point of view of accuracy of solution.
3.
Generality. While there are occasions when we design an algorithm to solve a single isolated problem, more often we design algorithms to handle a range of input data. A generalized algorithm, which can handle a range of input data, is better than one which can solve a problem for a single input data. For instance, the algorithm given for Example 8.1 is not a generalized one, because in Step 6 it checks if Total_Marksheets_Checked has become equal to 50. Hence, it will work only if there are exactly 50 students’ data as input. The algorithm can be made general by accepting the number of students ( N ) as input and then comparing the value of Total_Marksheets_Checked with N, instead of 50 in Step 6.
Representation of Algorithms Commonly used ways to represent an algorithm are: 1.
As program
3. As flowchart
2.
As pseudocode
4. As decision table
An algorithm represented in the form of a programming language is a program. Hence, any program is an algorithm, but the reverse is not true. Programmers often use flowcharts, pseudocodes, and decision tables for representing algorithms for their programs before coding them in programming languages. Subsequent sections describe these program-planning tools. To plan their programs, programmers generally select one of them depending on their choice.
■ Flowcharts
283
FLOWCHARTS What is a Flowchart? A flowchart is a pictorial representation of an algorithm. Programmers often use it as a program-planning tool for visually organizing a sequence of steps necessary to solve a problem using a computer. It uses boxes of different shapes to denote different types of instructions. Programmers write actual instructions within these boxes using clear and concise statements. Directed solid lines connecting these boxes indicate flow of operations – sequence in which to execute the instructions. The process of drawing a flowchart for an algorithm is known as flowcharting.
Why Use Flowcharts? Normally, we first represent an algorithm as a flowchart, and then express the flowchart in a programming language to prepare a computer program. The main advantage of this two-step approach in program writing is that, while drawing a flowchart, a programmer can concentrate on the logic of the solution to the problem at hand, without paying attention to the syntax and other details of the programming language. Moreover, since a flowchart shows the flow of operations in pictorial form, a programmer can detect any error in the logic with greater ease than in the case of a program. Once the flowchart is ready, the programmer can concentrate on coding the operations in each box of the flowchart as statements of the programming language. Normally, this two-step approach ensures an error-free program. Experienced programmers, sometimes, write programs without drawing a flowchart. However, beginners should first draw a flowchart to reduce the number of errors and omissions in a program. Moreover, it is a good practice to have a flowchart along with a computer program because the flowchart often serves as a document for the computer program. It is useful during the testing of the program, and while incorporating modifications in the program.
Flowchart Symbols Need for Flowchart Symbols A flowchart uses boxes of different shapes to denote different types of instructions. The use of standard symbols makes it easier to communicate program logic through flowcharts because it enables anyone using the flowchart to interpret the logic easily. For example, a diamond always means a decision. Hence, when a programmer looks at a flowchart, he/she can identify the decision points easily by looking for diamond-shaped boxes.
Basic Flowchart Symbols Flowcharts need only a few basic symbols to indicate necessary operations in algorithms. Figure 8.1 shows the basic flowchart symbols standardized by the American National Standards Institute (ANSI). Their functions are described here.
Chapter 8 ■ Planning the Computer Program
284
Input/Output
Terminal
Decision
Processing
Flow lines
Connectors
Figure 8.1. Basic flowchart symbols.
1.
Terminal. Terminal symbol indicates the beginning (Start), the end (Stop), and pauses (Halt) in a program’s logic flow. Normally, we use a pause in program logic under some error condition, or for doing some manual setting during processing of program such as changing forms in a computer’s printer.
2.
Input/Output. In a flowchart, input/output symbols indicate program instructions to input/output data from any type of input/output device (such as keyboard, mouse, scanner, monitor, printer, plotter, etc.). They also indicate instructions to input/output data from/to a storage device (such as disk, tape, etc.).
3.
Processing. A processing symbol represents arithmetic and data movement instructions. Hence, in a flowchart, processing symbols indicate arithmetic processes of adding, subtracting, multiplying, and dividing, as well as logical processes of moving data from one memory location to another (assignment statements). A processing symbol with more than one arithmetic and data movement instructions indicates consecutive execution of these instructions in the order of their placement.
4.
Decision. A decision symbol indicates a decision point, which is a point at which a branch to one of two or more alternative paths is possible. Figure 8.2 shows three different ways of using a decision symbol. The criterion for making a decision is in the corresponding decision box. A decision symbol also accounts for all possible exit paths depending on the result of decision. For each exit path, it has a label indicating the condition upon which the program logic takes that path for further execution.
AB
A=B
Yes (a) A two-way branch decision
(b) A three-way branch decision
I=? =0
=1
=2
=3
=4
=5
= Other
(c) A multiple-way branch decision Figure 8.2. Examples of decision symbols.
5.
Flow lines. Flow lines with arrowheads indicate flow of operation, which is the sequence of execution of instructions. Normal flow of a flowchart is from top-to-bottom and left-to-right. Although, a flowchart
■ Flowcharts
285
requires arrowheads only when normal flow of operation changes, but as a good practice and to avoid ambiguity, we draw flow lines usually with an arrowhead at the point of entry to all flowchart symbols. Good practice also dictates that we should avoid crossing of flow lines as far as practicable. 6.
Connectors. When a flowchart becomes so complex that the number of flow lines and their direction are confusing, or when the flowchart spreads over more than one page, it is useful to utilize connector symbols as a substitute for flow lines. This symbol represents an entry from, or an exit to another part of the flowchart. A connector symbol is a circle with a letter or digit placed in it to indicate the link. A pair of identically labeled connector symbols indicates a continued flow when the use of a line is confusing. Hence, two connectors with identical labels serve the same function as a long flow line. How is it possible to determine if a connector symbol is an entry or exit point? It is simple – if an arrow enters but does not leave a connector, it is an exit point, and program control transfers to the identically labeled connector having an outlet. Connectors help in simplifying the task of drawing large flowcharts that spread over several pages.
Additional Flowchart Symbols The basic flowchart symbols are sufficient to represent any program logic, but they do not specify the type of storage media, type of I/O device, or type of processing. Additional flowchart symbols enable programmers to incorporate such details in their flowcharts. Figure 8.3 shows some commonly used ones. Their functions are described here. 1.
Printed document. This symbol shows that either the input to the program is in the form of printed document or its output is in hard copy printed form.
2.
Display. This symbol shows that the program displays the output on a monitor.
3.
Keyed input. This symbol shows that the program inputs data using a keyboard device.
4.
Manual input. This symbol shows that the program inputs data using an input device, which requires manual operation, such as voice input system, touch screen kiosk, etc.
5.
Auxiliary operation. This symbol represents an auxiliary operation performed by a machine that is not directly under the control of the main computer system. The program must perform auxiliary operation for completion of the job.
6.
Manual operation. This symbol represents an offline activity performed by a person. The program must perform the offline activity for completion of the job.
7.
Online storage. This symbol represents any data stored on an online storage device, such as a disk.
8.
Offline storage. This symbol represents any data stored on an offline storage device, such as a tape.
9.
Magnetic tape. This symbol represents the use of magnetic tape for storage of some data.
10. Magnetic disk. This symbol represents the use of magnetic disk for storage of some data. 11. Communication link. This symbol represents data transmission from one location to another across a
network. 12. Annotation. This symbol provides explanatory notes to some parts of the flowchart to make it easier to
follow and understand. An explanatory note is within a three-sided annotation enclosure and a dotted line connects the annotation enclosure to that part of the flowchart to which the annotation corresponds. 13. Predefined process. This symbol represents a group of operations that form a separate module detailed in a
separate flowchart. 14. Merge. This symbol represents a process that combines two or more sets of items into one set. 15. Extract. This symbol represents a process that separates one or more specific sets of items from a given set of
items.
286
Chapter 8 ■ Planning the Computer Program
Printed document
Auxiliary operation
Magnetic tape
Predefined process
Display
Manual operation
Magnetic disk
Merge
Keyed input
Manual input
Online storage
Offline storage
Communication links
Extract
Sort
Annotation
Collate
Figure 8.3. Additional flowchart symbols.
16. Sort. This symbol represents a process that arranges a set of items in a specified sequence
(ascending/descending). 17. Collate. This symbol represents a process that performs merge and extract operations. The process first
combines two or more sets of items into one set and then separates one or more specific sets of items from the generated set.
Sample Flowcharts We introduce here the art of flowcharting (describing an algorithm in the form of a flowchart) with the help of some simple examples. Example 8.3. A student appears for an examination consisting of total 10 subjects with each subject having maximum marks of 100. The student’s roll number, name, and marks obtained by him/her in various subjects are input data. Such a collection of related data items, treated as a unit, is known as a record. Draw a flowchart for the algorithm to calculate the percentage marks obtained by the student, and to print it with his/her roll number and name.
Solution: Figure 8.4 shows the flowchart of the algorithm for this problem. The first symbol, labeled “Start”, is a terminal symbol indicating the beginning of flowchart logic. It does not mean that someone has to switch on the computer or press a start button. The second symbol is an I/O symbol labeled “Read input data”. This step inputs the roll number, name, and marks obtained by the student from an input device into the main storage of the computer. The third symbol is a processing symbol. Its label indicates that at this step the computer adds the marks obtained by the student in various subjects, and stores the sum in a memory location named Total.
■ Flowcharts
The fourth symbol is again a processing symbol. Its label indicates that at this stage, the program calculates percentage marks by dividing Total by 10, and stores the result in a memory location named Percentage. The fifth symbol is an I/O symbol labeled “Write output data”. This step outputs the roll number, name, and percentage marks obtained on an output device, such as a printer. Note that details such as roll number, name, marks obtained, or percentage being input or output are not part of the logical steps of data input or output operations. Such details are part of the system design document, and are included in the computer program as input and output descriptions. The sixth symbol, labeled “Stop”, is a terminal symbol indicating the end of flowchart logic. Directed flow-lines connect various symbols in the flowchart, indicating the sequence of instructions execution. The logic depicted in Figure 8.4, therefore, will read the student’s record, calculate the percentage marks obtained by him/her, print one line of output, and then stop. Certainly, one would not like to use a computer to solve a trivial problem like this. However, if several students appear for an examination and if we have to compute the percentage marks obtained for each student, we may like to take the help of a computer. The next example illustrates how to do this.
287
Start Read input data Add marks of all subjects giving Total Percentage = Total / 10 Write output data Stop Figure 8.4. Flowchart for Example 8.3.
Example 8.4. Fifty students of a class appear for the examination of Example 8.3. Draw a flowchart for the algorithm to calculate and print the percentage marks obtained by each student along with his/her roll number and name. Solution: Since all students appeared for the same examination, the process of calculating and printing percentage marks obtained by each student will be the same. Hence, an easy solution that comes to our mind for this problem is to repeat the intermediate four symbols of Figure 8.4 fifty times. However, if we do that, 202 (50 x 4 + 2) flowchart symbols will have to be drawn. Obviously, this is a time consuming and tedious job and hence, is not desirable. We will now see how to solve this problem in a simpler way. If the program has to repeat the same logical steps more than once, we use flow line symbols in a flowchart to form a process loop (see Figure 8.5). The flow line pointing upward forms a process loop indicating that after “Write” operation, control flows back to “Read” operation. Hence, the process loop of Figure 8.5 solves the problem of an exceedingly long flowchart by reusing the same logical steps repeatedly. However, the flowchart of Figure 8.5 is incomplete because the process loop does not have a logical ending. It will continue to attempt to repeat the four steps until someone cancels the job manually. This is an example of an infinite loop. Hence, the flowchart of Figure 8.5 does not represent an algorithm because an algorithm must terminate. Therefore, we have to find out a way to terminate the algorithm. We do this by using a decision symbol (see the flowchart of Figure 8.6).
Start Read input data Add marks of all subjects giving Total Percentage = Total / 10
Write output data Figure 8.5. Flowchart for the solution of Example 8.4 with an infinite (endless) process loop.
288
Chapter 8 ■ Planning the Computer Program
The flowchart of Figure 8.6 uses another variable Count. The program logic initializes it to zero outside the process loop and increments it by one, after processing the data for each student. Hence, the value of Count is always equal to the number of students whose data has been processed. The decision step compares the value of Count with 50, the total number of students. The program logic repeats the steps within the process loop until the value of Count becomes 50. When Count becomes 50, the instruction at the decision step causes the control to flow out of the loop, and processing stops because the program logic encounters a terminal symbol labeled Stop. Hence, the flowchart of Figure 8.6 is a complete and correct solution to the problem of Example 8.4. However, the flowchart of Figure 8.6 suffers from two major drawbacks. The first drawback is that for making the decision at the decision step, one must know the exact number of students who appeared for the examination. If the total number of students appearing for the examination of Example 8.4 is too large (say more than ten thousand), counting the total number of input records (data for each student) becomes a tedious job, and the logic will not work if there is a counting error. If the supplied number is less than the number of input records, the computer will not process the data for last few students. On the other hand, if it is more than the number of input records, the computer will try to read more records than supplied, causing an error in the logic. The second drawback is that the flowchart of Figure 8.6 is not a generalized solution to the given problem. If the examination is conducted every year, we will like to use the same program every year. However, the number of students appearing in the examination may not remain the same every year. This year it may be 50, but next year it can be 55, or 60, or anything. Hence, if the computer program to solve this problem is based on the flowchart of Figure 8.6, the statement corresponding to the decision step in that program will have to be changed every time to supply the exact number of students. This is not a good programming practice. A good algorithm (program) should be general in nature. For example, in this case, we should write a program that need not be modified every time even if the total number of students changes. The flowchart of Figure 8.6 suffers from these drawbacks because it uses the method of counting for controlling the process loop. We can overcome these drawbacks by using another method to control the process loop. In this method, a trailer record marks the end of input data. That is, a special record follows the last data record in input, whose sole purpose is to indicate the end of input data. For example, let us assume that the first seven characters of an input record represent a student’s roll number (Rollno). Since 0000000 is never a valid roll number, we can safely use a value of 0000000 as the first seven characters to represent the trailer record. As the program logic processes each input record, it compares the record’s Rollno with 0000000 to determine if processing is complete. Figure 8.7 shows the flowchart for the logic of this process. This logic requires a programmer to include instructions in the program to compare the Rollno of each record with 0000000 for determining whether to continue or stop. The concept of a trailer record uses the idea of selecting a field (a particular item of data) in input record and then selecting a trailer value, also known as sentinel value that never occurs as normal data value for that field. Sentinel value of 0000000 for Rollno field is a good example. When program logic uses a trailer record to mark the end of input data, the decision box for checking the trailer value is usually flowcharted immediately after the input symbol.
■ Flowcharts
289
Start Start Count = 0 Read input data
Read input data
Is Rollno = 0000000?
Add marks of all subjects giving Total
Percentage = Total / 10
No Add marks of all subjects giving Total
Write output data
Percentage = Total / 10
Add 1 to Count No
Is Count = 50? Yes Stop
Yes
Stop
Write output data
Figure 8.7.
Generalized flowchart for the solution of Example 8.4 using the concept of trailer record. Here the process loop is terminated by detecting a special nondata record.
Figure 8.6. Flowchart for the solution of Example 8.4.
Example 8.5. For the examination of Example 8.3, we want to make a list of only those students who passed the examination (obtained 30% or more marks). In the end, we also want to print the total number of students who passed. A trailer record having sentinel value of 9999999 for Rollno terminates input data of all students. Draw a flowchart for an algorithm to process this requirement. Solution: The flowchart of Figure 8.8 is an algorithm for this problem. It has two decision symbols. The first symbol checks for a trailer record by comparing Rollno with sentinel value of 9999999 to determine whether processing is complete. The second symbol checks whether the student has passed or failed by comparing percentage marks obtained by him/her with 30. If student’s Percentage is equal to or more than 30, he/she has passed, otherwise failed. Note from the flowchart that the program logic performs the operation “Write output data” if the student has passed. If he/she has failed, the program logic performs the operation “Read input data” directly without performing the “Write” operation. Hence, output list will contain details of only those students who passed the examination. Another notable point in this flowchart is the use of Count variable. The program logic initializes it to zero in the beginning and increments it by one every time it performs the operation “Write output data”. However, the program
290
Chapter 8 ■ Planning the Computer Program
logic performs the operation “Write output data” only for those students who passed. Thus, the program logic increments Count by one only in case of those students who passed. Hence, the value of Count is always equal to the number of students who passed, out of those whose data has been processed. Eventually, when program logic detects the trailer record, the operation “Write Count” prints the final value of Count, which is equal to the total number of students who passed the examination. Start Count = 0 Read input data
Is Rollno = 9999999?
Yes Write Count
No Add marks of all subjects giving Total
Stop
Percentage = Total / 10
Is Percentage >= 30? Yes
No
Write output data Add 1 to Count
Figure 8.8. Flowchart for the solution of Example 8.5.
Example 8.6. Input data of each student for the examination of Example 8.3 also contains information regarding his/her sex in a field named Sexcode having value M (for male) or F (for female). We want to make a list of only those female students who passed in second division (obtained 45% or more but less than 60% marks). In the end, we also want to print the total number of such students. A trailer record having a sentinel value of Z for Sexcode terminates input data of all students. Draw a flowchart for an algorithm to process this requirement. Solution: The flowchart of Figure 8.9 is an algorithm for this problem. It has four decision symbols. The first symbol checks for a trailer record by comparing Sexcode with sentinel value Z to determine if processing is complete. The second
■ Flowcharts
291
symbol checks whether the student is female by comparing his/her Sexcode with F. If Sexcode is not F, program logic does not process the data of that student and returns the control to the operation of reading input data. This step ensures that program logic takes the data of only female students for further processing.
Start Count = 0 Read input data
Is Sexcode = Z? No No
Is Sexcode = F? Yes Add marks of all subjects giving Total
Yes Write Count Stop
Percentage = Total / 10
No
No
Is Percentage >= 45? Yes Is Percentage < 60? Yes Write output data Add 1 to Count
Figure 8.9. Flowchart for the solution of Example 8.6.
The last two decision symbols check whether the student passed in second division. The first one ensures that she scored 45% or more, and the second one ensures that she scored less than 60%. If she scored less than 45%, she has not passed in second division. Hence, without making any further check, program logic returns the control to the operation of reading input data. If she scored 45% or more, program logic makes another check in the fourth decision symbol to ensure that her percentage is less than 60%. If this condition is false, the student scored 60% or more. Hence, she passed in first division (not second division). Therefore, once again, program logic returns the control to the operation of reading input data without writing any output data. If the condition in the fourth decision symbol is true, the candidate passes all checks for being a female student who passed in second division. Hence, only in this case, program logic performs the operation “Write output data”, and increments Count by one.
292
Chapter 8 ■ Planning the Computer Program
Start Count = 0 Read input data
1
Is Sexcode = Z?
Yes
2
No 1
No
Is Sexcode = F? Yes Add marks of all subjects giving Total Percentage = Total / 10
1
No
Is Percentage >= 45? Yes
1
No
2
Write Count
Stop
Is Percentage < 60? Yes Write output data Add 1 to Count 1
Figure 8.10. Flowchart of Figure 8.9 redrawn to illustrate the use of connectors.
Readers should go through the logic of this flowchart repeatedly until they are convinced that output list will contain details of only those female students who passed in second division. Readers should also get convinced that finally, when program logic encounters the trailer record and the operation “Write Count” prints the value of Count, the printed value is equal to the total number of female students passed in second division. This flowchart is an example of a multiple-decision chart. Figure 8.10 shows the flowchart of Figure 8.9 with the use of connectors. There are four exit connectors having label 1, all of which indicate flow of control to the flowchart symbol having an entry connector labeled 1. This symbol is the input symbol in the flowchart. Similarly, the exit connector having label 2 indicates flow of control to the entry
■ Flowcharts
293
connector labeled 2. Readers should compare the flowcharts of Figure 8.9 and Figure 8.10, and be convinced that both of them represent the same logic. A flowchart may seem simple to prepare, but we need much practice to think through a problem in discrete, logical steps, to assume nothing, and to forget nothing. Moreover, different people often solve a problem in different ways, and therefore, different flowcharts are possible for a problem. Also, note that a completed flowchart is not a complete computer program. It is only an aid to programming. For a problem, it defines the procedure and the logic for solving the problem.
Levels of Flowcharts There are no set standards for the amount of detail required in a flowchart. A flowchart that outlines the main segments of a program (shows only broad details) is a macro flowchart, and a flowchart with more details is a micro flowchart (also called detailed flowchart).
Part of a macro flowchart
Add marks of all subjects giving Total
1 I=1 Total = 0
A micro flowchart
Total = Total + Marks (I) I=I+1 No
Is I > 10? Yes 1
Figure 8.11. Detailed flowchart of add marks of all subjects giving Total.
For example, let us consider the processing box having the instruction “Add marks of all subjects giving Total” in the flowcharts of our examination problem. Figure 8.11 shows a detailed flowchart for the operation in this processing box. The detailed flowchart shows how program logic computes the value of Total. Similarly, we can also convert I/O boxes for “Read” and “Write” operations into detailed flowcharts.
Flowcharting Rules While programmers have a good deal of freedom in creating flowcharts, the American National Standards Institute (ANSI) has recommended a number of general rules and guidelines to help standardize flowcharting process. Various computer manufacturers and data processing departments usually have similar flowcharting standards. The basic flowcharting rules and guidelines are as follows: 1.
First chart the main line of logic, and then incorporate details.
Chapter 8 ■ Planning the Computer Program
294 2.
Maintain a consistent level of details in a flowchart.
3.
Do not chart every detail in a flowchart; otherwise, the flowchart will only be a graphic representation of all the steps of the corresponding program. A reader interested in details can refer to the program itself.
4.
Use easy to understand statements within flowchart symbols. Use descriptive titles written in your own language, rather than machine-oriented language.
5.
Be consistent in using names and variables in a flowchart.
6.
Go from left to right and top to bottom in constructing a flowchart.
7.
Keep a flowchart as simple as possible, and avoid crossing of flow lines.
8.
If you need a new page while drawing a flowchart, break the flowchart at an input or output point. Moreover, use labeled connectors to link portions of the flowchart on different pages.
Advantages and Limitations of Flowcharts Advantages Use of flowcharts for program planning provides the following benefits: 1.
Better communication. A flowchart is a pictorial representation of a program. Therefore, it is easier for a programmer to explain the logic of a program to others through a flowchart, rather than the program itself.
2.
Effective analysis. A macro flowchart, which charts the main line of logic of a software system, becomes the system’s model that can be broken down into detailed parts for study and further analysis of the system.
3.
Effective synthesis. Normally, several programmers are associated with the design of a large software system, with each programmer being responsible for only a part of the entire system. Hence, if each programmer draws a flowchart for his/her part of design, the flowcharts of all programmers put together can help visualize the overall system design. At this stage, we can detect any problem in linking various parts of the system and modify the design accordingly. Thus, we often use flowcharts as working models in the design of new programs and software systems.
4.
Proper program documentation. Program documentation involves collecting, organizing, storing, and maintaining a complete historical record of programs, and other documents associated with a system. Good documentation is needed because: (a) Documented knowledge belongs to an organization, and does not disappear with the departure (resignation/retirement) of a programmer. (b) If an organization decides to postpone a project, documented work helps in restarting the project later from the stopped stage. (c) When a programmer wants to modify an existing program, documented work provides him/her a more understandable record of originally done work. Flowcharts, therefore, provide valuable documentation support.
5.
Efficient coding. Once a flowchart is ready, programmers find it easy to write the corresponding program because the flowchart serves as a roadmap for them. It guides them to go from the starting point of the program to the final point, without omitting any step. The result is an error-free program developed at a faster pace.
■ Decision Tables
295
6.
Systematic debugging. A flowchart is helpful in detecting, locating, and removing mistakes (bugs) in a program in a systematic manner because programmers find it easier to follow the logic of the program in flowchart form. The process of removing errors (bugs) in a program is known as debugging.
7.
Systematic testing. Testing is the process of confirming whether a program does all its intended jobs under specified constraints. For testing a program, the testing team executes the program with different sets of data as input to test the different paths in program logic. For example, to test the complete logic of the program for Example 8.6, the following sets of data are necessary: (a) Data for a male student. (b) Data for a female student, who scored less than 45%. (c) Data for a female student, who scored exactly 45%. (d) Data for a female student, who scored more than 45% but less than 60%. (e) Data for a female student, who scored exactly 60%. (f) Data for a female student, who scored more than 60%. (g) The trailer data having sentinel value.
A flowchart is helpful in designing test data for systematic testing of programs.
Limitations In-spite-of their many advantages, flowcharts have the following limitations: 1.
Flowcharts are time consuming and laborious to draw with proper symbols and spacing, especially for large complex programs. In this chapter, you have seen examples of small flowcharts developed for relatively small programs. You can imagine how difficult it would be to develop a detailed flowchart for a program containing over 50,000 statements.
2.
Owing to the symbol-string nature of flowcharting, any change or modification in program logic usually requires a completely new flowchart. Redrawing a flowchart being a tedious task, many programmers do not redraw or modify the corresponding flowcharts when they modify their programs. This leaves a program and its flowchart in an inconsistent state. That is, the logic used in the program and that shown in its flowchart do not match. This defeats the purpose of use of flowcharts as documentation support for programs. To take care of this problem, many companies use software tools that generate flowcharts automatically from program code. These software tools read a program’s instructions and draw an equivalent flowchart of its logic. This backward approach draws flowcharts from program codes mainly for documentation purpose.
3.
There are no standards determining the amount of detail that should be included in a flowchart.
Because of such limitations, many organizations use alternative tools for representing the algorithms (logic) of their programs, two of which are described here.
DECISION TABLES What is a Decision Table? Decision tables define the word statement of a problem in a tabular form. They define complex program logic by charting what decisions or actions a computer is to take because of input data. Decision tables are preferred to flowcharts for charting the logic of those programs which have a large number of branches.
Chapter 8 ■ Planning the Computer Program
296
Most decision tables have six parts. Figure 8.12 shows the basic format of a decision table. Its first part, called “Table Heading”, contains the name and/or number of the decision table. Some larger applications may use two or more decision tables in the analysis part of program development. The second part, called “Condition Stub”, describes the conditions that could exist in program logic. Thus, the contents of condition stub correspond to the conditions contained in the decision symbols of a flowchart. The third part, called “Action stub”, contains the action statements. These statements correspond to the statements located in non-decision symbols of a flowchart. While the condition statements reveal possible states of input data, the action statements describe possible actions of a computer system. The right hand side of the decision table contains the rule numbers (part 4), the actual conditions (part 5), and the actions taken by a computer (part 6). The condition entries correspond to the paths leading out from decision symbols. Table heading
Decision rules
Condition stub
Condition entries
Action stub
Action entries
Figure 8.12. Format of a decision table.
We follow the following steps for constructing a decision table: 1.
Define the problem that we have to solve.
2.
List all conditions that we need to test in the problem.
3.
List actions that we need to take corresponding to each combination of conditions.
4.
Form a decision table using the two lists.
Sample Decision Table Figure 8.13 shows a decision table for the problem of Example 8.6, whose flowchart is in Figure 8.9. In this table, each rule number is a given condition followed by a specific action by a computer. The six rule numbers, six actual conditions, and associated actions by the computer are described here. Rule 1: The student is a female, and percentage marks obtained is 45% or more, and percentage marks obtained is less than 60%. The computer should write output data, add 1 to Count, and then read the next student’s record. It is a case of a female student, who passed in second division. Rule 2: The student is a female, and percentage marks obtained is 45% or more, and percentage marks obtained is not less than 60%. The computer should read the next student’s record without performing any other operation. It is a case of a female student, who scored 60% or more. Rule 3: The student is a female, and percentage marks obtained is not 45% or more. A dash (-) in this column against the last condition indicates that this condition is irrelevant for arriving at the action. In fact, if Percentage is not equal to or greater than 45, it has to be less than 60. Percentage greater than or equal to 60 is logically impossible. Such conditions in a decision table, which are irrelevant in arriving at an action, are known as don’t care conditions and are denoted by a dash. For this rule, the computer should read the next student’s record without performing any other operation. It is a case of a female student, who scored less than 45%.
■ Decision Tables Examination problem of Example 8.6 1
297
Decision rule number 2 3 4 5 6
Condition statements Sexcode = Z
N
N
N
N
N
Sexcode = F
Y
Y
Y
Y
N
Y -
Percentage = > 45
Y
Y
N
-
-
-
Percentage < 60
Y
N
-
-
-
-
Action taken Calculate Percentage
X
Write output data
X
Add 1 to Count
X
Read next student’s record
X
X
X
X
Write Count
X
Stop
X Figure 8.13. Decision table for the examination problem of Example 8.6.
Rule 4: The student is a female, and other conditions are don’t care. The computer should calculate Percentage after adding marks of all subjects and then proceed to test further conditions. It is a case of a female student whose Percentage is yet to be calculated. Rule 5: The student is a male, and other conditions are don’t care. The computer should read the next student’s record without performing any other operation. It is a case of a male student. Rule 6: In all previous five rules, it was ascertained that the current record is not a trailer record because all these rules have a value N (No) for the condition Sexcode = Z. In this rule, Sexcode is equal to Z, which indicates a trailer record. Hence, the computer should write the value of Count and then “Stop”. Other conditions in this rule are don’t care. It is a case of a trailer record. In a decision table, “Y” means yes, “N” means no, “-” means don’t care, and “X” means the computer should take this action.
Advantages and Limitations of Decision Tables Advantages Normally, we use decision tables in place of flowcharts because: 1.
They provide concise description of logically complex situations.
2.
They are easier to draw and change than flowcharts.
3.
They provide documentation that is more compact. A small table can replace several pages of flowcharts.
4.
It is also easier to follow a particular path down one column than through several flowchart pages.
Chapter 8 ■ Planning the Computer Program
298
Limitations Decision tables, however, are not so widely used as flowcharts because: 1.
Large decision tables can become incomprehensible and difficult to modify.
2.
Flowcharts are better able to express the total sequence of events necessary for solving a problem.
3.
Many programmers and beginners are more familiar with and prefer flowcharts.
PSEUDOCODE What is Pseudocode? Pseudocode is another program-planning tool used for planning program logic. “Pseudo” means imitation or false and “Code” refers to the instructions written in a programming language. Pseudocode, therefore, is an imitation of actual computer instructions. These pseudo-instructions are phrases written in a natural language (e.g., English, French, German, etc.) that a computer cannot understand. However, pseudocode uses a structure that resembles computer instructions. When a programmer uses pseudocode for planning a program, he/she can concentrate solely on developing the logic of the program without worrying about the syntax for writing program instructions because pseudocode does not have any syntax rules for formulating instructions. Once the programmer is convinced that the program logic is sound, he/she can convert the pseudocode easily into a suitable programming language. Since the emphasis of pseudocode is on program design, it is also known as Program Design Language (PDL).
Pseudocodes for Basic Logic (Control) Structures During early days of program development, many programmers developed program logics for large programs with mazes of branches (jumps from one portion of the program to another) that altered the sequence of processing operations. Such programs are now referred to as “spaghetti code” because their program flowcharts appeared more like a plate of spaghetti than like logical analyses of programming problems. Understanding the logic of such programs was difficult for someone other than the developer, and even for the developer after a lapse of few months. Hence, these programs were difficult to modify for incorporating suggested enhancements, and became nightmares for those responsible for their maintenance. Researchers later proved that it is possible to express any program logic, no matter how complex, by using only the following three simple logic (control) structures: 1.
Sequence logic
2.
Selection logic
3.
Iteration (or looping) logic
They also proved that programs structured by using only these three logic structures are easier to understand, because we can read them from top to bottom. That is, by conceptualizing the logic of a program in the form of these three logic structures, programmers can avoid writing spaghetti code, and produce programs that are easy to understand and maintain. This also helps in reducing program errors and the time spent in program testing. Writing of programs by using these three basic control structures soon became widely accepted as a more scientific approach to solving a programming problem, and was termed structured programming technique. As good programming practices, it recommended that programmers should develop program logics and write programs using the three basic logic structures. Therefore, we present here the pseudocodes for the three basic logic structures (along with their flowcharts for those who are more comfortable with flowcharts).
■ Pseudocode
299
Sequence Logic Sequence logic performs instructions one after another in sequence. Thus, for writing pseudocode instructions for sequence logic, we write the instructions in the order (or sequence) of their execution. Logic flow of psuedocode is from top to bottom. Figure 8.14 shows an example of sequence logic structure.
Process 1 Process 1 Process 2 (a) Flowchart
Process 2 (b) Pseudocode
Figure 8.14. Flowchart and pseudocode for sequence structure .
Selection Logic Selection logic, also known as decision logic, makes decisions. It selects a path out of two or more paths in program logic. We depict selection logic as an IF…THEN…ELSE, or an IF…THEN, or a CASE structure. The flowcharts of Figures 8.15, 8.16, and 8.17, respectively illustrate the logic of these structures along with their corresponding pseudocodes.
IF (condition)
Yes
THEN
No
IF Condition THEN Process 1
ELSE
Process 1
Process 2
ELSE Process 2 ENDIF (b) Pseudocode
(a) Flowchart
Figure 8.15. Flowchart and pseudocode for IF…THEN…ELSE selection structure.
Yes
IF (condition)
THEN Process 1
(a) Flowchart
No IF Condition THEN Process 1 ENDIF
(b) Pseudocode
Figure 8.16. Flowchart and pseudocode for IF…THEN selection structure.
300
Chapter 8 ■ Planning the Computer Program
Type 1
Yes
Process 1
No Type 2
Yes
Process 2
No
Type n
Yes
Process n
CASE Type Case Type 1:
Process 1
Case Type 2:
Process 2
Case Type n:
Process n
ENDCASE
No (a) Flowchart
(b) Pseudocode
Figure 8.17. Flowchart and pseudocode for CASE selection structure.
IF…THEN…ELSE structure means that if the condition is true, then do Process 1, else (if the condition is not true) do Process 2. Hence, program logic executes either Process 1 or Process 2 depending on whether the specified condition is true or false. However, if we do not want to choose between two processes, and simply want to decide whether to execute a process or not, then we use IF…THEN structure. IF…THEN structure means that if the condition is true, then do Process 1, otherwise (if the condition is not true) skip over Process 1. CASE structure represents multiple-way selection logic. It allows choosing from more than two control paths in a program logic enabling selection of one of any number of statements or statement groups. The CASE statement of Figure 8.17 indicates that if the value of Type is equal to Type-1, execute Process 1, if it is equal to Type-2, execute Process 2, if it is equal to Type-3, execute Process 3, and so on. In all these structures, Process 1, Process 2, Process 3, etc. can be one or more processes. They are not limited to a single process. In case of IF…THEN and IF…THEN…ELSE decision structures, ENDIF indicates their end; and in case of CASE structure, ENDCASE indicates its end.
Iteration (or Looping) Logic Iteration logic produces loops in program logic whenever there is a need to execute one or more instructions several times depending on some condition. It uses two structures called DO…WHILE and REPEAT…UNTIL. Figures 8.18 and 8.19 show the flowcharts and pseudocodes for DO…WHILE and REPEAT…UNTIL structures respectively. As both DO…WHILE and REPEAT…UNTIL are used for looping, it is important to know the difference between the two structures. In DO…WHILE, looping continues as long as the condition is true, and stops when the condition is not true. Since program logic tests the condition for looping at the beginning of the loop, if the condition is false when it first encounters the loop, it will not execute the statements in the loop at all (not even once). On the other hand, in REPEAT…UNTIL, looping continues until the condition becomes true. That is, program logic repeats execution of statements within the loop as long as the condition is not true. Since it tests the condition for looping at the end of the loop, it will execute the statements in the loop at least once. In both DO…WHILE and REPEAT…UNTIL, the loop must contain a statement, which changes the condition that controls the loop. If it does not, looping will continue without end, becoming an infinite loop. Remember that, no program should contain an infinite loop. ENDDO marks the end of a DO…WHILE structure, and UNTIL followed by some condition marks the end of a REPEAT...UNTIL structure.
■ Pseudocode
301
False
Condition?
DO WHILE Condition
True
Process 1
Process 1 Process n ENDDO Process n
(a) Flowchart
(b) Pseudocode
Figure 8.18. Flowchart and pseudocode for DO…WHILE iteration structure.
Process 1
Process n False
REPEAT Process 1 Process n
Condition?
UNTIL Condition
True (a) Flowchart
(b) Pseudocode
Figure 8.19. Flowchart and pseudocode for REPEAT…UNTIL iteration structure.
Sample Pseudocode Figure 8.20 shows the pseudocode version of the algorithm for the problem of Example 8.6, whose flowchart is in Figure 8.9. In the pseudocode, the first line initializes the value of Count to zero and the second line reads input data of the first student. The third line is the beginning of a loop using DO…WHILE structure. It indicates that the loop continues as long as Sexcode field is not equal to Z, that is, as long as program logic does not find the trailer record. In this example, the loop includes a series of decisions followed by an instruction to read next student’s record. The first statement in DO…WHILE loop checks if Sexcode is equal to F. If it is, the next statement calculates Percentage and again the third statement within the loop checks if Percentage is equal to or greater than 45. If it is, the next statement checks if Percentage is less than 60. This is a series of three IF…THEN decision structures. Each one ends with an ENDIF vertically aligned below the corresponding IF. Program logic performs the two instructions – “Write output data” and “Add 1 to Count” only if all the three conditions (that of Sexcode being F, Percentage being equal to or more than 45, and Percentage being less than 60) are true (answered yes). If any of the three conditions is not true, the logic path goes to the statement of reading the next student’s record. After program logic processes the last student’s record, the next record that it takes for processing is the trailer record with value of Sexcode equal to Z. This causes the DO…WHILE loop to stop because
Chapter 8 ■ Planning the Computer Program
302
the condition (Sexcode not equal to Z) is no longer true. Hence, the next logical step is the instruction following ENDDO. At this stage, program logic prints the value of Count (Write Count), and finally program execution stops (Stop). Set Count to zero Read first student record DO WHILE Sexcode is not equal to Z IF Sexcode = F THEN Calculate Percentage IF Percentage > = 45 THEN IF Percentage < 60 THEN Write output data Add 1 to Count ENDIF ENDIF ENDIF Read next student record ENDDO Write Count Stop Figure 8.20. Pseudocode for the examination problem of Example 8.6.
Set Count to zero Read first student record DO WHILE Sexcode is not equal to Z IF Sexcode = F THEN Calculate Percentage IF Percentage > = 45 THEN IF Percentage < 60 THEN Write output data Add 1 to Count ENDIF ENDIF ENDIF Read next student record ENDDO Write Count Stop Figure 8.21. Non-indented version of the pseudocode of Figure 8.20.
Indentation One important feature of pseudocode as a program-planning tool is use of indentation. Indentation of each statement within DO…WHILE loop, shows that it is part of the loop. Similarly, indentation of statements within each IF…THEN structure specifies the statements that are part of each structure. Use of indentation in pseudocode is the same technique used in various programming languages. Its sole purpose is to represent the logical structure of a program with greater clarity. With this technique, we can easily tell which program statements are part of which logic structure of the total program logic. To appreciate this factor, readers should compare the equivalent non-indented pseudocode of Figure 8.21 with that of Figure 8.20. The difference in clarity would be far greater, if this were a longer pseudocode covering, for instance, one or more pages.
Advantages and Limitations of Pseudocode Advantages 1.
As compared to a flowchart, converting a pseudocode to a programming language is much easier.
2.
As compared to a flowchart, it is easier to modify the pseudocode of program logic when program modifications are necessary.
3.
Writing of pseudocode involves much less time and effort than drawing an equivalent flowchart. Moreover, as compared to writing a program in a programming language, pseudocode is easier to write because it has only a few rules to follow, allowing a programmer to concentrate on the logic of the program.
Limitations 1.
In case of pseudocode, a graphic representation of program logic is not available.
■ Data Structures
303
2.
There are no standard rules to follow in writing pseudocode. Different programmers use their own style of writing pseudocode. Hence, communication problem occurs due to lack of standardization.
3.
As compared to flowchart, beginners often find it more difficult to write pseudocode of program logic or to follow a program’s logic from its pseudocode.
DATA STRUCTURES Another important aspect of program planning is the choice of right data structures for implementing an algorithm. Data structures define ways of organizing and storing different data types used in an algorithm. Knowledge of data structures is important for good program planning because: 1.
Choice of proper data structures allows easy conversion of an algorithm (which is not yet in program form) into a program.
2.
Properly chosen data structures enable execution of a program with minimal resources (CPU time, memory space, disk space, etc.).
3.
Execution time of a program depends heavily on the choice of data structures.
4.
Often, quality of the result produced by a program also depends on the choice of data structures.
Programming languages often implement commonly used data structures as data types and the references and operations they provide. Since data structures are so crucial in program planning and design, most modern programming languages include them as standard libraries in their program development environments. Examples of such program development environments are Standard Template Library Containers of C++, Collections Framework of Java, and .NET Framework of Microsoft.
Types of Data Structures Data structures are of two types: 1.
Primitive types. Basic data types of a programming language are called primitive data types. Programmers compose other data types using these data types. Examples of primitive data types are character (used to store a character), integer (used to store a positive or negative integer whose allowable range is programming language dependent), and real (used to store a positive or negative real number whose allowable range is programming language dependent).
2.
Composite types. These data types use primitive data types and other already defined composite data types. A programming language often defines commonly used composite data types and operations on them. For example, C language defines pointer, array, and string data structures with operations on them. Moreover, most modern programming languages provide the flexibility to programmers to define their own composite data types known as user defined data types. Hence, a user defined data type is a composite data type whose composition is not part of the specification of the user’s programming language. A programmer declares them in a program whenever required. Some commonly used composite data structures are array, linked list, stack, queue, hash table, tree, binary tree, binary search tree, graph, and heap. Their general structure, operations on them, and their uses are described here.
Array An array is a group of data elements in which we identify an individual element by its position in the group, relative to the first element. Hence, we access an individual element of an array by indexing. An array is a homogeneous data
Chapter 8 ■ Planning the Computer Program
304
structure in the sense that all elements of an array are of the same data type. It is also linear in the sense that all elements of an array are stored in contiguous memory locations. Figure 8.22 shows a few examples of arrays in C language.
1010
92
1005
Y
1008
63
1004
A
1006
82
1004
1012 1008
10.25
1003
B
250.00
1002
1004
155.50
1001
M O
1002
66 84
1000
45
1000
82.75
1000
B
int
marks[6];
float
price[4];
char
city[6];
Each element being an int occupies 2 bytes
Each element being a float occupies 4 bytes
Each element being a char occupies 1 byte
marks[0] = 45 marks[1] = 84 . . . marks[5] = 92
price[0] = 82.75 price[1] = 155.50 . . . price[3] = 10.25
city[0] = ‘B’ city[1] = ‘O’ . . . city[5] = ‘Y’
(a) An array of integers having 6 elements
(b) An array of real numbers having 4 elements
(c) An array of characters having 6 elements
Figure 8.22. Illustrating the concept of arrays.
Types of Indexing We access an individual element of an array by specifying its position in the group of elements with a subscript or index. Depending on the programming language, one of the following three implementations of indexing is possible: 1.
Zero-based indexing. In this case, the first element of an array (also called the “origin” element) is always denoted by subscript value of 0 (zero), increasing monotonically up to one less than the declared size of the array (see Figure 8.22). Thus, A[0] is the first element, and A[5] is the fourth element of array “A”. C programming language popularized zero-based indexing.
2.
One-based indexing. In this case, the first element of an array is always denoted by subscript value of 1 (one). Thus, A[1] is the first element, and A[5] is the fifth element of array “A”. Programming languages meant for scientific applications, such as FORTRAN, use one-based indexing.
3.
n-based indexing. In this case, a programmer is free to choose the lower bound of an array (subscript value of first element), which may even be negative to suit the problem at hand.
■ Data Structures
305
Types of Arrays Based on the binding to subscript value ranges and the binding to storage, arrays are of the following four types: 1.
Static arrays. In a static array, the subscript ranges are statically bound and storage allocation is static (done before run time). The main advantage of static arrays is efficiency because they do not require any dynamic allocation or deallocation of storage.
2.
Semi-static arrays. In a semi-static array, the subscript ranges are statically bound, but storage allocation is done at declaration elaboration time during execution. The main advantage of semi-static arrays over static arrays is storage space efficiency. For example, a large array in one procedure can use the same storage space as a large array in another procedure, as long as the two procedures are never active at the same time.
3.
Semi-dynamic arrays. In a semi-dynamic array, the subscript ranges are dynamically bound and storage allocation is dynamic (done during run time). However, once the subscript ranges are bound and storage space is allocated, they remain fixed during the lifetime of the array. The main advantage of semi-dynamic arrays over static and semi-static arrays is flexibility. The size of an array need not be known until the array is about to be used.
4.
Dynamic arrays. In a dynamic array, the binding of subscript ranges and storage allocation is dynamic and can change any number of times during the array’s lifetime. The main advantage of dynamic arrays over other types is greater flexibility. A dynamic array can grow and shrink during program execution as the need for space changes.
Multi-dimensional Arrays In Figure 8.22, we saw examples of arrays in which each array element is indexed by a single integer. Such arrays are called one-dimensional arrays. Several numerical and graphics applications use multidimensional arrays in which each array element is indexed by an ordered list of same number of integers. For example, each array element is indexed by an ordered list of two integers (such as A[3, 5]) in a two-dimensional array, and by an ordered list of three integers (such as B[1, 3, 4]) in a three-dimensional array. Hence, an array in which each array element is indexed by an ordered list of k integers is called k-dimensional array. Figure 8.23 shows an example of a two-dimensional array. Normally, we represent matrices as two-dimensional arrays. As Figure 8.22 shows, memory allocation to a one-dimensional array and mapping its elements to the allocated memory space is straightforward. This is because the memory of a computer itself is a large one-dimensional array. However, for an array of higher-dimension, although the allocated memory space is equal to the total storage space required for storing all its elements, the mapping of its elements to the allocated storage space is not straightforward. The two commonly used methods of doing this mapping are: 1.
Row-major order. In which the elements are stored in the allocated memory space row-by-row – first row elements, then second row elements, then third row elements, and so on. Obviously, in this case, the elements are indexed by [row number, column number]. Figure 8.23(b) shows the mapping of the elements of array “A” of Figure 8.23(a) in row-major order.
2.
Column-major order. In which the elements are stored in the allocated memory space column-by-column – first column elements, then second column elements, then third column elements, and so on. Obviously, in this case, the elements are indexed by [column number, row number]. Figure 8.23(c) shows the mapping of the elements of array “A” of Figure 8.23(a) in column-major order.
306
Chapter 8 ■ Planning the Computer Program
Row 1 Row 2 Row 3
1016
4
A [3, 3]
1016
4
A [3, 3]
Col 1
Col 2
Col 3
1014
9
A [3, 2]
1014
6
A [3, 2]
5
7
3
1012
1
A [3, 1]
1012
3
A [3, 1]
1010
6
A [2, 3]
1010
9
A [2, 3]
1008
8
A [2, 2]
1008
8
A [2, 2]
1006
2
A [2, 1]
1006
7
A [2, 1]
1004
3
A [1, 3]
1004
1
A [1, 3]
1002
7
A [1, 2]
1002
2
A [1, 2]
1000
5
A [1, 1]
1000
5
A [1, 1]
2 1
8 9
6 4
(a) A two-dimensional array “A”
(b) Memory allocation and storage of elements of array “A” in row-major order: A[Row, Col]
(c) Memory allocation and storage of elements of array “A” in columnmajor order: A[Col, Row]
Figure 8.23. A two-dimensional array, its memory allocation, and storage of its elements in row-major and column-major orders. The example uses one-based indexing. In case of zero-based indexing, the rows and columns would have been numbered as 0, 1, and 2, and the array elements would have been indexed as A [0, 0], A [0,1], A [0, 2], A [1, 0], A [1, 1], A [1, 2], A [2, 0], A [2, 1], and A [2, 2].
Operation
Meaning
Declare
Declares a new array with details such as array type (static, dynamic, etc.), data type of its elements, its dimension, etc. For instance, int marks [6] declares an array of integers having six elements in C language.
Access (an element)
Enables accessing of an array element by specifying its index value. For instance, the second element of array “marks” is accessed by marks [1] in C language.
Allocate
Allocates memory space to a dynamic array.
Deallocate
Destroys a dynamic array by deallocating memory space allocated to it.
Initialize
Initializes an array at the time it is allocated memory space. For instance, int list [ ] = {5, 8, 6, 2}; declares list to be an array of four integer elements initialized to the values 5, 8, 6, and 2 in C language.
Array operators
Languages for scientific and engineering applications, like FORTRAN 90 and APL, support both unary and binary operators for operations on arrays. Examples of such operators include those for reversing the elements of a vector, reversing the columns of a matrix, reversing the rows of a matrix, obtaining the transpose of a matrix, inverting a matrix, obtaining the sum of two vectors or matrices, obtaining the difference of two vectors or matrices, obtaining the product/multiplication of two vectors or matrices, etc. Several languages have library functions for performing such array operations.
Figure 8.24. Some commonly supported operations for handling arrays.
Operations for Handling Arrays Figure 8.24 lists some commonly supported operations for handling arrays. Note that not all programming languages support all these operations and some programming languages may support additional operations.
■ Data Structures
307
Uses of Arrays A A R R A Y Majority of computer programs need to model 5 collection of objects of same type, which they O F 2 need to process in the same way. Thus, the universal need for arrays is obvious. For 6 A R R A Y S example, computer programs often use arrays for representation of strings, vectors, and Figure 8.25. An array of arrays. Array “A” is an array of three elements. Each element of array matrices. Arrays can also have arrays as “A” is in turn an array of n elements, where elements. Computer programs use arrays of n is the value of the corresponding arrays for creation of ragged arrays (also element of array “A”. called jagged arrays), in which the valid m range of one index depends on the value of another. For example, Figure 8.25 shows an array of arrays in which different rows are of different sizes. Array of arrays are particularly of use for creating multi-dimensional arrays in those programming languages that support only one-dimensional arrays as primitives.
Linked List A linked list consists of a set of nodes linked together. Each node has two basic fields – data field and pointer field (see Figure 8.26). The data field contains the data, while the pointer field links the node to another node belonging to the same linked list. The data field itself may have multiple sub-fields when each node stores multiple data elements. Depending on the type of linked list, the pointer field may have only one pointer (single-linked lists) or two pointers (doubly-linked lists). In addition to the nodes, a linked list has a pointer called “head”, which points to the first node of the linked list. It enables us to keep track of the list. Some implementations of linked lists also use a “tail” pointer, which points to the last node of the linked list. This makes adding of elements to the tail of a list more efficient.
Data
Pointer
(a) The two basic fields of a node of a linked list
Last node’s pointer points to NULL Head
S
I
N
H
A
(b) A linked list having five nodes Figure 8.26. Linked list.
The main advantage of a linked list over a conventional array is that the order of the data elements in a linked list may be different from the order in which a computer stores the data elements in memory or on disk, allowing traversal of the data elements in a different order. Recall that the data elements of an array are stored in contiguous memory locations. Of course, this advantage of linked list comes at the cost of storing a pointer field with each data element, which requires additional storage space for the pointers. Linked lists also have the flexibility to grow and shrink dynamically because they permit insertion and removal of nodes at any point in the linked list. For a newly added node to a linked list, a computer can allocate the required memory space wherever it is available because a linked list does not require contiguous memory space for storage of its nodes. Some programming languages, such as LISP and Scheme, support linked list as a built in data structure with operations to access, insert, and remove data elements from linked lists. Other programming language, such as C, C++ and Java, rely on the use of primitive data types and language supported address referencing mechanism to create linked lists.
Chapter 8 ■ Planning the Computer Program
308
Types of Linked Lists Many different types of linked lists exist. Three of the commonly used types are: 1.
Singly-linked list. In its basic form, each node of a singly-linked list points to its successor node and the pointer of the last node is a NULL pointer [see Figure 8.27(a)]. The “head” pointer points to the first node, and we use it to keep track of the list. Inserting a new node at the head of a list is efficient because of the “head” pointer. However, for adding a node at the end of the list (after the last node), we need to locate the last node first, for which the entire list needs to be traversed, which is inefficient for lists with large number of nodes. To solve this problem, we use a “tail” pointer, which points to the last node of a list [see Figure 8.27(b)]. This simple enhancement allows efficient execution of both “prepend” and “append” operations.
Head S
I
N
H
A
(a) A singly-linked list in its basic form
Head
Tail I
S
N
H
A
(b) A singly-linked list with a tail pointer
Head I
S
N
H
A
(c) A circular list
Head S
I
N
H
A
(d) A doubly-linked list Figure 8.27. Commonly used types of linked lists.
2.
Singly-linked circular list. Each node of a singly-linked circular list (or simply circular list) points to its successor node and the pointer of the last node points to the first node (instead of being a NULL pointer). The “head” pointer also points to the first node. Figure 8.27(c) shows a circular list. Use of circular list instead of singly-linked list often simplifies an application code and makes the application run faster.
3.
Doubly-linked list. Each node of a doubly-linked list has two pointers – “left” and “right”. The left pointer of a node points to its left neighbour (if any) and the right pointer points to its right neighbour (if any). The left pointer of the leftmost node and the right pointer of the rightmost node are NULL pointers. The “head” pointer points to the first node. Figure 8.27(d) shows a doubly-linked list. Some applications are more convenient to program with a doubly-linked list than with a singly-linked list or a circular list.
■ Data Structures
309
Operations for Handling Linked Lists Figure 8.28 lists some commonly supported operations for handling linked lists. Note that all programming languages that support linked list do not necessarily support all these operations and some programming languages may support additional operations. Operation
Meaning
Create
Creates a new linked list. Creates an empty list if no elements are specified
Destroy
Destroys an existing linked list and frees the memory allocated to its various elements
Prepend
Add a new node at the head of the linked list (before the first node)
Append
Add a new node at the tail of the linked list (after the last node)
Insert (k)
Add a new node after the kth node of the linked list
Delete (k)
Delete the kth node of the linked list
Figure 8.28. Some commonly supported operations for handling linked lists.
Uses of Linked Lists We use linked lists primarily for list processing applications, in which we need to represent symbolic data in the form of lists (collection of non-contiguous memory cells linked together with pointers). Compiler development, pattern recognition, games simulation, and Artificial Intelligence (AI) applications need such data representation and their handling capability. We also use linked lists to implement other data structures (such as the one in Figure 8.25).
Stack A stack represents a pile of data items stacked one on top of another. It allows addition or removal of a data item from the top of the pile only. Since we can add or remove a data item from the top of a stack only, it is always the last item put into the stack, which becomes the first removable item from the stack. Hence, we also refer to a stack as last-infirst-out (LIFO) data structure. “Push” and “Pop” operations are commonly associated with stacks to respectively add and remove data items. Figure 8.29 shows a stack with “push” and “pop” operations.
Top Top Top
N H A
Push (I)
I N H A
Push (S)
S I N H A
Top
Pop S
I N H A
Figure 8.29. A stack. “Push” adds a data item at the top of the stack and “Pop” removes a data item from the top of the stack.
Operations for Handling Stacks Figure 8.30 lists the commonly supported operations for handling stacks. Some programming languages support additional operations.
310
Chapter 8 ■ Planning the Computer Program
Operation
Meaning
Create
Creates an empty stack
Push
Adds a new data item to the stack
Pop
Removes a data item from the stack
Top
Returns data item at the top of the stack without removing it from the stack
IsEmpty
Returns “true” if stack is empty, otherwise returns “false”
IsFull
Returns “true” if stack is full, otherwise returns “false”
Figure 8.30. Commonly supported operations for handling stacks.
Uses of Stacks There are several instances in programming where stacks are natural implementation choice. For example, in case of nested function calls, when a function calls another function, which in turn calls a third function, it is important that the third function returns control to the second function rather than the first function. Using a stack to store the return addresses of functions enforces the proper order of calls. Similarly, in the basic design of an operating system, designers use stacks for interrupt handling and operating system function calls. Compilers also use stacks for parenthesis matching to ensure that a program statement uses the same number of “open” and “close” parentheses in the correct order.
Queue A queue represents a container of data items, which is open from both sides and in which a data item can be added at one end only (called “rear” end) and removed from the other end only (called “front” end). Adding an element to a queue is known as “enqueue” operation and removing an element from the queue is known as “dequeue” operation. A queue data structure is like a line of customers waiting at a service counter (for example, a ticket-booking counter). As new customers arrive, they join at the end of the queue. The customer at the head of the queue receives service first and leaves the queue. Hence, we also refer to a queue as first-in-first-out (FIFO) data structure. Figure 8.31 shows a queue with “enqueue” and “dequeue” operations.
front S
front
rear I
Enqueue (H)
N
N front
H
A rear
S
Dequeue I
rear I
N
I front
front Enqueue (A)
H
N
H
A
S
rear I
N
H
Dequeue S
rear
Figure 8.31. A queue. “Enqueue” adds a data item at the rear of the queue and “Dequeue” removes a data item from the front of the queue.
A
■ Data Structures
311
Operations for Handling Queues Figure 8.32 lists the commonly supported operations for handling queues. Some programming languages support additional operations. Operation Create
Meaning Creates an empty queue
Enqueue
Adds a new data item to the queue
Dequeue
Removes a data item from the queue
First
Returns data item at the front of the queue without removing it from the queue
Last
Returns data item at the rear of the queue without removing it from the queue
IsEmpty
Returns “true” if queue is empty, otherwise returns “false”
IsFull
Returns “true” if queue is full, otherwise returns “false” Figure 8.32. Commonly supported operations for handling queues.
Uses of Queues The first-in-first-out property of a queue makes it the preferred data structure for implementing activities, which we need to perform on first-come-first-served basis. For example, we often use queues in programming networks, operating systems, and other situations in which many processes share resources such as CPU, memory, communication channel, I/O devices, etc., and the scheduler allocates resources on a first-come-first-served basis.
Hash Table A hash table is a table for storing data items, which uses a hash function to map data items into positions in the table. The hash function is applied on a data item itself or on a pre-specified field (called “key” field) of the data item (when the data item has more than one field) to map the data item into a specific position (called “index” or “bucket”) in the hash table. Hence, if “f” is the hash function and “ki” is the value of the key of data item “i”, then data item “i” is mapped to index position “pi” of the hash table, where pi = f(ki). A hash table enables quick storage and retrieval of data items. To search for a data item “a” with key “k a”, we compute pa = f(ka) and see if there is an element at position “pa” in the table. If so, we have found the data item, else the table has no data item with this key. If we have found the data item, we can retrieve or delete it from the table. If not found, we can add the data item to the table by inserting it at position “pa”. Here we have assumed an ideal hash table, which is large enough to have a position for each key value. Although, this is possible for applications having small range of key values, there are many applications having so large range of key values that such a large hash table is practically infeasible. For such applications, we use a hash table whose size is smaller than the range of key values and a hash function that maps several different keys into the same position of the hash table. In this case, the most commonly used hash function is the one based on modulo operator, which has the form f(k) = k % S where k is the key, S is the size of hash table (number of positions in it), and % is the modulo operator. The positions in the hash table are indexed from 0 to S-1. Figure 8.33 shows a hash table with 8 positions (indexed 0 to 7), in which data items are mapped using the hash function
312
Chapter 8 ■ Planning the Computer Program f(k) = k % 8
Figure 8.33(a) shows the original status of the hash table with three data items. Figure 8.33(b) and 8.33(c) show the status after inserting data items with key values 76 and 95, respectively. Now suppose we wish to enter another data item with key value 33 into the hash table. Since, f(33) = 33 % 8 = 1, this data item should be entered in position having index value 1. However, this position is already having a data item. In such a situation, we say that a collision has occurred. Hash tables use some mechanism to handle collisions. One such mechanism is to use linked lists with the hash table. In this case, a position itself points to a linked list, which contains all the data items mapped to that position. Figure 8.34 shows a hash table with the use of linked lists for handling collisions. In this hash table, each position has space for a pointer that points to a node of a linked list when the key of a data item maps to that position.
Data items
Table indices 0 1
0 65
1
2 3
1
2
Insert (76)
27
4 5
0 65
05
6
Insert (95)
3
27
3
27
4
76
4
76
5
05
5
05
6
7
65
2
6
7
7
f(05) = 05 % 8 = 5 f(27) = 27 % 8 = 3 f(65) = 65 % 8 = 1
f(76) = 76 % 8 = 4
(a) Original status
(b) Status after inserting 76
95
f(95) = 95 % 8 = 7
(c) Status after inserting 95
Figure 8.33. A hash table with 8 positions and hash function f(k) = k % 8.
Table indices 0
40
1
33
65
3
27
35
59
75
4
76
5
05
21
45
53
2
85
6 7
95
Figure 8.34. A hash table with linked lists for handling collisions. It uses the hash function f(k) = k % 8.
■ Data Structures
313
To search for a data item with key “k”, we first compute f(k) for obtaining the table index, and then search the linked list that is pointed to by the pointer at this position. To insert a data item, we need to first ensure that the hash table does not contain a data item with the same key. For this, we need to search only the linked list pointed to by the pointer at the position to which the hash function maps the key of the data item. As a search precedes each insert, maintaining the linked lists in ascending order of the key values can make search and insert operations more efficient. We delete a data item from the hash table by first searching it in the manner mentioned above, and then deleting the corresponding node from the linked list to which it belongs.
Operations for Handling Hash Tables Figure 8.35 lists the commonly supported operations for handling hash tables. Operation
Meaning
Create
Creates an empty hash table. Table size and hash function are specified
Insert
Adds a new data item to the hash table
Delete
Removes a data item from the hash table
Search (k)
Returns the data item with key “k”. Returns “false” if the data item is not found. Figure 8.35. Commonly supported operations for handling hash tables.
Uses of Hash Tables We often use hash tables for maintaining large tables (containing many data items) for those applications that need almost random access to any data item in the table. Such applications cannot rely on building a sorted table of data items and then using the binary search method for searching a data item because the performance of insert, delete, and search operations in this case are directly proportional to the number of data items. However, with the use of hash tables, performance of insert, delete and search operations are independent of the number of data items and depend largely on the hash function and the load on the hash table (lengths of associated linked lists).
Tree A tree consists of a finite set of elements (called nodes) that are connected by a finite set of directed lines (called branches). A branch coming out of a node is called its out-degree branch, while a branch going in a node is called its in-degree branch. Degree of a node is equal to the sum of its in-degree and out-degree branches. A non-empty tree (which has at lease one node) has a node with in-degree equal to zero. This node is called the root of the tree. All other nodes have in-degree equal to one. The out-degree of any node (including the root) can be zero, one, or more. Figure 8.36 shows an example of a tree.
Chapter 8 ■ Planning the Computer Program
314
Root Level 0
A Branch AD B
Level 1
C
D Branch DI
E
Level 2
Root: A Leaves: C, E, F, G, H, I Internal nodes: B, D Height: 2 + 1 = 3
F
G
H
I
Parents: A, B, D Children: B, C, D, E, F, G, H, I Siblings: {B, C, D}, {E, F, G}, {H, I} Nodes: A, B, C, D, E, F, G, H, I Branches: AB, AC, AD, BE, BF, BG, DH, DI
Figure 8.36. Example of a tree with associated terminologies.
Terminologies Used With Trees In addition to the above terminologies, we also frequently use the following terminologies with trees: 1.
Parent. A parent is a node that has one or more successors. Its out-degree is greater than zero. For example in Figure 8.36, node A is the parent of nodes B, C, D; node B is the parent of nodes E, F, G; and node D is the parent of nodes H, I.
2.
Child. A child is a node that has a parent (a predecessor node). Its in-degree is one. For example in Figure 8.36, nodes B, C, D are children of node A; nodes E, F, G are children of node B; and nodes H, I are children of node D.
3.
Siblings. Two or more nodes with the same parent are siblings. For example in Figure 8.36, sets of siblings are {B, C, D}, {E, F, G}, and {H, I}.
4.
Leaf. A leaf is a node that has no successor. Its out-degree is zero. For example in Figure 8.36, nodes C, E, F, G, H and I are leaves.
5.
Internal node. A node that is neither a root nor a leaf is an internal node. For example in Figure 8.36, nodes B and D are internal nodes.
6.
Path. A path between two nodes is a sequence of nodes between them such that each node in the sequence is adjacent to the next one (has a branch between them). For example in Figure 8.36, the path between nodes A and F is A B F, and the path between nodes G and H is G B A D H.
7.
Ancestor. Any node in the path from the root to a node is the node’s ancestor. For example in Figure 8.36, node E’s ancestors are nodes A and B; node I’s ancestors are nodes A and D; and node C’s ancestor is node A.
8.
Descendent. All nodes in the paths from a node to a leaf are descendents of the node. For example in Figure 8.36, node A’s descendents are nodes B, C, D, E, F, G, H, I; node B’s descendents are nodes E, F, G; and node D’s descendents are nodes H, I.
9.
Level. The level of a node is the number of branches in the path from the root to the node. Hence, the root is at level 0, its children (if any) are at level 1, their children (if any) are at level 2, and so on. For example in Figure 8.36, node A is at level 0; node B, C, D are at level 1; and node E, F, G, H, I are at level 2. Note that siblings are always at the same level, but not all nodes at the same level are necessarily siblings because they may have different parents.
■ Data Structures
315
10. Height. The height of a tree is the level of the leaf in the longest path from the root plus one. The height of
the tree of Figure 8.36 is 3. The height of an empty tree is -1. 11. Subtree. A subtree is any part of a tree below the root. The first node of a subtree is known as its root. A
subtree can be divided into other subtrees. For example in Figure 8.36, nodes B, E, F, G form a subtree with root B; C is a subtree with root C; and D, H, I form a subtree with root D. In addition, E is a subtree of the subtree with root B.
Operations for Handling Trees Figure 8.37 lists the commonly supported operations for handling trees. Operation
Meaning
Create
Create a tree
Delete
Delete a tree and free its nodes
Height
Determine the height of a tree
Size
Determine the number of nodes in a tree
Figure 8.37. Commonly supported operations for handling trees.
Uses of Trees Trees are natural choice as data structures for representing hierarchical relationship among a set of data elements. For example, we use trees to represent family structure, hierarchical administrative structure of an organization, module hierarchy of a software system, etc. We also often use trees in computer science for representing arithmetic expressions and for implementing efficient methods of searching large data sets.
Binary Tree A binary tree is a special type of tree in which the out-degree of any node is less than or equal to two (zero, one, or two). Hence, a node can have maximum two subtrees, which are called left subtree and right subtree of the node. If a node’s out-degree is zero, it is a leaf node, which has neither left subtree nor right subtree. If its out-degree is one, it has either left subtree or right subtree. If its out-degree is two, it has both left and right subtrees. Figure 8.38 shows examples of such binary trees. Linked representation is the most popular way to represent a binary tree. In this representation, each node has three fields – a data field, a “LeftChild” pointer field that points to the left child of the node, and a “RightChild” pointer field that points to the right child of the node. If a node has no left child or right child, the corresponding pointer(s) is set to zero (NULL). Additionally, it uses a pointer “Root” to keep track of the root of the binary tree. We can access all nodes of a binary tree by starting with the “Root” pointer and following “LeftChild” and “RightChild” pointers of the nodes. Figure 8.39 shows the linked representation of the binary tree of Figure 8.38(g).
316
Chapter 8 ■ Planning the Computer Program
A A
B (b) A two-node binary tree. Out-degree of A is one. It has a left subtree.
(a) A single-node binary tree. Out-degree of A is zero. It is a leaf node having neither left subtree nor right subtree.
A
A B
B (c) A two-node binary tree. Out-degree of A is one. It has a right subtree.
C
(d) A three-node binary tree. Out-degree of A is two. It has both left and right subtrees.
A
A B
B
C
C (e) A three-node binary tree. Out-degree of both A and B is one, and they both have a left subtree.
(f) A three-node binary tree. Out-degree of both A and B is one. A has a left subtree and B has a right subtree.
A
A B
B
C E
D
D
(g) A five-node binary tree. Out-degree of A, B, and C is 2, 0, and 2, respectively. Both A and C have both left and right subtees.
C E
F
Figure 8.38. A few examples of binary trees.
Root A 0
B
0 0
C D
0
G
(h) A seven-node binary tree. Out-degree of A, B, and C is 2. They all have both left and right subtrees.
0
E
0
Figure 8.39. Linked representation of the binary tree of Figure 8.38(g).
■ Data Structures
317
Properties of Binary Trees By their very nature, binary trees have the following properties: 1.
A binary tree with n nodes (n > 0) has exactly n-1 branches.
2.
A binary tree of height h (h ≥ 0) has at least h and at most 2h-1 nodes.
3.
The height of a binary tree having n nodes (n ≥ 0) is at most n and at least ┌log2 (n+1)┐.
Binary Tree Traversal Traversing a tree means visiting all its nodes in a defined order and listing (or processing) the data elements of the nodes in the sequence in which the process visits the nodes. The three common methods to traverse a binary tree are preorder, inorder and postorder traversal. They are described here. 1.
Preorder. In this method, the process visits each node first, followed by its left subtree and then its right subtree. For example, if we perform preorder traversal of the binary tree of Figure 8.40, we visit/process the root node (+) first, followed by its left subtree and then its right subtree. To traverse the left subtree, we visit/process its root node (*) first, followed by the only node in its left subtree (a) and then the only node in its right subtree (b). Since we have visited/processed all the nodes in the left subtree of the root node, we are now ready to process the right subtree of the root node (+). Following the rules of preorder traversal, we visit/process the nodes in the right subtree in the order /, c, d. This completes the traversal resulting in the sequence +*ab/cd of the data elements of the nodes (see Figure 8.40).
+ /
*
a
A binary tree representing the algebraic expression: (a * b) + (c / d). Output of its:
b
c
d
Preorder traversal is : +*ab/cd Inorder traversal is : a*b+c/d Postorder traversal is : ab*cd/+
Figure 8.40. Preorder, inorder and postorder traversals of a binary tree.
2.
Inorder. In this method, for each node, the process first visits/processes the node’s left subtree, followed by the node itself, and then its right subtree. For example, if we perform inorder traversal of the binary tree of Figure 8.40, since we have to first visit/process the left subtree of root node (+), we trace from the root to the far-left leaf node (a) before visiting/processing any other node. After visiting/processing node ‘a’, we visit/process its parent node ‘*’, followed by its right subtree node ‘b’. This completes the traversal of the left subtree of root node (+). Hence, we now visit/process the root node (+), followed by its right subtree. Following the rules of inorder traversal, we visit/process the nodes in the right subtree in the order c, /, d. This completes the traversal resulting in the sequence a*b+c/d of the data elements of the nodes (see Figure 8.40).
3.
Postorder. In this method, for each node, the process first visits/processes the node’s left subtree, followed by its right subtree, and then the node itself. For example, if we perform postorder traversal of the binary tree of Figure 8.40, since we have to first traverse the left subtree of root node (+), we trace from the root to the far-left leaf node (a) before visiting/processing any other node. After visiting/processing node ‘a’, we visit/process the right subtree of its parent node ‘*’, which happens to be a single node (b). We then visit/process the parent node ‘*’. This completes the traversal of the left subtree of the root node (+). Hence, we now traverse the right subtree of root node (+). Following the rules of postorder traversal, we visit/process the nodes in the right subtree in the order c, d, /. Finally, we visit/process the root node (+) itself. This completes the traversal resulting in the sequence ab*cd/+ of the data elements of the nodes (see Figure 8.40).
Chapter 8 ■ Planning the Computer Program
318
Operations for Handling Binary Tree Figure 8.41 lists the commonly supported operations for handling binary trees. Operation
Meaning
Create
Create a binary tree
Delete
Delete a binary tree and free its nodes
Height
Determine the height of a binary tree
Size
Determine the number of nodes in a binary tree
IsEmpty
Returns “true” if a binary tree is empty, othersie returns “false”
PreOrder
Output the data fields in preorder traversal method
InOrder
Output the data fields in inorder traversal method
PostOrder
Output the data fields in postorder traversal method
Figure 8.41. Commonly supported operations for handling binary trees.
Uses of Binary Trees We frequently use binary trees in computer science because of the interesting relationship between binary trees and binary number system. As Figure 8.40 shows, we also use them for representation of algebraic expressions involving binary operators (such as +, -, *, /). We will now see how to use binary trees to implement binary search trees and binary heaps.
Binary Search Tree A binary search tree is a binary tree with the following properties: 1.
The data element of each node has a key field and no two nodes have the same key value (all keys are distinct).
2.
The keys of all nodes in the left subtree of the root have values less than the key in the root.
3.
The keys of all nodes in the right subtree of the root have values greater than the key in the root.
4.
Each subtree is itself a binary search tree.
The above definition does not allow multiple data elements with same key value in a binary search tree. A binary search tree with duplicates allows multiple data elements with same key value. It is a binary tree with the following properties: 1.
The data element of each node has a key field and two or more nodes may have the same key value (all keys need not necessarily be distinct).
2.
The keys of all nodes in the left subtree of the root have values less than or equal to (≤) the key in the root.
3.
The keys of all nodes in the right subtree of the root have values greater than or equal to (≥) the key in the root.
4.
Each subtree is itself a binary search tree.
Figure 8.42 shows some valid binary search trees and Figure 8.43 shows some invalid binary search trees.
■ Data Structures
8
319
8
8 5
26
5
26
44
3 (a)
(c)
(b)
8
8 26
5
5
26 44
23 3 (d)
(e)
Figure 8.42. Some valid binary search trees.
8
8
10
26
5
(a) Violates property 2 because 10 > 8
7
(b) Violates property 3 because 7 < 8
8
8 5 6
3
7
(c) Violates property 4 because the left subtree of the root is not a valid binary search tree (6 > 5)
26
5
26
12
(d) Violates property 1 because the left subtree of the root has a node with key value 12 that is not less than the key value 8 of the root
Figure 8.43. Some invalid binary search trees.
Inserting a New Data Element in a Binary Search Tree To insert a new data element with key value ‘k’ in a binary search tree, we must first verify that there is no node in the binary search tree with data element having the same key value ‘k’. For this, we perform a binary search of the nodes for a data element having key value ‘k’. If the search is unsuccessful, we create a new node with the new data element at the point the search terminated. For example, to insert a data element with key 7 into the binary search tree of Figure 8.44(a), we first search for 7. This search terminates unsuccessfully, and the last node examined is the one with key 5. Hence, we insert the new data element in a node, which we create as the right child of this node. Figure 8.44(b) shows the resulting binary search tree. Similarly, Figure 8.44(c) shows the resulting binary search tree after inserting a new data element with key value 25 into the binary search tree of Figure 8.44(b).
Chapter 8 ■ Planning the Computer Program
320
5
8
8
8
3
3 (a) A binary search tree
3
7
26
5
26
5
26
7
25
(c) Resulting binary search tree after (b) Resulting binary search tree inserting a node with a data after inserting a node with a element having key value 25 data element having key value 7
Figure 8.44. Examples of inserting a new data element in a binary search tree.
Operations for Handling Binary Search Trees Figure 8.45 lists the commonly supported operations for handling binary search trees. Operation Create Search (k, e) Insert (e) Delete (k, e) Ascend
Meaning Create an empty binary search tree Search for the data element with key value ‘k’ and return the data in ‘e’. Returns “false” if the search fails Insert the data element in ‘e’ into the binary search tree. Key value ‘k’ is a predefined field of the data element Delete the data element with key value ‘k’ and return it in ‘e’. Ouput all data elements of a binary search tree in ascending order of their key values
Figure 8.45. Commonly supported operations for handling binary search trees.
Uses of Binary Search Trees Organizing data elements with unique key values in the form of binary search trees enables efficient execution of the following types of operations: 1.
Outputting of data elements in ascending order of their key values.
2.
Searching a data element with specified key value.
3.
Inserting a new data element.
4.
Deleting the kth data element.
Several applications need to implement these operations and benefit from the use of binary search trees.
Graph A graph is a collection of nodes (also called vertices) and edges (also called lines). Each edge (i, j) joins two nodes i and j. We frequently use the following terminologies with graphs:
■ Data Structures
321
1.
Directed edge. An edge with an orientation is a directed edge. Hence, a directed edge (i, j) is different from the directed edge (j, i), the former being oriented from node i to node j and the latter from node j to node i. The graph of Figure 8.46(b) has directed edges.
2.
Undirected edge. An edge with no orientation is an undirected edge. Hence, undirected edges (i, j) and (j, i) are the same. The graph of Figure 8.46(a) has undirected edges.
3.
Directed graph. If all edges of a graph are directed edges, then the graph is called a directed graph (also called digraph). The graph of Figure 8.46(b) is a digraph.
4.
Undirected graph. If all edges of a graph are undirected edges, then the graph is called an undirected graph (also called graph). The graph of Figure 8.46(a) is an undirected graph.
5.
Weighted graph/digraph. When we assign weights to the edges of a graph/digraph, we call the resulting graph/digraph a weighted graph/digraph. For example, we can draw a graph to represent major cities of a country connected by roads. In this graph, we can express the distance between two cities by adding weights to the edges. Figure 8.46(c) shows a weighted graph.
6.
Adjacent nodes. Two nodes i and j of a graph are adjacent, if (i, j) is an edge in the graph. In the graph of Figure 8.46(a), nodes a and b, a and c, a and d, b and d, and c and d are adjacent. However, in the digraph of Figure 8.46(b), a is adjacent to b, a is adjacent to d, b is adjacent to d, c is adjacent to a, and d is adjacent to c.
a
a c
b
c
b
b
d
(a) An unidirected graph
(b) A directed graph (digraph)
8 85
60
d
Loop
a
25
d
c 40
(c) A weighted graph
a
a
a c
b
c
b d
d (d) A digraph with a loop
c
b d
(e) Two disconnected (disjoint) graphs
Figure 8.46. Examples of different types of graphs.
7.
Path. A path is a sequence of nodes in which each node is adjacent to the next one. For example, in the graph of Figure 8.46(a), there are three paths from node a to node c. They are (a, c), (a, d, c) and (a, b, d, c). Similarly, (b, a, c), (b, d, c), (b, a, d, c) and (b, d, a, c) are the four paths from node b to node c. However, in the digraph of Figure 8.46(b), there is only one path from node a to node c, which is (a, c); and from node b to node c, which is (b, a, c). This is because of the difference in the definition of adjacent nodes in graph and digraph.
8.
Cycle. A cycle is a path, whose start and end nodes are the same. For instance, (a, b, d, a), (a, c, d, a) and (a, b, d, c, a) are examples of cycles in the graph of Figure 8.46(a); and (a, c, d, a) and (a, c, d, b, a) are examples of cycles in the digraph of Figure 8.46(b).
Chapter 8 ■ Planning the Computer Program
322 9.
Loop. A loop is a self-edge, which is of the form (i, i). It is a single edge, which starts and ends at the same node. Figure 8.46(d) shows a digraph with a loop. Note that a graph/digraph cannot contain any loop because a valid edge connects two different nodes. However, loops are useful in some graphical representations in computer science. For example, we use loops in state transition diagrams.
10. Connected graph. A connected graph has a path from any node to any other node. The graph of Figure
8.46(a) is a connected graph. 11. Disconnected/disjoint graph. A graph, which is not a connected graph, is a disconnected/disjoint graph. In a
disconnected/disjoint graph, certain groups of nodes form an island and do not have any connection with the rest of the graph. Figure 8.46(e) shows two disconnected/disjoint graphs. 12. Complete graph/digraph. A graph/digraph is complete when it has an edge between every pair of nodes.
Notice that a complete graph with n nodes will have n(n-1)/2 edges, and a complete digraph with n nodes will have n(n-1) edges. Figure 8.47 shows complete graphs for n = 1, 2, 3 and 4. Figure 8.48 shows complete diagraphs for n = 1, 2, 3 and 4.
a
a
a
a
c
n=1
c
b
b
b
d
n=2
n=3
n=4
Figure 8.47. Complete graphs for n = 1, 2, 3 and 4. Number of edges in each = n(n-1)/2.
a a
c b
n=1
a
a
n=2
b n=3
c
b d n=4
Figure 8.48. Complete digraphs for n = 1, 2, 3 and 4. Number of edges in each = n(n-1).
13. Strongly and weakly connected digraphs. A digraph is strongly connected, if there is a path from each node
to every other node in it. That is, it has a path from i to j and from j to i for every pair of nodes i and j. A digraph is weakly connected, if each node does not have a path to every other node in it. That is, in a weakly connected digraph there are at least two nodes i and j such that there is no path either from i to j or from j to i. Figure 8.49 shows examples of strongly and weakly connected digraphs.
■ Data Structures
g
h
g
f
f e
e a
323
a
d
b
d
b c
c
(b) A weakly connected digraph
(a) A strongly connected digraph
Figure 8.49. Examples of strongly and weakly connected digraphs.
14. Subgraph. If the set of nodes and set of edges of a graph G1 are subsets of the set of nodes and set of edges of
another graph G2, then G1 is a subgraph of G2. Figure 8.50 (b to i) are subgraphs of Figure 8.50(a). 15. Spanning tree. An undirected graph that is connected and has no cycles is a tree. A subgraph of a graph G
that contains all the nodes of G and is a tree is a spanning tree of G. Figure 8.50 (f to i) are some spanning trees of the graph in Figure 8.50(a).
a c
b
a
a
d
d
d
(a)
(b)
(c)
a
a c
b
c b d
d
(d)
(e)
a c
b
a
c
c
b
a
a c
b
c
b
d
d
d
d
(f)
(g)
(h)
(i)
Figure 8.50. Examples of subgraphs and spanning trees.
Representation of Graphs and Digraphs Adjacency matrix is the most frequently used representation for graphs and digraphs in computers. Let G(V, E) be a graph in which V is the set of nodes (vertices) and E is the set of edges. Let the graph has n nodes and V = {1, 2, …, n}. Then the adjacency matrix of G is an n x n matrix A, in which each element is either zero or one defined in the following manner:
324
Chapter 8 ■ Planning the Computer Program
If G is an undirected graph, then A (i, j) =
If G is a digraph, then
1 if (i, j) E or (j, i) E 0 otherwise
A (i, j) =
1 if (i, j) E 0 otherwise
Figure 8.51 (a) and (b) show the adjacency matrices for the graph of Figure 8.46(a) and the digraph of Figure 8.46(b), respectively.
a ↓
b ↓
c ↓
d ↓
a ↓
b ↓
c ↓
d ↓
1
2
3
4
1
2
3
4
→
1
0
1
1
1
a →
1
0
0
1
0
b →
2
1
0
0
1
b →
2
1
0
0
0
→
3
1
0
0
1
c
→
3
0
0
0
1
d →
4
1
1
1
0
d →
4
1
1
0
0
a c
(a) Adjacency matrix for the graph of Figure 8.46(a)
(b) Adjacency matrix for the digraph of Figure 8.46(b)
Figure 8.51. Adjacency matrices for the graph of Figure 8.46(a) and the digraph of Figure 8.46(b).
The adjacency matrix of a graph/digraph is subsequently stored using the method for storing arrays. There are several optimization methods on storage requirement for storing adjacency matrices of a graph/digraph. For example, notice that the adjacency matrix for undirected graphs is symmetric [A (i, j) = A (j, i)]. Hence, we need to explicitly store only the elements above (or below) the diagonal. Similarly, notice that all diagonal elements for the adjacency matrices of both undirected graphs and digraphs are zero. Hence, we need not store them.
Graph Search Methods In applications based on graphs, we often need to find things such as: 1. Is there a path between two nodes i and j in a given graph/digraph? 2. Does a graph/digraph contain one or more cycles? 3. Is a graph connected or disjoint? 4. Is a graph/digraph complete? 5. Is a digraph strongly or weakly connected? 6. Find one or more spanning trees of a graph. Many of these functions require us to find out all nodes of a graph that can be reached (has a path) from a given node (start node). The two standard methods to do this are breadth-first search and depth-first search. They are described here. A graph/digraph often has multiple paths to a node from a given node (start node). Hence, while performing breadthfirst search or depth-first search, we may arrive at the node from more than one direction. We must, therefore, ensure that we visit each node only once during the search process. The popular method to do this is to use a “Visited” flag at each node. This flag at each node is set to OFF before we start the search process, and is set to ON for a node as soon
■ Data Structures
325
as we visit the node for the first time. We do not revisit a node, whose flag is ON even if we arrive at it again during the search process. Out of the two, we use depth-first search method more frequently to obtain efficient algorithms for analyzing graphs. 1. Breadth-first search. The example of Figure 8.52 illustrates the method. In this method, we pick up the starting node (D) and determine the set of nodes adjacent to D, which is {I} for our example. We now pick up the nodes in this set one-by-one and determine the new nodes (nodes not yet visited) adjacent to each node in the set. The set of nodes adjacent to node I are {G, R}. We repeat this process until we find that there are no more adjacent nodes. For example, the set of new nodes adjacent to node G is {A} and adjacent to node R is {P}. Next, set of new nodes adjacent to node A is {H}, and set of new nodes adjacent to node P is null. Finally, set of new nodes adjacent to node H is null. Hence, the set of nodes reachable from starting node D is {D, I, G, R, A, P, H}. Normally, we use a queue to implement the breadth-first search algorithm. As we process each node, we place all of its adjacent nodes in the queue. We always pick up the node at the front of the queue as the next node in the search process. We delete a node from the queue when we pick it up for processing. Figure 8.52(b) shows the contents of the queue as we progress through the search process. Figure 8.52(c) shows the subgraph formed by the edges used to reach the nodes visited during the search process.
A
G D
I
H
S
P
R (a) A digraph
Front Rear
Empty
D
I
G R
R A
A P
P H
H
1
2
3
4
5
6
7
(b) Queue contents as we progress through the search process. The numbers indicate the sequence in which front node from the queue is picked up for processing.
1
2
D
I
3
5
7
G
A
H
R
P
4
6
(c) Subgraph formed by the edges used to reach the nodes visited during the search process. The numbers indicate the sequence in which the nodes are picked up for processing.
Figure 8.52. Example of breadth-first search method of graph search.
Empty
326
Chapter 8 ■ Planning the Computer Program 2. Depth-first search. In this method, we select a node (starting node is selected first) and determine the set of new nodes (nodes not yet visited) adjacent to it. If this set is empty (there is no new node adjacent to the selected node), the search terminates. Otherwise, we select the nodes in this set one-by-one and initiate a depth-first search for each, selecting the next node in the set when the depth-first search terminates for the previously selected node. The depth-first search for the graph terminates when we complete the depth-first search for all the nodes in the set.
Normally, we use a stack to implement depth-first search algorithm. The search process begins by pushing the first node (starting node) into the stack. We then pop the stack to select the next node for processing. After processing the selected node, we push all its adjacent new nodes into the stack. We repeat the above process until the stack becomes empty. The search process completes when the stack becomes empty. The same example of Figure 8.52(a), which is reproduced in Figure 8.53(a), illustrates the method. We select the starting node D and push it into the stack. We then pop the stack to select node D for processing and push node I into the stack, which is the only new node adjacent to D. We now pop the stack to select node I for processing and push nodes G and R into the stack, which are the new nodes adjacent to I. We now pop the stack to select node R for processing and push nodes A and P into the stack, which are the new nodes adjacent to R (node G is not new since we have already visited it). We now pop the stack to select node P for processing and push node H into the stack, which A
G D
I
H
S
P
R
(a) A digraph
Empty
D
I
R G
1
2
3
P A G
H A G
A G
G
4
5
6
7
Empty
(b) Stack contents as we progress through the search process. The numbers indicate the sequence in which the top node from the stack is selected for processing.
1
2
D
I
7
6
5
G
A
H
R
P
3
4
(c) Subgraph formed by the edges used to reach the nodes visited during the search process. The numbers indicate the sequence in which the nodes are selected for processing.
Figure 8.53. Example of depth-first search method of graph search.
is the new node adjacent to P. We now pop the stack to select node H for processing and do not push any node into the stack because there are no new nodes adjacent to H. We next pop the stack to select node A for processing and do not push any node into the stack because there are no new nodes adjacent to A (nodes P and H are not new since we have already visited them). We next pop the stack to select node G for processing and do not push any node into the
■ Data Structures
327
stack because there are no new nodes adjacent to G (node A is not new since we have already visited it). As the stack is empty now, the search process terminates. Figure 8.53(b) shows the contents of the stack as we progress through the search process. Figure 8.53(c) shows the subgraph formed by the edges used to reach the nodes visited during the search process.
Operations for Handling Graphs/Digraphs Figures 8.54 and 8.55 list the commonly supported operations for handling graphs and digraphs, respectively.
Operation
Meaning
Create(n)
Creates a graph (undirected) having n nodes and no edges
Add(i, j)
Adds the edge (i, j) to the graph
Delete(i, j)
Deletes the edge (i, j) from the graph
Exist(i, j)
Returns “true” if edge (i, j) exists in the graph, else returns “false”
Vertices
Returns the number of nodes in the graph
Edges
Returns the number of edges in the graph
Degree(i)
Returns the degree of node i
Figure 8.54. Commonly supported operations for handling graphs (undirected).
Operation Create(n)
Meaning Creates a digraph having n nodes and no edges
Add(i, j)
Adds the edge (i, j) to the digraph
Delete(i, j)
Deletes the edge (i, j) from the digraph
Exist(i, j)
Returns “true” if edge (i, j) exists in the digraph, else returns “false”
Vertices
Returns the number of nodes in the digraph
Edges
Returns the number of edges in the digraph
InDegree(i)
Returns the in-degree of node i
OutDegree(i)
Returns the out-degree of node i
Figure 8.55. Commonly supported operations for handling digraphs.
Uses of Graphs Graphs are a convenient means of expressing problems that involve relationships between objects, places, or concepts. For instance, we can describe who knows whom in a room as a collection of nodes, each representing a person, and directed edges from a node to other nodes representing all the persons whom that person (represented by the node) knows. Representation of such abstract problems as graphs simplifies the process of creating mental models of problems, which in turn leads to better problem solving. Hence, we often use graphs for modeling and analysis of a variety of situations such as: 1. Analysis of electrical and computer networks. 2.
Study of molecular structure of chemical compounds.
3.
Representation of routes in airline network, railway network, road network, or communication network.
Chapter 8 ■ Planning the Computer Program
328 4.
Representation of dependencies among modules of software.
5.
Representation of dependencies among various activities of a project for better project planning.
6.
In genetic studies.
Modeling a problem as a graph often presents new approaches to solve the problem or even leads directly to a solution derived from graph theory.
Heap A heap is a complete binary tree in which the value of each node is greater (less) than or equal to those of its children (if any). A complete tree is one that has every level filled in before adding a node to the next level, and one that has the nodes in a given level filled in from left to right, with no breaks. For instance, a tree with a root node that has only one child must have its child as the left node. If the value of each node in a heap is greater than or equal to those of its children (if any), then the heap is called a max heap. On the other hand, if the value of each node in a heap is less than or equal to those of its children (if any), then the heap is called a min heap. Obviously, the value of the root node is maximum in the max heap and minimum in a min heap. Figures 8.56(a) and (b) show examples of a max heap and a min heap, respectively. 5
25 20
4
50
15
8
60
3 80
(a) A max heap
12
65
18
15
70 (b) A min heap
Figure 8.56. Examples of a max heap and a min heap.
Operations for Handling Heaps Figures 8.57 and 8.58 list the commonly supported operations for handling max heaps, and min heaps, respectively.
Operation
Meaning
Max
Returns the value of the root node
DeleteMax
Deletes the root node and restructures the heap
Insert(i)
Inserts a new node with value i into the heap and restructures the heap
Delete(i)
Deletes a node with value i from the heap and restructures the heap
Merge
Joins two max heaps to form a new heap containing all the elements of both Figure 8.57. Commonly supported operations for handling max heaps.
■ Points to Remember
Operation
329
Meaning
Min
Returns the value of the root node
DeleteMin
Deletes the root node and restructures the heap
Insert(i)
Inserts a new node with value i into the heap and restructures the heap
Delete(i)
Deletes a node with value i from the heap and restructures the heap
Merge
Joins two min heaps to form a new heap containing all the elements of both Figure 8.58. Commonly supported operations for handling min heaps.
Uses of Heaps A heap serves as a priority queue in which the most important element (node) is always at the front of the queue, and when removed, its replacement becomes the most important element. Such a data structure is useful for implementing algorithms, which require processing of certain things in complete order, but when we do not want to perform a full sort or do not need to know anything about the rest of the elements. For example, we can optimize Dijkestra’s algorithm (a well-known algorithm) for finding the shortest path between two nodes in a graph by using a priority queue. We also use heaps to sort data by using the “heapsort” sorting algorithm.
Points to Remember 1.
To ensure that the instructions of a program are appropriate for the problem at hand and in the correct sequence, we must plan the program before writing it.
2.
The term algorithm refers to the logic of a program. It is a step-by-step description of how to arrive at a solution to a given problem.
3.
Factors often used to judge the quality of an algorithm are the time required to execute the corresponding program on a given computer, memory space required to execute it, accuracy of results produced by it, and its capability to handle a range of input data instead of a single input data (often referred to as generality property).
4.
An algorithm represented in the form of a programming language is a program. Hence, any program is an algorithm, but the reverse is not true.
5.
Programmers often use flowcharts, decision tables, and pseudocodes for representing algorithms for their programs before coding them in programming languages.
6.
A flowchart is a pictorial representation of an algorithm. Programmers often use it as a program-planning tool for visually organizing a sequence of steps necessary to solve a problem using a computer.
7.
We use decision tables to define the word statement of a problem in a tabular form. They define complex program logic by charting what decisions or actions a computer is to take because of input data.
8.
Pseudocode is a program-planning tool that allows programmers to plan program logic by writing program instructions in a natural language, such as English.
9.
Structured Programming is a more scientific approach to solving a programming problem by using only three basic logic (control) structures namely, sequence logic, selection logic, and iteration (or looping) logic.
10. Structured programming helps in producing programs that are easy to understand and maintain. It also helps in reducing program errors and the time spent in program testing. 11. We use sequence logic for performing instructions one after another in sequence. 12. We use selection logic for selecting a path out of two or more paths in program logic. It uses three control structures called IF…THEN, IF…THEN…ELSE, and CASE.
330
Chapter 8 ■ Planning the Computer Program
13. We use iteration (or looping) logic for producing loops in program logic when we need to execute one or more instructions several times depending on some condition. It uses two control structures called DO…WHILE and REPEAT…UNTIL. 14. Data structures define ways of organizing and storing different data types used in an algorithm. 15. Data structures are of two types – primitive and composite. Primitive data structures are basic data types defined in a programming language, while composite data structures are composed using primitive data structures. 16. An array is a group of data elements in which we identify an individual element by its position in the group, relative to the first element. Hence, we access an individual element by indexing. 17. A linked list consists of a set of nodes linked together. Each node has two basic fields – data field and pointer field. The data field contains the data, while the pointer field links the node to another node belonging to the same linked list. In addition to the nodes, a linked list has a pointer called “head”, which points to the first node of the linked list. We use it to keep track of the list. 18. A stack represents a pile of data items stacked one on top of another. We add/remove a data item from the top of the pile only. We also refer to it as last-in-first-out (LIFO) data structure. 19. A queue represents a container of data items, which is open from both sides and in which we can add a data item at one end only (called “rear” end) and remove from the other end only (called “front” end). We also refer to it as first-in-first-out (FIFO) data structure. 20. A hash table is a table for storing data items, which uses a hash function to map data items into positions in the table. If “f” is the hash function and “ki” is the value of the key of data item “i”, then data item “i” is mapped to index position “p i” of the hash table, where pi = f(ki). 21. A tree consists of a finite set of elements (called nodes) that are connected by a finite set of directed lines (called branches). A non-empty tree (which has at lease one node) has a node with in-degree equal to zero. This node is called the root of the tree. All other nodes have in-degree equal to one. The out-degree of any node (including the root) can be zero, one, or more. 22. A binary tree is a special type of tree in which the out-degree of any node is less than or equal to two (zero, one, or two). Hence, a node can have maximum two subtrees, which are called left subtree and right subtree of the node. 23. A binary search tree is a binary tree with the following properties: The data element of each node has a key field and no two nodes have the same key value (all keys are distinct). The keys of all nodes in the left subtree of the root have values less than the key in the root. The keys of all nodes in the right subtree of the root have values greater than the key in the root. Each subtree is itself a binary search tree. 24. A graph is a collection of nodes (also called vertices) and edges (also called lines). Each edge (i, j) joins two nodes i and j. If all the edges of a graph are directed edges, then the graph is called a digraph. When we assign weights to the edges of a graph/digraph, the resulting graph/digraph is called a weighted graph/digraph. 25. A heap is a complete binary tree in which the value of each node is greater (less) than or equal to those of its children (if any). If the value of each node in a heap is greater than or equal to those of its children (if any), then the heap is called a max heap. On the other hand, if the value of each node in a heap is less than or equal to those of its children (if any), then the heap is called a min heap.
Questions 1.
Why is it advisable to plan the logic of a program before writing it?
2.
What is an algorithm? What are the characteristics necessary for a sequence of instructions to qualify as an algorithm?
3.
Write the factors that we often use to judge the quality of an algorithm.
4.
What are the ways that we commonly use to represent algorithms? Which of these can we use to solve the corresponding problem on a computer?
5.
Any program is an algorithm, but the reverse is not true. Explain why.
6.
What is a program-planning tool? How is it useful? Name three commonly used program-planning tools.
7.
What is a flowchart?
8.
How does a flowchart help a programmer in program development?
■ Questions 9.
Why do flowcharts use standard symbols? Draw basic flowchart symbols and describe their functions.
10.
What is a record? What is a trailer record?
331
11.
What is a sentinel value? Discuss its use.
12.
What is a process loop? What is an infinite loop?
13.
Why is it necessary to avoid infinite loops in program design?
14.
“A loop consists of a body, a test for exit condition, and a return provision.” Explain this with one example each for DO…WHILE and REPEAT…UNTIL loops.
15.
What is a generalized algorithm? Why should programs be general in nature?
16.
Explain the difference between loop control by counting and loop control by the use of sentinel value. Which is preferable and why?
17.
Illustrate the use of a counter to keep track of number of times a process executes a loop.
18.
Can there be more than one flowchart for a given problem? Write reasons for your answer.
19.
What is the difference between a macroflowchart and a microflowchart? Illustrate with an example.
20.
Write the guidelines that a programmer must follow while drawing a flowchart.
21.
Write advantages and limitations of flowcharting.
22.
Why is proper documentation of a program necessary?
23.
What are program bugs? What is debugging?
24.
What is testing of a program? How do we test a program?
25.
What are decision tables? When are they preferred to flowcharts?
26.
What are the various steps that a programmer must follow for constructing a decision table?
27.
Draw the basic format of a decision table and write the role of each part.
28.
What are don’t care conditions in decision tables?
29.
Write advantages and limitations of decision tables.
30.
What is a pseudocode? Why is it so called? Write another name for pseudocode.
31.
What is a spaghetti code? Why is it so called? What are its major drawbacks?
32.
Name the three basic logic structures used in writing structured programs and write the use of each.
33.
What is structured programming? What are its main advantages?
34.
Write an example to illustrate the use of each of the three basic logic structures used in structured programming.
35.
Draw flowcharts for the two commonly used structures for selection logic.
36.
What is the difference between IF…THEN and IF…THEN…ELSE structures?
37.
Draw flowcharts for the two commonly used structures for iteration logic.
38.
We use both DO…WHILE and REPEAT…UNTIL for looping. Explain the difference between the two structures.
39.
If the condition for looping is found to be false when the loop is encountered for the first time, how many times will the instructions inside the loop be executed in case of (a) DO…WHILE loop (b) REPEAT…UNTIL loop
Write reasons for your answer. 40.
What is the purpose of ENDIF and ENDDO statements?
41.
What is indentation? Why do we use it for writing pseudocodes?
42.
Write advantages and limitations of pseudocode.
332
Chapter 8 ■ Planning the Computer Program
43.
Three numbers denoted by variables A, B, and C are supplied as input data. Draw a flowchart for the logic to identify and print the largest one.
44.
Draw a flowchart to print a list of all students above the age of 20 in a class. Input records contain the name and age of students. Assume a sentinel value of 99 for the age field of the trailer record.
45.
Draw a flowchart to print the name and age of the oldest student and the youngest student in a class. Input records contain the name and age of students. Assume a sentinel value of 99 for the age field of the trailer record.
46.
Draw a flowchart for an algorithm to read and print the first 20 records in a data set. Make sure that the processing stops after the twentieth record.
47.
Input data of all employees of a company is supplied. The first field of each input record contains employee number (EmployeeNo). Assume that a trailer record having a sentinel value of 99999 for EmployeeNo terminates the input data of all employees. Draw a flowchart for the logic to count and print the total number of employees.
48.
For the problem of Question 47, we want to count and print the number of only male employees in the age group of 25 to 30. Assume that input records contain SexCode and Age fields to provide this information. Draw a flowchart for an algorithm to process this requirement.
49.
Information collected from a population survey of a city is stored in a computer. Each record contains the name, address, sex, age, profession, etc., of one citizen. We want to print the details of all adults (aged 18 years or more) and finally the total number of adults. Assume a suitable sentinel value for any field in the trailer record, and draw a flowchart for an algorithm to process this requirement.
50.
We need to search a set of examination papers graded with scores from 0 to 100 to find how many of them are above 90. We need to print the total. Draw a flowchart for an algorithm to process this requirement. Assume a suitable sentinel value for the trailer record.
51.
Each paper in a set of examination papers includes a grade of A, B, C, D, or E. We want to have separate counts of the total number of papers having grade A and those having grade E. We need to print the total counts of both types at the end. Draw a flowchart for an algorithm to process this requirement. Assume a suitable sentinel value for the trailer record.
52.
A shopkeeper wants a general program for his personal computer to prepare a bill for each customer. His idea is that as soon as a customer purchases some goods from his shop, he will supply the description, unit price, and quantity purchased for each item as input to the computer, and the computer should print each item along with its unit price, quantity purchased, and total price. Finally, the computer should also print the total cost of all items purchased by the customer. Assuming a sentinel value of zero for the quantity purchased field in the trailer record, draw a flowchart for an algorithm to process this requirement.
53.
An employer plans to pay bonus to each employee. Those earning Rs. 5000 or above are to be paid 10 percent of their salary and those earning less than Rs. 5000 are to be paid Rs. 500. Each input record contains employee number, name, and salary of an employee. Printed output should contain employee number, name, and amount of bonus to each employee. Draw a flowchart for an algorithm to process this requirement. Assume a suitable sentinel value for any field in the trailer record.
54.
An employer wants to calculate and print weekly pay of his employees. An employee’s pay record includes hours worked and pay rate. For each employee, gross pay is equal to hours worked times pay rate. However, for number of hours worked in excess of 40, overtime rate of 1.5 times the regular rate applies. Draw a flowchart for an algorithm to process this requirement. Assume a suitable sentinel value for any field in the trailer record.
55.
A data file contains a set of examination scores followed by a trailer record with a value of -1. Draw a flowchart for an algorithm to calculate and print the average of the scores.
56.
The expanded form of the data file of Question 55 includes several sets of data, each requiring calculation of its average. A trailer record with a value of -1 follows each data set and a trailer record with a value of -2 follows the last data set. Draw a flowchart for an algorithm to process this requirement.
57.
Five numbers denoted by the variables A, B, C, D, and E are supplied as input. Draw a flowchart for an algorithm to print these numbers in descending order of magnitude.
58.
Draw a flowchart for an algorithm to add up all even numbers between 0 and 100 (both inclusive) and print the result.
59.
Draw a flowchart for an algorithm to find out whether a given triangle ABC is isosceles. Assume that angles of the triangle are supplied as input. Print the answer as yes or no.
■ Questions
333
60.
Draw a flowchart for an algorithm to find out whether a given triangle ABC is a right-angled triangle. Assume that sides of the triangle are supplied as input. Print the answer as yes or no.
61.
Draw a flowchart for an algorithm to find out whether a given quadrilateral ABCD is a rectangle. Assume that all four angles and four sides of the quadrilateral are supplied as input. Print the answer as yes or no.
62.
Draw a flowchart for an algorithm to convert a number from base 10 to a new base using division-remainder technique.
63.
Draw a flowchart for an algorithm to convert a number from another base, to base 10.
64.
Construct a decision table to solve the problem described in Question 43.
65.
Construct a decision table to solve the problem described in Question 48.
66.
Construct a decision table to solve the problem described in Question 51.
67.
Construct a decision table to solve the problem described in Question 59.
68.
Construct a decision table to solve the problem described in Question 60.
69.
Write a pseudocode to solve the problem described in Question 44.
70.
Write a pseudocode to solve the problem described in Question 45.
71.
Write a pseudocode to solve the problem described in Question 48.
72.
Write a pseudocode to solve the problem described in Question 51.
73.
Write a pseudocode to solve the problem described in Question 52.
74.
Write a pseudocode to solve the problem described in Question 53.
75.
Write a pseudocode to solve the problem described in Question 54.
76.
Write a pseudocode to solve the problem described in Question 58.
77.
Write a pseudocode to solve the problem described in Question 59.
78.
What are data structures? Why is knowledge of data structures important for good program planning?
79.
Differentiate between primitive and composite data structures. Write three examples of each.
80.
What is an array? How is an individual element of an array accessed? Explain with an example.
81.
Explain with an example how an array is stored in memory. How many bytes of memory will be occupied by an array of: (a) 100 characters (b) 100 real numbers (c) 100 integers
82.
Differentiate among zero-based, one-based, and n-based indexing of arrays.
83.
Differentiate among static, semi-static, semi-dynamic, and dynamic arrays.
84.
Differentiate between one-dimensional and multidimensional arrays. Write one example of each.
85.
Differentiate between row-major order and column-major order of mapping the elements of a multidimensional array to allocated memory space. Explain with an example of each.
86.
Write five commonly supported operations for handling arrays with the function of each operation.
87.
Write three common uses of arrays.
88.
A[3, 2, 4] is a three-dimensional array in which 3, 2, and 4 are the maximum index values for first, second, and third dimensions respectively. Using zero-based indexing, write the indexes of the array in (a) row-major order and (b) columnmajor order.
89.
What is a linked list? Draw a linked list having four elements (nodes).
90.
What are the two basic fields of a node of a linked list? What is the role of “head” in a linked list? Why do some implementations of linked list use a “tail” in addition to a “head”?
91.
Write advantages of linked list as compared to array.
92.
Differentiate among singly-linked list, singly-linked circular list, and doubly-linked list. Explain each type with a diagram.
93.
Write five commonly supported operations for handling linked lists with the function of each operation.
334
Chapter 8 ■ Planning the Computer Program
94.
Write three common uses of linked lists.
95.
96.
Write pseudocodes for performing the following operations on a singly-linked list: (a) Determining its length (number of nodes) (b) Searching an element in it (c) Deleting the ith element from it (d) Inserting a new element at the head (e) Inserting a new element at the tail (f) Inserting a new element after the ith element What is a stack? Why do we also call it LIFO? Explain with an example.
97.
Write five commonly supported operations for handling stacks with the function of each operation.
98.
Write two common uses of stacks.
99.
Draw a stack with “Push” and “Pop” operations to show the use of stack for matching parentheses in the expression: (a + (b + c) * d * (e + f) – g).
100. What is a queue? Why do we also call it FIFO? Explain with an example. 101. Write five commonly supported operations for handling queues with the function of each operation. 102. Write three common uses of queues. 103. What is a hash table? How it enables quick storage and retrieval of data items? Explain with an example. 104. What is a hash function? What is the most commonly used hash function for hash tables, whose size is smaller than the range of key values it has to handle? 105. When does a collision occur in a hash table? Write a mechanism to handle collisions. Explain the mechanism with an example. 106. Write three commonly supported operations for handling hash tables with the function of each operation. 107. Write two common uses of hash tables. 108. What is a tree data structure? Define the following terminologies used with trees and explain them with an example tree: (k) Descendent (f) Child (a) Root (l) Height (g) Parent (b) Level (h) Siblings (c) Leaf (m) Subtree (d) Internal node (i) Path (j) Ancestor (e) Branch 109. Write four commonly supported operations for handling trees with the function of each operation. 110. Write three common uses of tree data structure. 111. What is a binary tree? Write the properties of binary trees. Draw examples of binary trees having two, five, seven, and ten nodes. 112. Draw a binary tree having six nodes. Now draw the linked representation of this binary tree. 113. What does “traversing” a tree mean? Explain the preorder, inorder and postorder methods of traversing a binary tree. 114. Draw the binary tree corresponding to the expression: ((a + b) + c) + d) Write the results of preorder, inorder and postorder traversals of this tree. 115. Write five commonly supported operations for handling binary trees with the function of each operation. 116. Write three common uses of binary trees. 117. What is a binary search tree? Draw three valid binary search trees and three invalid binary search trees. For the invalid binary search trees, explain why each one is not a valid binary search tree. 118. Explain the process of inserting a new data element in a binary search tree with an example. 119. Write four commonly supported operations for handling binary search trees with the function of each operation. 120. Write three common uses of binary search trees.
■ Questions
335
121. What is a graph? Define the following terminologies used with graphs and explain them with suitable examples: (i) Loop (a) Directed edge (j) Connected graph (b) Undirected edge (k) Disjoint graph (c) Digraph (l) Complete graph (d) Undirected graph (m) Complete digraph (e) Weighted graph (n) Strongly connected digraph (f) Adjacent nodes (o) Subgraph (g) Path (p) Spanning tree (h) Cycle 122. Draw a graph having six nodes, seven edges, and three cycles. Now write the adjacency matrix representation of this graph. 123. Draw a digraph having five nodes, six edges, and two cycles. Now write the adjacency matrix representation of this graph. 124. Explain the breadth-first search method of graph search with an example. 125. Explain the depth-first search method of graph search with an example. 126. Write five commonly supported operations for handling graphs (undirected) with the function of each operation. 127. Write five commonly supported operations for handling digraphs with the function of each operation. 128. Write five common uses of graphs. 129. What is a heap? Differentiate between a max heap and a min heap. Draw an example of each. 130. Write three commonly supported operations for handling max heaps with the function of each operation. 131. Write three commonly supported operations for handling min heaps with the function of each operation. 132. Write three common uses of heaps.
Programming Languages
Once a developer/programmer has finished planning a computer program, he/she is now ready to write the steps of the corresponding algorithm in a programming language. Computer language or programming language is a language acceptable to a computer system, and the process of writing instructions in such a language is called programming or coding. This chapter introduces some popular programming languages.
ANALOGY WITH NATURAL LANGUAGES Language is a means of communication. We use a natural language, such as English, to communicate our ideas and emotions to others. Similarly, a programmer uses a computer language to instruct a computer what he/she wants it to do. All natural languages (English, French, German, etc.) use a standard set of words and symbols (+, -, :, ;, , etc.), which are understood by everyone using that language. The set of words allowed in a language is called its vocabulary. For example, the words used in English form the vocabulary of English language. Each word has a definite meaning. Similarly, every computer language has a vocabulary of its own. Each word in the vocabulary has a definite unambiguous meaning. The main difference between a natural language and a computer language is that natural languages have a large vocabulary, but most computer languages use a limited or restricted vocabulary. This is because a programming language by its very nature and purpose does not need to say much. Every problem to be solved by a computer has to be broken into discrete (simple and separate) logical steps comprising of four fundamental operations – input and output operations, arithmetic operations, operations for data movement within CPU and memory, and logical or comparison operations. Every natural language has a systematic method of using the words and symbols in that language, which is defined by the grammar rules of the language. Similarly, we need to use the words and symbols of a computer language as per the set rules, known as syntax rules, of the language. In case of a natural language, one can use poor or incorrect vocabulary and grammar and still communicate his/her thoughts. However, in case of a programming language, unless a programmer strictly adheres to the syntax rules of the language, even down to the punctuation marks, a computer will not understand his/her instructions.
■ Machine Language
337
Over the years, programming languages have progressed from machine-oriented languages (that use strings of binary 1s and 0s) to problem-oriented languages (that use common mathematical and/or English terms). However, all computer languages are broadly classified into the following three categories: 1.
Machine Language
2.
Assembly Language
3.
High-level Language
The nature and characteristics of these types of languages are described here.
MACHINE LANGUAGE Although we can design a computer to make it understand many computer languages, every computer understands only one language without using a translation program. This language is called its machine language. Normally, the machine language instructions of a computer are written as strings of binary 1s and 0s, and the circuitry of the computer converts them into electrical signals needed to execute them. A machine language instruction normally has a two-part format (see Figure 9.1). The first part is operation code that tells the computer what operation to perform, and the second part is operand that tells where to find or store the data on which the computer has to perform the operation. Every computer has a set of operation codes called its instruction set. Each operation code (or opcode) in the instruction set is meant to perform a basic operation or function. Typical operations included in the instruction set of a computer are: 1.
Arithmetic operations
2.
Logical operations
3.
Branch operations (either conditional or unconditional) for transfer of control to the address given in the operand field
4.
Data movement operations for moving data between memory locations and registers
5.
Data movement operations for moving data from input devices to memory, or from memory to output devices.
Figure 9.1 shows a typical single-address machine language instruction. Although some computers use only singleaddress instructions, many computers use multiple-address instructions that include addresses of two or more operands. For example, augend and addend may be the two operands of an addition operation. OPCODE (operation code)
OPERAND (address/location)
Figure 9.1. Instruction format.
All computers use binary digits (0s and 1s) for performing internal operations. Hence, most computers’ machine language instructions consist of strings of binary numbers. For example, a typical program instruction to print a number on a printer might be 101100111111010011101100 A program to add two numbers in memory, and print the result might appear as follows:
Chapter 9 ■ Programming Languages
338
001000000000001100111001 001100000000010000100001 011000000000011100101110 101000111111011100101110 000000000000000000000000
Obviously, this language is not easy to use because it uses a number system, with which we are not familiar. However, programmers who worked with the first few computers long ago, actually wrote their programs in binary form as this one.
Researchers soon found that without too much effort, a computer can be wired to accept the more familiar decimal numbers, which it can internally convert into binary before operating on them. With this change, the preceding program can be written as: 10001471 14002041 30003456 50773456 00000000
This set of instructions, whether in binary or decimal, which a computer can understand without the help of a translating program, is called machine code or machine language program. Hence, we need not necessarily code a machine language program as strings of binary digits (1s and 0s). We can also write it using decimal digits, if the computer’s circuitry permits this.
Advantages and Limitations of Machine Language A computer executes programs written in machine language at great speed because it can understand machine instructions without the need for any translation. However, writing a program in machine language has the following disadvantages: 1.
Machine dependent. As the internal design of one type of computer is different from other types, machine language differs from computer to computer. Hence, after the programmers of a company have become proficient in the machine language of one type of computer, if the company decides to change to another type, they will have to learn a new machine language, and would have to rewrite all existing programs.
2.
Difficult to program. It is difficult to write a program in machine language because the programmer has to either memorize the dozens of operation code numbers for the commands in the machine’s instruction set, or refer to a reference card constantly. The programmer must also keep track of storage locations of data and instructions for which he/she must be knowledgeable about the hardware structure of the computer.
3.
Error prone. Since a programmer has to remember the opcodes and must keep track of storage locations of data and instructions, it is difficult for him/her to concentrate fully on the logic of the problem, resulting in programming errors. Moreover, due to lack of any immediate visual indication of an error as the programmer writes machine language code, it makes removal of such errors difficult.
4.
Difficult to correct or modify. It is difficult to correct machine language programs because checking machine instructions to locate errors is difficult and time consuming. Similarly, modifying a machine language program later is so difficult that many programmers prefer to code the new logic afresh, instead of incorporating modifications in the old program.
Due to these limitations, present day programmers rarely use machine language for writing programs.
■ Assembly Language
339
ASSEMBLY LANGUAGE Programming in machine language is difficult and error-prone because a programmer needs to: 1.
Write numeric codes for the instructions in a computer’s instruction set.
2.
Write the storage locations of data and instructions in numeric form.
3.
Keep track of storage locations of data and instructions while writing a program.
Assembly language programming, introduced in 1952, helped in overcoming these limitations of machine language programming in the following manner: 1.
By using alphanumeric mnemonic codes instead of numeric codes for the instructions in the instruction set. For example, using ADD instead of 1110 (binary) or 14 (decimal) for the instruction to add, SUB instead of 1111 (binary) or 15 (decimal) for the instruction to subtract, and so on. With this feature, programmers can easily remember and use the instructions in the instruction set.
2.
By allowing use of alphanumeric names instead of numeric addresses for representing addresses of fixed storage locations. For example, we may choose to represent memory locations 1000, 1001, and 1002 as FRST, SCND, and ANSR, respectively in an assembly language program. With this feature, a programmer can easily remember and use the storage locations of the data and instructions used in an assembly language program. This was the same as using memory variables in programming languages.
3.
By providing additional instructions, called pseudo-instructions, in the instruction set for instructing the system how we want it to assemble the program in computer’s memory. For example, there may be pseudoinstructions for telling the system things like: START START SET SET SET
PROGRAM DATA ASIDE ASIDE ASIDE
AT AT AN AN AN
0000 1000 ADDRESS ADDRESS ADDRESS
FOR FOR FOR
FRST SCND ANSR
With this feature, a programmer need not keep track of storage locations of the data and instructions while writing an assembly language program. That is, he/she need not even tell the computer where to place each data item or instruction of a program. A language that allows use of letters and symbols instead of numbers for representing instructions and storage locations is called assembly language or symbolic language. A program written in an assembly language is called assembly language program or symbolic program.
Assembler Since a computer can directly execute only machine language programs, we must convert (translate) an assembly language program into its equivalent machine language program before it can be executed on the computer. A translator program called assembler does this translation. It is so called because in addition to translating an assembly language program into its equivalent machine language program, it also “assembles” the machine language program in main memory of the computer, and makes it ready for execution. As Figure 9.2 shows, input to the assembler is an assembly language program (often referred to as source program), and its output is the equivalent machine language program (often referred to as object program). Since the assembler translates each assembly language instruction into an equivalent machine language instruction, there is a one-to-one correspondence between the assembly language instructions of a source program and the machine language
340
Chapter 9 ■ Programming Languages
instructions of its equivalent object program. Note that during the process of translation of a source program into its equivalent object program by the assembler, the source program is not under execution. The assembler only converts it into a form that the computer can execute. Assembly language program
Input
Assembler
Output
Machine language program
One-to-one correspondence (Source program)
(Object program)
Figure 9.2. Illustrating the translation process of an assembler.
Due to the additional translation process involved, the computer has to spend more time in getting the desired result from an assembly language program as compared to a machine language program. However, assembly language programming saves so much time and effort of a programmer that the extra time and effort spent by the computer is worth it. Translation is a one-time process that requires relatively small time. Once the system obtains the object code, it can save it and subsequently execute it as many times as required. An example can best illustrate the process. Let us assume that a computer uses the mnemonics given in Figure 9.3 for the operation codes mentioned against each. For simplicity, out of more than hundred operation codes available in the instruction set of a computer, we have considered here only five for writing our example program. Mnemonic
Opcode
Meaning
HLT
00
Halt, used at the end of a program to stop Clear and add into A register
CLA
10
ADD
14
Add to the contents of A register
SUB
15
Subtract from the contents of A register
STA
30
Store A register
Figure 9.3. A subset of the set of instructions supported by a computer.
Figure 9.4 shows a simple assembly language program for adding two numbers and storing the result. To get an idea of how the assembler converts this program into an equivalent machine language program, let us follow its instructions one-by-one. Notice that the first five instructions of the program are pseudo-instructions for telling the assembler what to do. They are not part of the main program to add the two numbers. START START SET SET SET CLA ADD STA HLT
PROGRAM DATA ASIDE ASIDE ASIDE FRST SCND ANSR
AT AT AN AN AN
0000 1000 ADDRESS ADDRESS ADDRESS
FOR FOR FOR
FRST SCND ANSR
Figure 9.4. A sample assembly language program for adding two numbers and storing the result.
The first instruction of the assembly language program tells the assembler that the instructions for the main program (to add two numbers) should start at memory location 0000. Based on this directive, the assembler loads the first instruction of the main program (which happens to be CLA FRST in this example) at memory location 0000. It loads
■ Assembly Language
341
each subsequent instruction in the following memory location (that is, ADD SCND at location 0001, STA ANSR at location 0002, and HLT at location 0003). The second instruction of the assembly language program tells the assembler that the data of the program should start at memory location 1000. The next three instructions tell the assembler to set aside addresses for data items FRST, SCND, and ANSR. Based on these four directives, the assembler sets up a mapping table somewhere in the computer memory that looks somewhat like the one in Figure 9.5. That is, the assembler picks up the first free address in the data area of the program, which is location 1000 and calls it FRST. It picks up the next free address in the data area, which is location 1001, and calls it SCND. Finally, it picks up the next free address in the data area, which is location 1002, and calls it ANSR.
Symbolic name
Memory location
FRST
1000
SCND
1001
ANSR
1002
Figure 9.5. Mapping table set up by the assembler for the data items of the assembly language program of Figure 9.4.
The next instruction of the assembly language program is CLA FRST, which the assembler translates into 10 1000, by translating CLA into 10 as in Figure 9.3, and FRST into 1000 as in Figure 9.5. Similarly, the assembler translates the instructions ADD SCND into 14 1001, STA ANSR into 30 1002, and HLT into 00, thus, providing the resulting machine language program of Figure 9.6.
Advantages of Assembly Language over Machine Language Assembly languages have the following advantages over machine languages: 1.
Easier to understand and use. Due to the use of mnemonics instead of numeric op-codes and symbolic names for data locations instead of numeric addresses, assembly language programs are easier to understand and use than machine language programs.
2.
Easier to locate and correct errors. Due to the use of mnemonic op-codes and symbolic names for data locations, and also because programmers need not keep track of storage locations of the data and instructions, fewer errors are made while writing programs in assembly language, and those that are made are easier to find and correct. Additionally, assemblers detect and indicate errors automatically for use of an invalid mnemonic op-code or an undefined name. For example, if an assembly language program instruction reads ADD AREA and the programmer has not defined AREA in the program, the assembler will indicate the error after looking through its table and not finding AREA in it.
3.
Easier to correct or modify. Since they are easier to understand, it is easier to locate, correct, and modify instructions of an assembly language program than a machine language program. Moreover, insertion or removal of certain instructions in a program does not require change in the address part of the instructions following that part of the program. This is required in case of machine language.
4.
No worry about addresses. Assembly language programmers need not keep track of storage locations of data and instructions while writing an assembly language program. For example, let us assume that we have written a long machine language program involving many instructions and several references to itself within the program such as looping, branching, and so on. Towards the end of the program, we discover that we have left out an instruction in the middle. If we insert that instruction, we will have to go through the entire program to check and modify (if necessary) all references to other instructions. This is a tedious job. However, if we write the same program in assembly language, we merely add the extra instruction, and the assembler takes care of modifying the references automatically.
Chapter 9 ■ Programming Languages
342
Memory location
Contents Opcode Address
0000
10
1000
0001
14
1001
0002
30
1002
0003
00
Comments Clear and add the number stored at FRST to A register Add the number stored at SCND to the contents of A register Store the contents of A register into ANSR Halt
1000
Reserved for FRST
1001
Reserved for SCND
1002
Reserved for ANSR
Figure 9.6. Equivalent machine language program for the assembly language program of Figure 9.4.
5.
Easily relocatable. Availability of pseudo-instructions for instructing the system how we want it to assemble a program inside the computer’s memory makes assembly language programs easily re-locatable. This is because we can easily change a program’s location by changing the pseudo-instructions. An example can best illustrate this feature. Let us assume that an assembly language program starts at address 1000, and we suddenly find that this program uses another program, which also starts at location 1000. Obviously, we will have to rewrite one of the two programs to move it to some other location. While this is a complicated task in machine language, in assembly language, we merely have to change the pseudo-instructions for assembling the program for one of the two programs. For example, change the following pseudo-instructions: START PROGRAM AT 1000 START DATA AT 2000 to the following pseudo-instructions: START PROGRAM AT 3000 START DATA AT 4000 and run the assembly language program once again through the assembler. The equivalent machine language program will this time start at memory location 3000 instead of 1000, and there will be no conflict with the other program.
6.
Efficiency of machine language. An assembly language program also enjoys the efficiency of its corresponding machine language program because there is one-to-one correspondence between the instructions of an assembly language program and its corresponding machine language program. Hence, leaving out the translation time required by the assembler, the execution time for an assembly language program and its equivalent machine language program (written independently) would be more or less the same.
Limitations of Assembly Language Assembly language does not help in overcoming the following limitations of machine language: 1.
Machine dependent. Since the assembler translates each instruction of an assembly language program into one machine language instruction, assembly language programs are machine dependent. That is, assembly languages differ from computer to computer, and we can execute an assembly language program only on the
■ Assembly Language
343
computer, in whose assembly language is the program’s code. Hence, a decision to change to another computer will require learning a new language and conversion of all existing programs into assembly language of the new computer. .
2.
Knowledge of hardware required. Since assembly languages are machine dependent, an assembly language programmer must have knowledge of the characteristics and logical structure of his/her computer to write assembly language programs.
3.
Machine level coding. In case of an assembly language, we still have to write instructions at machine-code level. That is, one assembly language instruction corresponds to one machine language instruction. Hence, like machine language programs, writing assembly language programs is also time-consuming and difficult.
Typical Uses of Assembly Language Programmers rarely write programs of any significant size in assembly language today. They write assembly language code only when a program’s execution efficiency is important. Assembly language programming helps in producing compact, fast, and efficient code because it gives the programmer total control of the computer’s CPU, memory, and registers. Hence, programmers use assembly language programming mainly to fine-tune important parts of programs written in a higher-level language. High-level language programs that are candidates for this type of fine-tuning are those whose execution consumes a substantial part of the total computing resources, and hence offer real payoffs for any improvement in execution time. In such circumstances, a clever programmer can rewrite some small but critical section of the high-level language program in assembly language to improve its overall performance.
Assembly Languages with Macro Instructions In general, each instruction of an assembly language program when translated, results in one machine language instruction. However, quite often, a program uses a set of machine language or assembly language instructions repeatedly. For example, suppose we need three instructions, always in the same order, whenever we need to print a value. Instead of forcing a programmer to write the three instructions every time he/she wants to print a value, we might as well design the assembler (program) to substitute these three instructions for a single instruction, say PRINT. Every time the assembler encounters the PRINT instruction in a program, it would translate it into three machine language instructions instead of one, hence, supplying the complete set of instructions required for printing. Any instruction, such as PRINT, which an assembler translates into multiple machine language instructions, is called macro instruction. There might be many such macro instructions permitted by an assembler to speed up coding process. The use of macro instructions adds more work to a computer because the assembler must be able to supply the missing instructions as well, but it means a tremendous saving of work for a programmer. The programmer is relieved of the task of writing an instruction for every machine operation performed. Hence, the use of macro instructions reduces the length of programs, cuts down on errors, and simplifies programming. The macro instruction capability was developed early in the evolution of computer languages. In fact, it is this concept of multiple machine instructions from one macro instruction around which today’s machine-independent, high-level languages are designed.
Chapter 9 ■ Programming Languages
344
HIGH-LEVEL LANGUAGE Both machine and assembly languages have the following limitations: 1.
They are machine dependent. We cannot execute a machine/assembly language program on any computer other than the one for which it was written.
2.
They require programmers to have a good knowledge of the internal structure of the computer used.
3.
It is difficult, error prone, and time consuming to write programs in machine/assembly language because they deal with machine-level coding and require us to write one instruction for each machine-level operation.
Due to these limitations, we often refer to machine and assembly languages as low-level programming languages. High-level programming languages overcome these limitations of low-level programming languages. They have the following features: 1.
They are machine independent. We can easily port and execute a program written in a high-level language on any computer having the translator software for the high-level language.
2.
A programmer need not have knowledge of the internal structure of the computer on which a high-level language program written by him/her is executed. This allows programmers to concentrate on the logic of the problem rather than be concerned with the details of the internal structure of the computer. In fact, since highlevel languages are machine independent, a programmer writing a program in a high-level language may not even know which computer will execute the program.
3.
They deal with high-level coding, instead of machine-level coding, enabling the programmers to write instructions using English words and familiar mathematical symbols and expressions. High-level coding enables use of a single instruction for several machine language instructions. For example, let us consider the same problem of adding two numbers (FRST and SCND), and storing the sum in ANSR. We saw earlier that three low-level (machine/assembly) instructions are required to perform this job. However, if we use a highlevel language, only one instruction is needed to instruct the computer to do this job: ANSR = FRST + SCND This instruction is easy to understand and write because it resembles the familiar algebraic notation for adding two numbers: a = b + c.
Advent of high-level languages has enabled many users without any background in computer science and engineering to become computer programmers. This has resulted in the creation of a large number of computer applications in diverse areas.
Compiler A compiler is a translator program (more sophisticated than an assembler) that translates a high-level language program into its equivalent machine language program. It is so called because it compiles a set of machine language instructions for every program instruction of a high-level language. As Figure 9.7 shows, input to the compiler is a high-level language program (often referred to as source program), and its output is the equivalent machine language program (often referred to as object program). High-level language instructions are macro instructions. The compiler translates each high-level language instruction into a set of machine language instructions rather than a single machine language instruction. Hence, there is a one-to-many correspondence between the high-level language instructions of a source program and the machine language instructions of its equivalent object program. Note that during the process of translation of a source program into its
■ High-Level Language
345
equivalent object program by the compiler, the source program is not under execution. The compiler only converts it into a form that the computer can execute. Input
High-level language program
(Source program)
Compiler
Output
One-to-many correspondence
Machine language program
(Object program)
Figure 9.7. Illustrating the translation process of a compiler.
A compiler can translate only those source programs that are written in the language, which the compiler can translate. For example, a FORTRAN compiler can translate only source programs written in FORTRAN. Therefore, a computer requires a separate compiler for each high-level language that it supports. For example, to be able to execute both FORTRAN and COBOL programs, a computer must have both FORTRAN and COBOL compilers. Figure 9.8 illustrates this.
Program P1 in high-level language L1
Compiler for language L1
Machine code for P1
Program P2 in high-level language L2
Compiler for language L2
Machine code for P2
A computer supporting languages L1 and L2 Figure 9.8. Illustrating the requirement of a separate compiler for each high-level language supported by a computer.
Also, note that an object program for one computer may not be the same as the object program for another computer. Hence, each computer must have its own “personal” compiler for a language, say L1. Figure 9.9 illustrates how we can achieve machine-independence by using different compilers to translate the same high-level language program to machine language programs of different computers. Compilers are large programs residing permanently on secondary storage. To translate a source program, a computer first loads the compiler and the source program from secondary storage into its main memory. The computer then executes the compiler (which is also a program) with the source program as its input data. It generates the equivalent object program as its output, which the computer saves in a file on secondary storage. To execute the program, the computer loads the object program into main memory from secondary storage and executes it. Note that, there is no need to repeat the compilation process every time we wish to execute the program. This is because the object program stored on secondary storage is already in machine language, which we can load into main memory from the secondary storage and execute it whenever required. Also, note that compilation is necessary whenever we modify the program. That is, to incorporate changes in the program, we must load the original source program into main memory from secondary storage, carry out necessary changes in the source program, recompile the modified source program, and create and store an updated object program for execution.
Chapter 9 ■ Programming Languages
346
Compiler for language L1 on computer A
Machine code for P1 that will run on computer A
Program P1 in highlevel language L1
Executed on computer A Same results obtained
Compiler for language L1 on computer B
Machine code for P1 that will run on computer B
Executed on computer B
Figure 9.9. Illustrating the machine independence characteristic of a high-level language. Separate compilers are required for the same language on different computers.
In addition to translating high-level language instructions into machine language instructions, compilers also detect and indicate certain types of errors in source programs. We refer to these errors as syntax errors. They are of the following types: 1.
Illegal characters
3.
Improper sequencing of instructions
2.
Illegal combination of characters
4.
Use of undefined variable names
If a compiler detects one or more errors in a source program, it will not compile it into an object program. In this case, the compiler generates a list of coded error messages indicating the type of errors committed. The programmer uses this error list to re-edit the source program for removing the errors, and creates a modified source program for recompilation (see Figure 9.10). As the figure shows, we have to repeat the process of editing the source program to make necessary corrections and recompiling the modified source program until the source program is free of all syntax errors. Also, notice from the figure that the compiler generates the object program only when the source program is free of all syntax errors. A compiler, however, cannot detect logic errors. It can only detect grammatical (syntax) errors in the source program. It cannot know ones intentions. For example, if one has wrongly entered 35 instead of 45 as the value for second division percentage to be compared with Percentage, or 999999 (only six 9s) instead of 9999999 (seven 9s) as the sentinel value for Rollno, the compiler cannot detect this.
START Edit source program Source program Compile source program Syntax No Generate errors detected? object program Yes Generate list of coded error messages
Object program STOP
Figure 9.10. Illustrating recompilation of a source program, until it is free of syntax errors.
A compiler successfully compiles programs containing such errors, and generates the object code for it without generating any error message. However, such programs, when executed, do not produce correct result or do not behave as expected. Hence, we can detect logic errors in a program only after we execute the program, and find that the result does not tally with the desired result, or the program does not behave as expected.
■ High-Level Language
347
Linker A single programmer can write a small program and can store it in a single source code file. However, large size software often consist of several thousands, even several millions, of lines of program code. For such software, it is impractical to store its entire source code in a single file due to the following reasons: 1.
Large size of the file makes it difficult, if not impossible, to work with. For example, loading the file for compilation on a computer with limited main memory capacity may not be possible. In addition, editing the file may be tedious and time-consuming.
2.
Deploying multiple programmers to work concurrently for developing the software within specified time becomes difficult.
3.
We need to recompile the entire source program whenever we make any change in the source program, no matter how small. Recompilation of large source programs is often a time-consuming process.
To take care of these problems, software developers adapt a modular approach to develop any reasonably sized software. In this approach, they divide the software into functional modules and write separate source programs for each module. Often, they do not need to write source programs for some of the modules because they can find programs offering same functionalities in an already available program library. Software developers maintain these library programs in their object code form. In modular approach of software development, software consists of multiple source program files. We can modify and compile each source program file independent of other source program files to create a corresponding object program file. In this case, we use a program called linker to combine all object program files (modules) of the software, and to convert them into a final executable program, which is sometimes called load module. Hence, a linker is software that takes multiple object program files (modules) of some software and fits them together to assemble them into the software’s final executable form. Figure 9.11 illustrates the process of compiling multiple source program files of various modules of software and linking them to create its executable program (load module). Note that since converting the source programs into an executable program is a two-step process, when we modify the software, we need not recompile all the source programs. Rather, we need to recompile only the changed source programs to produce new object programs for them. We can then use the linker to create a new executable program by reassembling the newly created and the old object programs and library modules.
Compilation process
Prog-1.src
Prog-2.src
Prog-n.src
Source programs
Prog-1.obj
Prog-2.obj
Prog-n.obj
Object programs
Prog-i.lib
Library modules
Linking process Executable program file
Load module
Figure 9.11. Illustrating the process of compiling multiple source program files and linking them to create the executable program (load module).
Linkers are part of the development environment (which consists of a set of program development tools including compilers) of a computer system. One can also purchase third-party linkers that can sometimes improve the performance of a program over the standard linkers that are part of the system’s development environment.
348
Chapter 9 ■ Programming Languages
Interpreter Interpreter is another type of translator used to translate a high-level language program into its equivalent machine language program. It takes one statement of the high-level language program, translates it into machine language instructions, and executes the resulting machine language instructions immediately, before taking up the next statement. Hence, the translation and execution processes alternate for each statement encountered in the high-level language program. This differs from a compiler that merely translates the entire source program into an object program, and is not involved in its execution. The input to an interpreter is a source program, but unlike a compiler, its output is the result of program execution, instead of an object program (see Figure 9.12). Input Interpreter (translates and executes Output Result of program High-level language program statement-by-statement) execution (Source program) Figure 9.12. Illustrating the role of an interpreter.
After compilation of a source program, a user saves the resulting object program for future use, and uses it whenever he/she has to execute the program. Hence, repeated compilation (translation of the source code) is not necessary for repeated execution of a program. However, in case of an interpreter, since a user cannot save the object program for future use, repeated interpretation (translation plus execution) of a program is necessary for its repeated execution. Since an interpreter translates and executes a high-level language program statement-by-statement, it reinterprets (translates and executes) a program statement every time it encounters that statement during program execution. For example, during execution of a program, it reinterprets the instructions in a loop as many times as the process executes the loop. As compared to compilers, interpreters are easier to write because they are less complex programs than compilers. They also require less memory space for execution than compilers require. The main advantage of interpreters over compilers is that an interpreter flags a syntax error in a program statement to a programmer as soon as it interprets the program statement. This allows the programmer to make corrections during interactive program development. Therefore, interpreters make it easier and faster to correct programs. The main disadvantage of interpreters over compilers is that they are slower than compilers when running a finished program. This is because an interpreter translates each statement from the source program every time it executes it. A compiler translates each statement only once and saves it in the object program. We can execute the saved object program many times whenever needed, and no translation of any statement is required during the execution of the program. As the interpreter does not produce an object code for a program, it must perform the translation process each time it executes the program. To combine the advantages of both interpreters and compilers, sometimes, the program development environment of a computer system provides both a compiler and an interpreter for a high-level language. In such systems, a programmer first uses the interpreter to develop and debug his/her program. Then, after reaching a bug-free state, he/she compiles the program to obtain its object code and uses the object code for routine processing subsequently. We also refer to assemblers, compilers, and interpreters as language processors since they process language instructions.
Intermediate Language Compiler and Interpreter Yet another type of compiler and interpreter combination combines the speed, ease, and control of both compiler and interpreter in the same package and provides other features such as machine independence.
■ High-Level Language
349
In this approach, the compiler first compiles the source program to an intermediate object program. This intermediate object program is not a machine language code but a program written in an intermediate language that is virtually machine independent and understood by the combination interpreter. Then the interpreter takes the intermediate object program and converts it into machine language program and executes it on the desired computer (often referred to as host computer as it ‘hosts’ the virtual execution environment). The main benefits of this approach are: 1.
The intermediate object program is in compiled form and thus is not original source code. This makes it safer and easier for a programmer to share his/her programs with others when the programmer does not want to share the source code.
2.
The intermediate object program is in the form of a standard Intermediate Definition Language (IDL). Hence, writing a compiler and an interpreter is easier.
3.
We can write the interpreter for any computer architecture and operating system as long as it can interpret the intermediate object program on the host computer system. This provides virtual machine environment to the executing program.
4.
Another type of interpreter is available that can convert (or compile) the complete or selected sections of intermediate object program, in memory, to final host machine language program and then start execution at a faster speed as compared to line-by-line execution. This technique is called Just-In-Time (JIT) Compilation.
Advantages and Limitations of High-level Languages Advantages of High-level Languages High-level languages enjoy the following advantages over assembly and machine languages: 1.
Machine independence. We can execute a program written in a high-level language on different types of computers with little or practically no effort of porting it on different computers. This means that a company changing its computers, even to one from a different manufacturer, need not rewrite all the programs that it is currently using. This also means that commercial software will have a larger market because it need not be restricted to one type of computer. Hence, developers receive better reward for the time and effort spent on software developed with high-level language programming.
2.
Easier to learn and use. High-level languages are easier to learn because they are similar to the natural languages used by us in our day-to-day life. They are also easier to use because a programmer need not know the internal details of a computer for programming in a high-level language.
3.
Fewer errors. While programming in a high-level language, a programmer need not worry about how and where to store the instructions and data of the program, and need not write machine-level instructions for the computer. This allows the programmer to concentrate more on the logic of the program under development. All these factors lead to fewer programming errors during program development. Furthermore, compilers and interpreters detect and indicate syntax errors automatically. Hence, a programmer can easily locate and correct syntax errors, if any, in a program.
4.
Lower program preparation cost. Writing programs in high-level languages requires less time and effort, leading to lower program preparation cost. Generally, the cost of all phases of program preparation (coding, debugging, testing, etc.) is lower with a high-level language than with a machine/assembly language.
5.
Better documentation. Statements of a program written in a high-level language are similar to natural language statements used by us in our day-to-day life. Hence, a programmer who is familiar with the problem domain can understand them easily. As a result, only few comment statements are required in programs written in high-level languages. Due to this reason, we sometimes refer to high-level languages as selfdocumenting languages.
6.
Easier to maintain. Programs written in high-level languages are easier to maintain than assembly/machine language programs. This is because they are easier to understand, and hence, it is easier to locate, correct, and
Chapter 9 ■ Programming Languages
350
modify instructions whenever desired. We can even insert/remove instructions in/from a program without any complication. Hence, we can incorporate major changes in a program with little effort.
Limitations of High-level Languages The two main limitations of high-level languages are: 1.
Lower efficiency. Generally, a program written in a high-level language has lower time and space efficiency than one written in a machine/assembly language to do the same job. Hence, when a program’s efficiency is important, we write its performance critical parts in assembly language and non-critical parts in a high-level language. However, with the advent of efficient and optimizing compilers, this problem is now a non-issue.
2.
Less flexibility. Generally, high-level languages are less flexible than assembly languages because they do not normally have instructions or mechanism to directly access a computer’s CPU, memory, and registers. An assembly language provides the programmers access to all the special features of the machine they are using. Certain types of operations that a programmer can easily program by using the machine’s assembly language are impractical to attempt by using a high-level language. This lack of flexibility means that a programmer cannot code some tasks or can code it only with great difficulty in a high-level language.
In most cases, the advantages of high-level languages far outweigh the disadvantages. Hence, software developers use high-level languages for writing most programs, and use assembly languages for doing special tasks only, which they cannot do easily otherwise.
OBJECT-ORIENTED LANGUAGES What is Object-Oriented Programming (OOP)? Soon after the advent of the first few programming languages, software industry realized that programming is a difficult and time-consuming task. Hence, researchers have been continuously working towards developing better and better programming languages to make programming simpler, easier, and faster. The goal of the designers of every new programming language was to introduce some aspects or concepts in their programming language that will help to achieve the above-mentioned objectives. Object-Oriented Programming (often referred to as OOP, pronounced “oop”) is one such concept used in programming languages. Developers of Simula-67 programming language first introduced the concept of OOP in 1967. This concept started gaining popularity in 1980s with the development of another programming language named Smalltalk that made extensive use of this concept. Starting in mid-1980s, OOP became a popular term and object-oriented dialects of existing programming languages (like Object-Pascal, Objective C, and C++) started appearing. Subsequently, C++ became a popular object-oriented programming language because of its connection with C and its support by AT&T Bell Labs. Almost all popular programming languages (including Visual Basic and Java) now use OOP philosophy. The basic idea behind OOP is that we use programming languages for simulating real-world problems on computers. Since much of the real world is made of objects, a simulation of such a world must include simulated objects. Hence, the essence of OOP is to solve a problem by identifying the real-world objects of the problem and the processing
■ Object-Oriented Languages
351
required of those objects, and then creating simulations of those objects, their processes, and the required communications between the objects.
Fundamental Concepts of OOP The most essential part of support for OOP is the object mechanism. That is, an OOP language must have mechanisms to define, create, store, and manipulate objects, and some mechanism to allow different objects to communicate with each other. The basic concepts used in OOP languages to support the object mechanism are described here.
Object An object is the primitive element of a program written in an OOP language. Each object consists of a set of procedures (known as methods) and some data. It stores the data in variables and responds to messages received from other objects by executing the procedures (methods). A method is a collection of operations that specifies the reaction of the object when it receives a message that corresponds to that method. A program written in a pure OOP language is a collection of objects that communicate with each other. Virtually everything, from items as simple as an integer constant ‘8’ to a complex file-handling system, is an object. An OOP language treats them all uniformly as objects. They all have local memory, inherent processing ability, the capability to communicate with other objects, and the ability to inherit characteristics from ancestors. We can view an object as an abstract data type because each object is an autonomous entity with its private data and methods. Note that an object contains both functions (procedures/methods) and data that describe the characteristics of the object. In other words, an object encapsulates both data and functions.
Method A method of an object defines the set of operations that the object will execute when it receives a message corresponding to the method. Methods are like function definitions, complete with parameters and the capability to return values. Binding of a message to a method is dynamic. The entire collection of methods of an object is called the message protocol, or message interface, of the object. Figure 9.13 illustrates the object model used in OOP.
Method – 1 Object’s message interface
Method – 2 Method – 3
Object’s private data Implementations of Method – 1, Method – 2 and Method – 3
Figure 9.13. Model of an object used in OOP.
Message The mechanism to support communication between objects is through messages. That is, OOP language programs do any computing by sending a message to an object to invoke one of its methods. A reply to a message is an object, which returns to the sender the result of executing the method. There is a fundamental difference between a message sent to an object and a subprogram call. In the former case, the object executes the appropriate method with its own local data, whereas in the latter case, the caller process sends the data for processing (if any) to the subprogram code unit along with the subprogram call.
352
Chapter 9 ■ Programming Languages
Class A class is a description of one or more similar objects. Class concept is similar to “type” in conventional procedureoriented languages. For example, if “integer” is a type (class), then “8” is an instance (object) of type (class) “integer”. Similarly, “Ram” and “Sita” are two instances (objects) of a class “Person”. A class can have multiple instances. Each instance of a class is known as an object of the class. Each object has its own local data and represents a different instance of its class. Since all objects of a class share the same methods, the only difference between two objects of the same class is the state of their local variables. Also, note that there can be two categories of object variables – class variables and instance variables. Class variables are variables stored in the class. All instances (objects) of the class share its class variables. Instance variables are variables for which local storage is available in the instances (objects). Object-oriented programming languages also support class hierarchies. Sub-classes of a given class are refinements of it, inheriting the functionality and local variables of the parent-class, or super-class. Sub-classes can add new local variables and methods, and can modify or hide inherited methods.
Inheritance In OOP, inheritance is a mechanism to share code and behavior. It allows a programmer to reuse the behavior of a class in the definition of new classes. A program written in an object-oriented language may have a large hierarchy of classes. In fact, good programming in an object-oriented language depends largely on creating sub-classes of existing classes, because this leads to potential reuse of existing code. In a hierarchy of classes, a sub-class inherits all of the instance variables, instance methods, and class methods of its super-class/parent-class. The sub-class can also have its own instance variables, which must have different names than the variable names in its ancestor classes. Finally, the sub-class can define both new methods and methods that already exist in an ancestor class. When a sub-class has a method whose name or protocol is the same as an ancestor class, the sub-class method hides that of the ancestor class. Normally, a user has to prefix the message with the pseudo-variable “super” to access such a hidden method. This causes the method search to begin in the super-class rather than locally. Inheritance need not get limited to one parent-class. A child class can inherit from multiple parent classes. This is called multiple inheritance. Figure 9.14 describes persons by an inheritance hierarchy of classes (representing behaviors). “Person” class has “Male” and “Female” as its sub-classes. “Male” class has “Person” as its super-class and “Boy” and “Man” as its subclasses. Each instance (Ram, Shyam, Mohan, Seeta, and Geeta) has a unique base class. Person Male
Classes Boy
Instances
Ram
Female Man
Shyam
Mohan
Seeta
Geeta
Figure 9.14. Example of an inheritance hierarchy of classes.
Figure 9.15 illustrates the concepts of object, class, and inheritance with the help of an example. In the example, object ‘a’ is an instance of class ‘A’ and object ‘b’ is an instance of class ‘B’. Both A and B define behavior by operations shared by their instances, and have instance variables that cause a private copy to be created for each
■ Object-Oriented Languages
353
instance for the class or sub-class. Instance ‘a’ of class ‘A’ has a copy of A’s instance variables and a pointer to its base class. Instance ‘b’ of class ‘B’ has a copy of the instance variables of both class ‘B’ and its super-class ‘A’ and a pointer to the base class of ‘B’. Representation of class ‘B’ has a pointer to its super-class ‘A’, while class ‘A’ has no super-class pointer, since it has no super-class. When object ‘b’ receives a message to execute a method, it looks first in the methods of class ‘B’. If it finds the method in class ‘B’, it executes it using the instance variables of ‘b’ as data. Otherwise, it follows the pointer to its super-class. If it finds the method in class ‘A’, it executes it on the data of ‘b’. Otherwise, it searches A’s super-class if there is one. If it did not find the method in class ‘A’ and if class ‘A’ has no super-class, it reports an error. Object ‘a’ (Instance of ‘A’)
Class ‘A’ Variables of ‘A’
Class ‘A’
Methods of ‘A’
Instance variables of ‘A’
Class ‘B’ (Sub-class of Class ‘A’)
Object ‘b’ (Instance of ‘B’)
Inherits from ‘A’
Class ‘B’
Variables of ‘B’
Inherited instance variables of ‘A’
Methods of ‘B’
Instance variables of ‘B’
Figure 9.15. An example illustrating the concepts of object, class, and inheritance.
Key Elements of Object-Oriented Paradigm Figure 9.16 summarizes the key elements that a programming language must have for categorization as an objectoriented language. Sr. No.
Element
Meaning
1
Abstraction
Abstraction means that an object’s characteristics are broken into manageable chunks as defined and documented in its class description.
2
Encapsulation
Encapsulation stipulates that code and data are stored as one unit. Encapsulation also enables selective or total information hiding, since it can make portions of the code and data inaccessible from outside the unit.
3
Modularity
Modularity defines the unit reuse. These units group abstractions together.
4
Hierarchy
Hierarchy allows an object’s behaviors to be refined (subclasses) without recoding of the parent object (the superclass). Some OO languages allow an object to have more than one superclass, a feature that is known as multiple inheritance. Inheritance hierarchies enable ranking/ordering of abstractions.
5
Messages
Messages, similar in use to function calls, are requests to perform an operation on an instantiated object. Figure 9.16. Key elements of object-oriented paradigm.
354
Chapter 9 ■ Programming Languages
Procedure-oriented versus Object-oriented Programming Conventional procedure-oriented programming languages use procedural abstractions that encapsulate sequences of operations into procedures (also known as functions, subroutines, or subprograms). On the other hand, object-oriented programming languages encapsulate data as well as sequences of operations into abstract entities called objects, providing a stronger encapsulation mechanism than procedures. Hence, object-oriented programming (OOP) provides a more powerful modeling tool than procedure-oriented programming (POP). Notice that we can consider both procedures and objects to be server modules that clients may call to get certain actions performed. However, the basic difference between the two is in their roles. The role of procedures is to transform input data specified by parameters into values, while the role of objects is to serve as a repository of data (current system state) and to respond (perform the requested action) in a manner determined by the current system state. For example, response of a bank to a withdrawal request depends on the value of current balance. Call Procedure-2 Procedure-1 Algorithm parameters
Receive Return parameters
Procedure-2
Object-1
Object-2
Algorithm parameters
Attributes
Attributes
Methods M11, M12
Methods M21, M22
Receive/ return parameters
Send message 1 Object-3
Procedure-3
Send message 2
Algorithm Call Procedure-3 parameters (a) Communication among software modules in Procedure-Oriented Programming (POP)
Attributes Methods M31, M32
(b) Communication among software modules in Object-Oriented Programming (OOP)
Figure 9.17. Comparison of communication approaches in POP and OOP. In POP, procedures receive and return parameters, while in OOP, objects receive and send messages.
Figure 9.17 compares graphically the communication between modules in POP and OOP. In POP, procedures call each other to receive, return, or update data values through parameter passing, as shown in the left half of the figure. In OOP, a number of interacting objects send messages to one another, as shown in the right half of the figure. Each object responds to a prescribed collection of messages. Each data item can be an attribute of an object or an object and is capable of processing requests known as messages. Thus, the data item or objects are the active computation unit, while the methods or operations are passive. Also, observe that we can construct an object’s method in much the same way as we construct a procedure (function) for a structured program by using the same three basic building blocks: sequence, selection, and repetition. Procedure-oriented programming paradigm has strong organizing principles for managing actions and algorithms, but has weak organizing principles for managing shared data. Object-oriented programming paradigm has strong organizing principles both for managing actions and algorithms and for managing shared data because it provides the capability to restrict applicable operations to those associated with a specific object or class. Inheritance provides further strength to this capability by allowing classification of classes by factoring out properties common to several sub-classes into a super-class. Figure 9.18 provides a comparison of the features of POP and OOP.
■ Object-Oriented Languages Sr. No.
Procedure-Oriented Programming (POP) features
Object-Oriented Programming (OOP) features
1
Uses functional decomposition mechanism and modularizes a system based on functions.
Uses OO decomposition mechanism and modularizes a system based on data structures (objects).
2
In a program module (function), data and procedures are separate.
A program module (object) encapsulates both data and operations.
3
Programmers are responsible for calling the functions and for passing parameters (if any) to them.
Objects communicate with each other by passing messages to activate their operations.
4
Uses procedural abstraction and represents real world by logical entities and control flow.
Uses class and object abstraction, and represents real world by objects mimicking external entities.
5
Unit of structure is statement or expression.
Unit of structure is object treated as a software component.
6
Uses procedure-oriented languages, such as C or Pascal.
Uses OO languages, such as C++, Object Pascal, or Smalltalk-80.
Figure 9.18. Comparison of the features of POP and OOP.
Advantages of OOP Figure 9.19 lists some of the key advantages that OOP paradigm has over conventional POP paradigm. Sr. No.
Advantage
Description
1
Natural modeling
Designing and implementing a system is much easier in OOP because OOP paradigm serves as a more natural modeling tool for programming real world problems.
2
Modular design
In OOP, each object is an autonomous entity, which a programmer can define and implement independent of other objects. This facilitates modular system design. Modular design eases the development and maintenance of software systems.
3
Abstraction
In OOP, each object is an encapsulated entity because its data is private to itself (hidden from other objects) and its interaction with other objects is only through its message interface. OOP, therefore, provides a high degree of abstraction enabling programmers to concentrate more on problem specification rather than implementation details. Abstraction increases software reliability and modifiability by decoupling object specification and object implementation.
4
Dynamic binding
In conventional programming languages, operators, functions, etc. are bound to their respective operations at the time of compilation. This is called static binding. However, in OOP, binding of operator to an operation takes place at run time. This is called dynamic binding. Dynamic binding allows abstract data types to be truly generic because it enables a single operator to have more than one operation, which is called polymorphism. Polymorphism and dynamic binding allow creation of generic software components and new classes using existing code. Hence, they increase flexibility and reusability of code.
5
Code reusability
In OOP, inheritance provides an effective method of code reuse because all the classes and instances down to the whole class hierarchy can access the common operations defined and implemented in a class. This helps in tremendous saving of programming effort.
6
Easy maintenance
Modular design and code reusability features also help in making software systems easy to maintain. In OOP, if we make a change in a super-class, all its sub-classes change at once, without the need to identify and implement the change at a number of places. Moreover, if we make changes in one sub-class to get distinctions, the code in the super-class and the other subclasses remains safe.
Figure 9.19. Key advantages of OOP paradigm over conventional POP paradigm.
355
356
Chapter 9 ■ Programming Languages
SOME HIGH-LEVEL LANGUAGES Since the first high-level language in early 1950s, today there are more than 1000 high-level languages. Only a few of them have been truly significant, and even fewer have been widely used. Figure 9.20 lists some popular high-level languages. The primary objective here is to provide an overview of these languages. Readers interested in detailed knowledge required to write programs in any of these languages should refer to a good book dealing with the syntax and semantics of the language.
DEALING WITH SO MANY HIGH-LEVEL LANGUAGES Why So Many High-level Languages? Since high-level languages are machine independent, you must be wondering why so many high-level languages were developed, and why many of them are in use today, instead of developing and improving just one good high-level language. For an answer to this question, recall that one of the objectives of high-level languages is that they should be easier to learn and use by the intended users. We write computer programs to solve diversified class of problems, such as, systems-related problems, scientific and engineering problems, business and commercial problems, graphics problems, etc. The language statements and notations used in a particular problem domain may be quite different from those used in other problem domains. If we try to incorporate all types of statements and notations for solving all classes of problems in a single high-level language, the features of that high-level language will become too vast and difficult to master. Such a language will be difficult to learn and use. Moreover, designing an efficient compiler for such a language will also be a tedious task. Hence, it is more logical to have different high-level languages for different problem domains. The statements and notations used in a high-level language are picked up especially to be useful for solving a specific class of problems. For example, a scientist using a science-oriented language can use scientific formulae, and a businessman with a business-oriented language can use business terms. This makes a highlevel language, meant for a problem domain, easier to learn and use by the users dealing with problems in that problem domain. Hence, today many different high-level languages exist and are in use because each one was designed for a different purpose and area of applicability. This is also obvious from the discussion of the various highlevel languages in Figure 9.20.
■ Dealing With So Many High-Level Languages
357
358
Chapter 9 ■ Programming Languages
■ Dealing With So Many High-Level Languages
359
360
Chapter 9 ■ Programming Languages
■ Dealing With So Many High-Level Languages
361
Characteristics of a Good Programming Language There are some popular high-level languages, while there are others that could not become popular in-spite-of being powerful. There might be many reasons for the success of a language, but one obvious reason is its characteristics. Several characteristics believed to be important for a good programming language are: 1.
Simplicity. A good programming language must be simple and easy to learn and use. It should provide a clear, simple, and unified set of concepts to a programmer that he/she can grasp easily. The overall simplicity of a programming language affects the readability of the programs written in that language, and programs that are easier to read and understand are easier to maintain. It is also easy to develop and implement a compiler or an interpreter for a simple programming language. However, for achieving simplicity, designers should not sacrifice the power needed for the language. For example, many programmers like BASIC because of its simplicity.
2.
Naturalness. A good language for an application domain should be natural for programming applications of that domain. That is, it should provide appropriate operators, data structures, control structures, and a natural syntax to facilitate programmers to code their problems easily and efficiently. FORTRAN and COBOL are good examples of languages possessing high degree of naturalness in scientific and business application domains, respectively.
3.
Abstraction. Abstraction means the ability to define and then use complicated structures or operations in ways that allows programmers to ignore many of the details. The degree of abstraction allowed by a programming language affects its ease of programming. For example, object-oriented languages support high degree of abstraction. Hence, writing programs in object-oriented languages is much easier. Object-oriented languages also support reusability of program segments due to this feature.
4.
Efficiency. A program written in a good programming language enables a computer system to translate it into machine code efficiently, execute it efficiently, and manage it with less memory space. That is, a good programming language is supported with a good language translator (a compiler or an interpreter) that gives due consideration to space and time efficiency.
5.
Structured programming support. A good programming language should have necessary features to allow programmers to write their programs based on the concepts of structured programming. This property affects the ease with which a programmer can write, test, and maintain his/her programs. Moreover, it forces the programmer to look at a problem in a logical way so that he/she creates fewer errors while writing a program for the problem.
6.
Compactness. In a good programming language, programmers should be able to express the intended operations concisely without losing readability. Programmers do not like a verbose language because they need to write too much. Many programmers dislike COBOL because it is verbose in nature (lacks compactness).
7.
Locality. A good programming language should be such that while writing a program, a programmer need not jump around visually as the text of the program is prepared. This allows the programmer to concentrate on the part of the program around the statement he/she is currently working with. COBOL and to some extent C and Pascal lack locality because data definitions are separated from processing statements, perhaps by many pages of code, or have to appear before any processing statement in the function/procedure.
8.
Extensibility. A good programming language should also allow extension through simple, natural, and elegant mechanisms. Almost all languages provide subprogram definition mechanisms for this purpose, but some languages are weak in this aspect.
9.
Suitability to its environment. In addition to being suitable to its application domain, a good programming language must also be suitable to its operating environment. For example, a language for real-time applications must be interactive in nature. On the other hand, a language for data processing applications like payroll, stores accounting, etc., may operate in batch mode.
Chapter 9 ■ Programming Languages
362
Selecting a Language for Coding an Application Programmers often face the situation of selecting a programming language out of the many options available for coding an application. The following factors generally influence the selection process: 1.
Nature of the application. A programmer should select a language that is suitable for the application domain at hand. For example, FORTRAN is suitable for scientific and engineering applications, while COBOL is suitable for business applications.
2.
Familiarity with the language. If he/she finds that multiple languages are suitable for the application domain, he/she should select the one with which he/she and other programmers in his/her team (who will code the application) are most familiar.
3.
Ease of learning the language. If he/she finds that multiple languages are suitable for the application domain, and the programmers are not familiar with any of them, he/she should select the language that is easy to learn and use.
4.
Availability of program development tools. Before selecting a language, the programmer must also find out if the programming environment of the language has good program development tools like compiler, interpreter, debugger, linker, etc. A language with a good set of program development tools can significantly reduce the time and effort of the team of programmers involved in coding an application.
5.
Execution efficiency. If the execution efficiency of an application is important, the programmers can use an assembly language instead of a high-level language for coding the application. This is because an assembly language program written by a clever programmer usually has a shorter production run time, and takes less storage space than does a program written for the same application in a high-level language.
6.
Features of a good programming language. Finally, the features of a good programming language discussed in the previous section often influence the selection process.
OTHER TYPES OF LANGUAGES Scripting Languages We often use command statements to instruct a computer to do a job. For example, we use “run” or “exec” statement to execute a program and “copy” statement to copy a file from one storage device to another. Often we also need to execute a set of commands to perform a task. For example, to perform a task, a user may be required to execute two independent programs, and after execution of the first program, he/she may be required to copy the output file of the first program on to a device from which the second program expects the input. Therefore, in this case, the user needs to specify two execute commands for executing the two programs and a copy command before the second execute command. A scripting language is a programming language that allows a programmer to integrate several such commands into a single command so that the programmer need not type all the commands every time he/she needs to perform the intended task. We refer to a program written in a scripting language as a script. Scripting languages support variables and simple control structures to allow programmers to write scripts, which can execute conditionally or repeat the execution of certain commands. With these features, scripting languages are useful for defining complex commands execution patterns. Another common use of scripting languages is in specifying execution environment setup for applications that are quite complex, yet flexible enough for use in a variety of contexts. For an individual run of such an application, programmers must configure a setup to customize the application for its current run. A programmer can write a script in a scripting language to specify this setup. The script integrates individual commands into a single command that the programmer can use to drive application execution. A scripting language is interpreted, rather than compiled. Hence, a script can interact with a running program and change during the run. In addition to controlling a running program, scripts can preprocess and postprocess data.
■ Other Types of Languages
363
Scripting languages typically let programmers invoke individual application components. Application components are individual programs (such as data format converters and visualizers) that carry out specific data processing tasks and are usable in different contexts. Some commonly known scripting languages are Unix Shell Script, Perl, Tcl, and Python. A typical script looks like the one shown in Figure 9.21. users = ‘who wc –l’ if test $users –gt 10 then echo “More than 10 users logged on” fi Figure 9.21. A typical script. The first line of the script sets the variable ‘users’ equal to the value produced by the pipeline of commands ‘who I wc –l’. This is in fact a count of the number of users currently logged on to the system. This value is then compared with the number 10, and if it is greater, then the message is displayed.
Fourth and Fifth Generation Languages (4GLs and 5GLs) Some people prefer to categorize machine languages, assembly languages, and high-level languages as firstgeneration, second-generation, and third-generation languages, respectively. Every new generation of language brought significant improvement in programmer productivity by making programming languages more and more user-friendly. Studies indicate that the move from first-generation machine languages to second-generation assembly languages brought approximately a seven-to-one improvement in programmer productivity. The move from secondgeneration assembly languages to third-generation high-level languages brought much more improvement in programmer productivity. Even after the advent of third-generation high-level languages, researchers continued to make efforts for designing new programming languages, which could further improve programmer productivity. The result of these efforts has been the fourth-generation languages, which claim programmer productivity improvement of 200% to 1000% over third-generation high-level languages. There is a major shift in fourth-generation languages as compared to the languages of previous generations. All previous-generation languages were procedural languages whereas fourth-generation languages are non-procedural. A procedural language (such as C, C++, Java, COBOL, FORTRAN, BASIC) is one that requires a programmer to spell out the steps in the processing procedures needed to achieve a desired result. On the other hand, a non-procedural language allows a user to specify “what” the output should be without describing all the details of “how” to manipulate the data to produce that result. With this type of flexibility provided by fourth-generation languages, many computer users often find it easier and quicker to write a program themselves than to depend on professional programmers. Fourth-generation languages are so user-friendly that with a day or two of training and practice, a computer-competent user can learn to write programs, make inquiries, and generate the desired reports on his/her own. Fourth-generation languages use high-level English-like instructions. They require a programmer to specify only “what” to do, not “how” to do it. The language processor of a 4GL automatically generates most of the procedure portion (steps to carry out a task) of a fourth-generation language program. Fourth-generation language features include limited mathematical capabilities, automatic report formatting, sequencing, and record selection by criteria. They are primarily used in database programming and scripting. A few examples of fourth-generation languages are SQL, MatLab (MatrixLaboratory), FOCUS, RAMIS, Python, Pearl and Ruby. Figure 9.22 shows a typical fourth-generation language program, and Figure 9.23 shows the result of execution of this program.
364
Chapter 9 ■ Programming Languages
Recently, a new generation of programming languages have emerged that have visual tools to develop a program. These are categorized as fifth-generation programming languages (5GL). A few examples are Mercury, Prolog and OPS5. 1. 2. 3. 4. 5.
FILE IS STUDENTS_INFO LIST BY ROLL_NO: NAME SEX PHY CHEM MATH SELECT CLASS = 8 TITLE: “MARKS OBTAINED BY THE STUDENTS OF CLASS 8” COLUMN HEADINGS: “ROLL, NUMBER”; “STUDENT, NAME”; “SEX”; “MARKS IN, PHYSICS”; “MARKS IN, CHEMISTRY”; “MARKS IN, MATHEMATICS” Figure 9.22. A typical fourth-generation language program.
MARKS OBTAINED BY THE STUDENTS OF CLASS 8 ROLL NUMBER 001 002 003
STUDENT NAME
SEX
P. K. Sinha N. C. Rao S. S. Pawar
M M F
MARKS IN PHYSICS 85 72 86
MARKS IN CHEMISTRY 89 76 82
MARKS IN MATHEMATICS 96 77 94
Figure 9.23. Result of execution of the fourth-generation language program of Figure 9.22 (only few records are shown here to illustrate the nature of output).
Markup Languages We use a markup language primarily to format text documents (to specify the structure of a text document – the form in which we want to display/print the document). For example, a user uses it to specify a set of annotations (also called markup conventions) with a text document to indicate what format (such as font type, style, and font size) the user wants to apply to each part of the text document. Figure 9.24 shows an example. Notice from the figure that markup language instructions (known as tags) are enclosed in angle brackets in the main text of the text document. The codes h1, p, and em are examples of structural markup instructions. They describe the structure of the text included within them. For example, h1 means, “first level heading”, p means “paragraph”, and em means “emphasized word or phrase”. A program interpreting such structural markup instructions may apply its own rules or styles for presenting the resulting text document when displayed, using different formatting styles (such as font types, font sizes, bold or italic or underline for emphasize, etc.) as desired. Markup Language
A markup language is used primarily to format text documents. It uses a set of annotations to indicate what format (such as font type , style , and size ) to apply to each part of a text document.
(a) A text document with markup instructions of a markup language embedded in it