A random collection

Archive for February 2010

Protected: Running transmission-cli

This content is password protected. To view it please enter your password below:


Written by curious

February 26, 2010 at 12:49 pm

Posted in linux

TECH: Join PDF files

Make sure you have GhostView and PDFtk installed. Then, it is simple:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=joined.pdf file1.pdf file2.pdf

# or

pdftk file1.pdf file2.pdf ... output joined.pdf

Extract selected pages from a pdf file

pdftk file1.pdf cat 1 4 6-8 10-end output extracted-pages.pdf

Written by curious

February 26, 2010 at 12:33 pm

Posted in linux

TECH: killpstree

Killing a process tree (in perl)

#!/usr/bin/perl -w

$username = $ENV{"LOGNAME"};

if ($#ARGV > -1)
  $process = $ARGV[0];

# Get the list of processes
open(MYPROCESSES,"ps -fu $username |");
  @words = split(/\s+/);
  $pid = $words[1];
  $ppid = $words[2];
  $pname = $words[7];

  push(@{$children{$ppid}}, $pid);
  $procname{$pid} = $pname;

## pids     : stores all pids for processes, index: process name
## children : stores pids of all kids of a process, index: ppid
## procname : stores names of all processes, index: pid

for $k (keys(%pids))
  # look for pids for the process of interest
  if ($k =~ /$process/)
    # there may be more than 1 processes by that name
    for $pmonitor_id (@{$pids{$k}})
      # get all kids of each process

# to_kill has all the process ids to kill
foreach $k (keys(%to_kill))
  print "Kill $procname{$k} ($k)\n"
kill 9, keys(%to_kill);

sub cleanipc {
  open(MYIPCS,"ipcs -s |");
  while (<MYIPCS>)
    @words = split(/\s+/);
    $key = $words[0];
    $sid = $words[1];
    $owner = $words[2];

    if (defined $key and defined $sid)
      if ($owner =~ "$username" and $key =~ "0x00000000" and $sid =~ /^[0-9]/)
        print("Running: ipcrm -s $sid\n");
        system("ipcrm -s $sid");

# kids(parent) : gets entire subtree of parent in 'to_kill'
sub kids {
  my ($parent) = @_;

  # put parent in the list of to_kill
  $to_kill{$parent} = 1;

  # add its children to the stack
  foreach $k (@{$children{$parent}})
    $stack{$k} = 1;
  @stackarr = keys %stack;

  # stack contains all unprocessed processes
  while ($#stackarr >= 0)
    # pop from stack
    $next_elem = pop(@stackarr);
    delete $stack{$next_elem};

    # add it to the list of processes to kill
    $to_kill{$next_elem} = 1;

    if (exists($children{$next_elem}))
      # has children, so add all its children to the stack
      foreach $p (@{$children{$next_elem}})
        $stack{$p} = 1;
      @stackarr = keys %stack;

Written by curious

February 26, 2010 at 11:49 am

Posted in linux

TECH: SQL from Command Line

How to connect to SQL Server 2008 from Command Line?

"C:\Program Files\Microsoft SQL Server\90\Tools\binn\sqlcmd.exe" -U sa -P oyeoye -S hostname -d AhaDB

Use -i input-file to run a SQL script

Reference: MSDN sqlcmd

How do you connect to MySQL from command line (on Windows)?

"c:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe" -b -h host -P 3306 -u mysql -pchup-oye AhaDB

You will need MySQL Server command line tools installed.

Written by curious

February 24, 2010 at 3:48 pm

Posted in databases

TECH: Interesting Registry Entries

TNT VNC Viewer

My Computer\HKEY_CURRENT_USER\Software\ORL\VNCviewer\History
(REG_SZ, 0, linux:1)
My Computer\HKEY_CURRENT_USER\Software\ORL\VNCviewer\History\linux:1




Exporting/importing registry entries from command line

regedit /e c:\temp\regexport.txt "HKEY_CURRENT_USER\Software\SimonTatham"
regedit /ea c:\temp\regexport.reg "HKEY_CURRENT_USER\Software\SimonTatham"
regedit /c /s c:\temp\regexport.txt

Some tidbits

  1. HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU : recently run commands from “Start – Run …”
  2. HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\MUICache : History of commands run
  3. HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\File MRU
  4. HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Common\Open Find\Microsoft Office Excel\Settings\Save As\File Name MRU : Excel’s most recently used list
  5. HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Word\File MRU
  6. HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Common\Open Find\Microsoft Office Word\Settings\Save As\File Name MRU
  7. HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\FileMRUList
  8. HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\ProjectMRUList
  9. HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\General\ProjectLocationEntries
  10. HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU : Most recently used by file type/extension
  11. HKEY_USERS\S-1-5-21-3869893676-3761818361-3631654976-1800\Software\Microsoft\Office\12.0\Excel\File MRU
  12. HKEY_USERS\S-1-5-21-3869893676-3761818361-3631654976-1800\Software\Microsoft\Office\12.0\Common\Open Find\Microsoft Office Excel\Settings\Save As\File Name MRU
  13. HKEY_USERS\S-1-5-21-3869893676-3761818361-3631654976-1800\Software\Microsoft\Office\12.0\Word\File MRU
  14. HKEY_USERS\S-1-5-21-3869893676-3761818361-3631654976-1800\Software\Microsoft\Office\12.0\Common\Open Find\Microsoft Office Word\Settings\Save As\File Name MRU
  15. HKEY_USERS\S-1-5-21-3869893676-3761818361-3631654976-1800\Software\Microsoft\VisualStudio\9.0\FileMRUList
  16. HKEY_USERS\S-1-5-21-3869893676-3761818361-3631654976-1800\Software\Microsoft\VisualStudio\9.0\General\ProjectLocationEntries
  17. HKEY_USERS\S-1-5-21-3869893676-3761818361-3631654976-1800\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU\schema
  18. HKEY_USERS\S-1-5-21-3869893676-3761818361-3631654976-1800\Software\Microsoft\Windows\ShellNoRoam\MUICache : same as above

Written by curious

February 18, 2010 at 10:17 am

Posted in tech-tips

.NET: Processes, Application Domains and Threads


Threads are the basic unit to which an operating system allocates processor time, and more than one thread can be executing code inside a process (processes separate the different applications that an Operating System is executing). Each thread maintains exception handlers, a scheduling priority, and a set of structures the system uses to save the thread context until it is scheduled. The thread context includes all the information the thread needs to seamlessly resume execution, including the thread’s set of CPU registers and stack, in the address space of the thread’s host process.

Application Domains

Application domains help provide isolation, unloading, and security boundaries for executing managed code.

  • Use application domains to isolate tasks that might bring down a process. If the state of the AppDomain that’s executing a task becomes unstable, the AppDomain can be unloaded without affecting the process. This is important when a process must run for long periods without restarting. You can also use application domains to isolate tasks that should not share data.
  • If an assembly is loaded into the default application domain, it cannot be unloaded from memory while the process is running. However, if you open a second application domain to load and execute the assembly, the assembly is unloaded when that application domain is unloaded. Use this technique to minimize the working set of long-running processes that occasionally use large DLLs.


Written by curious

February 16, 2010 at 1:11 pm

Posted in dotNET

.NET: Collections in C#

Vanilla containers

  1. Arrays: Good old array type implicitly derives from System.Array
  2. Hashtable (Use Dictionary): Represents a collection of key/value pairs that are organized based on the hash code of the key.
  3. ArrayList (Use List): Implements the IList interface using an array whose size is dynamically increased as required.
  4. Queue (Use Queue<T>): Represents a first-in, first-out collection of objects.
  5. Stack (Use Stack<T>): Represents a simple last-in-first-out (LIFO) non-generic collection of objects.
  6. SortedList (Use SortedList<T>): Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.
  7. BitArray: Manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0).
  8. StringDictionary: Implements a hash table with the key and the value strongly typed to be strings rather than objects.
  9. StringCollection: Represents a collection of strings.
  10. OrderedDictionary: Represents a collection of key/value pairs that are accessible by the key or index.
  11. NameValueCollection: Represents a collection of associated String keys and String values that can be accessed either with the key or with the index.
  12. ListDictionary: Implements IDictionary using a singly linked list. Recommended for collections that typically contain 10 items or less.
  13. HybridDictionary: Implements IDictionary by using a ListDictionary while the collection is small, and then switching to a Hashtable when the collection gets large.
  14. BitVector32: Provides a simple structure that stores Boolean values and small integers in 32 bits of memory.

Newer Generic containers

  1. List<T>: Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
  2. LinkedList<T>: Represents a doubly linked list. implemented as a hash table
  3. Dictionary<TKey,TValue>: Represents a collection of keys and values.
  4. HashSet<T>: Represents a set of values.
  5. Queue<T>: Represents a first-in, first-out collection of objects.
  6. SortedDictionary<TKey,TValue>: Represents a collection of key/value pairs that are sorted on the key.
  7. SortedList<TKey,TValue>: Represents a collection of key/value pairs that are sorted on the key.
  8. SortedSet<T>: Represents a collection of objects in a sorted order.
  9. Stack<T>: Represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type.
  10. SynchronizedCollection<T>: Provides a thread-safe collection that contains objects of a type specified by the generic parameter as elements.
  11. SynchronizedKeyedCollection<K,T>: collection of objects grouped by keys
  12. SynchronizedReadOnlyCollection<T>: read-only collection of objects

Even Newer Generic containers

  1. ConcurrentDictionary<TKey, TValue>: a thread-safe collection of key-value pairs that can be accessed by multiple threads concurrently
  2. ConcurrentQueue<T>: thread-safe FIFO collection
  3. ConcurrentStack<T>: thread-safe LIFO collection
  4. ConcurrentBag<T>: thread-safe unordered collection
  5. BlockingCollection<T>: Provides blocking and bounding capabilities for thread-safe collections that implement IProducerConsumerCollection


Collection Insert(Head) Insert(Middle) Insert(End) Remove(Head) Remove(Middle) Remove(End) Get Find
List O(N) O(N) O(1) O(N) O(N) O(1) O(1) O(N)
LinkedList O(1) O(N) O(1) O(1) O(N) O(1) O(N) O(N)
Dictionary O(log N) O(log N) ~ O(1) O(log N)


Create a collection

		int[] intArray = new int[10];
		ArrayList arrayList = new ArrayList(); // default capacity
		ArrayList arrayList = new ArrayList(otherCollection); // initialize with a copy
		ArrayList arrayList = new ArrayList(initialCapacity);

		List<string> dinosaurs = new List<string>();

Initializer list for arrays

		int[] intArray = { 1, 2, 3, 4 };
		string[] strArray =	{
			"Today is Sun",
			"Why am I doing this on a Sun",
			"Go get a life",
		// reset the array
		intArray = new int[] { 100, 200, 300, 400, 500 };

Add an element to a collection

		intArray[2] = 3;
		arrayList.Add(100); // add to the end
		dinosaurs.Insert(10,"trex"); // insert "trex" at 11th slot in the list

Iterate through a collection

		IEnumerable collectionName;
		foreach(ElementType e in collectionName)
		  // do something with e (but can not modify it)
		foreach(KeyValuePair<int,string> kvp in myDictionary)
		  // do something with kvp (but can not modify it)

Fetch an element from a collection

Remove an element from a collection

Search for an element in a collection

		// find all elements bigger than 10
		int[] found = Array.FindAll(intArray, e => e > 10);
		// notice the lambda expression
		// find the index of an element in the array
		int index = Array.FindIndex(strArray, e => e == "Middle of the road");
		or equivalently
		int index = Array.IndexOf(strArray, "Middle of the road");

Automatic Sorting an array

		Array.Sort(myDearArray, (elem1, elem2) => elem1.Field1.CompareTo(elem2.Field1));

How to implement my own Container

yield return

  1. “yield return” can only be used inside a method that returns an Enumerable/Enumerator object
  2. it says: “I want to return this value as an item in the collection, but I might not
    be done yet—I could have more values to return.”
  3. A function that returns kinda IEnumerable without having to implement a class
    		IEnumerable<int> GenerateMagicNums(int count)
    			for(int i = 0; i < count; ++i)
    				// we keep coming back here everytime
    				// MoveNext() is called on the IEnumerable
    				// that we return from this function
    				yield return RandomNum();
    		// make use of it
    		foreach (int i in GenerateMagicNums(10))
    		    // wonderful

The code

    public class Range : IEnumerable<double>
        private int moveSensitivity;
        private double midPoint;
        private double granularity;
        private int windowSize;

        private double ignoredJumpLimit;

        public double low
                return midPoint - windowSize*granularity / 2;
        public double high
                return midPoint + windowSize * granularity / 2;

        public int count
            get { return windowSize; }

        public Range(double midpt, int sensitivity, int window, double granule)
            midPoint = midpt;
            windowSize = window;
            moveSensitivity = sensitivity;
            granularity = granule;
            ignoredJumpLimit = moveSensitivity * granularity;

        public bool reset(double val)
            bool moved = false;
            if (System.Math.Abs(midPoint - val) > ignoredJumpLimit)
                // time to do a move
                midPoint = val;
                moved = true;
            return moved;

        #region IEnumerable<double> Members

        public IEnumerator<double> GetEnumerator()
            double c = low;
            for (int i = 0; i < windowSize; ++i)
                yield return c+i*granularity;


        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            return this.GetEnumerator();


Written by curious

February 16, 2010 at 10:47 am

Posted in dotNET