Thursday, March 31, 2011

execute windows command line in background

http://serverfault.com/questions/189988/how-i-can-execute-windows-command-line-in-background execute windows command line in background : start /B /B Start application without creating a new window. The application has ^C handling ignored. Unless the application enables ^C processing, ^Break is the only way to interrupt the application.

Sunday, March 27, 2011

Heap (data structure)

http://en.wikipedia.org/wiki/Heap_%28data_structure%29

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Example of a complete binary max-heap

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if B is a child node of A, then key(A) ≥ key(B). This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap. (Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a min-heap.) There is no restriction as to how many children each node has in a heap. The heap is one maximally-efficient implementation of an abstract data type called a priority queue. Heaps are crucial in several efficient graph algorithms such as Dijkstra's algorithm.

Heaps are usually implemented in an array, and do not require pointers between elements.

The operations commonly performed with a heap are:

  • find-max or find-min: find the maximum item of a max-heap or a minimum item of a min-heap, respectively
  • delete-max or delete-min: removing the root node of a max- or min-heap, respectively
  • increase-key or decrease-key: updating a key within a max- or min-heap, respectively
  • insert: adding a new key to the heap
  • merge: joining two heaps to form a valid new heap containing all the elements of both.

Heaps are used in the sorting algorithm heapsort.


Master theorem

http://en.wikipedia.org/wiki/Master_theorem

Saturday, March 26, 2011

A brief introduction to Linux USB drivers (1)

USB devices are very popular these days, and I have been curious about USB drivers for a long time. Yesterday, finally, I read the book "Essential Linux Device Drivers", and here is a brief summary about the USB chapter on that book. Moreover, since I am more familiar with PCI devices, I will compare USB with PCI from time to time. The purpose of this article is to help me remember what I just read, and allow me to go back to review it if I meet USB again after a long time.

Unlike PCI, which has only one type of connector, USB has many connector types: type A, B, micro and OTG (on the go). Following are from wiki:

"The Standard-A type of USB plug is a flattened rectangle which inserts into a "downstream-port" receptacle on the USB host, or a hub, and carries both power and data. This plug is frequently seen on cables that are permanently attached to a device, such as one connecting a keyboard or mouse to the computer.

A Standard-B plug—which has a square shape with beveled exterior corners—typically plugs into an "upstream receptacle" on a device that uses a removable cable, e.g. a printer. "

USB mini and micor are for smaller devices, such as PDA.

USB OTG can be used as either host side or device side. In other words, it can be use as both type A and type B connectors.

In addition, there are also many proprietary connectors based on USB, such as those on cell phones. It is very interesting that USB has so many different connectors. I guess one reason is that it has to support many different devices with various sizes. This phenomenon also indicates that USB is a very successful standard.

A device driver for USB on Linux is similar to a PCI driver in some sense. You also need to register the device, allocate memory, fill out many data structures. One difference is that each USB device has an endpoint address in a private name space.

Another difference is that USB uses URB (USB request block) to configure and access the hardware. This is different from PCI, which you can directly read and write to the devices via IO port or IO memory.

USB devices support many classes, such as mass storage and HID (human interactive devices, e.g. keyboard, mouse). For mass storage, it uses SCSI commands to transfer data, which is also a surprise to me.

Thursday, March 24, 2011

关于User-Mode Driver Framework(UMDF)的一点感想

关于User-Mode Driver Framework(UMDF)的一点感想

最近又要在WINDOWS下面写点驱动,于是学习了下目前比较流行的WDF,主要是UMDF。因为以前用过一阵WDM,所以对KERNEL MODE的KMDF兴趣不是特别大。

WDF的全称是Windows Driver Foundation,其中又包括了UMDF和KMDF。前者是USER LEVEL的驱动,后者是KERNEL LEVEL。在WDF出来之前,流行的WINDOWS驱动结构叫做WDM (WINDOWS DRIVER MODEL)。在WDM之前,WIN NT和WIN 95的驱动模式是不一样的。WIN NT的比较规范,什么东西都只能在内核做。NT这一套一直发展下来,变成了后来的WDM, WDF。不知道未来又会出现啥新名词。WIN 95那一套很多地方限制没有那么严,这同时也造成了OS不够稳定。在WIN 95 之前,就是 WIN 3.X 和纯真的DOS年代了。现在的BIOS依然纯真,不过马上就要变成复杂的UEFI了。。。。=。=

回归正题,以前刚学WDM的时候,就被告知一切对硬件的操作都必须要在KERNEL LEVEL做才行。于是形成了一个思维定势,只有KERNEL LEVEL才可以操作硬件。多年以后,开始学习LINUX,惊讶的发现LINUX下面是可以允许USER LEVEL APP直接访问硬件端口的。再看INTEL 手册,发现硬件并没有设计成只能在KERNEL LEVEL才能访问。。。这时才明白,原来WINDOWS是故意那么设计的。。。

LINUX下的USER LEVEL APP也是有ROOT权限才可以访问硬件端口。同时也有一些限制,比如处理中断和DMA.不过能访问端口本身已经提供了很多方便。有的时候,只想写个小程序访问2、3个IO 端口。这个在LINUX下很简单,在WINDOWS下就要自己写驱动,或者借助其他工具和库了。

后来某一天,惊闻WINDOWS最新的UMDF也是在USER LEVEL下的驱动。于是心想,是不是WINDOWS终于想明白,要跟LINUX学,允许USER LEVEL APP 访问端口了呢?本来我以为是这样的,但是一直没仔细看UMDF。今天看了一下后,找了半天,也没有看到类似LINUX那样可以直接访问端口的东西。最后发现,UMDF还是要用FILE OBJECT 去访问那些硬件。。。也就是说WINDOWS下直接在USER LEVEL APP访问IO端口的梦想又泡汤了。。。

看来WINDOWS并没有更改他们的设计观念,UMDF也和LINUX下的东西有很大区别。当然,UMDF本身还是有一些优点的,让某些驱动的编写容易了很多,不用再工作在内核下,不再受很多限制了。另外一个有点惊讶的是,UMDF居然是基于COM的。当然也没有用到COM的全部功能,只是用了一点。不过这个也可以看出,COM这个东西还是比较成功的。感觉UMDF这个东西就是一个APP和DEVICE DRIVER的混合体。UMDF可以提供一些设备的符号链接,接受其他用户程序的调用,这点上比较像个驱动。但是在UMDF真正要访问硬件的时候,它又不能自己干,还要调用底层的KERNEL DEVICE DRIVER。还要用啥FILE OBJECT之类的。从这个角度看,UMDF也很像一个用户程序。

WDF最早也是跟着VISTA出来的。话说VISTA从技术上说还是不错的,可惜用户体验方面做的差了点,变成了一个失败的产品。又一次验证了光有技术是不够的。。。

Function caller in linux kernel

http://stackoverflow.com/questions/4141324/function-caller-in-linux-kernel

Note, printk with %pS is only supported on later version of Linux. Ref:
http://lwn.net/Articles/289064/

Another article about kallsyms_lookup:
http://daydreamer.idv.tw/rewrite.php/read-55.html

Note: when using __builtin_return_address(0), you cannot replace 0 with a variable such as int i; otherwise, you will get some compiling error.

Also, be careful when tracing back too much, such as __builtin_return_address(10). If the call stack is not as deep as 10, the kernel will happily crash.