Notes of AUPE, chp 1

1. File IO: buffered & unbuffered (From Section 1.5)

C标准库(stdio.h)提供的文件读写函数,如fread和fwrite是有缓存的。缓存写意味着内容不一定会立即写到磁盘上。其好处在于用户不需要考虑读写缓存,接口已经做了缓存;

系统(unistd.h)所提供的读写接口,如read和write是没有缓存的。这样写的内容会立即反映到磁盘上,适用于不能丢数据的场景。但是接口读写的buffer大小对效率会有影响

2. Process Control (From Section 1.6)

Process:

There are three primary functions for process control: fork, exec, and waitpid.

Control-D, or ^D, is the default end-of-file character.

Threads:

All the threads within a process share the same address space, file descriptors, stacks, and process-related attributes.

As with processes, threads are identified by IDs. Thread IDs, however, are local to a process.

Functions to control threads parallel those used to control processes.

3. Time

If the calendar time is stored as a signed 32-bit integer, in what year will it overflow?

对于calendar time,如果是32位有符号整型的时间值,只能计算到2038年(共68年)的秒数,会在2039年中溢出(具体哪天?)。如果是32位无符号整型,能计算到2109年(共139年)的秒数,在2110年溢出(具体哪天?)。

If the process time is stored as a signed 32-bit integer, and if the system counts 100 ticks per second, after how many days will the value overflow?

对于process time,如果是32位有符号整型的时间值,那么有

signed        100         248 day(0.6 year)

signed        60           414 day(1 year)

signed        50           497 day(1 year)

unsigned   100         497 day(1 year)

unsigned   60           828 day(2 year)

unsigned   50           994 day(2 year)

Posted in Notes of APUE2 | Leave a comment

黑螺丝和10年环保标志

原来尼康镜头的出厂时间可以通过镜头后部的螺丝颜色区分,白螺丝是07年后的,而黑螺丝则是07年前的。另外,如果镜头没有10年环保标志也是07年以前的。

详情见:http://bbs.fengniao.com/forum/2149216.html

可惜没有找到图

Posted in Uncategorized | Tagged | Leave a comment

http chunked的直观介绍

转自http://wuhua.iteye.com/blog/673841
Encoded response
HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked

25
This is the data in the first chunk

1A
and this is the second one
0

same as above, raw bytes in hex
0000-000F 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d HTTP/1.1 200 OK.
0010-001F 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74 .Content-Type: t
0020-002F 65 78 74 2f 70 6c 61 69 6e 0d 0a 54 72 61 6e 73 ext/plain..Trans
0030-003F 66 65 72 2d 45 6e 63 6f 64 69 6e 67 3a 20 63 68 fer-Encoding: ch
0040-004F 75 6e 6b 65 64 0d 0a 0d 0a 32 35 0d 0a 54 68 69 unked….25..Thi
0050-005F 73 20 69 73 20 74 68 65 20 64 61 74 61 20 69 6e s is the data in
0060-006F 20 74 68 65 20 66 69 72 73 74 20 63 68 75 6e 6b the first chunk
0070-007F 0d 0a 0d 0a 31 41 0d 0a 61 6e 64 20 74 68 69 73 ….1A..and this
0080-008F 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 6f is the second o
0090-009F 6e 65 0d 0a 30 0d 0a 0d 0a ne..0….

Posted in Uncategorized | Leave a comment

在SecureCRT中使用HOME/END/DELETE等键

Session Option->Terminal->Emulation
1. Emulation设置为Linux
2. Select an alternate keyboard emulation选择Linux
然后HOME等键就可以使用了

Posted in Uncategorized | Leave a comment

困扰

这两天一直被一个问题困扰:

同样一个程序,处理同样一个文件,有时候快有时候慢。快慢处理时间竟然相差5倍之多。

Posted in Uncategorized | Leave a comment

Is cantonese prefer to use stack?

> ulimit -a

stack size              (kbytes, -s) 8192

系统设置了栈大小为8M。如果程序里开了超过8M的数组,编译可以通过,但是在运行时却会core。这个问题困扰了我一个上午。

至于为什么我喜欢用数组(栈内存)而不用new的buffer呢?剑剑解释说:“因为广东人喜欢用栈。” 经典的回答。

Posted in Uncategorized | Leave a comment

华丽的挑战

周末在家看电视,偶然从TVB看到一部新开播的动画片“华丽的挑战”。看完第一集后竟立刻被其吸引住,赶忙上网把余下剧集一口气看完。

故事讲述的是关于一个小女孩为了报复抛弃她的娱乐圈巨星,而毅然加入娱乐圈的故事。总的来看,是个励志的故事,夹杂着爱情,友情,亲情等等情感(女版“灌篮高手”)。这些普通的情感,总是那么容易让人投入,产生共鸣。真挚的情感和人性美好的一面是时间所不能改变的,无论我是在8岁还是28岁时,同样都能感觉到。

这部动画片是09年的,已经出完第一季,共25集,计划11年7月出第二季。期待。

Posted in LIFE | Leave a comment

private constructor? 静态工厂?

今天卡在了这样一段代码里:

Thread ** m_ppThreads;      (code 1)

……

m_ppThreads = new Thread*[NUM];    (code 2)

其中,Thread是一个构造函数为private的类:

class Thread{

private:

Thread(…….) { …… }

public:

Thread * create_obj(……) { …… }

……

};

由于Thread类的构造函数是私有的,因此只能通过其提供的一个静态函数接口创建该类的对象。

对于code 2,我感到疑惑的地方在于:Thread类的构造函数函数是私有的,而new一个Thread数组是必定会调用Thread类的构造函数,这样会造成编译时出错。实际上,code 2所做的并不是new一个Thread数组,而是new一个Thread类的指针数组,new的是指针而不是类。因此,code 2并不会出错:-)

附注:

在网上搜索了一下关于private constructor的资料,发现这种奇怪的类是专用于实现设计模式(design pattern)里说的singlton pattern(中文叫什么?)。通过静态函数创建类实例,又叫静态工厂方法。这样可以控制创建类实例时的行为,使得全局范围内仅创建有限个实例。

Posted in TECH | Leave a comment

偶遇字节序

一直没有搞懂某个脚本的这句代码:
printf “\x01\x00\x00\x00” > $out_file
运行时,其作用是将一个4字节整数1写入文件中。
od -N4 -tx1 $out_file,以一字节十六进制dump数据,看到输出结果为:
0000000 01 00 00 00
与输入顺序相同。
od -N4 -tu4 $out_file,以四字节无符号整型dump数据,则结果为
0000000 1
明显与输入顺序不同,因为输入是01000000。其值绝对可能是1。
今天请教高手后发现,原来是字节序的问题。
似乎除IBM的CPU外,大部分机器CPU都是小端字节序,包括INTEL。
小端字节序在解析时从高位开始。例如,内存中4字节如下分布:
地址:01 02 03 04
内存:01 00 00 00
那么小端字节序CPU以4字节整型解析该段内存数据时,得到的值为00000001。即先从地址04开始,往小地址读4字节。
相反,大端字节序解析得到的值为01000000。即先从小地址01开始,往大地址读4字节。

Posted in TECH | Leave a comment

随手写的简单队列

昨晚一个偶然的机会,随手写了一段队列代码。很久没有写过data struct的代码,写出来后错漏百出。经过几番修改,应该可用了。

队列需要实现以下几个功能:

1. 删除给定节点;

2. 获取队列节点个数;

3. 获取队首节点。

代码如下:

class Queue
{
public:
 
 ///@brief 从队列链表中移除一个Elem
    void Remove(Elem* elem)
    { 
  if (elem == NULL)
  {
   ///TODO 出错处理
   return;
  }
  
  if (m_count == 1)
  {
   //队列只有一个元素
   m_head = NULL;
   m_tail = NULL;
  }
  else if (elem->GetNext() == NULL) /// 删除队尾元素
  {
   elem->GetPrev()->SetNext(NULL);
   m_tail = elem->GetPrev();
  }
  else if (elem->GetPrev() == NULL) /// 删除队首元素
  {
  elem->GetNext()->SetPrev(NULL);
   m_head = elem->GetNext();
  }
  else
  {
   elem->GetPrev()->SetNext(elem->GetNext());
   elem->GetNext()->SetPrev(elem->GetPrev());
  }
  
 elem->SetNext(NULL);
 elem->SetPrev(NULL);
  –m_count;
    }

    ///@brief 将Task放到队列尾
    void PushBack(Elem* elem)
    {
  if (elem == NULL)
  {
   ///TODO 出错处理
   return;
  }
  
  if (m_count == 0) ///队列为空
  {
   m_head = elem;
   m_tail = elem;
  }
  else
  {
   m_tail->SetNext(elem);
   elem->SetPrev(m_tail);
   m_tail = elem;
  }
  
  ++m_count;
    }

    ///@brief 返回指向队首Elem的指针,但不从队列中移除它
    Elem* GetFront() const
    {
        return m_head;
    }
 
 ///@brief 返回队列元素个数
 int GetCount() const
 {
  return m_count;
 }

private:
    Elem* m_head; /** 队头 */
    Elem* m_tail; /** 队尾 */
 int m_count; /** 保存队列元素个数 **/
};

Posted in TECH | Leave a comment