Fall 2002 CSCI 333 Homework 5 Solution

Problem 1

Consider an old-fashioned unzoned disk drive with the following characteristics.

The questions

Answer the following questions regarding the disk drive describe above.

  1. How many 512 byte sectors are on this disk drive?

    It's disk capacity divided by sector size; that is,16M or 8G bytes/512 bytes

  2. How many total tracks (on all cylinders) are on this disk drive?

    It's the number of surfaces multiplied by the number of cylinders; that is, 16k or 8*2048

  3. How many sectors are stored on each single track?

    It's the number of sectors on the disk divided by the number of tracks on the disk; that is, 1k or 16M/16k.

  4. How many sectors are stored on each cylinder?

    It's the number of sectors per track multiplied by the number of surfaces; that is, 8k or 8*1k.

  5. How long, in msec, does it take this disk to rotate?

    It's the inverse of the rotational speed; that is, 10 msec or 1min/6000 or 60sec/6000 or 1/100 sec.

  6. How many bytes can be read in one disk rotation?

    It's the number of sectors per track multiplied by the number of bytes per sector; that is, 512k or 1k*512.

  7. What is the average rotation delay in reading from the disk?

    It's one-half the time required for the disk to rotation; that is, 5msec or 10msec/2.

  8. What is the average time, in msec, required to read a random sector of data from the disk?

    It's the average seek time plus the average rotation delay; that is, 18 msec or 13msec plus 5msec. Note: We are ignoring the very small transfer time required to read a single sector.

  9. If a 80Mbyte file could be stored in consecutive sectors of the disk, how many cylinders and tracks would it require?

    An 80Mbyte file can be stored in 160k 512-byte sectors. Since there are 8k sectors per cylinder, 20 cylinders would be required.

  10. How long would it take to read this 80Mbyte file stored in consecutive sectors?

    Assuming the head is already positioned on a track, we can read its data on one revolution or 10msec. To read an entire cylinder would require 8 revolutions, or for each surface. This would be 80msec. Thus is would require 1,600 msec to read all twenty cylinders, if you ignore seek time. On the average the time for the first seek would be 13 msec and the track-to-track time for the remaining 19 seeks would be 4*19 or 96 msec. So, a bit over 1,700 msec would be required.

Problem 2

A file contains a sequence of integers, possibly separated by commas. Write a C++ subroutine which is passed an istream that has been opened to this file and returns the sum of all the integers in the file. Return -1 is the file is not in the correct format.

Here is an example of the file input:

   35  67, 89
13 5

13, 67
int SumEm(istream NumS) {
  int retSum = 0 ;
  int nxtNum ;
  while (!NumS.eof()) {
    NumS >> nxtNum ;
    if (!NumS.eof()) {
      if (NumS.fail())
        return -1 ;
      else {
        retSum += nxtNum ;
	// keep reading integers until you get a comma or EOF
	while (NumS >> nxtNum && !NumS.fail())
	  retSum += nxtNum ;
	if (!NumS.eof()) {
	  NumS.clear() ;
	  // This better be a comma
	  char c = 'X' ;
	  NumS >> c ;
	  if (c != ',')
	    return -1 ;
  return retSum ;

Problem 3

Write some C++ statements to replace the fifth character of a file with the character 'X'.

// Assume the file has been opened to ostream F
char OutBuff[1] = { 'X' } ;
F.seekp(5, ios::beg) ;
F.write(OutBuff, 1) ;