Jackey's 感悟

Do Research

Tag Archives: tool utils

Add a word count script for TeXWorks

We can add a new script to count the words of one tex file like this

Here is the script “wordcouter.js”

// TeXworksScript
// Title: Count the words in tex file
// Description: counter 
// Author: Jackey Lu
// Version: 0.1
// Date: 2011-09-21
// Script-Type: standalone
// Context: TeXDocument


var cmd = "e:\\local\\texlive\\2010\\bin\\win32\\texcount.exe " + TW.target.fileName;
var result = TW.system(cmd);
result.output;

Put the script in the TeXworks profile directory “scripts”, in windows 7, it is like this, C:\Users\[your username]\TeXworks\scripts.

PS: you must change the path of  texcount.exe with your real path.

To enable it, You should change the preference.

image

then you can count the words of your tex file now, use the menu Script –> Count the words in tex file.

image

Advertisements

Bibliography Entry Types for .bib file

Bibliography Entry Types for .bib file

Entry types

When entering a reference in the bibliography database, the first thing to decide is what type of entry it is. No fixed classification scheme can be complete, but BibTeX provides enough entry types to handle almost any reference reasonably well.

References to different types of publications contain different information; a reference to a journal might include the volume and number of the journal, which is usually not meaningful for a book. Therefore, database entries of different types have different fields for each entry type, the fields are divided into three classes:

Required
omitting the field will produce an error message and may result in a badly formatted bibliography entry. If the required information is not meaningful, you are using the wrong entry type.
Optional
the field’s information will be used if present, but can be omitted without causing any formatting problems. A reference should contain any available information that might help the reader, so you should include the optional field if it is applicable.
Ignored
the field is ignored. BibTeX ignores any field that is not required or optional, so you can include any fields you want in a bibliography entry. It’s often a good idea to put all relevant information about a reference in its bibliography entry – even information that may never appear in the bibliography. For example, if you want to keep an abstract of a paper in a computer file, put it in an ‘abstract’ field in the paper’s bibliography entry. The bibliography database file is likely to be as good a place as any for the abstract, and it is possible to design a bibliography style for printing selected abstracts.

BibTeX ignores the case of letters in the entry type.

Subtopics

article entry

An article from a journal or magazine.

Format:

     @ARTICLE{citation_key,
              required_fields [, optional_fields] }

Required fields: author, title, journal, year

Optional fields: volume, number, pages, month, note, key

book entry

A book with an explicit publisher.

Format:

     @BOOK{citation_key,
           required_fields [, optional_fields] }

Required fields: author or editor, title, publisher, year

Optional fields: volume, series, address, edition, month, note, key

booklet entry

A work that is printed and bound, but without a named publisher or sponsoring institution.

Format:

     @BOOKLET{citation_key,
              required_fields [, optional_fields] }

Required fields: title

Optional fields: author, howpublished, address, month, year, note, key

conference entry

An article in the proceedings of a conference. This entry is identical to the ‘inproceedings’ entry and is included for compatibility with another text formatting system.

Format:

     @CONFERENCE{citation_key,
                 required_fields [, optional_fields] }

Required fields: author, title, booktitle, year

Optional fields: editor, pages, organization, publisher, address, month, note, key

inbook entry

A part of a book, which may be a chapter and/or a range of pages.

Format:

     @INBOOK{citation_key,
             required_fields [, optional_fields] }

Required fields: author or editor, title, chapter and/or pages, publisher, year

Optional fields: volume, series, address, edition, month, note, key

incollection entry

A part of a book with its own title.

Format:

     @INCOLLECTION{citation_key,
                   required_fields [, optional_fields] }

Required fields: author, title, booktitle, year

Optional fields: editor, pages, organization, publisher, address, month, note, key

inproceedings entry

An article in the proceedings of a conference.

Format:

     @INPROCEEDINGS{citation_key,
                    required_fields [, optional_fields] }

Required fields: author, title, booktitle, year

Optional fields: editor, pages, organization, publisher, address, month, note, key

manual entry

Technical documentation.

Format:

     @MANUAL{citation_key,
             required_fields [, optional_fields] }

Required fields: title

Optional fields: author, organization, address, edition, month, year, note, key

mastersthesis entry

A Master’s thesis.

Format:

     @MASTERSTHESIS{citation_key,
                    required_fields [, optional_fields] }

Required fields: author, title, school, year

Optional fields: address, month, note, key

misc entry

Use this type when nothing else seems appropriate.

Format:

     @MISC{citation_key,
           required_fields [, optional_fields] }

Required fields: none

Optional fields: author, title, howpublished, month, year, note, key

phdthesis entry

A PhD thesis.

Format:

     @PHDTHESIS{citation_key,
                required_fields [, optional_fields] }

Required fields: author, title, school, year

Optional fields: address, month, note, key

proceedings entry

The proceedings of a conference.

Format:

     @PROCEEDINGS{citation_key,
                  required_fields [, optional_fields] }

Required fields: title, year

Optional fields: editor, publisher, organization, address, month, note, key

techreport entry

A report published by a school or other institution, usually numbered within a series.

Format:

     @TECHREPORT{citation_key,
                 required_fields [, optional_fields] }

Required fields: author, title, institution, year

Optional fields: type, number, address, month, note, key

unpublished entry

A document with an author and title, but not formally published.

Format:

     @UNPUBLISHED{citation_key,
                  required_fields [, optional_fields] }

Required fields: author, title, note

Optional fields: month, year, key

Del.icio.us : , ,
Flickr : , ,

《软件调试的艺术》学习笔记——GDB使用技巧摘要(4)——Pthread线程调试

Unix下最普遍的线程包是POSIX标准的Pthreads。Pthreads使用的抢占式线程管理策略,程序中的一个线程可能在任何时候被另一个线程中断。所以,使用Pthreads开发的应用程序有些错误不太容易重现。

GDB线程相关命令汇总

  • info threads 给出关于当前所有线程的信息
  • thread n 改为线程n,或者说是进入线程n的栈中进行观察
  • break line_num thread n 表示当线程n到达源码行line_num时停止执行
  • break line_num thread n if expression 上一命令增加条件断点而已

加入怀疑线程之间有死锁,可以用gdb进行调试定位。流程大致是:

  1. 用gdb启动或者插入待调试程序
  2. 当程序挂起时候,通过按下Ctrl+C组合键中断它;
  3. 这个时候用info threads查看所有线程都在干嘛,然后找到自己的工作线程(注意排除main线程和pthreads的管理线程)
  4. 分别查看自己的工作线程在干嘛,用bt(backtrace)查看对应的帧,记得用thread n切换进入对应线程的帧
  5. 关注像__pthread_wait_for_restart_signal()和lock等函数,如果有源码的话,会比较方便地定位到具体的问题代码位置

下面是一个简单的例子。如果在worker线程里面,上锁和解锁没有匹配,则会发生死锁

// finds the primes between 2 and n; uses the Sieve of Eratosthenes,
// deleting all multiples of 2, all multiples of 3, all multiples of 5,
// etc.; not efficient, e.g. each thread should do deleting for a whole
// block of values of base before going to nextbase for more 

// usage:  sieve nthreads n
// where nthreads is the number of worker threads

#include      <stdio.h>
#include      <math.h>
#include      <pthread.h>

#define MAX_N     100000000
#define MAX_THREADS  100

// shared variables
int nthreads,  // number of threads (not counting main())
    n,  // upper bound of range in which to find primes
    prime[MAX_N+1],  // in the end, prime[i] = 1 if i prime, else 0
    nextbase;  // next sieve multiplier to be used

int work[MAX_THREADS];  // to measure how much work each thread does,
                        // in terms of number of sieve multipliers checked

// lock index for the shared variable nextbase
pthread_mutex_t nextbaselock = PTHREAD_MUTEX_INITIALIZER;

// ID structs for the threads
pthread_t id[MAX_THREADS];

// "crosses out" all multiples of k, from k*k on
void crossout(int k)
{  int i;

   for (i = k; i*k <= n; i++)  {
      prime[i*k] = 0;
   }
}

// worker thread routine
void *worker(int tn)  // tn is the thread number (0,1,...)
{  int lim,base;

   // no need to check multipliers bigger than sqrt(n)
   lim = sqrt(n);

   do  {
      // get next sieve multiplier, avoiding duplication across threads
      pthread_mutex_lock(&nextbaselock);
      base = nextbase += 2; 
      pthread_mutex_unlock(&nextbaselock); 

         if (base <= lim)  {
         work[tn]++;  // log work done by this thread
         // don't bother with crossing out if base is known to be
         // composite
         if (prime[base])
            crossout(base);
      }
      else return;
   } while (1);
}

main(int argc, char **argv)
{  int nprimes,  // number of primes found 
       totwork,  // number of base values checked
       i;
   void *p;

   n = atoi(argv[1]);
   nthreads = atoi(argv[2]);
   for (i = 2; i <= n; i++)
      prime[i] = 1;
   crossout(2);
   nextbase = 1;
   // get threads started
   for (i = 0; i < nthreads; i++)  {
      pthread_create(&id[i],NULL,(void *) worker,(void *) i);
   }

   // wait for all done
   totwork = 0;
   for (i = 0; i < nthreads; i++)  {
      pthread_join(id[i],&p);
      printf(" %d
           values of base done
          \n
          ",work[i]);
      totwork += work[i];
   }
   printf("
          %d 
          total values of base done
          \n
          ",totwork);

   // report results
   nprimes = 0;
   for (i = 2; i <= n; i++)
      if (prime[i]) nprimes++;
   printf("the number of primes found was %d
          \n
          ",nprimes);

}

阅读余下文章

Keyboard shortchuts for Thunderbird

About keyboard shortcuts

In Thunderbird, keyboard shortcuts are context sensitive. That is, they vary according to the area of the Thunderbird interface that is currently “active”. For example, when a message is selected in the message list, the Cut, Copy and Paste keyboard shortcuts are disabled (because it doesn’t make sense to cut and paste text into the message list). Similarly, if you select text while you are reading a message, the Copy shortcut is available, but not the Cut or Paste shortcuts (because while it makes sense to copy text from a message you are reading, it doesn’t make sense to paste text into or cut text from a message you are reading).

Sometimes the keyboard shortcut functions themselves vary depending on the part of the Thunderbird interface that is active. For example, if you are writing a message, the Ctrl + A keyboard shortcut selects all the text in the message. However, if a message is highlighted in the message list, Ctrl + A selects all the messages in the message list.

Shortcut keys and operating systems

  • On Windows and Linux, the shortcut accelerator key is usually the Ctrl key
  • On Mac OS X, the shortcut accelerator key is usually the Command (⌘) key
  • On Mac OS X laptops, you must hold down the fn key to enable a function key

Message functions

New message (default format) Ctrl + M (Windows), Command + N (Mac)
New message (non-default format) Shift + Ctrl + M (Windows), Shift + Command + N (Mac)
Open message (in new window or tab) Ctrl / Command + O
Print Ctrl / Command + P
Copy Ctrl / Command + C
Move to previous folder Again Ctrl / Command + Alt / Option + M
Delete Del
Delete bypassing trash Shift + Del
Select All (messages or text in a message) Ctrl / Command + A
Select Thread Ctrl / Command + Shift + A
Undo Ctrl / Command + Z
Redo Ctrl / Command + Y
Edit Message as New Ctrl / Command + E
Expand All Threads *
Collapse All Threads \
Quick Filter Ctrl / Command + F
Find Text in Current Message Ctrl / Command + F, Ctrl / Command + F
Find Again in Current Message Ctrl / Command + G, F3
Find Previous in Current Message Ctrl / Command + Shift + G, Shift + F3
Search All Messages in Folder Ctrl / Command + Shift + F
Caret Browsing F7
Close window or tab Ctrl / Command + W
Forward Message Ctrl / Command + L
Get New Messages for Current Account Ctrl / Command + T, F9 (Linux / Mac), F5 (Windows)
Get New Messages for All Accounts Ctrl / Command + Shift + T
Increase Text Size Ctrl / Command + +
Decrease Text Size Ctrl / Command + –
Restore Text Size Ctrl / Command + 0 (zero)
Add/Remove Tag 1 to 9
Remove All Tags from Message 0 (zero)
Mark Message as Read/Unread M
Mark Thread as Read R
Mark All Read Shift + C
Mark as Read By Date C
Mark as Junk J
Mark as Not Junk Shift + J
Add Star S
Archive A
View Message Source Ctrl / Command + U
Reply to Message (sender only)(default format) Ctrl / Command + R
Reply to Message (sender only)(non-default format) Shift + Ctrl / Command + R
Reply to All in Message (all recipients)(default format) Ctrl / Command + Shift + R
Reply to All in Message (all recipients)(non-default format) Shift + Ctrl / Command + Shift + R
Reply to List Ctrl / Command + Shift + L
Save Message as File Ctrl / Command + S
Search Messages Ctrl / Command + Shift + F (Windows / Mac), Shift + F (Linux)
Send and Receive All Messages Ctrl / Command + T, F5
Stop Esc
Exit Ctrl / Command + Q

Newsgroup Messages

Expand Collapsed Thread right arrow
Collapse Expanded Thread left arrow
Ignore or Unignore Thread K
Ignore or Unignore Subthread Shift + K
Watch Thread W

Moving around Thunderbird

Toggle Message Pane F8
Go to Next Message F
Go to Next Unread Message N
Go to Previously Viewed Message [
Go to Next Viewed Message ]
Go to Next Unread Thread T
Go to Previous Message B
Go to Previous Unread Message P
Move to Next Mail Pane F6
Move to Search Bar (Global Search) Ctrl / Command + K
Help Ctrl / Command + ?
Minimize Ctrl / Command + M
Mail & Newsgroups Ctrl / Command + 1
Saved Files Ctrl / Command + J
Address Book Ctrl / Command + Shift + B
Preferences Ctrl / Command + 1
Hide Thunderbird Ctrl / Command + H
Hide Other Windows Ctrl / Command + Alt / Option + H
Quit Ctrl / Command + Q

Writing messages

New message Command + N (Mac), Ctrl + M (Windows)
Copy Ctrl / Command + C
Cut Ctrl / Command + X
Paste Ctrl / Command + V
Paste as quotation Ctrl / Command + Shift + O/
Paste without formatting Ctrl / Command + Shift + V
Move / Copy Again Ctrl / Command + Shift + M
Select All (text in a message) Ctrl / Command + A
Undo Ctrl / Command + Z
Redo Ctrl / Command + Y
Delete current word Ctrl + Del (Windows)
Delete previous word Ctrl + Backspace (Windows)
Find Text in Current Message Ctrl / Command + F
Find Again in Current Message Ctrl / Command + G, F3
Find Previous in Current Message Ctrl / Command + Shift + G, Shift + F3
Increase Text Size Ctrl / Command + +
Decrease Text Size Ctrl / Command + –
Restore Text Size Ctrl / Command + 0 (zero)
View Message Source Ctrl / Command + U
Save Draft Ctrl / Command + S
Send Message Now Ctrl / Command + Enter / Return
Send Message Later Ctrl / Command + Shift + Enter / Return
Add Additional Recipient Alt / Option + down arrow

cite from http://support.mozillamessaging.com/zh-CN/kb/Keyboard+shortcuts

《软件调试的艺术》学习笔记——GDB使用技巧摘要(3)——程序崩溃处理

  1. 程序为什么会崩溃
  2. 内存中的程序布局

当某个错误导致程序突然和异常地停止执行时,程序崩溃。迄今为止最为常见的导致程序崩溃的原因是试图在未经允许的情况下访问一个内存位置。硬件会感知这件事,并执行对操作系统的跳转。Unix系列的平台上,操作系统一般会宣布程序导致了段错误(seg
fault),并停止程序运行。在微软的windows系统上,对应的术语是一般保护错误(general protection
fault)。无论是哪个名称,硬件都必须支持虚拟内存,而且操作系统必须使用虚拟内存才会发生这个错误。虽然这是如今的通用计算机的标准,但是读者应记住,专用的小型计算机一般没有这种情况,比如用来控制机器的嵌入式计算机。

程序在内存中是如何分布的?

在Unix平台上,为程序分配的虚拟地址的布局通常如下:

.text

.data

.bss

未使用

env

这里虚拟地址0在最下方,箭头显示了其中两个组件(堆和栈)的增长方向,当它们增长时,消耗掉未使用的自由区域。各个部分的作用如下:

  • 文本区域(.text),由程序源代码中的编译器产生的机器指令组成。这一组件包括静态链接代码,包括做初始化工作然后调用main()的系统代码/usr/lib/crt0.o
  • .data 数据区域,包含在编译时分配的所有程序变量,即全局变量。如 int x = 5;
  • .bss 数据区域,包含的是存放未初始化数据的全局变量,如 int y;
  • 当程序在运行时从操作系统请求额外的内存时(例如,C语言中调用malloc,或者C++中的new),请求的内存在名为堆(heap)的区域中分配。如果堆空间不足,可以通过调用brk()来扩展堆(这正是malloc及相关函数所做的事情)
  • 栈区域(stack),是用来动态分配数据的空间。函数调用的数据(包括参数、局部变量和返回地址)都存储在栈上。每次进行函数调用时栈都会增长,每次函数返回到其调用者时栈都会收缩。
  • 上图没有显示程序的动态链接代码,它的位置与平台相关,但是它确实在某个地方存在,而且在每种操作系统上,虚拟内存中关于动态链接代码的地址是有规律的。
  1. int q[ 200 ] ;
  2. int main( void ) {
  3. int i,n, *p;
  4. p = malloc( sizeof ( int ) ) ;
  5. scanf( “%d” , &n) ;
  6. for (i = 0 ; i < 200 ; i++ )
  7. q[i] = i;
  8. printf ( “%x %x %x %x %x\n , main, q, p, &i, scanf) ;
  9. return 0 ;
  10. }

虽然上述程序本身作用不大,但是可以编写成一个工具来非正式地探索虚拟地址空间的布局。运行结果可能如下:

% a.out

5

80483f4 80496a0 9835008 bfb3abec 8048304

分别对应于文本区域、数据区域、堆、栈和动态链接函数位置。

另外可以通过查看这一过程的maps文件来得到程序在linux上的精确内存布局情况。加入进程号是21111,那么查看的文件应该是/proc/21111/maps

页的概念

虚拟地址空间是通过坐直成成为页(page)的块来查看的。在pentium硬件上,默认的页大小是4096字节。物理内存(包括RAM和ROM)也都是分成页来查看的。当程序被加载到内存中执行时,操作系统会安排程序的部分页存储在物理内存的页中。这些页成为被”驻留”,其余部分存储在磁盘上。

在执行期间的各个阶段,将需要一些当前没有驻留的程序页。当发生这种情况的时候,硬件会感知到,将控制权转移给操作系统。后者将所需页带到内存中,可能会替换掉当前驻留的另一个程序页(如果没有可用的自由内存页),然后将控制权返回给程序。如果有被驱逐的程序页,就会变成非驻留页,被存储在磁盘上。

为了管理所有这些操作,操作系统为每个过程设立了一个页表(page table)。(pentium的页表有一个层次结构,但是为了简单起见,假定只有一层,而且这里讨论的大多数内容都不是pentium特有的。)这一过程的每个虚拟页在表中都有对应的一个项(entry),其中包括如下信息:

  • 这个页在内存中或者磁盘上的当前物理位置。如果是在磁盘上,页表对应的项会指示页是非驻留的,可能包含一个指针,指向最终导致磁盘上的物理位置的一个列表。例如,它可能显示:程序的虚拟页12是驻留的,位于内存的物理页200中。
  • 该页的权限分为3种:读、写和执行

注意:操作系统不会将不完整的页分配给程序。例如,如果要运行的程序总共有10 000字节,如果完全加载,会占用3个内存页(3*4096),不会是占用2.5个页,因为页是虚拟内存系统能够操作的最小内存单元。这是调试时要记住的很重要的一点,因为这一点暗示了程序的一些错误内存访问不会触发段错误。换言之,在调试会话期间,不能这么想:”这行代码一定没有问题,因为它没有引起段错误。”

阅读余下文章

《软件调试的艺术》学习笔记——GDB使用技巧摘要(2)

command命令可以在每次发生断点的时候自动执行一系列命令,解放程序员的双手。
见示例:
(gdb) command 1
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just “end”
>silent
>printf “fibonacci was passed %d.\n”, n
> continue
>end
(gdb) run
Starting program: fibonacci
fibonacci was passed 3.
fibonacci was passed 2.
fibonacci was passed 1.
fibonacci was passed 0.
fibonacci was passed 1.
Fibonacci(3) is 3.

Program exited normally.
(gdb)

《软件调试的艺术》学习笔记——GDB使用技巧摘要(1)

《软件调试的艺术》,因为名是The Art of Debugging with GDB, DDD, and Eclipse. 作者是美国的Norman Matloff和Peter Jay Salzman,中文版由张云翻译。是人邮出版社图灵程序设计丛书初版。

这里称为”艺术”,个人觉得有点过了,但是其中关于gdb以及在gdb基础之上集成的DDD和Eclipse调试技巧的整理确实是做的很好,对于Linux/开源社区下的开发人员以及其他平台技术gdb调试的开发人员都是很有帮助。毕竟有句话叫做:程序是调出来的,不是写出来的。这句话的意思没有抹杀程序设计思想、框架、优良编码风格的意味,只是谁都知道,软件怎么可能没有bug呢?
下面是整理的内容:
(DDD和Eclipse在调试时确实有其独特之处,但是我都没有怎么用过,所以就没有阅读其中的内容,自然也没有整理)
  1. 调试的原则
    1. 调试的本质:确认原则
    2. 其他原则
      1. 从简单工作开始调试
      2. 使用自顶向下的方法
      3. 使用调试工具确定段错误的位置
      4. 通过发出中断确定无限循环的位置
      5. 使用二分搜索
调试的原则是和具体调试工具无关的,是程序员对代码世界的分析、认识的一种方法学和思路,是对程序运行时状态变化作出的一种习惯、相对正确的反应。
修改充满错误的程序(很多年轻的程序员开始都认为自己的程序是没有错误的,其实很多细节、编码规范都可能存在问题),就是逐个去确认你认为正确的代码确实是正确的。当你发现其中某个假设不成立时,就表示已经找到了关于程序错误所在位置的线索(甚至是错误本身)。这就是确认原则。
这里无意讨论基于文本的调试工具和基于GUI的调试工具的差别,应该说两者各有千秋,用在合适的地方就是好,在合适的人手里就是合适的武器。
值得介绍的是GDB有个-tui选项,和Ctrl+X+A组合键可以在文本模式和TUI模式之间切换。CGDB (http://cgdb.sf.net) 是对其的一种增强和改善。

>HexString

>


/**
* File: hexstring.c
* Author: Lvlin
* Date: 星期日, 九月 21 2008
* Abstract: get the C pattern string for inputs
*
*/
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char * argv[])
{
char c=0;
while(EOF != scanf("%c",&c))
{
if('\n' == c)
// ignore the input of carry,
// print it directly
printf("\n");
else
// & 0xFF to keep the only low 8 bits
printf("\\x%x",c & 0xFF);
}
printf("Done!\n");
return 0;
}