Additional Exercises and Questions
p. 29
What would be some methods to increase the speed of cpW
(Program 2-3 in the First Edition/Program 1-2 in the Second Edition) for large files? Here
are some suggestions. Which would be the most effective?
- Use a large buffer.
- Use the CopyFile function (p. 44).
- Set FILE_FLAG_SEQUENTIAL_SCAN.
- Use the C library: fopen, fread,
etc.
- Use memory-mapped files (Chapter 6).
- Use asynchronous I/O (Chapter 13).
p. 42
Program 3-4 (First Edition)/Program 2-4 (Second Edition): atou
- What does _taccess do? Is there an alternative? Compare.
- Explain Exclude.h.
- 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 (First Edition)/p. 56 (Second Edition)
Program 4-1 (First Edition)/Program 3-1 (Second Edition): tail
- Explain the casting use of LARGE_INTEGER
Program 4-3 (First Edition)/Program 3-2 (Second Edition): 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 (First Edition)/p. 98 (Second Edition)
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 (First Edition)/pp. 103-106 (Second Edition)
Programs 5-2 and 5-3
- 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?
- Try some access violations that are more interesting than the ones in the example.
p. 102-112 (First Edition)/pp. 145 ff (Second Edition)
Programs 6-1, 6-2, and 6-4
- Will sortFL work on "large" files? Why? What
about sortBT?
- What is the role of qsort?
- Which is faster for large files, sort FL or sortBT? Why?
p. 104 Entertainment (First Edition)/p. 148 (Second Edition)
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 (First Edition)/p. 155 (Second Edition)
Program 6-3: Asc2UnMM and atouMM
- What is the access mode of the output file?
- How is the access file length set?
p. 118 (First Edition)/p. 160 (Second Edition)
Program 6-6: sortMM
- Write a command, Find File Key, that finds the
"key" using File.idx.
- Write commands
Add File Key Date
Del File Key Date
to add and delete records while maintaining the sorted index file.
- Explain the KeyCompare function.
Page 119 (First Edition)/p. 162 (Second Edition)
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 (First Edition)/p. 132 (Second Edition)
All programs in Chapter 7 (First Edition)/Chapter 5 (Second Edition)
- 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.
- 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 (First Edition)/p. 189 (Second Edition)
Program 8-1 (First Edition)/Program 7-1 (Second Edition): grepMP
- How does it know whether the pattern was found in the file? Explain the logic that
decides whether or not to print the results.
- Are the .tmp files all deleted properly?
- Are ALL handles closed properly?
- Modify the program so it puts out the results in completion order rather than
command line order.
- How does the child process get access to the temp file?
- How does the parent construct the child's command line?
p. 160 (First Edition)/p. 192 (Second Edition)
Program 8-2 (First Edition)/Program 7-2 (Second Edition): timep
- How are elapsed times computed?
- Explain the results
timep grepMP xyz f1 f2 f3 f4
- 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.
- COMPARE atou, atouMM
timep atou Large.txt Large.uni
timep atouMM Large.txt Large.uni
p. 166 (First Edition)/p. 195 (Second Edition)
Programs 8-3 to 8-6 (First Edition)/Programs 7-3 to 7-6 (Second Edition), Job
Management
- What is the name of the file containing the JOBMGT data?
How is this name determined?
- Are all file locks released properly? What about handles?
- Does this work: jobbg jobbg cat f.txt? Explain.
- 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 (First Edition)/Exercise 7-8 (Second Edition).
- Why are process IDs stored in the JobMgt file? Why not
handles?
- Why the use of completion handlers?
- ENHANCE jobs so that it shows the time used so
far by the job.
- Why is the new process/thread created in a suspended state?
- Write a single "SHELL" that processes 3 commands: (Note that the Second
Edition does this.)
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 (First Edition)/pp. 302-306 (Second Edition)
Program 9-1 (First Edition)/Program 11-1 (Second Edition): pipe
- Why CloseHandle after the first CreateProcess?
After the second CreateProcess?
- Does the WaitForSingleObject order make a difference?
- Why is hWritePipe closed so soon?
- Are all the thread, process, and pipe handles closed properly?
- Are all the standard I/O handles set properly?
p. 190-200 (First Edition Only)
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:
- Server
a. Specify # instances as more than 1 BUT STILL create just 1 instance.
b. Remove the CreateProcess ("SrvrBcst.exe").
c. And what else?
- Use jobbg to bring up SrvrBcst.exe
one time and Server.exe several times (up to the #
of instances).
- PRESTO--A multi-process server!
- Now try multiple clients:
jobbg -c CLIENT sortBT LARGE1.txt
jobbg -c CLIENT sortBT LARGE2.txt
p. 210 (First Edition)/p. 216 (Second Edition)
Program 10-1 (First Edition)/Program 8-1 (Second Edition): grepMT
- timep GREPMP x f1 ... fN
was not accurate. What about timep GREPMT x f1 ... fN?
- 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?
- Try linking with the non-thread-safe CLIB and use CreateThread.
What happens?
- How is the "command line" passed to the thread?
- Explain what happens after WaitForMultipleObjects in
this program.
p. 213 (First Edition)/p. 221 (Second Edition)
Program 10-2 (First Edition)/Program 8-2 (Second Edition): sortMT
- 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 (First Edition)/Chapter 9 (Second Edition); the
correct solution is hinted at in the chapter. Alternative hint: try creating the threads
in reverse order.
- If the number of threads is 0 (on the command line), use a multiple of the number of
processors.
- 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.
- Remove the restriction on the file size.
p. 239-248 (First Edition Only)
Programs 11-1 to 11-5
- Note: serverMT is multi-threaded. ClientCO is not.
- How does serverMT compute CPU utilization? How accurate
is it? Improve as appropriate.
- Is the $Shutdown and $Quit
logic valid?
- What happens if the server gets heavily loaded? Describe. IMPROVE!
- The global variable ShutDown is modified without using
synchronization. Is that OK? Must it be volatile?
- When and why do you need to use volatile?
|