Tuesday, March 26, 2013

Slow SSH Connection Time

I got 99 problems but SSH ain't one...

I'm not much of a sysadmin, so when I noticed it takes me awhile to SSH into my tiny little $50 server I got off Ebay, I chocked it up to the slow clock speed. I ran into some benchmarks for the OpenSSH client/server pair today however, and how it should take less than a second on incredibly old hardware, so I decided to do a little debugging. Moments after running SSH in verbose mode I saw my issue.

My SSH client was running into some error code performing GSSAPI authentication and kept retrying for a number of seconds before trying to use my key. I don't know much about GSSAPI but I know enough about it to know that I'm not using it. After disabling it on my server SSH connects almost instantly.

Thursday, May 24, 2012

Bad Code

Non-productivity loves company...

I created a subreddit (see here) where anyone can post amusingly bad (all in good fun though) code snippets. I've posted quite a few snippets of my own code on it and the community is growing pretty fast.

Wednesday, May 23, 2012

FIxing Media Fast-Forward Problem in Linux Mint

I feel as if the Linux penguin wakes up some days thinking "John Sullivan needs to be brought down a few notches today"...

I sat down to work today and was planning on using Pandora to listen to some music but I soon noticed that it was as if my songs were playing in super fast forward: nothing was coming out of the speakers and the current position in the song was advancing at a rate of about a minute per second. Not wanting to deal with it I went to YouTube to try and find some songs on there I could listen to, but I soon found out that the same problem was occurring for the videos on there. Legitimately afraid for the well-being of my Linux box I tried to listen to music one last time by opening up Banshee but that didn't work either.

After some searching online I couldn't figure out what to do, so I started re-installing everything I could think of that might be causing the problem. Little did I know that somehow my selected audio device for output had changed to some device that didn't really exist. I found this out by (this is for Gnome 3, your desktop may be different) going to Sound Settings in the top right corner of my screen, selecting the Hardware tab, and then selecting Internal Audio Analog Stereo. How it got set to that in the first place is beyond me but I'm glad I can listen to music while I work on my Linux box again.

Friday, May 18, 2012

Setting up the network for a CentOS-minimal install

Setting up a computer is always a journey...

I've been setting up a server that I plan to deploy a project I've been working on, Galah. Due to the seriously limited specs of the computer, I installed CentOS-minimal onto it (for those trying to do this with a USB, I really recommend just burning a CD, for those who don't listen to advice on the internet, send me a message somehow and I can help you through it, a very smart sysadmin showed me the way the other day). Getting a CentOS-minimal server to connect to your network however is very difficult. I will describe the tasks I went through in order to get it working.

First thing I faced was that my wired connection was being listed under /dev/p2p1 rather than eth0. This made things unduly difficult so I found a nice way to override this courtesy the author of this lovely blog post (if your wondering about the why the renaming was happening I recommend reading the post).

After that it's a matter of setting up your gateway and whatnot. You'll have to do this by writing to the network config files yourself (located at /etc/network and /etc/network-script). Your network will likely be different, but these are the steps I took to get it connected to my home router, I recommend glancing over them to get an idea of what you want to do.

  1. I set up my /etc/network file first by specifying my gateway and the DNS servers I wanted to use. Here is my configuration file:
    /etc/network
    NETWORKING=yes HOSTNAME=galah-zero GATEWAY=192.168.0.1 DNS1=68.190.192.35 DNS2=71.9.127
  2. I then deleted /etc/network/ifcfg-p2p1 and then set up ifcfg-eth0 with my desired network settings. Here is my configuration file:
    /etc/network-scripts/ifcfg-eth0
    DEVICE=eth0 ONBOOT=yes BOOTPROTO=static IPADDR=192.168.0.69
  3. Finally I restarted the network service (service network restart) and then I could successfully access the network and the internet (test this with ping. First try pinging your gateway, if you can do that your connected to your network. Then ping www.google.com or some other site, if this fails but you can ping your gateway your DNS isn't set up right, if this works you're all set).

As you can see, it's a little tedious to get the network up without all those nice tools were used to, but not too bad.

Thursday, January 6, 2011

Interesting Templating Conundrum

So many areas of C++ are yet unexplored by me...

I think I am finally beginning to understand the logic behind many of the very confusing template rules in the C++ standard (though I also think I am a far way off from full zen-like understanding). I was faced with a difficult problem today and with the help of the good people of ##c++ I was able to arrive at a solution.

Take a look at the code below. A couple of errors where thrown by the compiler. Can you figure out what's going on?

Erroneous Code
template<class T> struct A { template<class Value> void foo() { } }; template<class T> void bar() { A<T> i; // error: expected primary-expression before ‘>’ token // error: expected primary-expression before ‘)’ token i.foo<T>(); } template<class T> void qux() { A<int> i; // No error i.foo<T>(); } int main() { return 0; }

I will give you a hint by transforming the code slightly. The elements involved are treated the same by the compiler, but conceptually it is a bit easier to grasp.

Transformed Erroneous Code
template<int T> struct A { template<int Value> void foo() { } }; template<int T> void bar() { A<T> i; // error: expected primary-expression before ‘)’ token i.foo<T>(); } template<int T> void qux() { A<1> i; // No error i.foo<T>(); } int main() { return 0; }

On line 14, you'll see the offending statement: i.foo<T>(). Now let's rewrite it a different way, the way the the compiler is seeing it: i.foo < T > ( /* Error: Missing Value! );. Since T is of type int, this code could very easily be comparing some function's (i.foo) address in memory* to the parameter T, and then comparing that result (a boolean value now) to some value (which is missing, thus the error). It isn't an obvious ambiguity but it is there nonetheless.

This ambiguity gets carried over to very similar circumstances, like the first example, where an ambiguity doesn't actually exist, but since it is so similar, it ends up violating the same rule. Truthfully this logic doesn't totally make sense to me, I feel as if an error should only be thrown in the second case, however, I did not write the standard, and I'm sure the committee had some reason to include both cases. Note: This might be a trait of just G++ which is the compiler I use. If anyone could check to see if both cases error on their compiler I would appreciate it.

Now that we know what the problem is, how do we go about solving it? Observe the code below, but be warned, it is not for the faint of heart.

Good Code
template<class T> struct A { template<class Value> void foo() { } }; template<class T> void bar() { A<T> i; i.template foo<T>(); } template<class T> void qux() { A<int> i; i.foo<T>(); } int main() { return 0; }

And there we have it. A very strange error with a very strange solution. I am still grappling with the intricacies of C++ templates, but I feel as if every day I get closer and closer to fully understanding them.

*: You might wonder why the qux() function has no errors then, and so do I. I believe that it is due to the fact that i is non-dependent in the qux() function, but is dependent In the foo() function. So the compiler knows that the function foo<T>() exists in A when we are accessing qux's i, but not when we are accessing bar's i. The amount of sense this makes is limited. Especially since when I added a simple void foo() non-templated function to A the qux function still compiled and ran normally. If you're interested and find out the logic behind all of this, please tell me, for I am still moderately confused by it all.

Wednesday, December 29, 2010

C++ Min-Max Heap Implementation

Update: See the GitHub Repo for the latest version of the code!

I wasted a decent amount of time yesterday...

I've recently come across the need for a double ended priority queue (where you can pop both the min and the max efficiently) and I have been fervently searching for the best way to go about implementing one. My initial searches brought me to a paper written in October of 1986, which described a novel data structure called a Min-Max Heap.

Given a set S of values, a min-max heap on S is a binary tree T with the following properties:
  1. T has the heap-shape
  2. T is min-max ordered: values stored at nodes on even (odd) levels are smaller (greater) than or equal to the values stored at their descendants (if any) where the root is at level zero. Thus, the smallest value of S is stored at the root of T, whereas the largest value is stored at one of the root’s children; an example of a min-max heap is shown in Figure 1 (p. 998).

Below is a diagram I created to illustrate a Min-Max Heap. You may notice that the ordering has been reversed, namely the even levels are max levels and the odd levels are min levels.

Figuring that a Min-Max Heap was perfect for my needs, I went ahead and implemented the data structure. However, after I finished, another data structure was brought to my attention: an Interval Heap. I have run across Interval Heaps before in my travels but only now do I recall that they too have the properties I want. I have also found a number of articles telling me that Interval Heaps are the best and Min-Max Heaps are the worst (as far as real time performance goes).

So now I need to create an implementation of an Interval Heap, not to mention I have this grand Min-Max Heap implementation that will surely never find use. I figure I should put it up on the Internet in the hopes that someone will find use for it though...

What exactly does a Min-Max Heap offer a programmer? It allows constant time lookup of its min and max values; logarithmic time insertion; logarithmic time deletion of min and max values; and linear time creation. Really quite a powerful structure. It is known to be about twice as slow as an interval heap in practice though (one article postulated that this was due to the higher likelihood of cache misses).

The reason I needed a structure with these properties was that I wanted to create a Stunted Priority Queue (a term I just now coined, not in any way an accepted term), or a Priority Queue that would allow me to set a maximum size, and it would never exceed that size. If an insertion made the queue exceed that size, the smallest value would be popped off to compensate.

I have created a very solid implementation of a Min-Max Heap, however, I have not included support for linear time creation. The only way to build the heap is to use the push function, which I believe gives you O(n ln(n)) time. If you would like support for that you will have to add it yourself, though if you do, please send me the modified code so I can update this site.

As always, please email me or post here if you find any problems with the code, or if any part of it is confusing so I may fix it.

I'd also like to hear comments on some of my implementation choices. Such as using a zero-indexed heap rather than a one-indexed heap (which would require an extra sizeof(T) memory). Or using template parameters rather than function parameters in my trickleUp() and trickleDown() functions.

Thursday, December 9, 2010

Traditional Inclusion Guards vs. #pragma once

Out with the old, in with the "new"...

My professors seem to like traditional inclusion guards as they never even mention the fact that #pragma once exists. I find this to be odd as #pragma once support has quickly ingrained itself into major compilers, including g++, and non-portability is the only reason I could think of for why one wouldn't use it.

Why is #pragma once so great? Take a gander at this wikipedia article. Also, consider the source tree below.

foo/foo.hpp
#ifndef FOO_HPP #define FOO_HPP double func() { return 1.2; } #endif
bar/foo.hpp
#ifndef FOO_HPP #define FOO_HPP double func2() { return 4.3; } #endif
main.cpp
#include <iostream> #include "foo/foo.hpp" #include "bar/foo.hpp" int main() { std::cout << func1(); std::cout << func2(); // Error: func2 is undefined }

Not all that pleasant right? Probably should have used #pragma once instead...