perl 命令行常用语法

替换
将所有C程序中的foo替换成bar,旧文件备份成.bak
perl -p -i.bak -e ‘s/bfoob/bar/g’ *.c
很强大的功能,特别是在大程序中做重构。记得只有在UltraEdit用过。 如果你不想备份,就直接写成 perl -p -i -e 或者更简单 perl -pie, 恩,pie这个单词不错
将每个文件中出现的数值都加一
perl -i.bak -pe ‘s/(d+)/ 1 + $1 /ge’ file1 file2 ….
将换行符rn替换成n
perl -pie ‘s/rn/n/g’ file
同dos2unix命令。
将换行符n替换成rn
perl -pie ‘s/n/rn/g’ file
同unix2dos命令。
取出文件的一部分
显示字段0-4和字段6,字段的分隔符是空格
perl -lane ‘print “@F[0..4] $F[6]”‘ file
很好很强大,同 awk ‘print $1, $2, $3, $4, $5, $7’。参数名称lane也很好记。
如果字段分隔符不是空格而是冒号,则用
perl -F: -lane ‘print “@F[0..4]n”‘ /etc/passwd
显示START和END之间的部分
perl -ne ‘print if /^START$/ .. /^END$/‘ file
恐怕这个操作只有sed才做得到了吧……
相反,不显示START和END之间的部分
perl -ne ‘print unless /^START$/ .. /^END$/‘ file
显示开头50行:
perl -pe ‘exit if $. > 50’ file
同命令 head -n 50
不显示开头10行:
perl -ne ‘print unless 1 .. 10’ file
显示15行到17行:
perl -ne ‘print if 15 .. 17’ file
每行取前80个字符:
perl -lne ‘print substr($, 0, 80) = “”‘ file
每行丢弃前10个字符:
perl -lne ‘print substr($
, 10) = “”‘ file
搜索
查找comment字符串:
perl -ne ‘print if /comment/‘ duptext
这个就是普通的grep命令了。
查找不含comment字符串的行:
perl -ne ‘print unless /comment/‘ duptext
反向的grep,即grep -v。
查找包含comment或apple的行:
perl -ne ‘print if /comment/ || /apple/‘ duptext
相同的功能就要用到egrep了,语法比较复杂,我不会……
计算
计算字段4和倒数第二字段之和:
perl -lane ‘print $F[4] + $F[-2]’
要是用awk,就得写成 awk ‘{i=NF-1;print $5+$i}’
排序和反转
文件按行排序:
perl -e ‘print sort <>’ file
相当于简单的sort命令。
文件按段落排序:
perl -00 -e ‘print sort <>’ file
多个文件按文件内容排序,并返回合并后的文件:
perl -0777 -e ‘print sort <>’ file1 file2
文件按行反转:
perl -e ‘print reverse <>’ file1
相应的命令有吗?有……不过挺偏,tac(cat的反转)
数值计算
10进制转16进制:
perl -ne ‘printf “%xn”,$
10进制转8进制: perl -ne ‘printf “%on”,$

16进制转10进制:
perl -ne ‘print hex($).”n”‘
8进制转10进制:
perl -ne ‘print oct($
).”n”‘
简易计算器。
perl -ne ‘print eval($_).”n”‘
其他
启动交互式perl:
perl -de 1
查看包含路径的内容:
perl -le ‘print for @INC’
备注
与One-Liner相关的Perl命令行参数:
-0<数字>
(用8进制表示)指定记录分隔符($/变量),默认为换行
-00
段落模式,即以连续换行为分隔符
-0777
禁用分隔符,即将整个文件作为一个记录
-a
自动分隔模式,用空格分隔$_并保存到@F中。相当于@F = split ‘’。分隔符可以使用-F参数指定
-F
指定-a的分隔符,可以使用正则表达式
-e
执行指定的脚本。
-i<扩展名>
原地替换文件,并将旧文件用指定的扩展名备份。不指定扩展名则不备份。
-l
对输入内容自动chomp,对输出内容自动添加换行
-n
自动循环,相当于 while(<>) { 脚本; }
-p
自动循环+自动输出,相当于 while(<>) { 脚本; print; }

C Linux下调用sendmail命令发送邮件

用C写了一个扫描类的监控程序,为了监控的实时性,所有要发送邮件。
前提机器中必须有sendmail程序,sendmail的目录自己定义。
我测试环境的信息

[root@localhost sendmail]# uname -a
Linux localhost.localdomain 2.6.32-358.el6.i686 #1 SMP Thu Feb 21 21:50:49 UTC 2013 i686 i686 i386 GNU/Linux
[root@localhost sendmail]# cat /etc/issue
CentOS release 6.4 (Final)
Kernel r on an m

代码如下,很简单只要是思路。

#include 
#include 
#include 
//借助sendmail命令发送邮件
//邮件格式如下:
/*
*From: John Doe 
*Sender: Michael Jones 
*To: Mary Smith 
*Subject: Saying Hello
*Date: Fri, 21 Nov 1997 09:55:06 -0600
*Message-ID: <1234@local.machine.example>
*
*This is a message just to say hello.
*So, "Hello".
*/
int main(void)
{
        FILE    *fp;
        char    buffer[512];
        if( (fp=popen("/usr/sbin/sendmail -t","w")) == NULL)
        {
                perror("open /usr/sbin/sendmail errorn");
                printf("%sn", strerror(errno));
        }
        fputs("From: John Doe n", fp);
        fputs("Sender: Michael Jones n", fp);
        fputs("To: key1088 n", fp);
        fputs("Subject: Hello,wordn", fp);
        fputs("Date: Fri, 21 Nov 1997 09:55:06 -0600n", fp);
        fputs("Message-ID: <1234@local.machine.example>n", fp);
        fputs("n", fp);
        fputs("This is a message just to say hello.n", fp);
        fputs("So, Hellon", fp);
        if( pclose(fp) != 0) {
                printf("send mail error.n");
                exit(1);
        }
        printf("send mail done.n");
}
[root@localhost sendmail]# cc sendmail.c
[root@localhost sendmail]# a.out
send mail done.
[root@localhost sendmail]# tail /var/log/maillog
Mar 16 15:43:45 localhost postfix/sendmail[2124]: fatal: Recipient addresses must be specified on the command line or via the -t option
Mar 16 15:47:33 localhost postfix/qmgr[1059]: 2B7F53F87B: from=, size=431, nrcpt=1 (queue active)
Mar 16 15:47:33 localhost postfix/qmgr[1059]: 667E83F89F: from=, size=431, nrcpt=1 (queue active)
Mar 16 15:47:33 localhost postfix/smtp[2130]: 2B7F53F87B: to=, relay=none, delay=1075, delays=1075/0.08/0/0, dsn=4.4.3, status=deferred (Host or domain name not found. Name service error for name=163.com type=MX: Host not found, try again)

因为测试环境没有联网,所以是发送不成功。

sscanf函数的用法

sscanf() - 从一个字符串中读进与指定格式相符的数据.
函数原型:
int sscanf( string str, string fmt, mixed var1, mixed var2 … );
int scanf( const char *format [,argument]… );
说明:
  sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源。
  其中的format可以是一个或多个

C专家编程—分析signal函数的原型声明

在ANSI标准中,signal()的声明如下:
void (*signal(int sig,void (func)(int)))(int)
signal是一个函数,它返回一个函数指针,后者所指向的函数(signal的返回值)接受一个int参数并返回void。signal函数有两个参数,一个是sig(为int类型),另一个是func(为void(
)(int)类型)。
void (func)(int)是一个函数指针,所指向的函数接受一个int参数,返回值是void。
下面用typedef进行简化:
typedef void(
ptr_to_func)(int);
ptr_to_func signal(int,ptr_to_func);
网上的一个例子:

#include 
enum { RED, GREEN, BLUE };
void OutputSignal(int sig)
{
    printf("The signal you /'ve input is: ");
    switch(sig)
    {
    case RED:
        puts("RED!");
        break;
    case GREEN:
        puts("GREEN!");
        break;
    case BLUE:
        puts("BLUE!");
        break;
    }
}
void ( *signal( int sig, void (*func)(int) ) ) (int)
{
    puts("Hello, world!");
    func(sig);
    return func;
}
int main(void)
{
    (*signal(GREEN, &OutputSignal))(RED);
    return 0;
}
Output:
Hello, world!
The signal you 've input is: GREEN!
The signal you 've input is: RED!

POSIX basic and extended

引用:http://en.wikipedia.org/wiki/Regular_expression

POSIX basic and extended[edit]

In the POSIX standard, Basic Regular Syntax, BRE, requires that the metacharacters ( ) and { } be designated () and {}, whereas Extended Regular Syntax, ERE, does not.
Metacharacter Description
. Matches any single character (many applications exclude newlines, and exactly which characters are considered newlines is flavor-, character-encoding-, and platform-specific, but it is safe to assume that the line feed character is included). Within POSIX bracket expressions, the dot character matches a literal dot. For example, a.c matches "abc", etc., but [a.c] matches only "a", ".", or "c".
[ ] A bracket expression. Matches a single character that is contained within the brackets. For example, [abc] matches "a", "b", or "c". [a-z]specifies a range which matches any lowercase letter from "a" to "z". These forms can be mixed: [abcx-z] matches "a", "b", "c", "x", "y", or "z", as does [a-cx-z].The - character is treated as a literal character if it is the last or the first (after the ^, if present) character within the brackets: [abc-], [-abc]. Note that backslash escapes are not allowed. The ] character can be included in a bracket expression if it is the first (after the ^) character: []abc].
[^ ] Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than "a", "b", or "c".[^a-z] matches any single character that is not a lowercase letter from "a" to "z". Likewise, literal characters and ranges can be mixed.
^ Matches the starting position within the string. In line-based tools, it matches the starting position of any line.
$ Matches the ending position of the string or the position just before a string-ending newline. In line-based tools, it matches the ending position of any line.
( ) Defines a marked subexpression. The string matched within the parentheses can be recalled later (see the next entry, n). A marked subexpression is also called a block or capturing group. BRE mode requires ( ).
n Matches what the nth marked subexpression matched, where n is a digit from 1 to 9. This construct is vaguely defined in the POSIX.2 standard. Some tools allow referencing more than nine capturing groups.
* Matches the preceding element zero or more times. For example, ab*c matches "ac", "abc", "abbbc", etc. [xyz]* matches "", "x", "y", "z", "zx", "zyx", "xyzzy", and so on. (ab)* matches "", "ab", "abab", "ababab", and so on.
{m,n} Matches the preceding element at least m and not more than n times. For example, a{3,5} matches only "aaa", "aaaa", and "aaaaa". This is not found in a few older instances of regular expressions. BRE mode requires {m,n}.
Examples:
  • .at matches any three-character string ending with "at", including "hat", "cat", and "bat".
  • [hc]at matches "hat" and "cat".
  • [^b]at matches all strings matched by .at except "bat".
  • [^hc]at matches all strings matched by .at other than "hat" and "cat".
  • ^[hc]at matches "hat" and "cat", but only at the beginning of the string or line.
  • [hc]at$ matches "hat" and "cat", but only at the end of the string or line.
  • [.] matches any single character surrounded by "[" and "]" since the brackets are escaped, for example: "[a]" and "[b]".
  • s.* matches any number of characters preceded by s , for example: "saw" and "seed".

POSIX extended[edit]

The meaning of metacharacters escaped with a backslash is reversed for some characters in the POSIX Extended Regular Expression (ERE) syntax. With this syntax, a backslash causes the metacharacter to be treated as a literal character. So, for example, ( ) is now ( ) and { } is now { }. Additionally, support is removed for n backreferences and the following metacharacters are added:
Metacharacter Description
? Matches the preceding element zero or one time. For example, ab?c matches only "ac" or "abc".
+ Matches the preceding element one or more times. For example, ab+c matches "abc", "abbc", "abbbc", and so on, but not "ac".
| The choice (also known as alternation or set union) operator matches either the expression before or the expression after the operator. For example, abc|def matches "abc" or "def".
Examples:
  • [hc]+at matches "hat", "cat", "hhat", "chat", "hcat", "cchchat", and so on, but not "at".
  • [hc]?at matches "hat", "cat", and "at".
  • [hc]*at matches "hat", "cat", "hhat", "chat", "hcat", "cchchat", "at", and so on.
  • cat|dog matches "cat" or "dog".
POSIX Extended Regular Expressions can often be used with modern Unix utilities by including the command line flag -E.

Character classes[edit]

The character class is the most basic regular expression concept after a literal match. It makes one small sequence of characters match a larger set of characters. For example, [A-Z] could stand for the alphabet, and d could mean any digit. Character classes apply to both POSIX levels. When specifying a range of characters, such as [a-Z] computer's locale settings determine the contents by the numeric ordering of the character encoding. They could store digits in that sequence, or the ordering could be abc...zABC...Z, or aAbBcC...zZ. So the POSIX standard defines a character class, which will be known by the regular expression processor installed. Those definitions are in the following table:
POSIX Non-standard Perl/Tcl Vim ASCII Description
[:alnum:] [A-Za-z0-9] Alphanumeric characters
[:word:] w w [A-Za-z0-9_] Alphanumeric characters plus "_"
W W [^A-Za-z0-9_] Non-word characters
[:alpha:] a [A-Za-z] Alphabetic characters
[:blank:] s t] Space and tab
b < > (?<=W)(?=w)|(?<=w)(?=W) Word boundaries
[:cntrl:] [x00-x1Fx7F] Control characters
[:digit:] d d [0-9] Digits
D D [^0-9] Non-digits
[:graph:] [x21-x7E] Visible characters
[:lower:] l [a-z] Lowercase letters
[:print:] p [x20-x7E] Visible characters and the space character
[:punct:] [][!"#$%&'()*+,./:;<=>?@^_`{|}~-] Punctuation characters
[:space:] s _s trnvf] Whitespace characters
S [^ trnvf] Non-whitespace characters
[:upper:] u [A-Z] Uppercase letters
[:xdigit:] x [A-Fa-f0-9] Hexadecimal digits
POSIX character classes can only be used within bracket expressions. For example, [[:upper:]ab] matches the uppercase letters and lowercase "a" and "b". An additional non-POSIX class understood by some tools is [:word:], which is usually defined as [:alnum:] plus underscore. This reflects the fact that in many programming languages these are the characters that may be used in identifiers. The editor Vim further distinguishes word and word-head classes (using the notation w and h) since in many programming languages the characters that can begin an identifier are not the same as those that can occur in other positions. Note that what the POSIX regular expression standards call character classes are commonly referred to as POSIX character classes in other regular expression flavors which support them. With most other regular expression flavors, the term character class is used to describe what POSIX calls bracket expressions. C默认在*NIX下面,可以使用这些规则,最基本的POSIX语法,不需要其他的扩展库。

Linux-2.6.25 select系统调用源码分析

Linux 2.6.25中的select系统调用
主要有4个函数:
sys_select:处理时间参数,调用core_sys_select。
core_sys_select:处理三个fd_set参数,调用do_select。
do_select:做select/poll的工作。在合适的时机把自己挂起等待,调用sock_poll。
sock_poll:用函数指针分派到具体的协议层函数tcp_poll、udp_poll、datagram_poll。
层层分工明确,我也要多学习这种方式啊。

/*
sys_select(fs/select.c)
处理了超时值(如果有),将struct timeval转换成了时钟周期数,调用core_sys_select,然后检查剩余时间,处理时间
*/
asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
                           fd_set __user *exp, struct timeval __user *tvp)
{
    s64 timeout = -1;
    struct timeval tv;
    int ret;
    if (tvp) {/*如果有超时值*/
        if (copy_from_user(&tv, tvp, sizeof(tv)))
            return -EFAULT;
        if (tv.tv_sec < 0 || tv.tv_usec < 0)/*时间无效*/
            return -EINVAL;
        /* Cast to u64 to make GCC stop complaining */
        if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS)
            timeout = -1;	/* 无限等待*/
        else {
            timeout = DIV_ROUND_UP(tv.tv_usec, USEC_PER_SEC/HZ);
            timeout += tv.tv_sec * HZ;/*计算出超时的相对时间,单位为时钟周期数*/
        }
    }
    /*主要工作都在core_sys_select中做了*/
    ret = core_sys_select(n, inp, outp, exp, &timeout);
    if (tvp) {/*如果有超时值*/
        struct timeval rtv;
        if (current->personality & STICKY_TIMEOUTS)/*模拟bug的一个机制,不详细描述*/
            goto sticky;
        /*rtv中是剩余的时间*/
        rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ));
        rtv.tv_sec = timeout;
        if (timeval_compare(&rtv, &tv) >= 0)/*如果core_sys_select超时返回,更新时间*/
            rtv = tv;
        /*拷贝更新后的时间到用户空间*/
        if (copy_to_user(tvp, &rtv, sizeof(rtv))) {
sticky:
            /*
            * If an application puts its timeval in read-only
            * memory, we don't want the Linux-specific update to
            * the timeval to cause a fault after the select has
            * completed successfully. However, because we're not
            * updating the timeval, we can't restart the system
            * call.
            */
            if (ret == -ERESTARTNOHAND)/*ERESTARTNOHAND表明,被中断的系统调用*/
                ret = -EINTR;
        }
    }
    return ret;
}
/*core_sys_select
为do_select准备好了位图,然后调用do_select,将返回的结果集,返回到用户空间
*/
static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
                           fd_set __user *exp, s64 *timeout)
{
    fd_set_bits fds;
    void *bits;
    int ret, max_fds;
    unsigned int size;
    struct fdtable *fdt;
    /* Allocate small arguments on the stack to save memory and be faster */
    /*SELECT_STACK_ALLOC 定义为256*/
    long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
    ret = -EINVAL;
    if (n < 0)
        goto out_nofds;
    /* max_fds can increase, so grab it once to avoid race */
    rcu_read_lock();
    fdt = files_fdtable(current->files);/*获取当前进程的文件描述符表*/
    max_fds = fdt->max_fds;
    rcu_read_unlock();
    if (n > max_fds)/*修正用户传入的第一个参数:fd_set中文件描述符的最大值*/
        n = max_fds;
    /*
    * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
    * since we used fdset we need to allocate memory in units of
    * long-words.
    */
    /*
    如果stack_fds数组的大小不能容纳下所有的fd_set,就用kmalloc重新分配一个大数组。
    然后将位图平均分成份,并初始化fds结构
    */
    size = FDS_BYTES(n);
    bits = stack_fds;
    if (size > sizeof(stack_fds) / 6) {
        /* Not enough space in on-stack array; must use kmalloc */
        ret = -ENOMEM;
        bits = kmalloc(6 * size, GFP_KERNEL);
        if (!bits)
            goto out_nofds;
    }
    fds.in      = bits;
    fds.out     = bits +   size;
    fds.ex      = bits + 2*size;
    fds.res_in  = bits + 3*size;
    fds.res_out = bits + 4*size;
    fds.res_ex  = bits + 5*size;
    /*get_fd_set仅仅调用copy_from_user从用户空间拷贝了fd_set*/
    if ((ret = get_fd_set(n, inp, fds.in)) ||
        (ret = get_fd_set(n, outp, fds.out)) ||
        (ret = get_fd_set(n, exp, fds.ex)))
        goto out;
    zero_fd_set(n, fds.res_in);
    zero_fd_set(n, fds.res_out);
    zero_fd_set(n, fds.res_ex);
    /*
    接力棒传给了do_select
    */
    ret = do_select(n, &fds, timeout);
    if (ret < 0)
        goto out;
    /*do_select返回,是一种异常状态*/
    if (!ret) {
        /*记得上面的sys_select不?将ERESTARTNOHAND转换成了EINTR并返回。EINTR表明系统调用被中断*/
        ret = -ERESTARTNOHAND;
        if (signal_pending(current))/*当当前进程有信号要处理时,signal_pending返回真,这符合了EINTR的语义*/
            goto out;
        ret = 0;
    }
    /*把结果集,拷贝回用户空间*/
    if (set_fd_set(n, inp, fds.res_in) ||
        set_fd_set(n, outp, fds.res_out) ||
        set_fd_set(n, exp, fds.res_ex))
        ret = -EFAULT;
out:
    if (bits != stack_fds)
        kfree(bits);/*对应上面的kmalloc*/
out_nofds:
    return ret;
}
/*do_select
真正的select在此,遍历了所有的fd,调用对应的xxx_poll函数
*/
int do_select(int n, fd_set_bits *fds, s64 *timeout)
{
    struct poll_wqueues table;
    poll_table *wait;
    int retval, i;
    rcu_read_lock();
    /*根据已经打开fd的位图检查用户打开的fd, 要求对应fd必须打开, 并且返回最大的fd*/
    retval = max_select_fd(n, fds);
    rcu_read_unlock();
    if (retval < 0)
        return retval;
    n = retval;
    /*将当前进程放入自已的等待队列table, 并将该等待队列加入到该测试表wait*/
    poll_initwait(&table);
    wait = &table.pt;
    if (!*timeout)
        wait = NULL;
    retval = 0;
    for (;;) {/*死循环*/
        unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
        long __timeout;
        /*注意:可中断的睡眠状态*/
        set_current_state(TASK_INTERRUPTIBLE);
        inp = fds->in; outp = fds->out; exp = fds->ex;
        rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
        for (i = 0; i < n; ++rinp, ++routp, ++rexp) {/*遍历所有fd*/
            unsigned long in, out, ex, all_bits, bit = 1, mask, j;
            unsigned long res_in = 0, res_out = 0, res_ex = 0;
            const struct file_operations *f_op = NULL;
            struct file *file = NULL;
            in = *inp++; out = *outp++; ex = *exp++;
            all_bits = in | out | ex;
            if (all_bits == 0) {
                /*
                __NFDBITS定义为(8 * sizeof(unsigned long)),即long的位数。
                因为一个long代表了__NFDBITS位,所以跳到下一个位图i要增加__NFDBITS
                */
                i += __NFDBITS;
                continue;
            }
            for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
                int fput_needed;
                if (i >= n)
                    break;
                /*测试每一位*/
                if (!(bit & all_bits))
                    continue;
                /*得到file结构指针,并增加引用计数字段f_count*/
                file = fget_light(i, &fput_needed);
                if (file) {
                    f_op = file->f_op;
                    mask = DEFAULT_POLLMASK;
                    /*对于socket描述符,f_op->poll对应的函数是sock_poll
                    注意第三个参数是等待队列,在poll成功后会将本进程唤醒执行*/
                    if (f_op && f_op->poll)
                        mask = (*f_op->poll)(file, retval ? NULL : wait);
                    /*释放file结构指针,实际就是减小他的一个引用计数字段f_count*/
                    fput_light(file, fput_needed);
                    /*根据poll的结果设置状态,要返回select出来的fd数目,所以retval++。
                    注意:retval是in out ex三个集合的总和*/
                    if ((mask & POLLIN_SET) && (in & bit)) {
                        res_in |= bit;
                        retval++;
                    }
                    if ((mask & POLLOUT_SET) && (out & bit)) {
                        res_out |= bit;
                        retval++;
                    }
                    if ((mask & POLLEX_SET) && (ex & bit)) {
                        res_ex |= bit;
                        retval++;
                    }
                }
                /*
                注意前面的set_current_state(TASK_INTERRUPTIBLE);
                因为已经进入TASK_INTERRUPTIBLE状态,所以cond_resched回调度其他进程来运行,
                这里的目的纯粹是为了增加一个抢占点。被抢占后,由等待队列机制唤醒。
                在支持抢占式调度的内核中(定义了CONFIG_PREEMPT),cond_resched是空操作
                */
                cond_resched();
            }
            /*根据poll的结果写回到输出位图里*/
            if (res_in)
                *rinp = res_in;
            if (res_out)
                *routp = res_out;
            if (res_ex)
                *rexp = res_ex;
        }
        wait = NULL;
        if (retval || !*timeout || signal_pending(current))/*signal_pending前面说过了*/
            break;
        if(table.error) {
            retval = table.error;
            break;
        }
        if (*timeout < 0) {
            /*无限等待*/
            __timeout = MAX_SCHEDULE_TIMEOUT;
        } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT - 1)) {
            /* 时间超过MAX_SCHEDULE_TIMEOUT,即schedule_timeout允许的最大值,用一个循环来不断减少超时值*/
            __timeout = MAX_SCHEDULE_TIMEOUT - 1;
            *timeout -= __timeout;
        } else {
            /*等待一段时间*/
            __timeout = *timeout;
            *timeout = 0;
        }
        /*TASK_INTERRUPTIBLE状态下,调用schedule_timeout的进程会在收到信号后重新得到调度的机会,
        即schedule_timeout返回,并返回剩余的时钟周期数
        */
        __timeout = schedule_timeout(__timeout);
        if (*timeout >= 0)
            *timeout += __timeout;
    }
    /*设置为运行状态*/
    __set_current_state(TASK_RUNNING);
    /*清理等待队列*/
    poll_freewait(&table);
    return retval;
}
static unsigned int sock_poll(struct file *file, poll_table *wait)
{
    struct socket *sock;
    /*约定socket的file->private_data字段放着对应的socket结构指针*/
    sock = file->private_data;
    /*对应了三个协议的函数tcp_poll,udp_poll,datagram_poll,其中udp_poll几乎直接调用了datagram_poll
    累了,先休息一下,这三个函数以后分析*/
    return sock->ops->poll(file, sock, wait);
}

其他重要函数一览
static int max_select_fd(unsigned long n, fd_set_bits *fds)
返回在fd_set中已经打开的,并且小于用户指定最大值,的fd
static inline int get_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset)
从用户空间拷贝fd_set到内核
static inline void zero_fd_set(unsigned long nr, unsigned long *fdset)
把fd_set清零
static inline unsigned long __must_check set_fd_set(unsigned long nr, void __user *ufdset, unsigned long *fdset)
把fd_set拷贝回用户空间
static inline int signal_pending(struct task_struct *p)
目前进程有信号需要处理
struct file *fget_light(unsigned int fd, int *fput_needed)
由fd得到其对应的file结构指针,并增加其引用计数
static inline void fput_light(struct file *file, int fput_needed)
释放由fget_light得到的file结构指针,减少其引用计数
set_current_state
设置当前进程的状态
static inline int cond_resched(void)
判断是否有进程需要抢占当前进程,如果是将立即发生调度。就是额外增加一个抢占点。
signed long __sched schedule_timeout(signed long timeout)
当前进程睡眠timeout个jiffies
rcu_read_lock
rcu_read_unlock
Linux 2.6新加入的rcu锁。读锁的加锁、解锁函数
参考http://www.ibm.com/developerworks/cn/linux/l-rcu
poll_freewait
poll_initwait
poll_wait

和文件IO,poll机制有关的几个函数,参考《Linux设备驱动(第三版)》6.3
tcp_poll
udp_poll
datagram_poll
协议层的poll函数
文件出处:http://zhangyafeikimi.iteye.com/blog/248815

PLSQL 两种循环for和while

第一种 while

declare
i number;
begin
i:=0;
while i<=100 loop
Dbms_Output.put_line('i=' || i);
i:=i+1;
end loop;
end;

第二种 for

begin
for i in 1..100 loop
Dbms_Output.put_line('i=' || i);
end loop;
commit;
end;

第三种 也可以直接使用loop( 死循环,游标中使用)

declare
i number;
begin
i:=0;
loop
Dbms_Output.put_line('i=' || i);
i:=i+1;
end loop;
commit;
end;

用PLSQL简单实现游标

PLSQL编程简单实现游标

declare
v_userid   t_eb_user.userid%TYPE;
v_username t_eb_user.username%TYPE;
cursor emp_cursor is
 select a.userid,a.username from t_eb_user a, t_eb_tables b where a.userid=b.userid;
begin
  open emp_cursor;
LOOP
  FETCH emp_cursor INTO v_userid, v_username;
  EXIT WHEN emp_cursor%NOTFOUND;
  dbms_output.put_line(v_userid||' '||v_username);
END LOOP;
end;

使用inotify 监控文件创建、修改

例子为:使用inotify 监控文件创建,可用于安全防护,在创建文件时,检测文件内容,以及后续的操作。
监控类型在/usr/include/sys/inotify.h头文件查看

#define IN_ACCESS        0x00000001     /* File was accessed.  */
#define IN_MODIFY        0x00000002     /* File was modified.  */
#define IN_ATTRIB        0x00000004     /* Metadata changed.  */
#define IN_CLOSE_WRITE   0x00000008     /* Writtable file was closed.  */
#define IN_CLOSE_NOWRITE 0x00000010     /* Unwrittable file closed.  */
#define IN_CLOSE         (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) /* Close.  */
#define IN_OPEN          0x00000020     /* File was opened.  */
#define IN_MOVED_FROM    0x00000040     /* File was moved from X.  */
#define IN_MOVED_TO      0x00000080     /* File was moved to Y.  */
#define IN_MOVE          (IN_MOVED_FROM | IN_MOVED_TO) /* Moves.  */
#define IN_CREATE        0x00000100     /* Subfile was created.  */
#define IN_DELETE        0x00000200     /* Subfile was deleted.  */
#define IN_DELETE_SELF   0x00000400     /* Self was deleted.  */
#define IN_MOVE_SELF     0x00000800     /* Self was moved.  */
/* Events sent by the kernel.  */
#define IN_UNMOUNT       0x00002000     /* Backing fs was unmounted.  */
#define IN_Q_OVERFLOW    0x00004000     /* Event queued overflowed.  */
#define IN_IGNORED       0x00008000     /* File was ignored.  */
/* Helper events.  */
#define IN_CLOSE         (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)    /* Close.  */
#define IN_MOVE          (IN_MOVED_FROM | IN_MOVED_TO)          /* Moves.  */
/* Special flags.  */
#define IN_ONLYDIR       0x01000000     /* Only watch the path if it is a
                                           directory.  */
#define IN_DONT_FOLLOW   0x02000000     /* Do not follow a sym link.  */
#define IN_EXCL_UNLINK   0x04000000     /* Exclude events on unlinked
                                           objects.  */
#define IN_MASK_ADD      0x20000000     /* Add to the mask of an already
#define IN_ONESHOT       0x80000000     /* Only send event once.  */
/* All events which a program can wait on.  */
#define IN_ALL_EVENTS    (IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE
                          | IN_CLOSE_NOWRITE | IN_OPEN | IN_MOVED_FROM
                          | IN_MOVED_TO | IN_CREATE | IN_DELETE
                          | IN_DELETE_SELF | IN_MOVE_SELF)

监控制定目录下面创建文件和目录

#include 
#include 
#include 
#include 
#include 
#define	EVENT_SIZE ( sizeof(struct inotify_event) )
#define	BUFF_LEN ( 1024 * ( EVENT_SIZE + 16 ))
int main(void)
{
    int	length, i = 0;
    int	fd;
    int	wd;
    char	buff[BUFF_LEN];
    fd = inotify_init();
    if ( fd < 0) {
        perror("inotify_init error:");
        exit(-1);
    }
    wd = inotify_add_watch(fd, "/home/key1088/code/c/test", IN_CREATE);
    struct inotify_event *event;
    while ( 1 ) {
        memset(buff, 0x00, sizeof(buff));
        length = read(fd, buff, BUFF_LEN);
        if ( length < 0) {
            perror("read:");
        }
        event = ( struct inotify_event * ) &buff[ i ];
        if ( event->len ) {
            if (event->mask & IN_CREATE) {
                if(event->mask & IN_ISDIR) {
                    printf("create dir,dirname=[%s]n", event->name);
                }else{
                    printf("create file,filename=[%s]n", event->name);
                }
            }
        }
    }
    inotify_rm_watch(fd, wd);
    close(fd);
    return 0;
}

参考:http://www.ibm.com/developerworks/cn/linux/l-ubuntu-inotify/