Hart.gif (5380 bytes)Win32 System Programming

Return to Addison-Wesley Home Page  


Return to Top Page

Additional Exercises and Questions


p. 29

What would be some methods to increase the speed of cpW (Program 2-3) for large files? Here are some suggestions. Which would be the most effective?

  1. Use a large buffer.
  2. Use the CopyFile function (p. 44).
  3. Set FILE_FLAG_SEQUENTIAL_SCAN.
  4. Use the C library: fopen, fread, etc.
  5. Use memory-mapped files (Chapter 6).
  6. Use asynchronous I/O (Chapter 13).

p. 42

Program 3-4: atou

  1. What does _taccess do? Is there an alternative? Compare.
  2. Explain Exclude.h.
  3. How are UNICODE text files displayed by
    a) type (DOS command)
    b) cat (Program 3-3)
    c) editors (Notepad, VC++ editor, Word, etc.). Explain.

p. 64

Program 4-1: tail

  • Explain the casting use of LARGE_INTEGER

Program 4-3: ls

  • Try displaying the file create and access times. What is the result? Does it depend on whether you are using NTFS or FAT?

p. 81

Here is the scheme for using completion handlers in a loop body. The example also shows how to determine how the loop body ended.
     BOOL Flag = FALSE;
           ...
         while (...) {
           _try {
               ...
           }
           _finally {
               Flag = AbnormalTerm ( );
           }
         }

p. 84-86

Programs 5-2 and 5-3

  1. How do we know whether a memory access exception was a READ or a WRITE? How do you know the virtual address that caused the violation?
  2. Try some access violations that are more interesting than the ones in the example.

p. 102-112

Programs 6-1, 6-2, and 6-4

  1. Will sortFL work on "large" files? Why? What about sortBT?
  2. What is the role of qsort?
  3. Which is faster for large files, sort FL or sortBT? Why?

p. 104 Entertainment

Take a look at the program Clear.c (on the disc under Chapter 6).

Explain its operation. Use it to determine the overhead in a heap. Experiment with the size of your paging file. Compare Windows 95/98 and NT operation.

p. 110

Program 6-3: Asc2UnMM and atouMM

  1. What is the access mode of the output file?
  2. How is the access file length set?

p. 118

Program 6-6: sortMM

  1. Write a command, Find File Key, that finds the "key" using File.idx.
  2. Write commands
    Add File Key Date
    Del File Key Date
    to add and delete records while maintaining the sorted index file.
  3. Explain the KeyCompare function.

Page 119

This chapter presented three sort programs (sortBT, sortFL, and sortMM), each using a different memory-management strategy. Appendix C (p. 343) shows that sortBT is the slowest of the three programs. Why is this so? Run sortBT on a large file and use the Performance Monitor to observe the total CPU load as well as the user space and kernel space (privileged) CPU utilization. What can you infer about the be behavior of HeapAlloc? In particular, does it run in user or kernel space?

Page 141

(All programs in Chapter 7)

  1. Modify lsFP and chmod so that they detect both the file system type and the operating system type, giving an error message if the operations cannot be carried out. Further modify lsFP so that it will fail gracefully if a file has a security descriptor that was not created by chmod.
  2. Visual C++ 5.0 added two convenience functions, BuildExplicitAccessWithName and BuildSecurityDescriptor, that can potentially simplify the chmod program. These functions allow you to specify an array of ACEs and build a security descriptor with a single call to BuildSecurityDescriptor. The sample code uses them for a variant function called InitializeSD. The code is much simpler, even in the very literal form that I've used. The problem is, it fails under NT 4.0, SP 3. The error number is 8, indicating insufficient memory. The exercise is to make this function operate correctly (and let me know what you did!).

p. 158

Program 8-1: grepMP

  1. How does it know whether the pattern was found in the file? Explain the logic that decides whether or not to print the results.
  2. Are the .tmp files all deleted properly?
  3. Are ALL handles closed properly?
  4. Modify the program so it puts out the results in completion order rather than command line order.
  5. How does the child process get access to the temp file?
  6. How does the parent construct the child's command line?

p. 160

Program 8-2: timep

  1. How are elapsed times computed?
  2. Explain the results
    timep grepMP xyz f1 f2 f3 f4
  3. Create a large-ish file as follows (this one will be 6.4MB):
        RandFile 100000 Large.txt
    RandFile.c
    is on the disc under Chapter 6.
  4. COMPARE atou, atouMM
    timep atou Large.txt Large.uni
    timep atouMM Large.txt Large.uni

p. 166

Programs 8-3 to 8-6, Job Management

  1. What is the name of the file containing the JOBMGT data? How is this name determined?
  2. Are all file locks released properly? What about handles?
  3. Does this work: jobbg jobbg cat f.txt? Explain.
  4. Suppose JOB 3 is grep MP xyz f1 f2 ...
    then kill 3.
    What could go wrong? How are job numbers reused? There is a subtle defect here; find it. Hint: See Exercise 8-7.
  5. Why are process IDs stored in the JobMgt file? Why not handles?
  6. Why the use of completion handlers?
  7. ENHANCE jobs so that it shows the time used so far by the job.
  8. Why is the new process/thread created in a suspended state?
  9. Write a single "SHELL" that processes 3 commands:
    jobbg, jobs, kill
    kill
    has 2 options:
    -c: generate
    -l: a CTRL-C or CTRL-BREAK console control signal that can be caught.
    The solution is JobShell.c on the disc.

p. 180-182

Program 9-1: pipe

  1. Why CloseHandle after the first CreateProcess? After the second CreateProcess?
  2. Does the WaitForSingleObject order make a difference?
  3. Why is hWritePipe closed so soon?
  4. Are all the thread, process, and pipe handles closed properly?
  5. Are all the standard I/O handles set properly?

p. 190-200

Programs 9-2 to 9-5

Here is how you can create a multi-process server capable of handling multiple, simultaneous requests. You can do this using Chapter 8's job management programs. There are several steps:

  1. Server
    a. Specify # instances as more than 1 BUT STILL create just 1 instance.
    b. Remove the CreateProcess ("SrvrBcst.exe").
    c. And what else?
  2. Use jobbg to bring up SrvrBcst.exe one time and Server.exe several times (up to the # of instances).
  3. PRESTO--A multi-process server!
  4. Now try multiple clients:
    jobbg -c CLIENT sortBT LARGE1.txt
    jobbg -c CLIENT sortBT LARGE2.txt

p. 210

Program 10-1 grepMT

  1. timep GREPMP x f1 ... fN
    was not accurate. What about timep GREPMT x f1 ... fN?
  2. Try GREPMT xxx f1 ... fN
    when the files are big-ish (1MB). Do the results always come out in the same order? Are threads really asynchronous?
  3. Try linking with the non-thread-safe CLIB and use CreateThread. What happens?
  4. How is the "command line" passed to the thread?
  5. Explain what happens after WaitForMultipleObjects in this program.

p. 213

Program 10-2: sortMT

  1. Why are the threads created suspended? What would happen if they were not suspended? Try it! Is there a different way to solve the same problem? Hint: Try using events or semaphores after reading Chapter 11; the correct solution is hinted at in the chapter. Alternative hint: try creating the threads in reverse order.
  2. If the number of threads is 0 (on the command line), use a multiple of the number of processors.
  3. Create a 64MB file:
    RandFile 1000000 M64.txt
    timep sortMT -n 1 M64.txt
    timep sortMT -n 2 M64.txt
    timep sortMT -n 4 M64.txt
    Explain the results.
  4. Remove the restriction on the file size.

p. 239-248

Programs 11-1 to 11-5

  1. Note: serverMT is multi-threaded. ClientCO is not.
  2. How does serverMT compute CPU utilization? How accurate is it? Improve as appropriate.
  3. Is the $Shutdown and $Quit logic valid?
  4. What happens if the server gets heavily loaded? Describe. IMPROVE!
  5. The global variable ShutDown is modified without using synchronization. Is that OK? Must it be volatile?
  6. When and why do you need to use volatile?