05 2013 档案

摘要:malloc,free详解Both brk() and mmap() cause pages to be mapped into the process's address space. mmap() can be used to map pages of a file into memory, but it can also be used only to map pages, i.e., allocate memory. brk() is just a specific interface to the VM subsystem which maps pages at a spec 阅读全文
posted @ 2013-05-27 20:41 tangr206 阅读(441) 评论(0) 推荐(0)
摘要:这几天看了不少资料,这里做一个简单的总结归纳:几个不错的网站:https://computing.llnl.gov/tutorials/pthreads/http://cpp.ezbty.org/content/doc_list/libevent_%E4%BD%BF%E7%94%A8%E4%B8%8E%E6%BA%90%E7%A0%81%E5%89%96%E6%9E%90http://www.kerneltravel.net/journal/iv/syscall.htm 线程的三个主要同步原语:互斥锁,信号量和条件变量,一下简称mutex,sem,cond。 其中mutex和sem都是对应f. 阅读全文
posted @ 2013-05-25 18:51 tangr206 阅读(1089) 评论(0) 推荐(0)
摘要:http://leetcode.com/onlinejudge#question_1 1 class Solution { 2 public: 3 vector<int> twoSum(vector<int> &numbers, int target) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 7 vector<int> result; 8 map<int, int> myma... 阅读全文
posted @ 2013-05-23 17:47 tangr206 阅读(198) 评论(0) 推荐(0)
摘要:copy_from_user函数的目的是从用户空间拷贝数据到内核空间,失败返回没有被拷贝的字节数,成功返回0.这么简单的一个函数却含盖了许多关于内核方面的知识,比如内核关于异常出错的处理.从用户空间拷贝数据到内核中时必须非常小心,如果用户空间的数据地址是个非法的地址,或是超出用户空间的范围,或是那些地址还没有被映射到,都可能对内核产生很大的影响,如oops,或者被造成系统安全的影响.所以copy_from_user函数的功能就不只是从用户空间拷贝数据那样简单了,它还要做一些指针检查以及处理这些问题的方法.下面我们来仔细分析下这个函数.函数原型在[arch/i386/lib/usercopy.c 阅读全文
posted @ 2013-05-22 10:39 tangr206 阅读(955) 评论(0) 推荐(0)
摘要:strace 用法: -f :除了跟踪当前进程外,还跟踪其子进程。 -o file :将输出信息写到文件file中,而不是显示到标准错误输出(stderr)。 -p pid :绑定到一个由pid对应的正在运行的进程。此参数常用来调试后台进程。 用处: 查看别的命令或进程都进行了哪些系统调用。比如当前web.py进程号为7393; 用strace -p 7393 -f 等下一个客户请求过来后就可以看到webserver都干 了些什么.CP 拷贝指定文件情景:一个文件夹有各种文件:sh,log,data,py。我们只想把脚背相关的文件拷贝出来备份。解决:cp --p... 阅读全文
posted @ 2013-05-16 20:29 tangr206 阅读(334) 评论(0) 推荐(0)
摘要:最近看了UNP,这是对服务器编程模型的笔记1.简单服务器模型(迭代) 服务器进程接受连接,处理请求,然后等待下一个连接。从进程控制的角度来说这种模型是最快的,因为没有进程间的切换,但是客户需要等待在listen中等待服务器accept。2.多进程模型 服务器进程接受连接,fork一个子进程为客户服务,然后等待下一个连接。多进程模型适用于单个客户服务需要消耗较多的 CPU 资源,例如需要进行大规模或长时间的数据运算或文件访问。多进程模型具有较好的安全性。在实现中需要注意 1.监听套接口和链接套接口的关闭时机; 2.及时处理已经结束的子进程(信号处理); 3.被中断的系统调用的恢复(... 阅读全文
posted @ 2013-05-16 16:15 tangr206 阅读(580) 评论(0) 推荐(0)
摘要:摘选: TcpV2 15.10节tsleep and wakeup Functions阻塞: When a process executing within the kernel cannot proceed because a kernelresource is unavailable, it waits for the resource by calling tsleep, which hasthe following prototype: int tsleep (caddr_t chan, int pri, char *mesg, int timeo); The first... 阅读全文
posted @ 2013-05-16 13:50 tangr206 阅读(525) 评论(0) 推荐(0)
摘要:copy— Shallow and deep copy operations Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other. This module p. 阅读全文
posted @ 2013-05-09 10:11 tangr206 阅读(301) 评论(0) 推荐(0)
摘要:记录tornado文档中对自己有用的信息:A Tornado web application maps URLs or URL patterns to subclasses oftornado.web.RequestHandler. Those classes defineget()orpost()methods to handle HTTPGETorPOSTrequests to that URL.The request handler can access the object representing the current request withself.request.TheHTT 阅读全文
posted @ 2013-05-07 17:26 tangr206 阅读(341) 评论(0) 推荐(0)
摘要:之前在讨论闭包的时候有提到: Python会按LEGB的顺序来搜索变量:要说明的是,这里的访问规则只对普通变量有效, 对象属性的规则与这无关(简单地说,访问一个对象的属性与此无关)。L. Local. 局部作用域,即函数中定义的变量(没有用global声明)E. Enclosing. 嵌套的父级函数的局部作用域,即包含此函数的上级函数的局部作用域,比如上面的示例中的labmda所访问的x就在其父级函数test的局部作用域里。通常也叫non-local作用域。G. Global(module). 在模块级别定义的全局变量(如果需要在函数内修改它,需要用global声明)B. Built-... 阅读全文
posted @ 2013-05-07 16:28 tangr206 阅读(1322) 评论(0) 推荐(0)
摘要:区别Being educated under Java background, static method and class method are the same thing.But not so in Python, there is subtle difference:Sayfunction a()is defined inParentClass, whileSubClass extendsParentClassIf functiona()has@staticmethoddecorator,Sub.a()still refers to definition insideParentCl 阅读全文
posted @ 2013-05-07 13:03 tangr206 阅读(552) 评论(0) 推荐(0)
摘要:Sincestr.format()is quite new, a lot of Python code still uses the%operator. However, because this old style of formatting will eventually be removed from the language,str.format()should generally be used.More information can be found in theString Formatting Operationssection.老式的("%s id %s" 阅读全文
posted @ 2013-05-06 13:10 tangr206 阅读(265) 评论(0) 推荐(0)
摘要:连接5.5 Stopping and Starting Multi-thread Programsgdbsupports debugging programs with multiple threads (seeDebugging Programs with Multiple Threads). There are two modes of controlling execution of your program within the debugger. In the default mode, referred to asall-stop mode, when any thread in 阅读全文
posted @ 2013-05-06 10:56 tangr206 阅读(252) 评论(0) 推荐(0)
摘要:AstralWind的博客上有对函数式编程很好地讲解,一下做一些节选1.1. 什么是函数式编程?函数式编程使用一系列的函数解决问题。函数仅接受输入并产生输出,不包含任何能影响产生输出的内部状态。任何情况下,使用相同的参数调用函数始终能产生同样的结果。 在一个函数式的程序中,输入的数据“流过”一系列的函数,每一个函数根据它的输入产生输出。函数式风格避免编写有“边界效应”(side effects)的函数:修改内部状态,或者是其他无法反应在输出上的变化。完全没有边界效应的函数被称为“纯函数式的”(purely functional)。避免边界效应意味着不使用在程序运行时可变的数据结构,输出只依赖. 阅读全文
posted @ 2013-05-03 16:11 tangr206 阅读(238) 评论(0) 推荐(0)
摘要:自省 自省。正如你所知道的,Python中万物皆对象,自省是指代码可以查看内存中以对象形式存在的其它模块和函数,获取它们的信息,并对它们进行操作。用这种方法,你可以定义没有名称的函数,不按函数声明的参数顺序调用函数,甚至引用事先并不知道名称的函数。反射 有时候我们会碰到这样的需求,需要执行对象的某个方法,或是需要对对象的某个字段赋值,而方法名或是字段名在编码代码时并不能确定,需要通过参数传递字符串的形式输入。举个具体的例子:当我们需要实现一个通用的DBM框架时,可能需要对数据对象的字段赋值,但我们无法预知用到这个框架的数据对象都有些什么字段,换言之,我们在写框架的时候需要通过某种机制访... 阅读全文
posted @ 2013-05-03 11:44 tangr206 阅读(699) 评论(0) 推荐(0)
摘要:... Complex Processes - Threads...Some OSes, including otherUnixes, have the seperate concept of aprocessoptionally consisting of several seperatethreads, orlight weight processes. Eachthreadis part of the same program but are scheduled individually.Until comparatively recently Linux just hadprocess 阅读全文
posted @ 2013-05-02 20:18 tangr206 阅读(1481) 评论(0) 推荐(0)
摘要:linux系统调用fork, vfork, clone:fork()、 vfork()、 clone()的区别 Linux下的进程与线程比较相近:它们都需要相同的数据结构来表示,即task_struct。区别在于一个有独立的用户空间,一个是共享的用户空间(如果完全没有用户空间则是内核线程,不需要)。Linux的用户进程不能直接被创建出来,因为不存在这样的API。它只能从某个进程中复制出来,再通过exec这样的API来切换到实际想要运行的程序文件。复制的API包括三种:fork、clone、vfork。这三个API的内部实际都是调用一个内核内部函数do_fork,只是填写的参数不同而已。而for 阅读全文
posted @ 2013-05-02 19:34 tangr206 阅读(616) 评论(0) 推荐(0)
摘要:LinuxThread(内核2.0到内核2.4期间) Linux内核只提供了轻量进程的支持,限制了更高效的线程模型的实现,但Linux着重优化了进程的调度开销,一定程度上也弥补了这一缺陷。目前最流行 的线程机制LinuxThreads所采用的就是线程-进程"一对一"模型,调度交给核心,而在用户级实现一个包括信号处理在内的线程管理机制。 Linux-LinuxThreads的运行机制正是本文的描述重点 最初的进程定义都包含程序、资源及其执行三部分,其中:1.程序通常指代码,2.资源通常包括内存资源、IO资源、信号处理等部分,3.程序的执行通常理解为执行上下文,包括对cpu的占 阅读全文
posted @ 2013-05-02 19:02 tangr206 阅读(541) 评论(0) 推荐(0)
摘要:字符串分割1 #!/bin/bash 2 pwd=$(dirname $0) && cd ${pwd} 3 4 echo ================ [ $(date "+%F %T") BEGIN ] ======================= 5 6 sms_list="18610489065,13581697175,18600276158,18600574510,18600883672" 8 content="fuck" 9 10 if [[ -n "${content}" ]]; 阅读全文
posted @ 2013-05-02 18:09 tangr206 阅读(234) 评论(0) 推荐(0)