Get Processor Count

April 10, 2009

From time to time you may need to know at runtime how many processors (or more accurately processor cores) are available to your program.  For example,  you might want to determine how many threads to use with multi-threaded FFTW.

Darel Finley succinctly shows how to do this programmatically on Mac OS X.  The same code unchanged compiles on FreeBSD, and I’ve added code for Linux, and wrapped it in CPP conditionals to detect which code to use at compile time.  If compiled on something other than Mac OS X, FreeBSD or Linux, the value returned defaults to 1. So without further ado, here’s nproc.c:
/* Compile with: gcc -o nproc nproc.c */
#include <stdio.h>
#include <sys/param.h>
#include <sys/sysctl.h>

int CPUCount()
{
    int    count;
#if defined(__APPLE__) || defined(__FreeBSD__)
    size_t size = sizeof(int);
    if (sysctlbyname("hw.ncpu",&count,&size,NULL,0)!=0)
        return(1);
    else
        return(count);
#elif defined(__linux__)
    char *fname = "/proc/cpuinfo";
    char  input[256];
    FILE *ptr;
    if ((ptr = fopen(fname, "r")) == NULL) return(1);
    count = 0;
    while (fgets(input, 256, ptr))
        if (strncmp("processor", input, 9) == 0)
            count++;
    fclose(ptr);
    return((count>0)?count:1);
#else
    return(1);
#endif
}

int main(void)
{
    printf("CPU Count: %d\n", CPUCount());
    return(0);
}


Wrapper for vpnc

April 8, 2009

Here’s a small bash script wrapper for vpnc, the open-source VPN client for Cisco’s vpn concentrator. Put it in your path, and make sure you have an entry for you in your /etc/sudoers file (see comments in the script file for more info), and then connect, query status, or disconnect with:

vpn on
vpn status
vpn off

As noted in the script it might be just as easy to “vpnc” or “vpnc-disconnect” but this has the added bonus of reporting elapsed connection time.


Fixing Word 2000 on Crossover Office/Linux

April 2, 2009

Ok, short and sweet, this one.  On newer flavors of 64-bit Linux (Cent OS 5.2 x86_64 in my case) you may notice Word 2000 (via Crossover Office) fails to start, instead putting out lots of lines like this one:

wine: Unhandled exception (thread 0009), starting debugger...
wine: Unhandled exception (thread 000b), starting debugger...
wine: Unhandled exception (thread 000d), starting debugger...
wine: Unhandled exception (thread 000f), starting debugger...

There’s a two-stage fix, the first being noted in the CodeWeavers support forums (run these commands as root, or via sudo):

# sysctl -w vm.mmap_min_addr=0
# cp -p /etc/sysctl.conf /etc/sysctl.conf~
# echo "# Support Crossover Office" >> /etc/sysctl.conf
# echo "vm.mmap_min_addr = 0" >> /etc/sysctl.conf

It should be noted that if /etc/sysctl.conf already has a setting for vm.mmap_min_addr, it should be changed, instead of adding a second definition of it as I did here.

At this point (x86_64 only I believe), you may see Word launching, but crashing without any apparent errors after a few seconds.  Here, the fix is to modify how system prelinking is performed, first by undoing all prelinking, and then by modifying the prelinker settings to override Exec-Shield support (see the prelink man page for details):

# /usr/sbin/prelink -ua
# cd /etc/sysconfig
# mv prelink prelink~
# sed 's/-mR/"-mR --no-exec-shield"/' prelink~ > prelink