Monthly Archives: 06月 2011

Distributed System-remote procedure call

What's local procedure call? A program invoke a local procedure call, pass it parameters and get the return data. See example: ret_type func(para_type arg); int main(int argc, char* argv[]){ ... ret_type ret=func((para_type)xxx); ... } What's remote procedure call? In an … Continue reading

Posted in old things | Leave a comment

重写operator new&delete

重写new关键字需要注意: void * operator new(size_t size); 1> c++标准要求,即使在请求分配0字节内存时,operator new也要返回一个合法指针。比如分配一个1字节大小的内存。 2> operator new内部包含一个无限循环,跳出循环的唯一办法是内存分配成功或出错处理函数完成了条款7所描述的事件中的一种:得到了更多的可用内存、安装了一个新的new-handler(出错处理函数)、卸除了mem_out_handler、抛出了一个std::bad_alloc或其派生类型的异常、或者返回失败。 3> operator new经常会被子类继承,由于存在继承,基类中的operator new可能会被调用去为一个子类对象分配内存。从new函数的原型---------可知,调用父类的new函数时,传入的size比父类的sizeof大,因此简单的做法是分配此“错误”的size内存(: perator new(size))。 重写delete函数需要注意: void operator delete(void *rawmemory, size_t size); 1> C++标准要求,允许rawmemory为空指针。 2> 子类继承时,delete子类时,基类中的operator delete传入的size参数可能和父类的sizeof不一致,此时也应该delete子类的内存空间(: perator delete(size))。

Posted in C++技术 | Leave a comment

处理new操作内存不足

********************************************************************************* 简单的方法 类定义 typedef void (*mem_out_handler)(); class x { public: static mem_out_handler set_handler(mem_out_handler p); static void * operator new(size_t size); private: static mem_out_handler currenthandler; }; mem_out_handler x::set_handler(mem_out_handler p) { mem_out_handler oldhandler = currenthandler; currenthandler = p; return oldhandler; } void … Continue reading

Posted in C++技术 | Leave a comment

how to redirect output of an running process

http://etbe.coker.com.au/2008/02/27/redirecting-output-from-a-running-process/ Note: redirecting output of an running process, not when starting a process, so '>' is ignored. Take cat > /home/fist/output for example, this command would accept input of prompt, and write user inputs to the file "/home/fist/output". What should … Continue reading

Posted in old things | Leave a comment

Architecture of MessagePack

OverView MessagePack offers two layered software components. 1.MessagePack is an efficient object serialization library.It enables to exchange structured objects between many languages like JSON. But unlike JSON, it is very fast and small. 2.MessagePack-RPC is the remote procedure call system … Continue reading

Posted in 进程通信 | Leave a comment

Paxos算法

http://zh.wikipedia.org/wiki/Paxos%E7%AE%97%E6%B3%95 http://en.wikipedia.org/wiki/Paxos_(computer_science) Paxos 算法解决的问题是一个分布式系统如 何就某个值(决议)达成一致。一个典型的场景是,在一个分布式数据库系统中,如果各节点的初始状态一致,每个节点都执行相同的操作序列,那么他们最后能得 到一个一致的状态。为保证每个节点执行相同的命令序列,需要在每一条指令上执行一个“一致性算法”以保证每个节点看到的指令一致。这在节点可能无法连接的情况下变得更加困难。一个通用的一致性算法可 以应用在许多场景中,是分布式计算中的重要问题。因此从20世纪80年代起对于一致性算法的研究就没有停止过。节点通信存在两种模型:共享内存(Shared memory)和消息传递(Messages passing)。Paxos 算法就是一种基于消息传递模型的一致性算法。 场景定义: 将节点的角色分为 proposers,acceptors,和 learners(允许身兼数职)。proposers 提出决议,acceptors 批准决议,learners“学习”决议。 需求定义: 决议(value)只有在被 proposers 提出后才能批准(未经批准的决议称为“提案(proposal)”); 在一次 Paxos 算法的执行实例中,只批准一个 Value; learners 只能获得被批准(chosen)的 Value。 算法的内容: proposer 提出一个提案前,首先要和足以形成多数派的 acceptors 进行通信,获得他们进行的最近一次批准活动的编号(prepare 过程),之后根据回收的信息决定这次提案的 value,形成提案开始投票。当获得多数 acceptors 批准后,提案获得通过,由 proposer 将这个消息告知 learner。这个简略的过程经过进一步细化后就形成了 Paxos … Continue reading

Posted in 算法 | Leave a comment