Category: Programming

Can’t create thread (11) (ThreadError) in Ruby

I have been working on some networked code in Ruby which uses EventMachine. This is part of my work as a Research Associate at the University of Birmingham. I recently had a headache with threading/processes whereby I would get this error during thread creation:

Here is the code I was running:

I knew I wasn’t creating too many threads and I knew I wasn’t running out of memory. After some serious digging, and two hundred browser tabs later,  I found the reason: zombie/defunct processes.

The ’11’ in the error code is actually not from Ruby at all, but from the  pthread class which is used to create new threads in Linux (among other OSes). This is the numerical value for the error code EAGAIN, returned by the function pthread_create, which occurs when:

Insufficient resources to create another thread, or a system-imposed limit on the number of threads was encountered

I had used Ruby’s Thread.list.size to ensure I wasn’t using too many threads, so what could it be?

Continue reading »

Compile GTK+ code with Cygwin Tutorial

To compile code with GTK+ in Windows, the recommended toolchain is MinGW (for a variety of reasons). However, if you want to use Cygwin anyway here is some guidance, which may save you some searching time and head-scratching.

I’ll assume:

  • You have Cygwin installed and you know how to use it
  • You have gcc installed within Cygwin
  • You know how to add entries to the system PATH safely

(Note before we start that it may be possible to so some of this from Cygwin’s installer but I found that manual setup was easier)

Step 1

Download the ‘all-in-one bundle’ from the GTK+ download page and unzip it to your preferred installation directory, e.g. C:/gtk . This is the example directory I’ll use below.

Step 2

Set your system variables:

  • Add the ‘bin’ directory to your system PATH (e.g. C:/gtk/bin )
  • Add a variable called GTK_BASEPATH  with the value of the installation directory (e.g. C:/gtk )
  • Add a variable called LIB  with the value of the ‘lib’ subdirectory (e.g.   C:/gtk/lib )
  • Add a variable called PKG_CONFIG_PATH  with the value of the pkgconfig directory (e.g. C:/gtk/lib/pkconfig )

Note that these can also be done on the Cygwin command line or in your bash profile using ‘export’, e.g.

Continue reading »

Running Gnuplot as a live graph, with automatic updates

I am working on some GPU-based software whose output I would like to visualise with a graph or two, and thought Gnuplot would do the job. Being able to watch the graph change as the program ran would be much better than plotting after all the data to be produced. Unfortunately, Gnuplot cannot be fed data from its standard-in, only commands.

Gnuplot Live Update

After a bit of looking around I found a Perl script that allows live updating. However, this is a bit more heavy-duty than I would like. Indeed, there is a much simpler way of getting live updates on a graph.

Gnuplot has some useful commands we can use:

These are fairly self-explanatory, so let’s make a Gnuplot file, liveplot.gnu, that refreshes itself once every second.

We set the bounds of our graph, then plot the data from the file. using 1:2  means plot columns 1 and 2 as x and y, respectively. with lines  means that the points are joined together rather than plotted separately. We pause for 1 second and then reread, meaning that the command file is re-executed.

To test this, we can write to the file plot.dat  over time and watch the graph update live. Here is a sample bash script to do that:

The function writedata  writes the data points for a basic quadratic graph, y=x^2, where the x and y values are separated. The function sleeps for a second after writing every line. The function writedata  is executed in the background and the Gnuplot script we wrote above is launched.

When this bash script is launched in the same directory as liveplot.gnu as above, the graph can be seen to be generated in real time.

Enabling command history in terminal programs like Sicstus

When you get used to pressing the up arrow for history items, the tab auto-complete feature and other handy features in the Linux terminal, it can be irritating when the occasional program behaves differently.

I have been using SICStus Prolog for my CS Prolog course and have been irritated by SICStus picking up the up arrow key as ^[[A and not retrieving the last thing I typed.

To get it to work like the Bash shell and other programs do, you can use rlwrap [program].

This program allows you to ‘wrap’ a program you’re running, like SICStus, in the readline attribute. This contains the features you’re used to. Just type something like this at the terminal, e.g.

It will ‘wrap’ your program in the readline function and work as expected. Voila!

If you want more information on rlwrap, check out the man (manual) page.

First line disappears from Eclipse console output

I was just running a standard Java file straight from Eclipse when I noticed something strange. The first line of output was not showing at all.

My code ran an analysis, got back a result and printed a couple of attributes from the result. I was getting all but the first line of output (even though I knew there was nothing wrong with the System.out.println code).

When I ran it in the terminal, I found a few hundred (or thousand) lines of debug output from the analyser which I had forgotten to disable. For some reason, Eclipse was ignoring this and only printing the remaining output. This consumed the first line too, for some reason.

As soon as I disabled the debug output from the analysis code, the output was behaving normally again.

When Eclipse won’t generate/locate

I’ve just come across a rather irritating habit of the ADT toolset for Eclipse and thought I’d share it.

If you get a red line under “R” in a reference to a layout, string or id, (e.g. the “R” in R.layout.main) then your problem could be one of the following:

  • You have ‘android.R’ as an import in your java class. This should not be there, remove it
  • You need to ‘clean’ your project by going to Project > Clean on the Eclipse menu bar
  • You do not have Eclipse set to ‘Build Automatically’. Do this by going to Project in the menu bar and ticking ‘Build Automatically’

My problem, however, was none of these. Eclipse just refused to generate

It turns out that if you have any errors in your xml files (strings.xml, layouts, etc.), then your file will not be generated and you will not be told this is the reason.

Make sure any errors in your xml files are fixed and your file should be generated automatically when you save.

SQLite timestamp comparison causes SQLiteException or error

When getting the “next” or “previous” entry in an SQLite table, I’ve been using the rowId so far and it’s been okay.

Recently, though, it’s not been that easy. For example, when a user imports a set of entries, some of which have an earlier timestamp than the current entries, they end up in the middle of the entries but their rowIds are newer and so are considered to be at the end of the list.

This causes weird navigation issues, so it’s best to stick with a single way to order entries.

Here is a sample of a SELECT statement that will get a rowId of the next entry in order of timestamp:

However, this causes an SQLite exception in Android/Java and other errors in other SQLite implementations. The problem is the space in the timestamp given. Whenever there is a space there should be quotation marks surrounding the piece of text.

So the timestamp should be “2010-09-15 13:15:20.250″, giving a SELECT statement that looks like this:

This selects one row where the timestamp is the next greatest than the given one, i.e. the one right after the given one. It should also be error-free, now that we’ve included the quotation marks.

Backing up, importing and restoring databases on Android

If your Android application has a database and you want your users to be able to backup the database and restore it as they see fit, you’ll need to mess about with database files.

Below is a class called DbExportImport. Once you’ve set it up with the correct values, all you need to do is call exportDb(), importDb() or restoreDb() from your application to perform the necessary operations.

This is also useful as a temporary measure when changing your package name or key for application signing, as your application will be newly installed and you will lose your database.

I have only left in a few comments, so for any clarification, leave a comment and I’ll make it a little clearer.

How to change the refresh rate/interval of AdMob ads on Android

Once you’ve set up AdMob ads for your application, you might want to change how often they are refreshed.

If they are refreshed often, your users will see more ads when they’re on the same screen. This is good in that they see more ads, but can also be very annoying.

In the XML entry for your AdMob banner, just add this attribute to set your refresh rate to 60 seconds, as an example:

AdMob only allows you to have a refresh rate between 12-120 seconds

The complete XML element for the AdMob banner should look something like this: