Friday, December 9, 2016

Consequence without compassion

There are some things in this world that are unforgiving. This post is about a couple of things that you can type into a computer's command line with devastating results.

Just today, I needed to use a file named SROSTERS.TXT, which had been written by a DOS program on a Windows machine. But, I needed to use it on my MacBook Pro. It was important to today's work, but I noticed that each of its 33,000+ lines ended with a carriage return, line feed. Those two characters are invisible, so I only noticed it because one line of interest was exactly one character longer in the file than the equivalent line produced on the Mac, although they appeared to be identical.

There is a handy Bash command that will remove all the carriage return characters, leaving only a line feed at the end of each line, which is what the Mac expects. So, I set out to use that command:
cat SROSTERS.TXT  |  tr  -d  '\r'  >srosters.txt
 The cat command outputs the file contents to standard output, which is then piped (notice the vertical bar aka "pipe") into the standard input of the tr (translate) command, with the option to delete all  carriage return characters (-d '\r'). It was my intention that the output of the translation would go into a new file named srosters.txt which could then be used on the Mac.

Now the greater-than sign (>) in Bash has two different roles. It is followed by a file name, and if the file doesn't already exist, it is created (empty) before the command runs, which was my intention. But, if it does exist, it is truncated to an empty file, again before the command runs.

In my mind, based on years of experience using UNIX® and various linux systems, the two file names SROSTERS.TXT and srosters.txt would be for different files. Not so on the Mac's OS X version of linux. These two* names are both names for the same file!

So, when I pressed the enter key, my computer first truncated the file to an empty file, then sent its contents (also empty) through the pipe into the translate command whose output went into the file (still empty!).

And that is how I experienced a severe consequence, with my computer expressing no compassion.

You know, the greater-than sign has bitten me before, so maybe I should have been more careful.

As a graduate student, 30 years ago at the University of Calgary, I had spent most of a day working on a class assignment. When I finished at six o'clock, I did what I ordinarily did at the end of a day, which is to type this command to remove all of my backup files:
rm  *.bak
This would remove all files whose names ended with ".bak" and that is what it had done every day before. But that night, I was in a hurry, and my left pinkie finger didn't leave the shift key quite fast enough as my right ring finger went for the period key. So, that key got shifted and the command that I actually typed was instead:
rm  *>bak
Ouch.  If only I'd had time to look at the command line before pressing the enter key, but my fingers were on automatic. After running the command, I did, as always, check to see that the backup files had indeed been removed. To my astonishment, all of the files were gone, and there was a new one, named bak, which was empty. The rm (remove) command removed all of the files (as specified by the wild-card *) and sent its (empty) output into the newly created file named bak. Just as my command specified. Again, no apologies from the computer!

That night, I learned that sometimes you can do a project better the second time you try it. I had to have the assignment done, so I stayed another hour or so and re-wrote all of the code from memory.

Today, I'm going to resolve my problem by having a friend who works near my desk in the office find a USB key in my drawer and email me the file contents.

A bonus story, where a great deal of much-appreciated compassion was shown, from 20 years ago, when I worked in the Advanced Technology Group of WordPerfect Corporation as a senior scientist. Our administrative assistant (who I will call Virginia) was having some trouble with her computer and emailed the group a screenshot** of a rather ominous warning message. I flippantly fired back an email message saying (as best as I can remember)
It looks like you have the dreaded M$ Windows virus on your machine. It's easy to get rid of. Just type DELTREE C:\WINDOWS and that'll take care of it.
About half an hour later, when I'd forgotten all about my little joke, I was called into my boss's office. He tried to keep a straight face as he reprimanded me. Virginia hadn't seen my message as a joke, and had issued the command, which completely removed the operating system from her computer. A technician came and it took most of the afternoon to get her machine working again.

The compassion came as my boss chose to forgive me, and we still laugh about it when our paths cross.

* And not just these two names, but all of the 2,048 possible variations, mixes of upper and lower-case letters, of this file name. Whichever of the variants you use, you will be referring to the same file.
** Unfortunately, I no longer have access to that email system, or I could have displayed the warning message in this blog post.

Monday, May 23, 2016

Computation without compilation

The notion of programming is customarily associated with the practice of coding. Developing a program by coding requires the use of a compiler to translate the code you write into a form usable by the computational device.

I own a fairly simple machine, a DeLonghi TRN0812T space heater (the link is to a review by Consumer Reports, from which I also borrow the picture below). The primary purpose of the device is to heat a small room, but it can be programmed, which is my interest in this post. It can be programmed without requiring any written code to be compiled. Thus it does a (very simple) computation without compilation.

Two asides are needed: 1) the distinction between operating a machine versus programming a machine, and 2) bootstrap loading via toggle switches. In the interest of tl;dr I am going to defer these to later in this blog post.

Here is a picture of part of the control panel of the space heater. It shows two large switches which have four possible configurations: 0 (off), MIN (minimum), MED (medium), MAX (maximum). Above them, and not shown in the image, is a thermostat. Below the two large switches are 96 tiny switches arranged in a circle. These are used to program the heater.


The heater photographed above is set to maximum and programmed to run continuously, except from 5:30 a.m. when it will be switched off until 6:00 a.m. when it will switch back on. The time at which the picture was taken* is 10:30 p.m. and the heater is on.

Operating the machine is straight-forward. Turn it on to maximum (with all the tiny switches left in the factory setting: towards the center of the circle). Set the thermostat to its maximum position (again, the factory setting).When the room reaches the desired temperature, turn the thermostat down slowly until you hear a click, then stop at that point. The thermostat will then act to maintain that temperature. To save energy, you can switch off one of the large switches, thus setting the heater to either medium or minimum.

Aside: operating the machine requires you to be present and attentive. In contrast, programming the machine involves you doing something now which will have an effect later even in your absence.

Programming the machine is also quite simple. First, turn the knob clockwise (in the direction of the long arrow) until the pointer is aligned with a number representing the current time of day (in a 24 hour clock). Then push the tiny switches outward for all of the times (15 minute intervals) when you want the heater off.

Having programmed the machine, you can now leave it unattended and it will cycle on and off under control of your program. (The owners manual soberly points out that "you can ... program up to 48 cycles ... over a period of 24 hours")

I would like to point out that since there are 96 of the tiny switches, and since each one can occupy, independently, one of two positions, that there are 296 (=79,228,162,514,264,337,593,543,950,336**) possible programs. (The reader may find it interesting to compare this calculation to the approximation suggested by my Powers of 2 post which would have 296 to be approximately 64 x 1027, which is close... but quite a bit on the low side)

Of all the possible programs, I'll mention four or five. With all the switches out, the heater will never turn on -- not a very useful program, except, perhaps, as a practical joke. With all the switches in (the factory setting), the heater is always on. With every other switch out, you have one of the two possible programs which cycle the heater on and off 48 times over a 24 hour period (the possibility promised in the manual). Finally, the program currently on my heater, with half the switches in and the other half out, turns the heater off at 5:30 p.m. and back on again at 5:30 a.m.

A note about large numbers. If there were a billion owners and each owner tried a different program every day (with some mechanism to avoid duplication), it would take nearly 217,000 trillion years for all possible programs to be tried.

Finally, the aside about bootstrap loading. This heater intrigued me because it reminded me of old days in computing. Early machines would load their programs from punched paper tape. But the program which loaded the programs from paper tapes had to itself be loaded into the machine. And this was done by setting small switches to place an initial program into the machine. Wikipedia has a nice article about booting computers which you can find by clicking here.

This is a nice demonstration of a fact that I point out in my dissertation, which is that one can look at programming as choosing a number. Here instead of the more customary "think of a number between one and a hundred" we have, with this heater, "think of a number between one and 79,228,162,514,264,337,593,543,950,336." Once having the number, we can convert it to binary and set the 96 switches according to the ones and zeroes, programming it without needing to compile code.

* the arrow is pointing at the mark between 22 and 23, meaning that the current time when the picture was taken is 22:30 (24 hour time) which corresponds to 10:30 p.m.
** the number was computed using TOOL which can do large integer arithmetic on numbers of up to 147,456 digits. This number doesn't stress the system much because it is only 29 digits long.