When I read an assigned textbook for one of my courses, I expect certain aspects of it to be somewhat invariant: if it is above a certain version, its errors will not be glaring and its examples will be workable.
Clearly, not everyone expects that.
For my Operating Systems course, I have the assigned text Operating System Concepts, by Silberschatz, Galvin, and Gagne. The book is amusing for various reasons, including its heavily-CGI’d dinosaurs on the cover and its unrepentantly libertarian viewpoint (to paraphrase, an operating system is a lot like a government, since neither does useful work but enables other entities in doing so.) I will admit, I was not expecting Political Science in my Computer Science, and from that point on I was a little leery of the text. Unfortunately, my concerns have been validated repeatedly.
Of interest to me is a certain program described to access the Windows API and fork a new process; interestingly, although this is in the ninth edition—and I had assumed it had been added for that edition—it is also in the seventh edition on page 94. There are several absolutely astounding things to be said about this program:
// allocate memory
ZeroMemory(&si, sizeof (si)) ;
si.cb = sizeof (si) ;
ZeroMemory(&pi, sizeof(pi)) ;
// create child process
if (!CreateProcess(NULL, // use command line
"C:\\WINDOWS\\system32\\mspaint.exe", // command line
NULL, // don't inherit process handle
NULL, // don't inherit thread handle
FALSE, // disable handle inheritance
0, // no creation flags
NULL, // use parent's environment block
NULL, // use parent's existing directory
fprintf(stderr, "Create Process Failed");
// Parent will wait for the child to complete
// close handles
First off, just a note about the indentation: the indenting is exactly as it was in the textbook. Yes, it’s hideous. Yes, some things are spaced out and some are not. That is exactly how it is presented.
Secondly, the authors clearly do not understand how to read their own code, or how the Windows API works. Both
PROCESS_INFORMATION are structs; as is seen there,
pi are allocated onto the stack when they are declared. It’s idiotic and imbecilic, then, to claim that
ZeroMemory() allocates them.
ZeroMemory() exists as a macro (not a function, as is claimed by the textbook) to do exactly what it says and no more. You hand it a reference to your struct, it writes
‘\0’ to that struct for the size of the struct. Super easy. No allocation anywhere in sight.
(Remember, we’re five lines into
int main() right now.)
Thirdly, there’s the
CreateProcess() call. This is an absolute nightmare; not only does it ignore the actual format of the call and drop parameters left and right, it also attempts to run
mspaint.exe as a console application. Let me tell you something about
mspaint.exe; it’s an application you’ve probably heard of called Microsoft Paint, and it requires a graphical interface, because that’s sort of why it exists. Calling it as a console application does absolutely nothing and in fact results in the program not launching (as of Windows 8.1 Preview, it should be said. However, 8.1 did not radically change anything about the operating system, and this book was reprinted this year, after the release of Windows 8, so this is a massive bug in the source provided no matter what current version of Windows you’re using.) In addition to that obscene function call, there’s the state of the printfs in use: they’re ugly and they will uglify your console when you run the program, all because there’s no newlines. It’s two keystrokes, folks. Use your newlines.
Additionally, there’s the return statement (and/or lack thereof.) There is only one return statement in this program, and normally I’d say, “That’s great! Someone understands how flow through a function should work!” … except for the bit where that return is inside a conditional that’s only hit if the program is in a bad state. That is unacceptable in a textbook; these are supposed to present how a thing should be done, and in code particularly correctness matters.
In summation, it seems to me that the authors of this textbook neither know how to write code nor how to read API documentation.
I can only hope they can count the money we’ve been told to spend on their books.