Archive for the ‘computing’ Category.
December 3, 2008, 8:37 pm
This is a common riddle, so if you’ve heard it, don’t shout out the answer (people might think your strange for shouting at the internet….)
Take a number. Then, half that number. Then half that half and add it to the first half. Before that gives people a headache, lets do a little example:
Lets use 10 . Then half of ten gives us 5. Then, half five (2.5) and add it to the first half, 5. This gives us 7.5
Continue to half the last half, adding it to the sum of the previous halves. Phew, I hope thats explained properly.i.e. half two point five, and add it the the 7.5 we already have, which give us 8.25
Okay, so the question is this. If we keeping adding on the halves, will we ever reach 10 again? Well, I like Java, so lets get all geeky and model the problem. For this, the double and float data types won’t give us enough decimal places. For this, we’ll need: math.BigDecimal . The Big Decimal. Sounds ominous!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| import java.math.BigDecimal;
public class Half_of_a_half {
public static Integer times_to_loop;
/**
* @param args the number of times you want to run the loop
*/
public static void main(String[] args) {
calculate(Integer.valueOf(args[0]));
}
public static void calculate(int times_to_loop) {
//The Number we want to half
BigDecimal number_to_half = new BigDecimal("10");
//The result of doing the half
BigDecimal the_half = new BigDecimal("0");
//The number we want to divide by
BigDecimal divider = new BigDecimal("2");
for (int i = 0; i < times_to_loop; i++) {
number_to_half = number_to_half.divide(divider);
the_half = the_half.add(number_to_half);
System.out.println(the_half);
}
System.out.println("The last number has " +
((Integer) the_half.toString().length() - 2) + " decimal places");
}
} |
This code takes a single parameter on the command line: the number of times to half by
e.g.
anton@anton-laptop classes $ java Half_of_a_half 10
5
7.5
8.75
9.375
9.6875
9.84375
9.921875
9.9609375
9.98046875
9.990234375
The last number has 9 decimal places
So as we can see, the decimal places just get long and longer. The additions are never big enough to get us close enough to the original number. Problem solved!
The fun bit is realising the significance of the calculation, or rather, the speed with which it was done. Performing 1000 divisions and additions takes less that 0.2 of a second! Maybe thats not so fast by todays computing ability, but by human standards thats pretty extreme.
anton@anton-laptop classes $ time java Half_of_a_half 1000 > /dev/null
real 0m1.387s
user 0m1.916s
sys 0m0.036s
October 4, 2008, 9:14 pm
This post has long code segments in it, so I’ll explain what it all does at the top. Then at the end, just copy paste into your favourite editor/IDE to play with it.
So the title kind of gives it away, but the concept behind it was quite interesting. Imagine you have some kind of resource, like a network connection, a file etc, which is needs to be accessed by many threads, but only one thread at a time. Using synchronized methods achieves this. Its especially handy where you might have hundreds of threads which need to modify the same resource, but should do it, one at a time. This code can do that (although its not perfect by far)
In this example, we have a padlock, which we can open , and close. Of course, we can’t open it whilst its being closed, and vica versa. You could just run the openLock and closeLock methods one after the other, but using threads gives us some bonus features. For example, we might want to open the padlock, get half way through, and then realise we want to pause, leaving the rest of the work for later. Or maybe we want to get halfway through closing the padlock, and then change our mind and open it (i.e. without fully closing it)
So heres the code, enjoy!
The padlock:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
| package workingWithSynchronizedThreads;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class represents a simple padlock. You know, like the kind you use to
* lock up your bike. I use it as an example of how we can make fields in
* java , then try and access them through different threads. Using syncronised
* methods, we will prevent the lock from being opened whilst were still trying
* to close it.
* @author anton
*/
public class padlock {
/**
* Constructor object to make a new padlock
* @param isThePadlockOpen Whether the padlock is open or closed
*/
public padlock(boolean isThePadlockOpen) {
this.isThePadlockOpen = isThePadlockOpen;
}
/**
* This field tells us whether or not the lock is open or closed (true for
* open, false for locked)
*/
public boolean isThePadlockOpen;
/**
* This method simply sets the padlock to be closed by setting isOpen to false
* Closing the lock takes two seconds (hence the sleep statements) ).
*/
public synchronized void closeLock() {
try {
Thread.sleep(1000);
isThePadlockOpen = false;
System.out.println("I'm closing the lock");
Thread.sleep(1000);
tellMeIfThisLockIsOpen();
} catch (InterruptedException ex) {
System.out.println("I couldn't close the lock");
Logger.getLogger(padlock.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* This method simply sets the padlock to be open by setting isOpen to true.
* Opening a lock takes two seconds (hence the sleep statements ).
*/
public synchronized void openLock() {
try {
Thread.sleep(1000);
isThePadlockOpen = true;
System.out.println("I'm opening the lock");
Thread.sleep(1000);
tellMeIfThisLockIsOpen();
} catch (InterruptedException ex) {
System.out.println("I couldn't open the lock");
Logger.getLogger(padlock.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Lets us know if our padlock object is open or closed
*/
public void tellMeIfThisLockIsOpen() {
if (isThePadlockOpen == true) {
System.out.println("The lock is open!");
} else if (isThePadlockOpen == false) {
System.out.println("The lock is closed!");
} else {
System.out.println("Something is seriously wrong!");
}
}
} |
The closeLockThread:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| package workingWithSynchronizedThreads;
/**
*
* @author anton
*/
public class closeLockThread extends Thread {
public padlock padlock;
public closeLockThread(Object padlock) {
this.padlock = (padlock) padlock;
}
@Override
public void run() {
padlock.closeLock();
}
} |
The openLockThread:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| package workingWithSynchronizedThreads;
/**
*
* @author anton
*/
public class openLockThread extends Thread {
public padlock padlock;
public openLockThread(Object padlock) {
this.padlock = (padlock) padlock;
}
@Override
public void run() {
padlock.openLock();
}
} |
Okay, less mess with some threads!!!:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| package workingWithSynchronizedThreads;
/**
*
* @author anton
*/
public class messAroundWithSomePadlocks {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
padlock myLock = new padlock(true);
closeLockThread iWannaCloseTheLock = new closeLockThread(myLock);
openLockThread iWannaOpenTheLock = new openLockThread(myLock);
iWannaCloseTheLock.start();
iWannaOpenTheLock.start();
}
} |
The things that need attention are:
- Do we need to do that casting from padlock to padlock in the openLock and closeLock thread classes?
- Is it ok/understandable to make a field a question? (public boolean isThePadlockOpen;) ?
The inspiration for this work came from looking at some code were working on in my department, but the learning was really done looking at the Java Tutorials, which are ace!
September 6, 2008, 10:50 pm
You can find XMMS in the ports collection, which is great. XMMS will even play you wav files out of the box. To get mp3s to play, you’ll need to do a little bit more work and also install:
xmms-mad
which you can find in /usr/ports/audio (presuming thats where you put your ports files)
In unix pretty much everything is a file (some people don’t like this), including audio devices. So if you fancy listening to some white noise, just cat a file to /dev/sound
Its got to be said, the documentation from the bsd guys is very thorough. Its not good enough for them that they get your mp3s playing. They tell you that wav and au files use little-endian byte ordering and signed linear quantization. You could figure this out by reading the header with hexdump(1). Reading the hex of your audio files, wow, thats a bit hardcore! Having said that, if you cared a little bit more and read up what that meant, you might be a better person. Or maybe a little more bitter….or maybe neither. Who knows!
Either way, enjoy the audible goodness!
September 2, 2008, 6:02 pm
Search, advertising, email, android,
Now its Chrome , Google’s browser to be. Its promising to be good, with some quite smart ideas
Theres a little comic strip explaining all the juicy details. Its all comes down to:
- Separate process for each tab
- New javascript engine (It compiles it into a machine code for a virtual machine)
- Their huge test suite (they can run loads of rendering tests on the own web page index. Nice)
- Being able to close tabs which have hung
- Gears- transferable features between browsers. Need something from another browser which isn’t a plugin? Just use a gear!
Lets hope they deliver! Even if they didn’t its a good contribution to the open source mind share.
August 23, 2008, 1:32 pm
Ok, so people who know me, will know that I think RAM is pretty cool stuff, and that you cant really have enough. OK, so you can have enough, but it is very handy stuff.
A register article today was talking about how MetaRAM can now get 288GB of RAM in a single machine. As someone quickly pointed out in the comments, Sun Microsystems have a machine out for almost 2 years now which supports 256GB of RAM. Its an x86 machine called the x4600. Sun also sell a SPARC machine called the M9000. It supports 2TB of RAM. Thats 2000GB of RAM. Oh, and its got 64 processors.
People should really not be surprised that the limit of how much RAM they have, is not to do with their choice of hardware, but rather choice of Operating System. At this time of writing, thew most advanced version of Windows Server 2003 (Datacenter Edition) CAN address up to 2TB of RAM. But because its written for the x86 architecture, you’ll struggle to run it anywhere to address it all. I should imagine that its partly to do with market demand; you can be sure that if customers were crying out for high capacity x86 equipment, the manufacturers would be making it. Maybe Microsoft should make a port of Windows to run on SPARC?
I find that on the whole this is quite typical of Sun. Their hardware can cost more, but it comes out earlier, and its better than kit in the same class. Sort of like Volkswagen or BMW. Higher cost, but higher quality, and more innovation.
July 31, 2008, 10:14 am
So HP have put 1000 cores in a rack ( with some serious cut backs ). Well thats great. Web 2.0 can keep growing nicely. Sun however, can put 336 cores in a rack. Yes, thats less, but its units of execution that count (i.e. threads) ( of course it all depends on your workload, but still, we’ll play the numbers game). So in these 336 cores, Sun pack 2688 threads. Using these t5240 machines, you ‘ll also get 2688GB of RAM. 42 DVD drives, 84 * 10Gb Ethernet interfaces. You also get on-cpu cryptographic acceleration for DES, 3DES, AES, RC4, SHA1, SHA256, MD5, RSA to 2048 key, ECC, CRC32. Nice. Oh, with HP, you get no storage. Not one bit. With the Sun configuration, you’ll get 336 * 146GB drives. Thats 49056GB of storage (49TB). Theres a Sun released white paper which explains more about how the new generation of T2 and T2+ chips work.
Working for Sun may change my views on hardware, but really these figures speak for themselves!
May 6, 2008, 4:48 pm
First up, its cute. Thats right, the branding is so adorable, you just want a little puffy, like its was a Pokemon or something. No, but really, I do think the branding tells you something about the product; its lean and functional. Of course, you pick whichever product you feel solves the problem, but heres my reasons for liking openbsd:
- The download is relatively small: about 207MB for the iso image
- It takes up little resources: 30MB once booted up (and 13 processes). Of course, you’ll be expecting to load the machine up with services, thats the whole point of a good OS, right? Unless you don’t actually intend on using it…
- Easy to use ports system. You just download the ports.tar.gz file for your version of openbsd, uncompress and then untar in the right directory (
/usr/ports), then navigate to the directory of the software you want and type
make && make install
Of course, this is only really needed for people who like to compile from source! Everyone else can just use the usual packages system
- Easy network configuration. You can make a bridged connection with two commands:
ifconfig bridge0 up
brconfig bridge0 add xl0 add fxp0 (insert your own network cards here)
- It ships with apache! Just type:
apachectl start and your running! (the htdocs directory is /var/www/htdocs )
- The documentation is fantastic. No really, it really is good, its always a pleasure to RTM on openbsd
- After installing a window manager like fluxbox, it takes one command to get it working, provided you want to type
startx each time you boot. Also bear in mind you’ll need to add /usr/local/bin/fluxbox to your users .xinitrc file , from a fresh install you’ll need to make this file.
- It recognised my ancient 3COM pccard Ethernet adapter! I can just pull it out and openbsd doesn’t die, it just kills off the
dhclient process that was using it!
- You get stickers if you buy the CD set!!!
May 5, 2008, 10:26 am
The latest version of Opensolaris can be found at opensolaris.com . This was known as project indiana whilst in development and is now yours absolutely free! (support does cost). Its basically Sun’s home rolled distro of opensolaris.org , and now contains IPS (Image Packaging System) . Essentially IPS is like apt for ubuntu and debian:
you want netbeans?
pkg install netbeans
Theres also a graphical installer too! Theres a screencast you might want to watch which explains a little about how to use it, and what its about!
You should also note that its made it onto distrowatch.com, to rank 69!!!!Hopefully enough people will blog about the release to push that figure up!!!Surely it should make it to the top ten, for all the features it has?
March 20, 2008, 8:58 pm
Today we finally moved our computing equipment into a room that has been provided for us by the faculty (BIG THANK YOU TO THE FACULTY TEAM, MIKE PHILIPS AND IAN ROBINSON!
). Its in the Surrey Club, just across the road from the Penryhn road campus:
Hopefully we’ll be getting a network connection soon, its been difficult to find a place on the University network where we can have complete autonomy without breaking anything else!
Theres plenty of room, with about five tables and twice as many chairs, as well as about 14 power sockets in the walls!


Its a bit messy in these shots, maybe we should have taken an after shot! The tables are now lined against the walls, and the beastly Dell servers are under the table to the left behind the door. We were initially rejected by the uni marketing team for compsoc.kingston.ac.uk because we might not represent the views of the uni, much like the case of The River who also applied for a subdomain of kingston, but were denied. However, Ian is going to restate our case, given that we are affiliated to the Student Union and so by default only represent our own views anyway!
Progress!
March 14, 2008, 11:28 pm
I’m sure my readers at some time or another have had the pleasure of installing Windows XP. They will also probably had the pleasure of having to wait 3/4 of an hour for it to finish! Well, there is a solution!
Furthering the idea of putting the disk install image in RAM before installing, why not write to a Virtual hard drive, also in RAM (if your using VirtualBox)! Specify /tmp as the prefix to your disks name in the “Virtual Disk Location and Size” dialogue window. The result? An installation of windows that will reboot in about 15 seconds. Of course, this is good for some tasks, but not all! Your millage will vary!
NOTE01: I would recommend your have at least 3GB of RAM before trying this!
NOTE02: If you want to keep the install for good, you will need to copy it off /tmp before you reboot, otherwise your virtual drive will be gone!
I must admit though, it still wont solve any post-install issues!