打印自身的程序

八月 10th, 2010 626 次阅读 3 条评论

     话说一转眼就一个多月过去了,也不知道自己在干什么,连这个Blog也荒芜了。Drcom写到一半,网络层的东西实在是蛋疼,而我本人对socket和进程多线程方面的白痴,尽管有现成的Python程序在那,对Python却一头雾水,也不知道啥时候能搞定……

   不过今天在CU上看到了一个好玩的东西,传说中打印自己的程序。后来查了下才知道原来这个叫Quine ,我无知了……Quine以哲学家 Willard van Orman Quine (1908-2000) 而命名,表示一个可以 生成他自己的完全的源代码的程序。编写出某个语言中最简短的 quine 通常作为 黑客们的消遣。作为真正的 quine ,有一些约定:程序不能接受输入或者是打开文件,因为那 样就可以直接输入源代码或者是把源代码文件直接打开再重新打印出来,就没有 什么意思了;同时,一个完全空白的程序(产生完全空白的输出,即没有输出)也 并不能称作 quine 。quine 的想法最初出现在 Bratley, Paul and Jean Millo. "Computer Recreations; Self-Reproducing Automata", Software — Practice & Experience, Vol. 2 (1972). pp. 397-400 中 Bratley 在看到已知的第一个 这样的程序以后对 quine 产生了兴趣。这个程序于二十世纪六十年代由爱丁堡 大学的 Hamish Dewar 以 Atlas Autocode 语言写成。

这个据说是用 C 语言写成的 quine 中最简短的一个是由 Vlad Taeerov 和 Rashit Fakhreyev 所写成的仅包含 64 个字符的程序:

main(a){printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}",34);}
看过之后才晓得对最常见的printf函数认识也是如此浮浅……

下面是几个C程序:

char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}

#define q(k)main(){return!puts(#k"\nq("#k")");}q(#define q(k)main(){return!puts(#k"\nq("#k")");})

还有几个shell脚本:

b=\\;bb=$b$b$b$b;q=\';x='echo "b=$bb;bb=\$b\$b\$b\$b;q=$b$q;x=$q$x$q";echo $x'echo "b=$bb;bb=\$b\$b\$b\$b;q=$b$q;x=$q$x$q";echo $x

#! /bin/shq="'" qq='echo \#! /bin/sh;echo q=\"$q\" qq=${q}$qq$q;echo eval \$qq'eval $qq

#!/bin/cat
任何内容

最后一个也挺有意思~~




分类: Linux Code 标签: Quine C SHELL 

MMap 函数

六月 29th, 2010 674 次阅读 0 条评论
#include <sys/mman.h>

void *mmap(void *addr, size_t len, int prot, int flags,int fildes, off_t off);

The mmap() function shall establish a mapping between the address space of the process at an address pa forlen bytes to the memory object represented by the file descriptor fildes at offset off for len bytes.The value of pa is an implementation-defined function of the parameter addr and the values of flags, furtherdescribed below. A successful mmap() call shall return pa as its result. The address range starting at pa andcontinuing for len bytes shall be legitimate for the possible (not necessarily current) address space of the process.……哥英文不太好,来自http://www.opengroup.org/onlinepubs/000095399/functions/mmap.html看不懂……

总之,mmap函数把一个文件的内容在内存里面做一个映像。于是可以用mmap和/dev/zero来分配内存。为指针分配内存效果应该和malloc差不多。帖个小程序:

#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <string.h>
int main(int argc, char** argv) {
    struct tms *a;
    clock_t b;
    long int c;
    int fd=open("/dev/zero",O_RDWR);
    a=mmap(0,sizeof(struct tms),PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,0);
    sleep(2);
    b=times(a);
    c=sysconf(_SC_CLK_TCK);
    printf("The user time is %d\nThe sys time is %d\n ",(int)a->tms_utime,(int)a->tms_stime);
    printf("The all time is %d \n",(int)b);
    printf("The f is %d\n",c);
    return (EXIT_SUCCESS);
}

sysconf(_SC_CLK_TCK)返回内核创建时的时钟频率,times()将返回过去一段时间时钟滴答的次数。用mmap分配内存地址当然可以用malloc来代替……


分类: Linux Code 标签: mmap malloc 

import this

六月 18th, 2010 597 次阅读 0 条评论
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

分类: Python 标签: Python 

电子技术实验考试

六月 17th, 2010 523 次阅读 2 条评论
今天电子技术实验考试,也算得上是大学最后一门实验课了~给的题目是20秒倒计时计数器~题目虽小,简单但也五脏俱全了,分频,计数,片选,译码再加一个使能端。结果哥上来就犯了一个错误,把文件名命名为all,结果自动创建的元件名也为all,而all是vhdl中的关键字,所以原文件无法加入project,还好及时发现。老实说,对于vhdl总有一种莫名的排斥的感觉,代码码起来总是感觉很不爽……尽管我是码代码的白痴……经过一个半小时的折腾,最终还是在老师最后检查的时候驱动了LED准确的倒计时,看着就像俺滴大学要倒计时了一样~
分类: Life 标签: LED VHDL