Tag Archives: windows

How to use core affinity to pin a process to a core on Windows using C#

Previously I wrote about how to use core affinity to pin a process to a core on Windows using C/C++. This is just a quick note on how to do so in C#. It’s actually a one liner so much easier than C/C++ unsurprisingly.

System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)(1 << coreId);

Above the coreId is a zero indexed number of the core you’d like to pin to. If you pass a coreId that’s incorrect this line of code will fail as below so there’s no need for explicit error checks.

Unhandled Exception: System.ComponentModel.Win32Exception: The parameter is incorrect

In case you’re wondering why anyone would want to limit themselves to one core in a multicore world it’s useful for checking how well single threaded processes perform when running one process per core and how well they scale as the number of processes goes up.

For example you may have 16 cores and because your process is single threaded you may want to run 16 processes each pinned to its respective core. As you deploy more and more processes however you may experience a degradation in how long each takes to perform a set amount of work. This will usually be due to cache overflow and reaching memory bandwidth limitations.

VirtualBox: Sharing folders between host and guest

Recently, having finally refused to surrender to windows, I installed Ubuntu virtualised as a guest on Windows as a host using Oracle’s recently released VirtualBox. Here’s a tip on how to share folders between guest and host in the official way.

On the guest VM virtualbox menu open ‘Shared folders’.

Open Shared Folders

Open Shared Folders

On the top right of the dialogue box that comes up click the ‘+’ icon. Fill in the dialogue by adding a name and location.

Add share

Add share

After that you should have a shares dialogue as below.

Shares Dialogue

Shares Dialogue

Next, as root, mount manually.

mkdir /mnt/share
mount -t vboxsf virtual-box-ubuntu-share /mnt/share/

And, finally, add the following entry into /etc/fstab for future boots.

virtual-box-ubuntu-share /mnt/share vboxsf defaults 0 0

Done. Ubuntu on VirtualBox running as guest on a Windows host is by far the best and most compelling complement to your development environment if you are forced into using Windows as a host. VirtualBox even supports seamless mode which means that you can have Linux and Windows windows intermingled on the windows desktop. Superb. And best of all – both VirtualBox and Ubuntu being completely free.

Update: Great news. VirtualBox 4.0 is out. Here’s what’s new.