03 2014 档案

摘要:void swap(void *v[], int a, int b){ void* tmp; tmp = v[a]; v[a]=v[b]; v[b]=tmp;}/* qsort: sort v[left]...v[right] into increasing order */void qsort(void *v[], int left, int right, int (*comp)(void *, void *)){ int i, last; if (left >= right) /* do nothing if array contains */ retur... 阅读全文
posted @ 2014-03-25 18:57 samu 阅读(243) 评论(0) 推荐(0) 编辑
摘要:一、利用select多路复用I/O的Web服务应用模型/* 可读、可写、异常三种文件描述符集的申明和初始化。*/ fd_set readfds, writefds, exceptionfds; FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptionfds); int max_fd; /* socket配置和监听。*/ sock = socket(...); bind(sock, ...); listen(sock, ...); /* 对socket描述符上发生关心的事件进行注册。*/ FD_SET(&r 阅读全文
posted @ 2014-03-21 20:23 samu 阅读(689) 评论(0) 推荐(0) 编辑
摘要:#include #include int main(){ FILE* stream = popen ("sort", "w"); fprintf (stream, "This is a test.\n" ); fprintf (stream, "Hello, world.\n"); fprintf (stream, "My dog has fleas.\n"); fprintf (stream, "This program is great.\n"); fprintf (s 阅读全文
posted @ 2014-03-21 19:55 samu 阅读(209) 评论(0) 推荐(0) 编辑
摘要:#include #include #include #include int main(){ int fds[2]; pid_t pid; pipe( fds ); pid = fork(); if( pid == (pid_t)0 ) { close( fds[1] ); dup2(fds[0], STDIN_FILENO ); execlp( "sort", "sort", 0 ); } ... 阅读全文
posted @ 2014-03-21 19:51 samu 阅读(241) 评论(0) 推荐(0) 编辑
摘要:flags must include read,write,execute permission.for examples:semget( 3333, 1, IPC_CREAT | IPC_EXCL | 0666 );if there is not a '0666', then 'semop' wait will succed always. 阅读全文
posted @ 2014-03-21 14:10 samu 阅读(163) 评论(0) 推荐(0) 编辑
摘要:#include #include #include int main (){int segment_id;char* shared_memory;struct shmid_ds shmbuffer;int segment_size;const int shared_segment_size = 0x6400;/* Allocate a shared memory segment. */segment_id = shmget (IPC_PRIVATE, shared_segment_size,IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);/* Attach 阅读全文
posted @ 2014-03-20 20:30 samu 阅读(378) 评论(0) 推荐(0) 编辑
摘要:Shared memory permits processes to communicate by simply reading and writing to a specified memory location.Mapped memory is similar to shared memory, except that it is associated with a file in the filesystem.Pipes permit sequential communication from one process to a related process.FIFOs are simi 阅读全文
posted @ 2014-03-20 20:18 samu 阅读(151) 评论(0) 推荐(0) 编辑
摘要:#include int thread_flag;pthread_cond_t thread_flag_cv;pthread_mutex_t thread_flag_mutex;void initialize_flag (){/* Initialize the mutex and condition variable.pthread_mutex_init (&thread_flag_mutex, NULL);pthread_cond_init (&thread_flag_cv, NULL);/* Initialize the flag value. */thread_flag 阅读全文
posted @ 2014-03-20 20:10 samu 阅读(187) 评论(0) 推荐(0) 编辑
摘要:#include #include #include struct job {/* Link field for linked list.struct job* next;*//* Other fields describing work to be done... */};/* A linked list of pending jobs.struct job* job_queue;*//* A mutex protecting job_queue. */pthread_mutex_t job_queue_mutex = PTHREAD_MUTEX_INITIALIZER;/* A semap 阅读全文
posted @ 2014-03-20 20:07 samu 阅读(180) 评论(0) 推荐(0) 编辑
摘要:[(lambda x: x*x)(x) for x in range(10)]Or better yet:[x*x for x in range(10)] 阅读全文
posted @ 2014-03-19 19:47 samu 阅读(149) 评论(0) 推荐(0) 编辑
摘要:约瑟夫环问题: 输入:1)总共人数;2)每次被杀的序号数; 输出:最后一个活着的序号python代码如下:n=int (input('please input the number of people:') )k=int (input('please input the discard number:'))a=[]for i in range(n): a.append(i+1)print 'all the serial number of people:'print ai=0j=1while len(a)>1: if j==k: d... 阅读全文
posted @ 2014-03-19 18:35 samu 阅读(175) 评论(0) 推荐(0) 编辑
摘要:普通表达式一般由数字、变量与+-*/()运算符号组成。例如:(a+b)*3 - (a-b)/2其逆波兰表达式是:a b + 3 * a b - 2 / -本质上,普通表达式是中序表达式,逆波兰表达式是后序表达式。一)、由普通表达式生成逆波兰表达式 1、初始化1个逆波兰字符串变量outstr,以及1个符号栈;自左向右输入表达式串; 2、如果当前字符为数值或变量,则直接添加到逆波兰字符串后outstr; 3、如果当前字符为运算符号,如果是“(”,则直接压入符号栈;如果是“)”,则从栈中逐项pop运算符号,追加在outstr后;如果是其他运算符号,比较当前与栈顶的运算符优先级,如果当前低,则pop栈 阅读全文
posted @ 2014-03-18 13:12 samu 阅读(1637) 评论(0) 推荐(1) 编辑
摘要://8-bucket sortvoid bucket_sort( int *arr, int n ){ vector > buckets; int i; for( i=0; i tmps; buckets.push_back( tmps ); } int base; int *backup = new int[n]; int *pos = new int[n]; for( i=0; i<n; backup[i]=arr[i], pos[i]=i, ++... 阅读全文
posted @ 2014-03-18 12:54 samu 阅读(133) 评论(0) 推荐(0) 编辑
摘要:堆排序//7-heap sort methodvoid build_heap( int *arr, int start, int n ){ if( (start+1)*2 > n ) return; if( (start+1)*2 == n ) { if( arr[start] 1; offset-=2 ) { if( arr[offset/2-1] 1; ) { --offset; ... 阅读全文
posted @ 2014-03-18 12:53 samu 阅读(153) 评论(0) 推荐(0) 编辑
摘要:#includeusing namespace std;//1-insert sort methodvoid insert_sort( int *arr, int n ){ int i, k; for( i=1; i0; --k ) { if( arr[k] 0; --k ) { for( i=0; i arr[i+1] ) { int tm... 阅读全文
posted @ 2014-03-10 21:29 samu 阅读(199) 评论(0) 推荐(0) 编辑
摘要:基类为抽象类,在不同的动态库中实现不同的执行行为,但是每个动态库要提供2个统一的方法:1) baseClass * create(); 2) void destroy( baseClass* );,调用该实际类的上下文,通过dlopen,dlsym( dl, "create"), dlsym( dl, "destroy")来获得实际对象的句柄。实际上是一种工厂/builder模型。1. 基类//base.h#include class baseClass {public: virtual void test(){}; virtual ~baseClass 阅读全文
posted @ 2014-03-06 20:59 samu 阅读(575) 评论(0) 推荐(0) 编辑
摘要:char* urlencode(const void* buf, size_t size) { _assert_(buf && size = 'A' && c = 'a' && c = '0' && c > 4; if (num = ep) break; c = *str; if (c >= '0' && c = 'a' && c = 'A' && c = ep) break; 阅读全文
posted @ 2014-03-01 15:41 samu 阅读(1924) 评论(0) 推荐(0) 编辑
摘要:http://www.infoq.com/cn/articles/designing-restful-http-apps-roth摘要:本文对RESTful HTTP的基础原理做了一个概览,探讨了开发者在设计RESTful HTTP应用时所面临的典型问题,展示了如何在实践中应用REST架构风格,描述了常用的URI命名方法,讨论了如何使用统一接口进行资源交互,何时使用PUT或POST以及如何支持非CURD操作等。Java 调用HttpClient httpClient = new HttpClient();IHttpRequest request = new GetRequest(central 阅读全文
posted @ 2014-03-01 08:29 samu 阅读(402) 评论(0) 推荐(0) 编辑